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
|
|
|
|
|
|
|
use common\models\Account;
|
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
|
|
|
|
|
|
|
public function testSetPassword() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidatePassword() {
|
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
|
|
|
|
$model->password_hash_strategy = null; // To be sure it's not set
|
|
|
|
$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
|
|
|
|
$model->password_hash_strategy = null; // To be sure it's not set
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2016-04-24 00:14:10 +05:30
|
|
|
public function testHasMojangUsernameCollision() {
|
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
|
|
|
}
|
|
|
|
|
2016-08-06 21:22:03 +05:30
|
|
|
public function testGetProfileLink() {
|
|
|
|
$model = new Account();
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsAgreedWithActualRules() {
|
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
|
|
|
}
|
|
|
|
|
2016-08-18 05:25:52 +05:30
|
|
|
public function testSetRegistrationIp() {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetRegistrationIp() {
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
2018-07-08 20:50:19 +05:30
|
|
|
public function testAfterSaveInsertEvent() {
|
|
|
|
$account = new Account();
|
|
|
|
$account->afterSave(true, [
|
|
|
|
'username' => 'old-username',
|
|
|
|
]);
|
|
|
|
$this->assertNull($this->tester->grabLastQueuedJob());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAfterSaveNotMeaningfulAttributes() {
|
|
|
|
$account = new Account();
|
|
|
|
$account->afterSave(false, [
|
|
|
|
'updatedAt' => time(),
|
|
|
|
]);
|
|
|
|
$this->assertNull($this->tester->grabLastQueuedJob());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAfterSavePushEvent() {
|
|
|
|
$changedAttributes = [
|
|
|
|
'username' => 'old-username',
|
|
|
|
'email' => 'old-email@ely.by',
|
|
|
|
'uuid' => 'c3cc0121-fa87-4818-9c0e-4acb7f9a28c5',
|
|
|
|
'status' => 10,
|
|
|
|
'lang' => 'en',
|
|
|
|
];
|
|
|
|
|
|
|
|
$account = new Account();
|
|
|
|
$account->afterSave(false, $changedAttributes);
|
|
|
|
/** @var CreateWebHooksDeliveries $job */
|
|
|
|
$job = $this->tester->grabLastQueuedJob();
|
|
|
|
$this->assertInstanceOf(CreateWebHooksDeliveries::class, $job);
|
|
|
|
$this->assertSame($job->payloads['changedAttributes'], $changedAttributes);
|
|
|
|
}
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
}
|