2016-03-13 23:54:49 +05:30
|
|
|
<?php
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace api\tests\_support\models\authentication;
|
2016-03-13 23:54:49 +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\RepeatAccountActivationForm;
|
2016-03-13 23:54:49 +05:30
|
|
|
use Codeception\Specify;
|
2016-05-11 01:10:06 +05:30
|
|
|
use common\models\EmailActivation;
|
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;
|
2016-03-13 23:54:49 +05:30
|
|
|
use Yii;
|
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
class RepeatAccountActivationFormTest extends TestCase {
|
2016-03-13 23:54:49 +05:30
|
|
|
use Specify;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
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-03-13 23:54:49 +05:30
|
|
|
}
|
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
public function _fixtures() {
|
2016-03-13 23:54:49 +05:30
|
|
|
return [
|
2016-10-29 03:17:31 +05:30
|
|
|
'accounts' => AccountFixture::class,
|
2016-05-16 04:03:19 +05:30
|
|
|
'activations' => EmailActivationFixture::class,
|
2016-03-13 23:54:49 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateEmailForAccount() {
|
|
|
|
$this->specify('error.email_not_found if passed valid email, but it don\'t exists in database', function() {
|
|
|
|
$model = new RepeatAccountActivationForm(['email' => 'me-is-not@exists.net']);
|
|
|
|
$model->validateEmailForAccount('email');
|
|
|
|
expect($model->getErrors('email'))->equals(['error.email_not_found']);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('error.account_already_activated if passed valid email, but account already activated', function() {
|
2016-10-29 03:17:31 +05:30
|
|
|
$fixture = $this->tester->grabFixture('accounts', 'admin');
|
|
|
|
$model = new RepeatAccountActivationForm(['email' => $fixture['email']]);
|
2016-03-13 23:54:49 +05:30
|
|
|
$model->validateEmailForAccount('email');
|
|
|
|
expect($model->getErrors('email'))->equals(['error.account_already_activated']);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('no errors if passed valid email for not activated account', function() {
|
2016-10-29 03:17:31 +05:30
|
|
|
$fixture = $this->tester->grabFixture('accounts', 'not-activated-account');
|
|
|
|
$model = new RepeatAccountActivationForm(['email' => $fixture['email']]);
|
2016-03-13 23:54:49 +05:30
|
|
|
$model->validateEmailForAccount('email');
|
|
|
|
expect($model->getErrors('email'))->isEmpty();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateExistsActivation() {
|
|
|
|
$this->specify('error.recently_sent_message if passed email has recently sent message', function() {
|
2016-10-29 03:17:31 +05:30
|
|
|
$fixture = $this->tester->grabFixture('activations', 'freshRegistrationConfirmation');
|
|
|
|
$model = $this->createModel(['emailKey' => $fixture['key']]);
|
2016-03-13 23:54:49 +05:30
|
|
|
$model->validateExistsActivation('email');
|
|
|
|
expect($model->getErrors('email'))->equals(['error.recently_sent_message']);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('no errors if passed email has expired activation message', function() {
|
2016-10-29 03:17:31 +05:30
|
|
|
$fixture = $this->tester->grabFixture('activations', 'oldRegistrationConfirmation');
|
|
|
|
$model = $this->createModel(['emailKey' => $fixture['key']]);
|
2016-03-13 23:54:49 +05:30
|
|
|
$model->validateExistsActivation('email');
|
|
|
|
expect($model->getErrors('email'))->isEmpty();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendRepeatMessage() {
|
2017-11-27 04:59:15 +05:30
|
|
|
$model = new RepeatAccountActivationForm();
|
|
|
|
$this->assertFalse($model->sendRepeatMessage(), 'no magic if we don\'t pass validation');
|
|
|
|
$this->assertEmpty($this->tester->grabQueueJobs());
|
2016-03-13 23:54:49 +05:30
|
|
|
|
2017-11-27 04:59:15 +05:30
|
|
|
/** @var \common\models\Account $account */
|
|
|
|
$account = $this->tester->grabFixture('accounts', 'not-activated-account-with-expired-message');
|
|
|
|
$model = new RepeatAccountActivationForm(['email' => $account->email]);
|
|
|
|
$this->assertTrue($model->sendRepeatMessage());
|
|
|
|
$activation = $model->getActivation();
|
|
|
|
$this->assertNotNull($activation);
|
|
|
|
/** @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-03-13 23:54:49 +05:30
|
|
|
}
|
|
|
|
|
2016-05-14 05:17:17 +05:30
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
* @return RepeatAccountActivationForm
|
|
|
|
*/
|
|
|
|
private function createModel(array $params = []) {
|
|
|
|
return new class($params) extends RepeatAccountActivationForm {
|
|
|
|
public $emailKey;
|
|
|
|
|
|
|
|
public function getActivation() {
|
|
|
|
return EmailActivation::findOne($this->emailKey);
|
|
|
|
}
|
|
|
|
};
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|