diff --git a/console/controllers/CleanupController.php b/console/controllers/CleanupController.php new file mode 100644 index 0000000..f08985b --- /dev/null +++ b/console/controllers/CleanupController.php @@ -0,0 +1,22 @@ +andWhere(['<', 'expire_time', time()]) + ->each(1000); + + foreach($accessTokens as $token) { + /** @var OauthAccessToken $token */ + $token->delete(); + } + + return self::EXIT_CODE_NORMAL; + } + +} diff --git a/tests/codeception/common/fixtures/OauthAccessTokenFixture.php b/tests/codeception/common/fixtures/OauthAccessTokenFixture.php new file mode 100644 index 0000000..d51f14f --- /dev/null +++ b/tests/codeception/common/fixtures/OauthAccessTokenFixture.php @@ -0,0 +1,17 @@ + [ + 'access_token' => '07541285-831e-1e47-e314-b950309a6fca', + 'session_id' => 1, + 'expire_time' => time() + 3600, + ], + 'admin-ely-expired' => [ + 'access_token' => '2977ec21-3022-96f8-544db-2e1df936908', + 'session_id' => 1, + 'expire_time' => time() - 3600, + ], +]; diff --git a/tests/codeception/common/fixtures/data/oauth-clients.php b/tests/codeception/common/fixtures/data/oauth-clients.php index c7b11a9..2ca68b2 100644 --- a/tests/codeception/common/fixtures/data/oauth-clients.php +++ b/tests/codeception/common/fixtures/data/oauth-clients.php @@ -6,7 +6,7 @@ return [ 'name' => 'Ely.by', 'description' => 'Всем знакомое елуби', 'redirect_uri' => 'http://ely.by', - 'account_id' => NULL, + 'account_id' => null, 'is_trusted' => 0, 'created_at' => 1455309271, ], @@ -16,7 +16,7 @@ return [ 'name' => 'TLauncher', 'description' => 'Лучший альтернативный лаунчер для Minecraft с большим количеством версий и их модификаций, а также возмоностью входа как с лицензионным аккаунтом, так и без него.', 'redirect_uri' => '', - 'account_id' => NULL, + 'account_id' => null, 'is_trusted' => 0, 'created_at' => 1455318468, ], diff --git a/tests/codeception/common/fixtures/data/oauth-sessions.php b/tests/codeception/common/fixtures/data/oauth-sessions.php index d0b9c34..42d2ea2 100644 --- a/tests/codeception/common/fixtures/data/oauth-sessions.php +++ b/tests/codeception/common/fixtures/data/oauth-sessions.php @@ -1,3 +1,10 @@ [ + 'id' => 1, + 'owner_type' => 'user', + 'owner_id' => 1, + 'client_id' => 'ely', + 'client_redirect_uri' => 'http://ely.by/authorization/oauth', + ], ]; diff --git a/tests/codeception/console/unit/controllers/CleanupControllerTest.php b/tests/codeception/console/unit/controllers/CleanupControllerTest.php new file mode 100644 index 0000000..ddae0ee --- /dev/null +++ b/tests/codeception/console/unit/controllers/CleanupControllerTest.php @@ -0,0 +1,31 @@ + OauthAccessTokenFixture::class, + ]; + } + + public function testActionAccessTokens() { + /** @var OauthAccessToken $validAccessToken */ + $validAccessToken = $this->tester->grabFixture('accessTokens', 'admin-ely'); + /** @var OauthAccessToken $expiredAccessToken */ + $expiredAccessToken = $this->tester->grabFixture('accessTokens', 'admin-ely-expired'); + + $controller = new CleanupController('cleanup', Yii::$app); + $this->assertEquals(0, $controller->actionAccessTokens()); + + $this->tester->canSeeRecord(OauthAccessToken::class, ['access_token' => $validAccessToken->access_token]); + $this->tester->cantSeeRecord(OauthAccessToken::class, ['access_token' => $expiredAccessToken->access_token]); + } + +}