2016-03-13 23:54:49 +05:30
|
|
|
<?php
|
2019-08-02 18:27:17 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
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;
|
2019-02-23 04:41:57 +05:30
|
|
|
use api\tests\unit\TestCase;
|
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;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures\AccountFixture;
|
|
|
|
use common\tests\fixtures\EmailActivationFixture;
|
2019-02-23 04:41:57 +05:30
|
|
|
use GuzzleHttp\ClientInterface;
|
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;
|
|
|
|
|
2019-08-02 18:27:17 +05:30
|
|
|
protected function setUp(): void {
|
2016-03-13 23:54:49 +05:30
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-14 04:28:29 +05:30
|
|
|
public function _fixtures(): array {
|
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');
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertSame(['error.email_not_found'], $model->getErrors('email'));
|
2016-03-13 23:54:49 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$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');
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertSame(['error.account_already_activated'], $model->getErrors('email'));
|
2016-03-13 23:54:49 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$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');
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertEmpty($model->getErrors('email'));
|
2016-03-13 23:54:49 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertSame(['error.recently_sent_message'], $model->getErrors('email'));
|
2016-03-13 23:54:49 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$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');
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertEmpty($model->getErrors('email'));
|
2016-03-13 23:54:49 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2019-12-14 00:57:13 +05:30
|
|
|
public function getActivation(): ?EmailActivation {
|
2016-05-14 05:17:17 +05:30
|
|
|
return EmailActivation::findOne($this->emailKey);
|
|
|
|
}
|
|
|
|
};
|
2016-05-11 01:10:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|