2016-08-29 04:47:45 +05:30
|
|
|
<?php
|
2019-08-02 18:27:17 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-29 04:47:45 +05:30
|
|
|
namespace codeception\api\unit\modules\authserver\models;
|
|
|
|
|
|
|
|
use api\models\authentication\LoginForm;
|
2019-08-02 18:27:17 +05:30
|
|
|
use api\modules\authserver\exceptions\ForbiddenOperationException;
|
2016-08-29 04:47:45 +05:30
|
|
|
use api\modules\authserver\models\AuthenticateData;
|
|
|
|
use api\modules\authserver\models\AuthenticationForm;
|
2019-02-23 04:41:57 +05:30
|
|
|
use api\tests\unit\TestCase;
|
2016-08-29 04:47:45 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use common\models\MinecraftAccessKey;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\_support\ProtectedCaller;
|
|
|
|
use common\tests\fixtures\AccountFixture;
|
|
|
|
use common\tests\fixtures\MinecraftAccessKeyFixture;
|
2019-02-23 04:41:57 +05:30
|
|
|
use Ramsey\Uuid\Uuid;
|
2016-08-29 04:47:45 +05:30
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
class AuthenticationFormTest extends TestCase {
|
2016-08-29 04:47:45 +05:30
|
|
|
use ProtectedCaller;
|
|
|
|
|
2019-05-14 04:28:29 +05:30
|
|
|
public function _fixtures(): array {
|
2016-08-29 04:47:45 +05:30
|
|
|
return [
|
|
|
|
'accounts' => AccountFixture::class,
|
|
|
|
'minecraftAccessKeys' => MinecraftAccessKeyFixture::class,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthenticateByWrongNicknamePass() {
|
2019-08-02 18:27:17 +05:30
|
|
|
$this->expectException(ForbiddenOperationException::class);
|
|
|
|
$this->expectExceptionMessage('Invalid credentials. Invalid nickname or password.');
|
|
|
|
|
2016-08-29 04:47:45 +05:30
|
|
|
$authForm = $this->createAuthForm();
|
|
|
|
|
|
|
|
$authForm->username = 'wrong-username';
|
|
|
|
$authForm->password = 'wrong-password';
|
|
|
|
$authForm->clientToken = Uuid::uuid4();
|
|
|
|
|
|
|
|
$authForm->authenticate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthenticateByWrongEmailPass() {
|
2019-08-02 18:27:17 +05:30
|
|
|
$this->expectException(ForbiddenOperationException::class);
|
|
|
|
$this->expectExceptionMessage('Invalid credentials. Invalid email or password.');
|
|
|
|
|
2016-08-29 04:47:45 +05:30
|
|
|
$authForm = $this->createAuthForm();
|
|
|
|
|
|
|
|
$authForm->username = 'wrong-email@ely.by';
|
|
|
|
$authForm->password = 'wrong-password';
|
|
|
|
$authForm->clientToken = Uuid::uuid4();
|
|
|
|
|
|
|
|
$authForm->authenticate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthenticateByValidCredentialsIntoBlockedAccount() {
|
2019-08-02 18:27:17 +05:30
|
|
|
$this->expectException(ForbiddenOperationException::class);
|
|
|
|
$this->expectExceptionMessage('This account has been suspended.');
|
|
|
|
|
2016-08-29 04:47:45 +05:30
|
|
|
$authForm = $this->createAuthForm(Account::STATUS_BANNED);
|
|
|
|
|
|
|
|
$authForm->username = 'dummy';
|
|
|
|
$authForm->password = 'password_0';
|
|
|
|
$authForm->clientToken = Uuid::uuid4();
|
|
|
|
|
|
|
|
$authForm->authenticate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthenticateByValidCredentials() {
|
|
|
|
$authForm = $this->createAuthForm();
|
|
|
|
|
|
|
|
$minecraftAccessKey = new MinecraftAccessKey();
|
|
|
|
$minecraftAccessKey->access_token = Uuid::uuid4();
|
|
|
|
$authForm->expects($this->once())
|
|
|
|
->method('createMinecraftAccessToken')
|
2019-08-02 18:27:17 +05:30
|
|
|
->willReturn($minecraftAccessKey);
|
2016-08-29 04:47:45 +05:30
|
|
|
|
|
|
|
$authForm->username = 'dummy';
|
|
|
|
$authForm->password = 'password_0';
|
|
|
|
$authForm->clientToken = Uuid::uuid4();
|
|
|
|
|
|
|
|
$result = $authForm->authenticate();
|
|
|
|
$this->assertInstanceOf(AuthenticateData::class, $result);
|
2019-12-04 23:40:15 +05:30
|
|
|
$this->assertSame($minecraftAccessKey->access_token, $result->getToken()->access_token);
|
2016-08-29 04:47:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateMinecraftAccessToken() {
|
|
|
|
$authForm = new AuthenticationForm();
|
|
|
|
$authForm->clientToken = Uuid::uuid4();
|
|
|
|
/** @var Account $account */
|
2016-10-29 03:17:31 +05:30
|
|
|
$account = $this->tester->grabFixture('accounts', 'admin');
|
2016-08-29 04:47:45 +05:30
|
|
|
/** @var MinecraftAccessKey $result */
|
|
|
|
$result = $this->callProtected($authForm, 'createMinecraftAccessToken', $account);
|
|
|
|
$this->assertInstanceOf(MinecraftAccessKey::class, $result);
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame($account->id, $result->account_id);
|
|
|
|
$this->assertSame($authForm->clientToken, $result->client_token);
|
2016-10-15 20:05:03 +05:30
|
|
|
$this->assertInstanceOf(MinecraftAccessKey::class, MinecraftAccessKey::findOne($result->access_token));
|
2016-08-29 04:47:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateMinecraftAccessTokenWithExistsClientId() {
|
|
|
|
$authForm = new AuthenticationForm();
|
2016-10-29 03:17:31 +05:30
|
|
|
$minecraftFixture = $this->tester->grabFixture('minecraftAccessKeys', 'admin-token');
|
|
|
|
$authForm->clientToken = $minecraftFixture['client_token'];
|
2016-08-29 04:47:45 +05:30
|
|
|
/** @var Account $account */
|
2016-10-29 03:17:31 +05:30
|
|
|
$account = $this->tester->grabFixture('accounts', 'admin');
|
2016-08-29 04:47:45 +05:30
|
|
|
/** @var MinecraftAccessKey $result */
|
|
|
|
$result = $this->callProtected($authForm, 'createMinecraftAccessToken', $account);
|
|
|
|
$this->assertInstanceOf(MinecraftAccessKey::class, $result);
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame($account->id, $result->account_id);
|
|
|
|
$this->assertSame($authForm->clientToken, $result->client_token);
|
2016-10-29 03:17:31 +05:30
|
|
|
$this->assertNull(MinecraftAccessKey::findOne($minecraftFixture['access_token']));
|
2016-10-15 20:05:03 +05:30
|
|
|
$this->assertInstanceOf(MinecraftAccessKey::class, MinecraftAccessKey::findOne($result->access_token));
|
2016-08-29 04:47:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private function createAuthForm($status = Account::STATUS_ACTIVE) {
|
2019-05-14 04:28:29 +05:30
|
|
|
/** @var LoginForm|\PHPUnit\Framework\MockObject\MockObject $loginForm */
|
2016-08-29 04:47:45 +05:30
|
|
|
$loginForm = $this->getMockBuilder(LoginForm::class)
|
|
|
|
->setMethods(['getAccount'])
|
|
|
|
->getMock();
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
$account = new Account();
|
2016-08-29 04:47:45 +05:30
|
|
|
$account->username = 'dummy';
|
|
|
|
$account->email = 'dummy@ely.by';
|
|
|
|
$account->status = $status;
|
|
|
|
$account->setPassword('password_0');
|
|
|
|
|
2019-08-02 18:27:17 +05:30
|
|
|
$loginForm
|
2016-08-29 04:47:45 +05:30
|
|
|
->method('getAccount')
|
2019-08-02 18:27:17 +05:30
|
|
|
->willReturn($account);
|
2016-08-29 04:47:45 +05:30
|
|
|
|
2019-05-14 04:28:29 +05:30
|
|
|
/** @var AuthenticationForm|\PHPUnit\Framework\MockObject\MockObject $authForm */
|
2016-08-29 04:47:45 +05:30
|
|
|
$authForm = $this->getMockBuilder(AuthenticationForm::class)
|
|
|
|
->setMethods(['createLoginForm', 'createMinecraftAccessToken'])
|
|
|
|
->getMock();
|
|
|
|
|
2019-08-02 18:27:17 +05:30
|
|
|
$authForm
|
2016-08-29 04:47:45 +05:30
|
|
|
->method('createLoginForm')
|
2019-08-02 18:27:17 +05:30
|
|
|
->willReturn($loginForm);
|
2016-08-29 04:47:45 +05:30
|
|
|
|
|
|
|
return $authForm;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|