2016-09-01 13:01:43 +05:30
|
|
|
<?php
|
2019-12-04 23:40:15 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace api\tests\functional\authserver;
|
2016-09-01 13:01:43 +05:30
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
use api\tests\FunctionalTester;
|
2019-12-04 23:40:15 +05:30
|
|
|
use Codeception\Example;
|
2021-03-08 15:56:47 +05:30
|
|
|
use OTPHP\TOTP;
|
2019-02-23 04:41:57 +05:30
|
|
|
use Ramsey\Uuid\Uuid;
|
2016-09-01 13:01:43 +05:30
|
|
|
|
|
|
|
class AuthorizationCest {
|
|
|
|
|
|
|
|
/**
|
2021-03-06 15:07:58 +05:30
|
|
|
* # Matrix:
|
|
|
|
* # * login: username/email
|
|
|
|
* # * requestUser: true/false
|
|
|
|
* # * json: true/false
|
|
|
|
*
|
|
|
|
* JSON: false
|
2019-12-04 23:40:15 +05:30
|
|
|
* @example {"login": "admin", "password": "password_0"}
|
2021-03-06 15:07:58 +05:30
|
|
|
* @example {"login": "admin", "password": "password_0", "requestUser": true}
|
2019-12-04 23:40:15 +05:30
|
|
|
* @example {"login": "admin@ely.by", "password": "password_0"}
|
2021-03-06 15:07:58 +05:30
|
|
|
* @example {"login": "admin@ely.by", "password": "password_0", "requestUser": true}
|
|
|
|
*
|
|
|
|
* JSON: true
|
|
|
|
* @example {"json": true, "login": "admin", "password": "password_0"}
|
|
|
|
* @example {"json": true, "login": "admin", "password": "password_0", "requestUser": true}
|
|
|
|
* @example {"json": true, "login": "admin@ely.by", "password": "password_0"}
|
|
|
|
* @example {"json": true, "login": "admin@ely.by", "password": "password_0", "requestUser": true}
|
2016-09-01 13:01:43 +05:30
|
|
|
*/
|
2021-03-06 15:07:58 +05:30
|
|
|
public function authenticate(FunctionalTester $I, Example $case) {
|
|
|
|
$params = [
|
|
|
|
'username' => $case['login'],
|
|
|
|
'password' => $case['password'],
|
2016-09-01 13:01:43 +05:30
|
|
|
'clientToken' => Uuid::uuid4()->toString(),
|
2021-03-06 15:07:58 +05:30
|
|
|
];
|
|
|
|
if ($case['requestUser'] ?? false) {
|
|
|
|
$params['requestUser'] = true;
|
|
|
|
}
|
2016-09-01 13:01:43 +05:30
|
|
|
|
2021-03-06 15:07:58 +05:30
|
|
|
if ($case['json'] ?? false) {
|
|
|
|
$params = json_encode($params);
|
|
|
|
}
|
2016-09-01 13:01:43 +05:30
|
|
|
|
2021-03-06 15:07:58 +05:30
|
|
|
$I->sendPOST('/api/authserver/authentication/authenticate', $params);
|
2017-05-31 05:40:22 +05:30
|
|
|
|
2021-03-06 15:07:58 +05:30
|
|
|
$I->canSeeResponseJsonMatchesJsonPath('$.accessToken');
|
|
|
|
$I->canSeeResponseJsonMatchesJsonPath('$.clientToken');
|
|
|
|
$I->canSeeResponseContainsJson([
|
|
|
|
'selectedProfile' => [
|
|
|
|
'id' => 'df936908b2e1544d96f82977ec213022',
|
|
|
|
'name' => 'Admin',
|
|
|
|
],
|
|
|
|
'availableProfiles' => [
|
|
|
|
[
|
|
|
|
'id' => 'df936908b2e1544d96f82977ec213022',
|
|
|
|
'name' => 'Admin',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($case['requestUser'] ?? false) {
|
|
|
|
$I->canSeeResponseContainsJson([
|
|
|
|
'user' => [
|
|
|
|
'id' => 'df936908b2e1544d96f82977ec213022',
|
|
|
|
'username' => 'Admin',
|
|
|
|
'properties' => [
|
|
|
|
[
|
|
|
|
'name' => 'preferredLanguage',
|
|
|
|
'value' => 'en',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$I->cantSeeResponseJsonMatchesJsonPath('$.user');
|
|
|
|
}
|
2017-05-31 05:40:22 +05:30
|
|
|
}
|
|
|
|
|
2017-02-23 22:45:03 +05:30
|
|
|
public function byEmailWithEnabledTwoFactorAuth(FunctionalTester $I) {
|
|
|
|
$I->wantTo('get valid error by authenticate account with enabled two factor auth');
|
2019-12-04 23:40:15 +05:30
|
|
|
$I->sendPOST('/api/authserver/authentication/authenticate', [
|
2017-02-23 22:45:03 +05:30
|
|
|
'username' => 'otp@gmail.com',
|
|
|
|
'password' => 'password_0',
|
|
|
|
'clientToken' => Uuid::uuid4()->toString(),
|
|
|
|
]);
|
|
|
|
$I->canSeeResponseCodeIs(401);
|
|
|
|
$I->canSeeResponseIsJson();
|
|
|
|
$I->canSeeResponseContainsJson([
|
|
|
|
'error' => 'ForbiddenOperationException',
|
|
|
|
'errorMessage' => 'Account protected with two factor auth.',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-03-08 15:56:47 +05:30
|
|
|
public function byEmailWithEnabledTwoFactorAuthAndCorrectToken(FunctionalTester $I) {
|
|
|
|
$I->sendPOST('/api/authserver/authentication/authenticate', [
|
|
|
|
'username' => 'otp@gmail.com',
|
2021-03-09 02:51:10 +05:30
|
|
|
'password' => 'password_0:' . TOTP::create('BBBB')->now(),
|
2021-03-08 15:56:47 +05:30
|
|
|
'clientToken' => Uuid::uuid4()->toString(),
|
|
|
|
]);
|
|
|
|
$I->canSeeResponseCodeIs(200);
|
|
|
|
$I->canSeeResponseContainsJson([
|
|
|
|
'selectedProfile' => [
|
|
|
|
'id' => '15d0afa7a2bb44d39f31964cbccc6043',
|
|
|
|
'name' => 'AccountWithEnabledOtp',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-11-24 14:55:22 +05:30
|
|
|
public function withoutClientToken(FunctionalTester $I): void {
|
|
|
|
$I->sendPOST('/api/authserver/authentication/authenticate', [
|
|
|
|
'username' => 'admin',
|
|
|
|
'password' => 'password_0',
|
|
|
|
]);
|
|
|
|
$I->canSeeResponseCodeIs(200);
|
|
|
|
$clientToken = $I->grabDataFromResponseByJsonPath('$.clientToken')[0];
|
|
|
|
$I->assertNotEmpty($clientToken);
|
|
|
|
}
|
|
|
|
|
2018-01-02 22:52:56 +05:30
|
|
|
public function tooLongClientToken(FunctionalTester $I) {
|
|
|
|
$I->wantTo('send non uuid clientToken with more then 255 characters length');
|
2019-12-04 23:40:15 +05:30
|
|
|
$I->sendPOST('/api/authserver/authentication/authenticate', [
|
2018-01-02 22:52:56 +05:30
|
|
|
'username' => 'admin@ely.by',
|
|
|
|
'password' => 'password_0',
|
|
|
|
'clientToken' => str_pad('', 256, 'x'),
|
|
|
|
]);
|
|
|
|
$I->canSeeResponseCodeIs(400);
|
|
|
|
$I->canSeeResponseIsJson();
|
|
|
|
$I->canSeeResponseContainsJson([
|
|
|
|
'error' => 'IllegalArgumentException',
|
|
|
|
'errorMessage' => 'clientToken is too long.',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-09-01 13:01:43 +05:30
|
|
|
public function wrongArguments(FunctionalTester $I) {
|
|
|
|
$I->wantTo('get error on wrong amount of arguments');
|
2019-12-04 23:40:15 +05:30
|
|
|
$I->sendPOST('/api/authserver/authentication/authenticate', [
|
2016-09-01 13:01:43 +05:30
|
|
|
'key' => 'value',
|
|
|
|
]);
|
|
|
|
$I->canSeeResponseCodeIs(400);
|
|
|
|
$I->canSeeResponseIsJson();
|
|
|
|
$I->canSeeResponseContainsJson([
|
|
|
|
'error' => 'IllegalArgumentException',
|
|
|
|
'errorMessage' => 'credentials can not be null.',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function wrongNicknameAndPassword(FunctionalTester $I) {
|
|
|
|
$I->wantTo('authenticate by username and password with wrong data');
|
2019-12-04 23:40:15 +05:30
|
|
|
$I->sendPOST('/api/authserver/authentication/authenticate', [
|
2016-09-01 13:01:43 +05:30
|
|
|
'username' => 'nonexistent_user',
|
|
|
|
'password' => 'nonexistent_password',
|
|
|
|
'clientToken' => Uuid::uuid4()->toString(),
|
|
|
|
]);
|
|
|
|
$I->canSeeResponseCodeIs(401);
|
|
|
|
$I->canSeeResponseIsJson();
|
|
|
|
$I->canSeeResponseContainsJson([
|
|
|
|
'error' => 'ForbiddenOperationException',
|
|
|
|
'errorMessage' => 'Invalid credentials. Invalid nickname or password.',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
public function deletedAccount(FunctionalTester $I) {
|
|
|
|
$I->wantTo('authenticate in account marked for deletion');
|
|
|
|
$I->sendPOST('/api/authserver/authentication/authenticate', [
|
|
|
|
'username' => 'DeletedAccount',
|
|
|
|
'password' => 'password_0',
|
|
|
|
'clientToken' => Uuid::uuid4()->toString(),
|
|
|
|
]);
|
|
|
|
$I->canSeeResponseCodeIs(401);
|
|
|
|
$I->canSeeResponseContainsJson([
|
|
|
|
'error' => 'ForbiddenOperationException',
|
|
|
|
'errorMessage' => 'Invalid credentials. Invalid nickname or password.',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-09-01 13:01:43 +05:30
|
|
|
public function bannedAccount(FunctionalTester $I) {
|
|
|
|
$I->wantTo('authenticate in suspended account');
|
2019-12-04 23:40:15 +05:30
|
|
|
$I->sendPOST('/api/authserver/authentication/authenticate', [
|
2016-09-01 13:01:43 +05:30
|
|
|
'username' => 'Banned',
|
|
|
|
'password' => 'password_0',
|
|
|
|
'clientToken' => Uuid::uuid4()->toString(),
|
|
|
|
]);
|
|
|
|
$I->canSeeResponseCodeIs(401);
|
|
|
|
$I->canSeeResponseContainsJson([
|
|
|
|
'error' => 'ForbiddenOperationException',
|
|
|
|
'errorMessage' => 'This account has been suspended.',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|