2016-01-15 14:51:27 +05:30
|
|
|
<?php
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace api\tests\_support\models\authentication;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
use api\components\ReCaptcha\Validator as ReCaptchaValidator;
|
2016-05-14 05:17:17 +05:30
|
|
|
use api\models\authentication\RegistrationForm;
|
2016-01-15 14:51:27 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use common\models\EmailActivation;
|
2016-09-21 13:43:43 +05:30
|
|
|
use common\models\UsernameHistory;
|
2017-11-27 04:59:15 +05:30
|
|
|
use common\tasks\SendRegistrationEmail;
|
2017-05-18 04:39:26 +05:30
|
|
|
use GuzzleHttp\ClientInterface;
|
2019-02-21 01:28:52 +05:30
|
|
|
use api\tests\unit\TestCase;
|
|
|
|
use common\tests\fixtures\AccountFixture;
|
|
|
|
use common\tests\fixtures\EmailActivationFixture;
|
|
|
|
use common\tests\fixtures\UsernameHistoryFixture;
|
|
|
|
use common\tests\helpers\Mock;
|
2016-01-15 14:51:27 +05:30
|
|
|
use Yii;
|
2017-09-19 22:36:16 +05:30
|
|
|
use yii\validators\EmailValidator;
|
2016-08-18 05:25:52 +05:30
|
|
|
use yii\web\Request;
|
2016-10-29 03:17:31 +05:30
|
|
|
use const common\LATEST_RULES_VERSION;
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
class RegistrationFormTest extends TestCase {
|
2016-01-15 14:51:27 +05:30
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2016-08-18 05:25:52 +05:30
|
|
|
$this->mockRequest();
|
2017-05-18 04:39:26 +05:30
|
|
|
Yii::$container->set(ReCaptchaValidator::class, new class(mock(ClientInterface::class)) extends ReCaptchaValidator {
|
2016-09-19 03:31:19 +05:30
|
|
|
public function validateValue($value) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
public function _fixtures() {
|
2016-01-15 14:51:27 +05:30
|
|
|
return [
|
2016-10-29 03:17:31 +05:30
|
|
|
'accounts' => AccountFixture::class,
|
2017-08-03 00:25:46 +05:30
|
|
|
'emailActivations' => EmailActivationFixture::class,
|
|
|
|
'usernameHistory' => UsernameHistoryFixture::class,
|
2016-01-15 14:51:27 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
public function testValidatePasswordAndRePasswordMatch() {
|
2017-11-27 04:59:15 +05:30
|
|
|
$model = new RegistrationForm([
|
|
|
|
'password' => 'enough-length',
|
|
|
|
'rePassword' => 'but-mismatch',
|
|
|
|
]);
|
|
|
|
$this->assertFalse($model->validate(['rePassword']));
|
|
|
|
$this->assertSame(['error.rePassword_does_not_match'], $model->getErrors('rePassword'));
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2017-11-27 04:59:15 +05:30
|
|
|
$model = new RegistrationForm([
|
|
|
|
'password' => 'enough-length',
|
|
|
|
'rePassword' => 'enough-length',
|
|
|
|
]);
|
|
|
|
$this->assertTrue($model->validate(['rePassword']));
|
|
|
|
$this->assertEmpty($model->getErrors('rePassword'));
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
2016-03-20 04:55:26 +05:30
|
|
|
public function testSignup() {
|
2017-09-19 22:36:16 +05:30
|
|
|
Mock::func(EmailValidator::class, 'checkdnsrr')->andReturnTrue();
|
2016-01-15 14:51:27 +05:30
|
|
|
$model = new RegistrationForm([
|
|
|
|
'username' => 'some_username',
|
|
|
|
'email' => 'some_email@example.com',
|
|
|
|
'password' => 'some_password',
|
|
|
|
'rePassword' => 'some_password',
|
|
|
|
'rulesAgreement' => true,
|
2016-05-13 14:33:00 +05:30
|
|
|
'lang' => 'ru',
|
2016-01-15 14:51:27 +05:30
|
|
|
]);
|
|
|
|
|
2016-05-13 14:33:00 +05:30
|
|
|
$account = $model->signup();
|
2016-01-15 14:51:27 +05:30
|
|
|
|
2016-05-13 14:33:00 +05:30
|
|
|
$this->expectSuccessRegistration($account);
|
2016-10-29 03:17:31 +05:30
|
|
|
$this->assertEquals('ru', $account->lang, 'lang is set');
|
2016-05-13 14:33:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testSignupWithDefaultLanguage() {
|
2017-09-19 22:36:16 +05:30
|
|
|
Mock::func(EmailValidator::class, 'checkdnsrr')->andReturnTrue();
|
2016-05-13 14:33:00 +05:30
|
|
|
$model = new RegistrationForm([
|
|
|
|
'username' => 'some_username',
|
|
|
|
'email' => 'some_email@example.com',
|
|
|
|
'password' => 'some_password',
|
|
|
|
'rePassword' => 'some_password',
|
|
|
|
'rulesAgreement' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$account = $model->signup();
|
|
|
|
|
|
|
|
$this->expectSuccessRegistration($account);
|
2016-10-29 03:17:31 +05:30
|
|
|
$this->assertEquals('en', $account->lang, 'lang is set');
|
2016-05-13 14:33:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Account|null $account
|
|
|
|
*/
|
|
|
|
private function expectSuccessRegistration($account) {
|
2016-10-29 03:17:31 +05:30
|
|
|
$this->assertInstanceOf(Account::class, $account, 'user should be valid');
|
|
|
|
$this->assertTrue($account->validatePassword('some_password'), 'password should be correct');
|
|
|
|
$this->assertNotEmpty($account->uuid, 'uuid is set');
|
|
|
|
$this->assertNotNull($account->registration_ip, 'registration_ip is set');
|
|
|
|
$this->assertEquals(LATEST_RULES_VERSION, $account->rules_agreement_version, 'actual rules version is set');
|
|
|
|
$this->assertTrue(Account::find()->andWhere([
|
2016-01-15 14:51:27 +05:30
|
|
|
'username' => 'some_username',
|
|
|
|
'email' => 'some_email@example.com',
|
2016-10-29 03:17:31 +05:30
|
|
|
])->exists(), 'user model exists in database');
|
2017-06-11 23:36:55 +05:30
|
|
|
/** @var EmailActivation $activation */
|
|
|
|
$activation = EmailActivation::find()
|
|
|
|
->andWhere([
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'type' => EmailActivation::TYPE_REGISTRATION_EMAIL_CONFIRMATION,
|
|
|
|
])
|
|
|
|
->one();
|
|
|
|
$this->assertInstanceOf(EmailActivation::class, $activation, 'email activation code exists in database');
|
2016-10-29 03:17:31 +05:30
|
|
|
$this->assertTrue(UsernameHistory::find()->andWhere([
|
2016-09-21 13:43:43 +05:30
|
|
|
'username' => $account->username,
|
|
|
|
'account_id' => $account->id,
|
|
|
|
'applied_in' => $account->created_at,
|
2016-10-29 03:17:31 +05:30
|
|
|
])->exists(), 'username history record exists in database');
|
2017-11-27 04:59:15 +05:30
|
|
|
|
|
|
|
/** @var SendRegistrationEmail $job */
|
|
|
|
$job = $this->tester->grabLastQueuedJob();
|
|
|
|
$this->assertInstanceOf(SendRegistrationEmail::class, $job);
|
|
|
|
$this->assertSame($account->username, $job->username);
|
|
|
|
$this->assertSame($account->email, $job->email);
|
|
|
|
$this->assertSame($account->lang, $job->locale);
|
|
|
|
$this->assertSame($activation->key, $job->code);
|
|
|
|
$this->assertSame('http://localhost/activation/' . $activation->key, $job->link);
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|
|
|
|
|
2016-08-18 05:25:52 +05:30
|
|
|
private function mockRequest($ip = '88.225.20.236') {
|
|
|
|
$request = $this->getMockBuilder(Request::class)
|
|
|
|
->setMethods(['getUserIP'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request
|
|
|
|
->method('getUserIP')
|
2019-02-21 01:28:52 +05:30
|
|
|
->willReturn($ip);
|
2016-08-18 05:25:52 +05:30
|
|
|
|
|
|
|
Yii::$app->set('request', $request);
|
|
|
|
|
|
|
|
return $request;
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:51:27 +05:30
|
|
|
}
|