2016-03-13 23:54:49 +05:30
|
|
|
<?php
|
2016-05-14 05:17:17 +05:30
|
|
|
namespace tests\codeception\api\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-05-18 04:39:26 +05:30
|
|
|
use GuzzleHttp\ClientInterface;
|
2016-10-29 03:17:31 +05:30
|
|
|
use tests\codeception\api\unit\TestCase;
|
2016-03-13 23:54:49 +05:30
|
|
|
use tests\codeception\common\fixtures\AccountFixture;
|
|
|
|
use tests\codeception\common\fixtures\EmailActivationFixture;
|
|
|
|
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() {
|
|
|
|
$this->specify('no magic if we don\'t pass validation', function() {
|
|
|
|
$model = new RepeatAccountActivationForm();
|
|
|
|
expect($model->sendRepeatMessage())->false();
|
2016-10-29 03:17:31 +05:30
|
|
|
$this->tester->cantSeeEmailIsSent();
|
2016-03-13 23:54:49 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('successfully send new message if previous message has expired', function() {
|
2016-10-29 03:17:31 +05:30
|
|
|
$email = $this->tester->grabFixture('accounts', 'not-activated-account-with-expired-message')['email'];
|
2016-03-13 23:54:49 +05:30
|
|
|
$model = new RepeatAccountActivationForm(['email' => $email]);
|
|
|
|
expect($model->sendRepeatMessage())->true();
|
2016-05-11 01:10:06 +05:30
|
|
|
expect($model->getActivation())->notNull();
|
2016-10-29 03:17:31 +05:30
|
|
|
$this->tester->canSeeEmailIsSent(1);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|