From 6bd054e74356c076f3e4226e549cdeeae464a12f Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Thu, 1 Aug 2019 19:58:18 +0300 Subject: [PATCH] Fix some tests --- api/models/authentication/LoginForm.php | 6 ++---- api/tests/_support/FunctionalTester.php | 2 +- .../functional/_steps/AuthserverSteps.php | 2 +- .../unit/components/User/ComponentTest.php | 16 ++-------------- .../User}/JwtIdentityTest.php | 2 +- .../tests/unit/rbac/rules/AccountOwnerTest.php | 18 ++++++------------ .../unit/rbac/rules/OauthClientOwnerTest.php | 10 ++++------ 7 files changed, 17 insertions(+), 39 deletions(-) rename api/tests/unit/{models => components/User}/JwtIdentityTest.php (97%) diff --git a/api/models/authentication/LoginForm.php b/api/models/authentication/LoginForm.php index 88c86ed..455bfa4 100644 --- a/api/models/authentication/LoginForm.php +++ b/api/models/authentication/LoginForm.php @@ -112,22 +112,20 @@ class LoginForm extends ApiForm { Assert::true($account->save(), 'Unable to upgrade user\'s password'); } - $refreshToken = null; + $session = null; if ($this->rememberMe) { $session = new AccountSession(); $session->account_id = $account->id; $session->setIp(Yii::$app->request->userIP); $session->generateRefreshToken(); Assert::true($session->save(), 'Cannot save account session model'); - - $refreshToken = $session->refresh_token; } $token = TokensFactory::createForAccount($account, $session); $transaction->commit(); - return new AuthenticationResult($token, $refreshToken); + return new AuthenticationResult($token, $session ? $session->refresh_token : null); } } diff --git a/api/tests/_support/FunctionalTester.php b/api/tests/_support/FunctionalTester.php index 5061011..86bef30 100644 --- a/api/tests/_support/FunctionalTester.php +++ b/api/tests/_support/FunctionalTester.php @@ -13,7 +13,7 @@ use Yii; class FunctionalTester extends Actor { use FunctionalTesterActions; - public function amAuthenticated(string $asUsername = 'admin'): int { + public function amAuthenticated(string $asUsername = 'admin') { // Do not declare type /** @var Account $account */ $account = Account::findOne(['username' => $asUsername]); if ($account === null) { diff --git a/api/tests/functional/_steps/AuthserverSteps.php b/api/tests/functional/_steps/AuthserverSteps.php index 84102a8..97cb5bf 100644 --- a/api/tests/functional/_steps/AuthserverSteps.php +++ b/api/tests/functional/_steps/AuthserverSteps.php @@ -9,7 +9,7 @@ use Ramsey\Uuid\Uuid; class AuthserverSteps extends FunctionalTester { - public function amAuthenticated(string $asUsername = 'admin', string $password = 'password_0'): array { + public function amAuthenticated(string $asUsername = 'admin', string $password = 'password_0') { $route = new AuthserverRoute($this); $clientToken = Uuid::uuid4()->toString(); $route->authenticate([ diff --git a/api/tests/unit/components/User/ComponentTest.php b/api/tests/unit/components/User/ComponentTest.php index 6dd371b..4232c39 100644 --- a/api/tests/unit/components/User/ComponentTest.php +++ b/api/tests/unit/components/User/ComponentTest.php @@ -25,7 +25,7 @@ class ComponentTest extends TestCase { public function _before() { parent::_before(); - $this->component = new Component($this->getComponentConfig()); + $this->component = new Component(); } public function _fixtures(): array { @@ -72,7 +72,6 @@ class ComponentTest extends TestCase { /** @var Component|\PHPUnit\Framework\MockObject\MockObject $component */ $component = $this->getMockBuilder(Component::class) ->setMethods(['getIsGuest']) - ->setConstructorArgs([$this->getComponentConfig()]) ->getMock(); $component @@ -91,7 +90,7 @@ class ComponentTest extends TestCase { $session = $this->tester->grabFixture('sessions', 'admin2'); /** @var Component|\Mockery\MockInterface $component */ - $component = mock(Component::class . '[getActiveSession]', [$this->getComponentConfig()])->makePartial(); + $component = mock(Component::class . '[getActiveSession]')->makePartial(); $component->shouldReceive('getActiveSession')->times(1)->andReturn($session); /** @var Account $account */ @@ -140,15 +139,4 @@ class ComponentTest extends TestCase { Yii::$app->request->headers->set('Authorization', $bearerToken); } - private function getComponentConfig() { - return [ - 'identityClass' => IdentityFactory::class, - 'enableSession' => false, - 'loginUrl' => null, - 'secret' => 'secret', - 'publicKeyPath' => 'data/certs/public.crt', - 'privateKeyPath' => 'data/certs/private.key', - ]; - } - } diff --git a/api/tests/unit/models/JwtIdentityTest.php b/api/tests/unit/components/User/JwtIdentityTest.php similarity index 97% rename from api/tests/unit/models/JwtIdentityTest.php rename to api/tests/unit/components/User/JwtIdentityTest.php index 04855ea..828eaf4 100644 --- a/api/tests/unit/models/JwtIdentityTest.php +++ b/api/tests/unit/components/User/JwtIdentityTest.php @@ -1,7 +1,7 @@ 'secret', - 'publicKeyPath' => 'data/certs/public.crt', - 'privateKeyPath' => 'data/certs/private.key', - ]]); - $component->shouldDeferMissing(); + $component = mock(Component::class . '[findIdentityByAccessToken]'); + $component->makePartial(); $component->shouldReceive('findIdentityByAccessToken')->andReturn(null); Yii::$app->set('user', $component); @@ -38,12 +36,8 @@ class AccountOwnerTest extends TestCase { $identity = mock(IdentityInterface::class); $identity->shouldReceive('getAccount')->andReturn($account); - $component = mock(Component::class . '[findIdentityByAccessToken]', [[ - 'secret' => 'secret', - 'publicKeyPath' => 'data/certs/public.crt', - 'privateKeyPath' => 'data/certs/private.key', - ]]); - $component->shouldDeferMissing(); + $component = mock(Component::class . '[findIdentityByAccessToken]'); + $component->makePartial(); $component->shouldReceive('findIdentityByAccessToken')->withArgs(['token'])->andReturn($identity); Yii::$app->set('user', $component); diff --git a/common/tests/unit/rbac/rules/OauthClientOwnerTest.php b/common/tests/unit/rbac/rules/OauthClientOwnerTest.php index 7148c3b..31520fb 100644 --- a/common/tests/unit/rbac/rules/OauthClientOwnerTest.php +++ b/common/tests/unit/rbac/rules/OauthClientOwnerTest.php @@ -1,4 +1,6 @@ shouldReceive('getAccount')->andReturn($account); /** @var Component|\Mockery\MockInterface $component */ - $component = mock(Component::class . '[findIdentityByAccessToken]', [[ - 'secret' => 'secret', - 'publicKeyPath' => 'data/certs/public.crt', - 'privateKeyPath' => 'data/certs/private.key', - ]]); - $component->shouldDeferMissing(); + $component = mock(Component::class . '[findIdentityByAccessToken]'); + $component->makePartial(); $component->shouldReceive('findIdentityByAccessToken')->withArgs(['token'])->andReturn($identity); Yii::$app->set('user', $component);