From 22ed0942e8a80fc01158f55ba875b0b357f4ad4a Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Fri, 29 Sep 2017 02:04:16 +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=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=BE=D0=BD=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=BE=D1=87=D0=B8=D1=81=D1=82=D0=BA=D0=B8=20=D1=83?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D1=80=D0=B5=D0=B2=D1=88=D0=B8=D1=85=20Accoun?= =?UTF-8?q?tSessions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/models/AccountSession.php | 15 ++++++------ console/controllers/CleanupController.php | 23 +++++++++++++++++++ docker/cron/cleanup | 1 + .../common/fixtures/data/account-sessions.php | 16 +++++++++++++ .../controllers/CleanupControllerTest.php | 20 +++++++++++++++- 5 files changed, 67 insertions(+), 8 deletions(-) diff --git a/common/models/AccountSession.php b/common/models/AccountSession.php index b165802..4cc01c2 100644 --- a/common/models/AccountSession.php +++ b/common/models/AccountSession.php @@ -3,6 +3,7 @@ namespace common\models; use Yii; use yii\behaviors\TimestampBehavior; +use yii\db\ActiveQuery; use yii\db\ActiveRecord; /** @@ -22,32 +23,32 @@ use yii\db\ActiveRecord; */ class AccountSession extends ActiveRecord { - public static function tableName() { + public static function tableName(): string { return '{{%accounts_sessions}}'; } - public function behaviors() { + public function behaviors(): array { return [ [ 'class' => TimestampBehavior::class, 'updatedAtAttribute' => 'last_refreshed_at', - ] + ], ]; } - public function getAccount() { + public function getAccount(): ActiveQuery { return $this->hasOne(Account::class, ['id' => 'account_id']); } - public function generateRefreshToken() { + public function generateRefreshToken(): void { $this->refresh_token = Yii::$app->security->generateRandomString(96); } - public function setIp($ip) { + public function setIp($ip): void { $this->last_used_ip = ip2long($ip); } - public function getReadableIp() { + public function getReadableIp(): string { return long2ip($this->last_used_ip); } diff --git a/console/controllers/CleanupController.php b/console/controllers/CleanupController.php index 41fc301..60caf85 100644 --- a/console/controllers/CleanupController.php +++ b/console/controllers/CleanupController.php @@ -1,6 +1,7 @@ $className) { diff --git a/docker/cron/cleanup b/docker/cron/cleanup index e6a0308..9c7e9c0 100644 --- a/docker/cron/cleanup +++ b/docker/cron/cleanup @@ -1,3 +1,4 @@ # https://crontab.guru/every-day 0 0 * * * root /usr/local/bin/php /var/www/html/yii cleanup/email-keys >/dev/null 2>&1 0 1 * * * root /usr/local/bin/php /var/www/html/yii cleanup/minecraft-sessions >/dev/null 2>&1 +0 2 * * * root /usr/local/bin/php /var/www/html/yii cleanup/web-sessions >/dev/null 2>&1 diff --git a/tests/codeception/common/fixtures/data/account-sessions.php b/tests/codeception/common/fixtures/data/account-sessions.php index 1a68919..7bc8867 100644 --- a/tests/codeception/common/fixtures/data/account-sessions.php +++ b/tests/codeception/common/fixtures/data/account-sessions.php @@ -24,4 +24,20 @@ return [ 'created_at' => time(), 'last_refreshed_at' => time(), ], + 'very-expired-session' => [ + 'id' => 4, + 'account_id' => 1, + 'refresh_token' => 'dkzIbUdtVLU3LOotSofUU0BQTvClMqmiIGwVKz2VHXOENifj', + 'last_used_ip' => ip2long('127.0.0.1'), + 'created_at' => 1477414260, + 'last_refreshed_at' => 1480092660, + ], + 'not-refreshed-session' => [ + 'id' => 5, + 'account_id' => 1, + 'refresh_token' => 'NM9g7fZyn1o3F87BkDtRCQaHLuudDxyuup3ttyDRIjmwPPJx', + 'last_used_ip' => ip2long('127.0.0.1'), + 'created_at' => time() - 1814400, // 3 weeks + 'last_refreshed_at' => time() - 1814400, // 3 weeks + ], ]; diff --git a/tests/codeception/console/unit/controllers/CleanupControllerTest.php b/tests/codeception/console/unit/controllers/CleanupControllerTest.php index a947023..b957c05 100644 --- a/tests/codeception/console/unit/controllers/CleanupControllerTest.php +++ b/tests/codeception/console/unit/controllers/CleanupControllerTest.php @@ -1,9 +1,11 @@ EmailActivationFixture::class, - 'minecraftSessions' => MinecraftAccessKeyFixture::class + 'minecraftSessions' => MinecraftAccessKeyFixture::class, + 'accountsSessions' => AccountSessionFixture::class, ]; } @@ -38,4 +41,19 @@ class CleanupControllerTest extends TestCase { $this->tester->cantSeeRecord(MinecraftAccessKey::class, ['access_token' => $expiredSession->access_token]); } + public function testActionWebSessions() { + /** @var AccountSession $expiredSession */ + $expiredSession = $this->tester->grabFixture('accountsSessions', 'very-expired-session'); + /** @var AccountSession $notRefreshedSession */ + $notRefreshedSession = $this->tester->grabFixture('accountsSessions', 'not-refreshed-session'); + $totalSessionsCount = AccountSession::find()->count(); + + $controller = new CleanupController('cleanup', Yii::$app); + $this->assertEquals(0, $controller->actionWebSessions()); + + $this->tester->cantSeeRecord(AccountSession::class, ['id' => $expiredSession->id]); + $this->tester->cantSeeRecord(AccountSession::class, ['id' => $notRefreshedSession->id]); + $this->assertEquals($totalSessionsCount - 2, AccountSession::find()->count()); + } + }