Добавлена проверка, что если приходить nil uuid, то бросается IllegalArgumentException для sessionserver

This commit is contained in:
ErickSkrauch 2016-11-04 19:33:57 +03:00
parent 67cbf2a502
commit d3a2a37f11
5 changed files with 77 additions and 20 deletions

View File

@ -90,6 +90,7 @@ class JoinForm extends Model {
}
$validator = new UuidValidator();
$validator->allowNil = false;
$validator->validateAttribute($this, $attribute);
if ($this->hasErrors($attribute)) {

View File

@ -7,6 +7,8 @@ use yii\validators\Validator;
class UuidValidator extends Validator {
public $allowNil = true;
public $skipOnEmpty = false;
public $message = '{attribute} must be valid uuid';
@ -18,6 +20,10 @@ class UuidValidator extends Validator {
} catch (InvalidArgumentException $e) {
$this->addError($model, $attribute, $this->message, []);
}
if (isset($uuid) && $this->allowNil === false && $uuid === Uuid::NIL) {
$this->addError($model, $attribute, $this->message, []);
}
}
}

View File

@ -111,6 +111,21 @@ class JoinCest {
]);
}
public function joinWithNilUuids(FunctionalTester $I) {
$I->wantTo('join to some server with nil accessToken and selectedProfile');
$this->route->join([
'accessToken' => '00000000-0000-0000-0000-000000000000',
'selectedProfile' => 'df936908-b2e1-544d-96f8-2977ec213022',
'serverId' => Uuid::uuid(),
]);
$I->canSeeResponseCodeIs(400);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
'error' => 'IllegalArgumentException',
'errorMessage' => 'credentials can not be null.',
]);
}
private function expectSuccessResponse(FunctionalTester $I) {
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();

View File

@ -84,6 +84,17 @@ class JoinLegacyCest {
$I->canSeeResponseContains('Ely.by authorization required');
}
public function joinWithNilUuids(FunctionalTester $I) {
$I->wantTo('join to some server by legacy protocol with nil accessToken and selectedProfile');
$this->route->joinLegacy([
'sessionId' => 'token:00000000-0000-0000-0000-000000000000:00000000-0000-0000-0000-000000000000',
'user' => 'SomeUser',
'serverId' => Uuid::uuid(),
]);
$I->canSeeResponseCodeIs(400);
$I->canSeeResponseContains('credentials can not be null.');
}
private function expectSuccessResponse(FunctionalTester $I) {
$I->seeResponseCodeIs(200);
$I->canSeeResponseEquals('OK');

View File

@ -12,42 +12,66 @@ class UuidValidatorTest extends TestCase {
public function testValidateAttribute() {
$this->specify('expected error if passed empty value', function() {
$model = new UuidTestModel();
expect($model->validate())->false();
expect($model->getErrors('attribute'))->equals(['Attribute must be valid uuid']);
$validator = new UuidValidator();
$model = $this->createModel();
$validator->validateAttribute($model, 'attribute');
$this->assertTrue($model->hasErrors());
$this->assertEquals(['Attribute must be valid uuid'], $model->getErrors('attribute'));
});
$this->specify('expected error if passed invalid string', function() {
$model = new UuidTestModel();
$validator = new UuidValidator();
$model = $this->createModel();
$model->attribute = '123456789';
expect($model->validate())->false();
expect($model->getErrors('attribute'))->equals(['Attribute must be valid uuid']);
$validator->validateAttribute($model, 'attribute');
$this->assertTrue($model->hasErrors());
$this->assertEquals(['Attribute must be valid uuid'], $model->getErrors('attribute'));
});
$this->specify('no errors if passed nil uuid and allowNil is set to true', function() {
$validator = new UuidValidator();
$model = $this->createModel();
$model->attribute = '00000000-0000-0000-0000-000000000000';
$validator->validateAttribute($model, 'attribute');
$this->assertFalse($model->hasErrors());
});
$this->specify('no errors if passed nil uuid and allowNil is set to false', function() {
$validator = new UuidValidator();
$validator->allowNil = false;
$model = $this->createModel();
$model->attribute = '00000000-0000-0000-0000-000000000000';
$validator->validateAttribute($model, 'attribute');
$this->assertTrue($model->hasErrors());
$this->assertEquals(['Attribute must be valid uuid'], $model->getErrors('attribute'));
});
$this->specify('no errors if passed valid uuid', function() {
$model = new UuidTestModel();
$validator = new UuidValidator();
$model = $this->createModel();
$model->attribute = Uuid::uuid();
expect($model->validate())->true();
$validator->validateAttribute($model, 'attribute');
$this->assertFalse($model->hasErrors());
});
$this->specify('no errors if passed uuid string without dashes and converted to standart value', function() {
$model = new UuidTestModel();
$validator = new UuidValidator();
$model = $this->createModel();
$originalUuid = Uuid::uuid();
$model->attribute = str_replace('-', '', $originalUuid);
expect($model->validate())->true();
expect($model->attribute)->equals($originalUuid);
$validator->validateAttribute($model, 'attribute');
$this->assertFalse($model->hasErrors());
$this->assertEquals($originalUuid, $model->attribute);
});
}
}
class UuidTestModel extends Model {
/**
* @return Model
*/
public function createModel() {
return new class extends Model {
public $attribute;
public function rules() {
return [
['attribute', UuidValidator::class],
];
};
}
}