From d3a2a37f112e3966eab107bbf42f4a732082f151 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Fri, 4 Nov 2016 19:33:57 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B0?= =?UTF-8?q?,=20=D1=87=D1=82=D0=BE=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=D1=85=D0=BE=D0=B4=D0=B8=D1=82=D1=8C=20nil=20uuid,?= =?UTF-8?q?=20=D1=82=D0=BE=20=D0=B1=D1=80=D0=BE=D1=81=D0=B0=D0=B5=D1=82?= =?UTF-8?q?=D1=81=D1=8F=20IllegalArgumentException=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?sessionserver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/modules/session/models/JoinForm.php | 1 + common/validators/UuidValidator.php | 6 ++ .../api/functional/sessionserver/JoinCest.php | 15 +++++ .../sessionserver/JoinLegacyCest.php | 11 ++++ .../unit/validators/UuidValidatorTest.php | 64 +++++++++++++------ 5 files changed, 77 insertions(+), 20 deletions(-) diff --git a/api/modules/session/models/JoinForm.php b/api/modules/session/models/JoinForm.php index e0ac1de..648338e 100644 --- a/api/modules/session/models/JoinForm.php +++ b/api/modules/session/models/JoinForm.php @@ -90,6 +90,7 @@ class JoinForm extends Model { } $validator = new UuidValidator(); + $validator->allowNil = false; $validator->validateAttribute($this, $attribute); if ($this->hasErrors($attribute)) { diff --git a/common/validators/UuidValidator.php b/common/validators/UuidValidator.php index e0ccced..1103bf7 100644 --- a/common/validators/UuidValidator.php +++ b/common/validators/UuidValidator.php @@ -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, []); + } } } diff --git a/tests/codeception/api/functional/sessionserver/JoinCest.php b/tests/codeception/api/functional/sessionserver/JoinCest.php index 00d9cf3..986521e 100644 --- a/tests/codeception/api/functional/sessionserver/JoinCest.php +++ b/tests/codeception/api/functional/sessionserver/JoinCest.php @@ -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(); diff --git a/tests/codeception/api/functional/sessionserver/JoinLegacyCest.php b/tests/codeception/api/functional/sessionserver/JoinLegacyCest.php index 7818870..e82695a 100644 --- a/tests/codeception/api/functional/sessionserver/JoinLegacyCest.php +++ b/tests/codeception/api/functional/sessionserver/JoinLegacyCest.php @@ -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'); diff --git a/tests/codeception/common/unit/validators/UuidValidatorTest.php b/tests/codeception/common/unit/validators/UuidValidatorTest.php index ef90735..d41775e 100644 --- a/tests/codeception/common/unit/validators/UuidValidatorTest.php +++ b/tests/codeception/common/unit/validators/UuidValidatorTest.php @@ -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 { - public $attribute; - - public function rules() { - return [ - ['attribute', UuidValidator::class], - ]; + /** + * @return Model + */ + public function createModel() { + return new class extends Model { + public $attribute; + }; } }