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

@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace api\tests\unit\modules\accounts\models;
use api\modules\accounts\models\DeleteAccountForm;
use api\tests\unit\TestCase;
use common\models\Account;
use common\tasks\CreateWebHooksDeliveries;
use common\tasks\DeleteAccount;
use common\tests\fixtures\AccountFixture;
use ReflectionObject;
use Yii;
use yii\queue\Queue;
class DeleteAccountFormTest extends TestCase {
/**
* @var Queue|\PHPUnit\Framework\MockObject\MockObject
*/
private Queue $queue;
public function _fixtures(): array {
return [
'accounts' => AccountFixture::class,
];
}
public function _before(): void {
parent::_before();
$this->queue = $this->createMock(Queue::class);
Yii::$app->set('queue', $this->queue);
}
public function testPerformAction() {
/** @var Account $account */
$account = $this->tester->grabFixture('accounts', 'admin');
$this->queue
->expects($this->once())
->method('delay')
->with($this->equalToWithDelta(60 * 60 * 24 * 7, 5))
->willReturnSelf();
$this->queue
->expects($this->exactly(2))
->method('push')
->withConsecutive(
[$this->callback(function(CreateWebHooksDeliveries $task) use ($account): bool {
$this->assertSame($account->id, $task->payloads['id']);
return true;
})],
[$this->callback(function(DeleteAccount $task) use ($account): bool {
$obj = new ReflectionObject($task);
$property = $obj->getProperty('accountId');
$property->setAccessible(true);
$this->assertSame($account->id, $property->getValue($task));
return true;
})],
);
$model = new DeleteAccountForm($account, [
'password' => 'password_0',
]);
$this->assertTrue($model->performAction());
$this->assertSame(Account::STATUS_DELETED, $account->status);
$this->assertEqualsWithDelta(time(), $account->deleted_at, 5);
}
public function testPerformActionWithInvalidPassword() {
/** @var Account $account */
$account = $this->tester->grabFixture('accounts', 'admin');
$model = new DeleteAccountForm($account, [
'password' => 'invalid password',
]);
$this->assertFalse($model->performAction());
$this->assertSame(['password' => ['error.password_incorrect']], $model->getErrors());
}
public function testPerformActionForAlreadyDeletedAccount() {
/** @var Account $account */
$account = $this->tester->grabFixture('accounts', 'deleted-account');
$model = new DeleteAccountForm($account, [
'password' => 'password_0',
]);
$this->assertFalse($model->performAction());
$this->assertSame(['account' => ['error.account_already_deleted']], $model->getErrors());
}
}

View File

@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace api\tests\unit\modules\accounts\models;
use api\modules\accounts\models\RestoreAccountForm;
use api\tests\unit\TestCase;
use common\models\Account;
use common\tasks\CreateWebHooksDeliveries;
use common\tests\fixtures\AccountFixture;
use Yii;
use yii\queue\Queue;
class RestoreAccountFormTest extends TestCase {
/**
* @var Queue|\PHPUnit\Framework\MockObject\MockObject
*/
private Queue $queue;
public function _fixtures(): array {
return [
'accounts' => AccountFixture::class,
];
}
public function _before(): void {
parent::_before();
$this->queue = $this->createMock(Queue::class);
Yii::$app->set('queue', $this->queue);
}
public function testPerformAction() {
/** @var Account $account */
$account = $this->tester->grabFixture('accounts', 'deleted-account');
$this->queue
->expects($this->once())
->method('push')
->withConsecutive(
[$this->callback(function(CreateWebHooksDeliveries $task) use ($account): bool {
$this->assertSame($account->id, $task->payloads['id']);
return true;
})],
);
$model = new RestoreAccountForm($account);
$this->assertTrue($model->performAction());
$this->assertSame(Account::STATUS_ACTIVE, $account->status);
$this->assertNull($account->deleted_at);
}
public function testPerformActionForNotDeletedAccount() {
/** @var Account $account */
$account = $this->tester->grabFixture('accounts', 'admin');
$model = new RestoreAccountForm($account);
$this->assertFalse($model->performAction());
$this->assertSame(['account' => ['error.account_not_deleted']], $model->getErrors());
}
}

View File

@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
namespace api\tests\unit\modules\internal\models;
use api\modules\accounts\models\BanAccountForm;
@@ -6,8 +8,9 @@ use api\modules\internal\helpers\Error as E;
use api\tests\unit\TestCase;
use common\models\Account;
use common\tasks\ClearAccountSessions;
use ReflectionObject;
class BanFormTest extends TestCase {
class BanAccountFormTest extends TestCase {
public function testValidateAccountActivity() {
$account = new Account();
@@ -25,13 +28,9 @@ class BanFormTest extends TestCase {
public function testBan() {
/** @var Account|\PHPUnit\Framework\MockObject\MockObject $account */
$account = $this->getMockBuilder(Account::class)
->setMethods(['save'])
->getMock();
$account->expects($this->once())
->method('save')
->willReturn(true);
$account = $this->createPartialMock(Account::class, ['save']);
$account->expects($this->once())->method('save')->willReturn(true);
$account->id = 123;
$model = new BanAccountForm($account);
$this->assertTrue($model->performAction());
@@ -39,7 +38,10 @@ class BanFormTest extends TestCase {
/** @var ClearAccountSessions $job */
$job = $this->tester->grabLastQueuedJob();
$this->assertInstanceOf(ClearAccountSessions::class, $job);
$this->assertSame($job->accountId, $account->id);
$obj = new ReflectionObject($job);
$property = $obj->getProperty('accountId');
$property->setAccessible(true);
$this->assertSame(123, $property->getValue($job));
}
}

View File

@@ -39,14 +39,25 @@ class AccountOwnerTest extends TestCase {
Yii::$app->user->setIdentity($identity);
// Assert that account id matches
$this->assertFalse($rule->execute('token', $item, ['accountId' => 2]));
$this->assertFalse($rule->execute('token', $item, ['accountId' => '2']));
$this->assertTrue($rule->execute('token', $item, ['accountId' => 1]));
$this->assertTrue($rule->execute('token', $item, ['accountId' => '1']));
// Check accepted latest rules
$account->rules_agreement_version = null;
$this->assertFalse($rule->execute('token', $item, ['accountId' => 1]));
$this->assertTrue($rule->execute('token', $item, ['accountId' => 1, 'optionalRules' => true]));
$account->rules_agreement_version = LATEST_RULES_VERSION;
$this->assertTrue($rule->execute('token', $item, ['accountId' => 1]));
// Check deleted account behavior
$account->status = Account::STATUS_DELETED;
$this->assertFalse($rule->execute('token', $item, ['accountId' => 1]));
$this->assertTrue($rule->execute('token', $item, ['accountId' => 1, 'allowDeleted' => true]));
// Banned account should always be not allowed
$account->status = Account::STATUS_BANNED;
$this->assertFalse($rule->execute('token', $item, ['accountId' => 1]));
$this->assertFalse($rule->execute('token', $item, ['accountId' => 1, 'optionalRules' => true]));