2016-08-29 04:47:45 +05:30
|
|
|
<?php
|
|
|
|
namespace codeception\api\unit\modules\authserver\models;
|
|
|
|
|
|
|
|
use api\models\authentication\LoginForm;
|
|
|
|
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,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \api\modules\authserver\exceptions\ForbiddenOperationException
|
|
|
|
* @expectedExceptionMessage Invalid credentials. Invalid nickname or password.
|
|
|
|
*/
|
|
|
|
public function testAuthenticateByWrongNicknamePass() {
|
|
|
|
$authForm = $this->createAuthForm();
|
|
|
|
|
|
|
|
$authForm->username = 'wrong-username';
|
|
|
|
$authForm->password = 'wrong-password';
|
|
|
|
$authForm->clientToken = Uuid::uuid4();
|
|
|
|
|
|
|
|
$authForm->authenticate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \api\modules\authserver\exceptions\ForbiddenOperationException
|
|
|
|
* @expectedExceptionMessage Invalid credentials. Invalid email or password.
|
|
|
|
*/
|
|
|
|
public function testAuthenticateByWrongEmailPass() {
|
|
|
|
$authForm = $this->createAuthForm();
|
|
|
|
|
|
|
|
$authForm->username = 'wrong-email@ely.by';
|
|
|
|
$authForm->password = 'wrong-password';
|
|
|
|
$authForm->clientToken = Uuid::uuid4();
|
|
|
|
|
|
|
|
$authForm->authenticate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \api\modules\authserver\exceptions\ForbiddenOperationException
|
|
|
|
* @expectedExceptionMessage This account has been suspended.
|
|
|
|
*/
|
|
|
|
public function testAuthenticateByValidCredentialsIntoBlockedAccount() {
|
|
|
|
$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')
|
|
|
|
->will($this->returnValue($minecraftAccessKey));
|
|
|
|
|
|
|
|
$authForm->username = 'dummy';
|
|
|
|
$authForm->password = 'password_0';
|
|
|
|
$authForm->clientToken = Uuid::uuid4();
|
|
|
|
|
|
|
|
$result = $authForm->authenticate();
|
|
|
|
$this->assertInstanceOf(AuthenticateData::class, $result);
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame($minecraftAccessKey->access_token, $result->getMinecraftAccessKey()->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');
|
|
|
|
|
|
|
|
$loginForm->expects($this->any())
|
|
|
|
->method('getAccount')
|
|
|
|
->will($this->returnValue($account));
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
$authForm->expects($this->any())
|
|
|
|
->method('createLoginForm')
|
|
|
|
->will($this->returnValue($loginForm));
|
|
|
|
|
|
|
|
return $authForm;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|