2016-03-20 04:55:26 +05:30
|
|
|
<?php
|
2018-07-08 20:50:19 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace common\tests\unit\models;
|
2016-03-20 04:55:26 +05:30
|
|
|
|
2020-10-02 20:44:43 +05:30
|
|
|
use Codeception\Util\ReflectionHelper;
|
2016-03-20 04:55:26 +05:30
|
|
|
use common\models\Account;
|
2020-10-02 20:44:43 +05:30
|
|
|
use common\notifications\AccountDeletedNotification;
|
|
|
|
use common\notifications\AccountEditNotification;
|
2018-07-08 20:50:19 +05:30
|
|
|
use common\tasks\CreateWebHooksDeliveries;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures\MojangUsernameFixture;
|
|
|
|
use common\tests\unit\TestCase;
|
2016-08-06 21:22:03 +05:30
|
|
|
use const common\LATEST_RULES_VERSION;
|
2016-03-20 04:55:26 +05:30
|
|
|
|
2018-07-08 20:50:19 +05:30
|
|
|
/**
|
|
|
|
* @covers \common\models\Account
|
|
|
|
*/
|
2016-10-29 05:53:29 +05:30
|
|
|
class AccountTest extends TestCase {
|
2016-03-20 04:55:26 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testSetPassword(): void {
|
2016-11-02 02:27:42 +05:30
|
|
|
$model = new Account();
|
|
|
|
$model->setPassword('12345678');
|
|
|
|
$this->assertNotEmpty($model->password_hash, 'hash should be set');
|
|
|
|
$this->assertTrue($model->validatePassword('12345678'), 'validation should be passed');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame(Account::PASS_HASH_STRATEGY_YII2, $model->password_hash_strategy, 'latest password hash should be used');
|
2016-03-20 04:55:26 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidatePassword(): void {
|
2019-12-21 04:56:06 +05:30
|
|
|
// Use old hashing algorithm
|
|
|
|
$model = new Account();
|
|
|
|
$model->email = 'erick@skrauch.net';
|
|
|
|
$model->password_hash = '2cfdb29eb354af970865a923335d17d9'; // 12345678
|
|
|
|
$this->assertTrue($model->validatePassword('12345678', Account::PASS_HASH_STRATEGY_OLD_ELY), 'valid password should pass');
|
|
|
|
$this->assertFalse($model->validatePassword('87654321', Account::PASS_HASH_STRATEGY_OLD_ELY), 'invalid password should fail');
|
|
|
|
|
|
|
|
// Modern hash algorithm should also work
|
|
|
|
$model = new Account();
|
|
|
|
$model->password_hash = '$2y$04$N0q8DaHzlYILCnLYrpZfEeWKEqkPZzbawiS07GbSr/.xbRNweSLU6'; // 12345678
|
|
|
|
$this->assertTrue($model->validatePassword('12345678', Account::PASS_HASH_STRATEGY_YII2), 'valid password should pass');
|
|
|
|
$this->assertFalse($model->validatePassword('87654321', Account::PASS_HASH_STRATEGY_YII2), 'invalid password should fail');
|
|
|
|
|
|
|
|
// If the second arg isn't passed model's value should be used
|
|
|
|
$model = new Account();
|
|
|
|
$model->email = 'erick@skrauch.net';
|
|
|
|
$model->password_hash = '2cfdb29eb354af970865a923335d17d9'; // 12345678
|
|
|
|
$model->password_hash_strategy = Account::PASS_HASH_STRATEGY_OLD_ELY;
|
|
|
|
$this->assertTrue($model->validatePassword('12345678'), 'valid password should pass');
|
|
|
|
$this->assertFalse($model->validatePassword('87654321'), 'invalid password should fail');
|
|
|
|
|
|
|
|
// The same case for modern algorithm
|
|
|
|
$model = new Account();
|
|
|
|
$model->password_hash = '$2y$04$N0q8DaHzlYILCnLYrpZfEeWKEqkPZzbawiS07GbSr/.xbRNweSLU6'; // 12345678
|
|
|
|
$model->password_hash_strategy = Account::PASS_HASH_STRATEGY_YII2;
|
|
|
|
$this->assertTrue($model->validatePassword('12345678'), 'valid password should pass');
|
|
|
|
$this->assertFalse($model->validatePassword('87654321'), 'invalid password should fail');
|
2016-03-20 04:55:26 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testHasMojangUsernameCollision(): void {
|
2019-12-21 04:56:06 +05:30
|
|
|
$model = new Account();
|
|
|
|
$model->username = 'ErickSkrauch';
|
|
|
|
$this->assertFalse($model->hasMojangUsernameCollision());
|
|
|
|
|
2016-11-06 00:35:01 +05:30
|
|
|
$this->tester->haveFixtures([
|
|
|
|
'mojangUsernames' => MojangUsernameFixture::class,
|
|
|
|
]);
|
|
|
|
|
2019-12-21 04:56:06 +05:30
|
|
|
$this->assertTrue($model->hasMojangUsernameCollision());
|
2016-04-24 00:14:10 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testGetProfileLink(): void {
|
2016-08-06 21:22:03 +05:30
|
|
|
$model = new Account();
|
2024-12-02 15:40:55 +05:30
|
|
|
$model->id = 123;
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame('http://ely.by/u123', $model->getProfileLink());
|
2016-08-06 21:22:03 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testIsAgreedWithActualRules(): void {
|
2019-12-21 04:56:06 +05:30
|
|
|
$model = new Account();
|
|
|
|
$this->assertFalse($model->isAgreedWithActualRules(), 'field is null');
|
|
|
|
|
|
|
|
$model->rules_agreement_version = 0;
|
|
|
|
$this->assertFalse($model->isAgreedWithActualRules(), 'actual version is greater than zero');
|
|
|
|
|
|
|
|
$model->rules_agreement_version = LATEST_RULES_VERSION;
|
|
|
|
$this->assertTrue($model->isAgreedWithActualRules());
|
2016-08-06 21:22:03 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testSetRegistrationIp(): void {
|
2016-08-18 05:25:52 +05:30
|
|
|
$account = new Account();
|
|
|
|
$account->setRegistrationIp('42.72.205.204');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame('42.72.205.204', inet_ntop($account->registration_ip));
|
2016-08-18 05:25:52 +05:30
|
|
|
$account->setRegistrationIp('2001:1620:28:1:b6f:8bca:93:a116');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame('2001:1620:28:1:b6f:8bca:93:a116', inet_ntop($account->registration_ip));
|
2016-08-18 05:25:52 +05:30
|
|
|
$account->setRegistrationIp(null);
|
|
|
|
$this->assertNull($account->registration_ip);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testGetRegistrationIp(): void {
|
2016-08-18 05:25:52 +05:30
|
|
|
$account = new Account();
|
|
|
|
$account->setRegistrationIp('42.72.205.204');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame('42.72.205.204', $account->getRegistrationIp());
|
2016-08-18 05:25:52 +05:30
|
|
|
$account->setRegistrationIp('2001:1620:28:1:b6f:8bca:93:a116');
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame('2001:1620:28:1:b6f:8bca:93:a116', $account->getRegistrationIp());
|
2016-08-18 05:25:52 +05:30
|
|
|
$account->setRegistrationIp(null);
|
|
|
|
$this->assertNull($account->getRegistrationIp());
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testAfterSaveInsertEvent(): void {
|
2018-07-08 20:50:19 +05:30
|
|
|
$account = new Account();
|
|
|
|
$account->afterSave(true, [
|
|
|
|
'username' => 'old-username',
|
|
|
|
]);
|
|
|
|
$this->assertNull($this->tester->grabLastQueuedJob());
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testAfterSaveNotMeaningfulAttributes(): void {
|
2018-07-08 20:50:19 +05:30
|
|
|
$account = new Account();
|
|
|
|
$account->afterSave(false, [
|
|
|
|
'updatedAt' => time(),
|
|
|
|
]);
|
|
|
|
$this->assertNull($this->tester->grabLastQueuedJob());
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testAfterSavePushEvent(): void {
|
2018-07-08 20:50:19 +05:30
|
|
|
$changedAttributes = [
|
|
|
|
'username' => 'old-username',
|
|
|
|
'email' => 'old-email@ely.by',
|
|
|
|
'uuid' => 'c3cc0121-fa87-4818-9c0e-4acb7f9a28c5',
|
|
|
|
'status' => 10,
|
|
|
|
'lang' => 'en',
|
|
|
|
];
|
|
|
|
|
|
|
|
$account = new Account();
|
2020-10-02 20:44:43 +05:30
|
|
|
$account->id = 123;
|
2018-07-08 20:50:19 +05:30
|
|
|
$account->afterSave(false, $changedAttributes);
|
|
|
|
/** @var CreateWebHooksDeliveries $job */
|
|
|
|
$job = $this->tester->grabLastQueuedJob();
|
|
|
|
$this->assertInstanceOf(CreateWebHooksDeliveries::class, $job);
|
2020-10-02 20:44:43 +05:30
|
|
|
/** @var AccountEditNotification $notification */
|
|
|
|
$notification = ReflectionHelper::readPrivateProperty($job, 'notification');
|
|
|
|
$this->assertInstanceOf(AccountEditNotification::class, $notification);
|
|
|
|
$this->assertSame(123, $notification->getPayloads()['id']);
|
|
|
|
$this->assertSame($changedAttributes, $notification->getPayloads()['changedAttributes']);
|
2020-06-12 02:57:02 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testAfterDeletePushEvent(): void {
|
2020-06-12 02:57:02 +05:30
|
|
|
$account = new Account();
|
|
|
|
$account->id = 1;
|
2020-10-02 20:44:43 +05:30
|
|
|
$account->status = Account::STATUS_REGISTERED;
|
|
|
|
$account->created_at = time() - 60 * 60 * 24;
|
|
|
|
$account->deleted_at = time();
|
|
|
|
|
|
|
|
$account->afterDelete();
|
|
|
|
$this->assertNull($this->tester->grabLastQueuedJob());
|
|
|
|
|
|
|
|
$account->status = Account::STATUS_ACTIVE;
|
2020-06-12 02:57:02 +05:30
|
|
|
$account->afterDelete();
|
|
|
|
/** @var CreateWebHooksDeliveries $job */
|
|
|
|
$job = $this->tester->grabLastQueuedJob();
|
|
|
|
$this->assertInstanceOf(CreateWebHooksDeliveries::class, $job);
|
2020-10-02 20:44:43 +05:30
|
|
|
/** @var AccountDeletedNotification $notification */
|
|
|
|
$notification = ReflectionHelper::readPrivateProperty($job, 'notification');
|
|
|
|
$this->assertInstanceOf(AccountDeletedNotification::class, $notification);
|
|
|
|
$this->assertSame(1, $notification->getPayloads()['id']);
|
2018-07-08 20:50:19 +05:30
|
|
|
}
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
}
|