mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Rework tests structure. Upgrade codeception to 2.5.3. Merge params configuration into app configuration.
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
namespace codeception\api\unit\validators;
|
||||
|
||||
use api\validators\EmailActivationKeyValidator;
|
||||
use Codeception\Specify;
|
||||
use common\helpers\Error as E;
|
||||
use common\models\confirmations\ForgotPassword;
|
||||
use common\models\EmailActivation;
|
||||
use api\tests\unit\TestCase;
|
||||
use common\tests\_support\ProtectedCaller;
|
||||
use common\tests\fixtures\EmailActivationFixture;
|
||||
use yii\base\Model;
|
||||
|
||||
class EmailActivationKeyValidatorTest extends TestCase {
|
||||
use Specify;
|
||||
use ProtectedCaller;
|
||||
|
||||
public function testValidateAttribute() {
|
||||
/** @var Model $model */
|
||||
$model = new class extends Model {
|
||||
public $key;
|
||||
};
|
||||
|
||||
/** @var EmailActivationKeyValidator|\PHPUnit_Framework_MockObject_MockObject $validator */
|
||||
$validator = $this->getMockBuilder(EmailActivationKeyValidator::class)
|
||||
->setMethods(['findEmailActivationModel'])
|
||||
->getMock();
|
||||
|
||||
$expiredActivation = new ForgotPassword();
|
||||
$expiredActivation->created_at = time() - $expiredActivation->expirationTimeout - 10;
|
||||
|
||||
$validActivation = new EmailActivation();
|
||||
|
||||
$validator->expects($this->exactly(3))
|
||||
->method('findEmailActivationModel')
|
||||
->willReturnOnConsecutiveCalls(null, $expiredActivation, $validActivation);
|
||||
|
||||
$validator->validateAttribute($model, 'key');
|
||||
$this->assertEquals([E::KEY_REQUIRED], $model->getErrors('key'));
|
||||
$this->assertNull($model->key);
|
||||
|
||||
$model->clearErrors();
|
||||
$model->key = 'original value';
|
||||
$validator->validateAttribute($model, 'key');
|
||||
$this->assertEquals([E::KEY_NOT_EXISTS], $model->getErrors('key'));
|
||||
$this->assertEquals('original value', $model->key);
|
||||
|
||||
$model->clearErrors();
|
||||
$validator->validateAttribute($model, 'key');
|
||||
$this->assertEquals([E::KEY_EXPIRE], $model->getErrors('key'));
|
||||
$this->assertEquals('original value', $model->key);
|
||||
|
||||
$model->clearErrors();
|
||||
$validator->validateAttribute($model, 'key');
|
||||
$this->assertEmpty($model->getErrors('key'));
|
||||
$this->assertEquals($validActivation, $model->key);
|
||||
}
|
||||
|
||||
public function testFindEmailActivationModel() {
|
||||
$this->tester->haveFixtures(['emailActivations' => EmailActivationFixture::class]);
|
||||
|
||||
$key = $this->tester->grabFixture('emailActivations', 'freshRegistrationConfirmation')['key'];
|
||||
$model = new EmailActivationKeyValidator();
|
||||
/** @var EmailActivation $result */
|
||||
$result = $this->callProtected($model, 'findEmailActivationModel', $key);
|
||||
$this->assertInstanceOf(EmailActivation::class, $result, 'valid key without specifying type must return model');
|
||||
$this->assertEquals($key, $result->key);
|
||||
|
||||
/** @var EmailActivation $result */
|
||||
$result = $this->callProtected($model, 'findEmailActivationModel', $key, 0);
|
||||
$this->assertInstanceOf(EmailActivation::class, $result, 'valid key with valid type must return model');
|
||||
|
||||
/** @var EmailActivation $result */
|
||||
$result = $this->callProtected($model, 'findEmailActivationModel', $key, 1);
|
||||
$this->assertNull($result, 'valid key, but invalid type must return null');
|
||||
|
||||
$model = new EmailActivationKeyValidator();
|
||||
$result = $this->callProtected($model, 'findEmailActivationModel', 'invalid-key');
|
||||
$this->assertNull($result, 'invalid key must return null');
|
||||
}
|
||||
|
||||
}
|
36
api/tests/unit/validators/PasswordRequiredValidatorTest.php
Normal file
36
api/tests/unit/validators/PasswordRequiredValidatorTest.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace codeception\api\unit\validators;
|
||||
|
||||
use api\validators\PasswordRequiredValidator;
|
||||
use common\helpers\Error as E;
|
||||
use common\models\Account;
|
||||
use common\rbac\Permissions as P;
|
||||
use api\tests\unit\TestCase;
|
||||
use common\tests\_support\ProtectedCaller;
|
||||
use yii\web\User;
|
||||
|
||||
class PasswordRequiredValidatorTest extends TestCase {
|
||||
use ProtectedCaller;
|
||||
|
||||
public function testValidateValue() {
|
||||
$account = new Account(['password' => '12345678']);
|
||||
$model = new PasswordRequiredValidator(['account' => $account]);
|
||||
|
||||
// Get error.password_required if password is empty
|
||||
$this->assertEquals([E::PASSWORD_REQUIRED, []], $this->callProtected($model, 'validateValue', ''));
|
||||
|
||||
// Get error.password_incorrect if password is incorrect
|
||||
$this->assertEquals([E::PASSWORD_INCORRECT, []], $this->callProtected($model, 'validateValue', '87654321'));
|
||||
|
||||
// No errors, if password is correct for provided account
|
||||
$this->assertNull($this->callProtected($model, 'validateValue', '12345678'));
|
||||
|
||||
// Skip validation if user can skip identity verification
|
||||
/** @var User|\Mockery\MockInterface $component */
|
||||
$component = mock(User::class . '[can]', [['identityClass' => '']]);
|
||||
$component->shouldReceive('can')->withArgs([P::ESCAPE_IDENTITY_VERIFICATION])->andReturn(true);
|
||||
$model->user = $component;
|
||||
$this->assertNull($this->callProtected($model, 'validateValue', ''));
|
||||
}
|
||||
|
||||
}
|
57
api/tests/unit/validators/TotpValidatorTest.php
Normal file
57
api/tests/unit/validators/TotpValidatorTest.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
namespace api\tests\unit\validators;
|
||||
|
||||
use api\validators\TotpValidator;
|
||||
use common\helpers\Error as E;
|
||||
use common\models\Account;
|
||||
use OTPHP\TOTP;
|
||||
use api\tests\unit\TestCase;
|
||||
use common\tests\_support\ProtectedCaller;
|
||||
|
||||
class TotpValidatorTest extends TestCase {
|
||||
use ProtectedCaller;
|
||||
|
||||
public function testValidateValue() {
|
||||
$account = new Account();
|
||||
$account->otp_secret = 'AAAA';
|
||||
$controlTotp = TOTP::create($account->otp_secret);
|
||||
|
||||
$validator = new TotpValidator(['account' => $account]);
|
||||
|
||||
$result = $this->callProtected($validator, 'validateValue', 123456);
|
||||
$this->assertEquals([E::TOTP_INCORRECT, []], $result);
|
||||
|
||||
$result = $this->callProtected($validator, 'validateValue', $controlTotp->now());
|
||||
$this->assertNull($result);
|
||||
|
||||
$result = $this->callProtected($validator, 'validateValue', $controlTotp->at(time() - 31));
|
||||
$this->assertEquals([E::TOTP_INCORRECT, []], $result);
|
||||
|
||||
$validator->window = 2;
|
||||
$result = $this->callProtected($validator, 'validateValue', $controlTotp->at(time() - 31));
|
||||
$this->assertNull($result);
|
||||
|
||||
$at = time() - 400;
|
||||
$validator->timestamp = $at;
|
||||
$result = $this->callProtected($validator, 'validateValue', $controlTotp->now());
|
||||
$this->assertEquals([E::TOTP_INCORRECT, []], $result);
|
||||
|
||||
$result = $this->callProtected($validator, 'validateValue', $controlTotp->at($at));
|
||||
$this->assertNull($result);
|
||||
|
||||
$at = function() {
|
||||
return null;
|
||||
};
|
||||
$validator->timestamp = $at;
|
||||
$result = $this->callProtected($validator, 'validateValue', $controlTotp->now());
|
||||
$this->assertNull($result);
|
||||
|
||||
$at = function() {
|
||||
return time() - 700;
|
||||
};
|
||||
$validator->timestamp = $at;
|
||||
$result = $this->callProtected($validator, 'validateValue', $controlTotp->at($at()));
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user