-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[shopsys] immediate access token revocation (#3417)
- Loading branch information
Showing
6 changed files
with
138 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
app/tests/FrontendApiBundle/Functional/Login/LoginTokenInvalidationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\FrontendApiBundle\Functional\Login; | ||
|
||
use Tests\FrontendApiBundle\Test\GraphQlTestCase; | ||
|
||
class LoginTokenInvalidationTest extends GraphQlTestCase | ||
{ | ||
public function testAccessTokenIsNotValidAfterPasswordChange(): void | ||
{ | ||
$this->loginCustomerUser(); | ||
|
||
$this->checkAccessTokenIsValid(); | ||
|
||
$this->changeCustomerUserPassword(); | ||
|
||
$this->checkAccessTokenIsRevoked(); | ||
} | ||
|
||
private function loginCustomerUser(): void | ||
{ | ||
$response = $this->getResponseContentForGql( | ||
__DIR__ . '/graphql/LoginMutation.graphql', | ||
$this->getDefaultCredentials(), | ||
); | ||
$responseData = $this->getResponseDataForGraphQlType($response, 'Login'); | ||
|
||
$this->assertArrayHasKey('tokens', $responseData); | ||
$this->assertArrayHasKey('accessToken', $responseData['tokens']); | ||
$accessToken = $responseData['tokens']['accessToken']; | ||
|
||
$clientOptions = ['HTTP_X-Auth-Token' => sprintf('Bearer %s', $accessToken)]; | ||
$this->configureCurrentClient(null, null, $clientOptions); | ||
} | ||
|
||
private function checkAccessTokenIsValid(): void | ||
{ | ||
$response = $this->getResponseContentForGql( | ||
__DIR__ . '/../_graphql/query/CurrentCustomerUserQuery.graphql', | ||
); | ||
$responseData = $this->getResponseDataForGraphQlType($response, 'currentCustomerUser'); | ||
|
||
$this->assertArrayHasKey('email', $responseData); | ||
$this->assertSame($this->getDefaultCredentials()['email'], $responseData['email']); | ||
} | ||
|
||
private function changeCustomerUserPassword(): void | ||
{ | ||
$response = $this->getResponseContentForGql( | ||
__DIR__ . '/graphql/ChangePasswordMutation.graphql', | ||
[ | ||
'email' => $this->getDefaultCredentials()['email'], | ||
'oldPassword' => $this->getDefaultCredentials()['password'], | ||
'newPassword' => 'user124', | ||
], | ||
); | ||
$responseData = $this->getResponseDataForGraphQlType($response, 'ChangePassword'); | ||
|
||
$this->assertArrayHasKey('email', $responseData); | ||
$this->assertSame($this->getDefaultCredentials()['email'], $responseData['email']); | ||
} | ||
|
||
private function checkAccessTokenIsRevoked(): void | ||
{ | ||
$response = $this->getResponseContentForGql( | ||
__DIR__ . '/../_graphql/query/CurrentCustomerUserQuery.graphql', | ||
); | ||
$this->assertResponseContainsArrayOfErrors($response); | ||
|
||
$this->assertSame('Token is not valid.', $response['errors'][0]['message']); | ||
$this->assertSame('invalid-token', $response['errors'][0]['extensions']['userCode']); | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
private function getDefaultCredentials(): array | ||
{ | ||
return [ | ||
'email' => 'no-reply@shopsys.com', | ||
'password' => 'user123', | ||
]; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
app/tests/FrontendApiBundle/Functional/Login/graphql/ChangePasswordMutation.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
mutation ChangePasswordMutation ( | ||
$email: String! | ||
$oldPassword: Password! | ||
$newPassword: Password! | ||
) { | ||
ChangePassword(input: { | ||
email: $email | ||
oldPassword: $oldPassword | ||
newPassword: $newPassword | ||
}) { | ||
firstName | ||
lastName | ||
telephone | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
app/tests/FrontendApiBundle/Functional/Login/graphql/LoginMutation.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
mutation LoginMutation ($email: String!, $password: Password!) { | ||
Login(input: { | ||
email: $email | ||
password: $password | ||
}) { | ||
tokens { | ||
accessToken | ||
refreshToken | ||
} | ||
} | ||
} |