Implemented account deletion. Not all cases covered with tests [skip ci]

This commit is contained in:
ErickSkrauch
2020-06-12 00:27:02 +03:00
parent c86817a93d
commit 0183e54442
56 changed files with 1041 additions and 188 deletions

View File

@@ -13,6 +13,7 @@ return [
'created_at' => 1451775316,
'updated_at' => 1451775316,
'password_changed_at' => 1451775316,
'deleted_at' => null,
],
'user-with-old-password-type' => [
'id' => 2,
@@ -27,6 +28,7 @@ return [
'created_at' => 1385225069,
'updated_at' => 1385225069,
'password_changed_at' => 1385225069,
'deleted_at' => null,
],
'not-activated-account' => [
'id' => 3,
@@ -41,6 +43,7 @@ return [
'created_at' => 1453146616,
'updated_at' => 1453146616,
'password_changed_at' => 1453146616,
'deleted_at' => null,
],
'not-activated-account-with-expired-message' => [
'id' => 4,
@@ -55,6 +58,7 @@ return [
'created_at' => 1457890086,
'updated_at' => 1457890086,
'password_changed_at' => 1457890086,
'deleted_at' => null,
],
'account-with-fresh-forgot-password-message' => [
'id' => 5,
@@ -69,6 +73,7 @@ return [
'created_at' => 1462891432,
'updated_at' => 1462891432,
'password_changed_at' => 1462891432,
'deleted_at' => null,
],
'account-with-expired-forgot-password-message' => [
'id' => 6,
@@ -83,6 +88,7 @@ return [
'created_at' => 1462891612,
'updated_at' => 1462891612,
'password_changed_at' => 1462891612,
'deleted_at' => null,
],
'account-with-change-email-init-state' => [
'id' => 7,
@@ -97,6 +103,7 @@ return [
'created_at' => 1463427287,
'updated_at' => 1463427287,
'password_changed_at' => 1463427287,
'deleted_at' => null,
],
'account-with-change-email-finish-state' => [
'id' => 8,
@@ -111,6 +118,7 @@ return [
'created_at' => 1463349615,
'updated_at' => 1463349615,
'password_changed_at' => 1463349615,
'deleted_at' => null,
],
'account-with-old-rules-version' => [
'id' => 9,
@@ -125,6 +133,7 @@ return [
'created_at' => 1470499952,
'updated_at' => 1470499952,
'password_changed_at' => 1470499952,
'deleted_at' => null,
],
'banned-account' => [
'id' => 10,
@@ -139,6 +148,7 @@ return [
'created_at' => 1472682343,
'updated_at' => 1472682343,
'password_changed_at' => 1472682343,
'deleted_at' => null,
],
'account-with-usernames-history' => [
'id' => 11,
@@ -153,6 +163,7 @@ return [
'created_at' => 1474404139,
'updated_at' => 1474404149,
'password_changed_at' => 1474404149,
'deleted_at' => null,
],
'account-with-otp-secret' => [
'id' => 12,
@@ -169,6 +180,7 @@ return [
'created_at' => 1485124615,
'updated_at' => 1485124615,
'password_changed_at' => 1485124615,
'deleted_at' => null,
],
'account-with-enabled-otp' => [
'id' => 13,
@@ -185,6 +197,7 @@ return [
'created_at' => 1485124685,
'updated_at' => 1485124685,
'password_changed_at' => 1485124685,
'deleted_at' => null,
],
'account-with-two-oauth-clients' => [
'id' => 14,
@@ -201,5 +214,23 @@ return [
'created_at' => 1519487320,
'updated_at' => 1519487320,
'password_changed_at' => 1519487320,
'deleted_at' => null,
],
'deleted-account' => [
'id' => 15,
'uuid' => '6383de63-8f85-4ed5-92b7-5401a1fa68cd',
'username' => 'DeletedAccount',
'email' => 'deleted-account@ely.by',
'password_hash' => '$2y$13$2rYkap5T6jG8z/mMK8a3Ou6aZxJcmAaTha6FEuujvHEmybSHRzW5e', # password_0
'password_hash_strategy' => \common\models\Account::PASS_HASH_STRATEGY_YII2,
'lang' => 'ru',
'status' => \common\models\Account::STATUS_DELETED,
'rules_agreement_version' => \common\LATEST_RULES_VERSION,
'otp_secret' => null,
'is_otp_enabled' => false,
'created_at' => 1591893532,
'updated_at' => 1591893532,
'password_changed_at' => 1591893532,
'deleted_at' => time(),
],
];

View File

@@ -21,4 +21,11 @@ return [
'created_at' => time() - 10,
'updated_at' => time() - 10,
],
'deleted-token' => [
'access_token' => '239ba889-7020-4383-8d99-cd8c8aab4a2f',
'client_token' => '47443658-4ff8-45e7-b33e-dc8915ab6421',
'account_id' => 15,
'created_at' => time() - 10,
'updated_at' => time() - 10,
],
];

View File

@@ -24,4 +24,10 @@ return [
'account_id' => 11,
'applied_in' => 1474404143,
],
[
'id' => 5,
'username' => 'DeletedAccount',
'account_id' => 15,
'applied_in' => 1591893532,
],
];

View File

@@ -133,7 +133,19 @@ class AccountTest extends TestCase {
/** @var CreateWebHooksDeliveries $job */
$job = $this->tester->grabLastQueuedJob();
$this->assertInstanceOf(CreateWebHooksDeliveries::class, $job);
$this->assertSame($job->payloads['changedAttributes'], $changedAttributes);
$this->assertSame('account.edit', $job->type);
$this->assertSame($changedAttributes, $job->payloads['changedAttributes']);
}
public function testAfterDeletePushEvent() {
$account = new Account();
$account->id = 1;
$account->afterDelete();
/** @var CreateWebHooksDeliveries $job */
$job = $this->tester->grabLastQueuedJob();
$this->assertInstanceOf(CreateWebHooksDeliveries::class, $job);
$this->assertSame('account.deletion', $job->type);
$this->assertSame(1, $job->payloads['id']);
}
}

View File

@@ -3,7 +3,6 @@ declare(strict_types=1);
namespace common\tests\unit\tasks;
use common\models\Account;
use common\tasks\ClearAccountSessions;
use common\tests\fixtures;
use common\tests\unit\TestCase;
@@ -23,18 +22,10 @@ class ClearAccountSessionsTest extends TestCase {
];
}
public function testCreateFromAccount() {
$account = new Account();
$account->id = 123;
$task = ClearAccountSessions::createFromAccount($account);
$this->assertSame(123, $task->accountId);
}
public function testExecute() {
/** @var \common\models\Account $bannedAccount */
$bannedAccount = $this->tester->grabFixture('accounts', 'banned-account');
$task = new ClearAccountSessions();
$task->accountId = $bannedAccount->id;
$task = new ClearAccountSessions($bannedAccount->id);
$task->execute($this->createMock(Queue::class));
$this->assertEmpty($bannedAccount->sessions);
$this->assertEmpty($bannedAccount->minecraftAccessKeys);

View File

@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\tasks;
use common\models\OauthClient;
@@ -33,22 +35,18 @@ class ClearOauthSessionsTest extends TestCase {
}
public function testExecute() {
$task = new ClearOauthSessions();
$task->clientId = 'deleted-oauth-client-with-sessions';
$task->notSince = 1519510065;
$task = new ClearOauthSessions('deleted-oauth-client-with-sessions', 1519510065);
$task->execute($this->createMock(Queue::class));
$this->assertFalse(OauthSession::find()->andWhere(['legacy_id' => 3])->exists());
$this->assertTrue(OauthSession::find()->andWhere(['legacy_id' => 4])->exists());
$task = new ClearOauthSessions();
$task->clientId = 'deleted-oauth-client-with-sessions';
$task = new ClearOauthSessions('deleted-oauth-client-with-sessions');
$task->execute($this->createMock(Queue::class));
$this->assertFalse(OauthSession::find()->andWhere(['legacy_id' => 4])->exists());
$task = new ClearOauthSessions();
$task->clientId = 'some-not-exists-client-id';
$task = new ClearOauthSessions('some-not-exists-client-id');
$task->execute($this->createMock(Queue::class));
}

View File

@@ -52,9 +52,7 @@ class CreateWebHooksDeliveriesTest extends TestCase {
}
public function testExecute() {
$task = new CreateWebHooksDeliveries();
$task->type = 'account.edit';
$task->payloads = [
$task = new CreateWebHooksDeliveries('account.edit', [
'id' => 123,
'uuid' => 'afc8dc7a-4bbf-4d3a-8699-68890088cf84',
'username' => 'mock-username',
@@ -68,7 +66,7 @@ class CreateWebHooksDeliveriesTest extends TestCase {
'email' => 'old-email@ely.by',
'status' => 0,
],
];
]);
$task->execute($this->createMock(Queue::class));
/** @var DeliveryWebHook[] $tasks */
$tasks = $this->tester->grabQueueJobs();

View File

@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace common\tests\unit\tasks;
use common\models\Account;
use common\tasks\DeleteAccount;
use common\tests\fixtures;
use common\tests\unit\TestCase;
use yii\queue\Queue;
/**
* @covers \common\tasks\DeleteAccount
*/
class DeleteAccountTest extends TestCase {
public function _fixtures(): array {
return [
'accounts' => fixtures\AccountFixture::class,
'authSessions' => fixtures\AccountSessionFixture::class,
'emailActivations' => fixtures\EmailActivationFixture::class,
'minecraftAccessKeys' => fixtures\MinecraftAccessKeyFixture::class,
'usernamesHistory' => fixtures\UsernameHistoryFixture::class,
'oauthClients' => fixtures\OauthClientFixture::class,
'oauthSessions' => fixtures\OauthSessionFixture::class,
];
}
public function testExecute() {
/** @var Account $account */
$account = $this->tester->grabFixture('accounts', 'admin');
$account->status = Account::STATUS_DELETED;
$account->deleted_at = time() - 60 * 60 * 24 * 7;
$account->save();
$task = new DeleteAccount($account->id);
$task->execute($this->createMock(Queue::class));
$this->assertEmpty($account->emailActivations);
$this->assertEmpty($account->sessions);
$this->assertEmpty($account->minecraftAccessKeys);
$this->assertEmpty($account->oauthSessions);
$this->assertEmpty($account->usernameHistory);
$this->assertEmpty($account->oauthClients);
$this->assertFalse($account->refresh());
}
/**
* When a user restores his account back, the task doesn't removed
* @throws \Throwable
*/
public function testExecuteOnNotDeletedAccount() {
/** @var Account $account */
$account = $this->tester->grabFixture('accounts', 'admin');
// By default, this account is active
$task = new DeleteAccount($account->id);
$task->execute($this->createMock(Queue::class));
$this->assertNotEmpty($account->emailActivations);
$this->assertNotEmpty($account->sessions);
$this->assertNotEmpty($account->minecraftAccessKeys);
$this->assertNotEmpty($account->oauthSessions);
$this->assertNotEmpty($account->usernameHistory);
$this->assertNotEmpty($account->oauthClients);
$this->assertTrue($account->refresh());
}
/**
* User also might delete his account, restore it and delete again.
* For each deletion the job will be created, so assert, that job for restored deleting will not work
* @throws \Throwable
*/
public function testExecuteOnDeletedAccountWhichWasRestoredAndThenDeletedAgain() {
/** @var Account $account */
$account = $this->tester->grabFixture('accounts', 'admin');
$account->status = Account::STATUS_DELETED;
$account->deleted_at = time() - 60 * 60 * 24 * 2;
$account->save();
$task = new DeleteAccount($account->id);
$task->execute($this->createMock(Queue::class));
$this->assertNotEmpty($account->emailActivations);
$this->assertNotEmpty($account->sessions);
$this->assertNotEmpty($account->minecraftAccessKeys);
$this->assertNotEmpty($account->oauthSessions);
$this->assertNotEmpty($account->usernameHistory);
$this->assertNotEmpty($account->oauthClients);
$this->assertTrue($account->refresh());
}
}