|
| 1 | +<?php |
| 2 | + |
| 3 | +use Carbon\CarbonImmutable; |
| 4 | +use Laravel\Passport\Bridge\AccessToken; |
| 5 | +use Laravel\Passport\Bridge\AccessTokenRepository as BridgeAccessTokenRepository; |
| 6 | +use Laravel\Passport\Bridge\AuthCode; |
| 7 | +use Laravel\Passport\Bridge\AuthCodeRepository as BridgeAuthCodeRepository; |
| 8 | +use Laravel\Passport\Bridge\RefreshToken; |
| 9 | +use Laravel\Passport\Bridge\RefreshTokenRepository as BridgeRefreshTokenRepository; |
| 10 | +use Laravel\Passport\RefreshTokenRepository; |
| 11 | +use Laravel\Passport\Tests\Feature\PassportTestCase; |
| 12 | +use Laravel\Passport\TokenRepository; |
| 13 | +use Mockery as m; |
| 14 | +use Orchestra\Testbench\Concerns\WithLaravelMigrations; |
| 15 | + |
| 16 | +class RevokedTest extends PassportTestCase |
| 17 | +{ |
| 18 | + use WithLaravelMigrations; |
| 19 | + |
| 20 | + public function test_it_can_determine_if_a_access_token_is_revoked() |
| 21 | + { |
| 22 | + $repository = $this->accessTokenRepository(); |
| 23 | + $this->persistNewAccessToken($repository, 'tokenId'); |
| 24 | + |
| 25 | + $repository->revokeAccessToken('tokenId'); |
| 26 | + |
| 27 | + $this->assertTrue($repository->isAccessTokenRevoked('tokenId')); |
| 28 | + } |
| 29 | + |
| 30 | + public function test_a_access_token_is_also_revoked_if_it_cannot_be_found() |
| 31 | + { |
| 32 | + $repository = $this->accessTokenRepository(); |
| 33 | + |
| 34 | + $this->assertTrue($repository->isAccessTokenRevoked('notExistingTokenId')); |
| 35 | + } |
| 36 | + |
| 37 | + public function test_it_can_determine_if_a_access_token_is_not_revoked() |
| 38 | + { |
| 39 | + $repository = $this->accessTokenRepository(); |
| 40 | + $this->persistNewAccessToken($repository, 'tokenId'); |
| 41 | + |
| 42 | + $this->assertFalse($repository->isAccessTokenRevoked('tokenId')); |
| 43 | + } |
| 44 | + |
| 45 | + public function test_it_can_determine_if_a_auth_code_is_revoked() |
| 46 | + { |
| 47 | + $repository = $this->authCodeRepository(); |
| 48 | + $this->persistNewAuthCode($repository, 'tokenId'); |
| 49 | + |
| 50 | + $repository->revokeAuthCode('tokenId'); |
| 51 | + |
| 52 | + $this->assertTrue($repository->isAuthCodeRevoked('tokenId')); |
| 53 | + } |
| 54 | + |
| 55 | + public function test_a_auth_code_is_also_revoked_if_it_cannot_be_found() |
| 56 | + { |
| 57 | + $repository = $this->authCodeRepository(); |
| 58 | + |
| 59 | + $this->assertTrue($repository->isAuthCodeRevoked('notExistingTokenId')); |
| 60 | + } |
| 61 | + |
| 62 | + public function test_it_can_determine_if_a_auth_code_is_not_revoked() |
| 63 | + { |
| 64 | + $repository = $this->authCodeRepository(); |
| 65 | + $this->persistNewAuthCode($repository, 'tokenId'); |
| 66 | + |
| 67 | + $this->assertFalse($repository->isAuthCodeRevoked('tokenId')); |
| 68 | + } |
| 69 | + |
| 70 | + public function test_it_can_determine_if_a_refresh_token_is_revoked() |
| 71 | + { |
| 72 | + $repository = $this->refreshTokenRepository(); |
| 73 | + $this->persistNewRefreshToken($repository, 'tokenId'); |
| 74 | + |
| 75 | + $repository->revokeRefreshToken('tokenId'); |
| 76 | + |
| 77 | + $this->assertTrue($repository->isRefreshTokenRevoked('tokenId')); |
| 78 | + } |
| 79 | + |
| 80 | + public function test_a_refresh_token_is_also_revoked_if_it_cannot_be_found() |
| 81 | + { |
| 82 | + $repository = $this->refreshTokenRepository(); |
| 83 | + |
| 84 | + $this->assertTrue($repository->isRefreshTokenRevoked('notExistingTokenId')); |
| 85 | + } |
| 86 | + |
| 87 | + public function test_it_can_determine_if_a_refresh_token_is_not_revoked() |
| 88 | + { |
| 89 | + $repository = $this->refreshTokenRepository(); |
| 90 | + $this->persistNewRefreshToken($repository, 'tokenId'); |
| 91 | + |
| 92 | + $this->assertFalse($repository->isRefreshTokenRevoked('tokenId')); |
| 93 | + } |
| 94 | + |
| 95 | + private function accessTokenRepository(): BridgeAccessTokenRepository |
| 96 | + { |
| 97 | + $events = m::mock('Illuminate\Contracts\Events\Dispatcher'); |
| 98 | + $events->shouldReceive('dispatch'); |
| 99 | + |
| 100 | + return new BridgeAccessTokenRepository(new TokenRepository, $events); |
| 101 | + } |
| 102 | + |
| 103 | + private function persistNewAccessToken(BridgeAccessTokenRepository $repository, string $id): void |
| 104 | + { |
| 105 | + $accessToken = m::mock(AccessToken::class); |
| 106 | + $accessToken->shouldReceive('getIdentifier')->andReturn($id); |
| 107 | + $accessToken->shouldReceive('getUserIdentifier')->andReturn('1'); |
| 108 | + $accessToken->shouldReceive('getClient->getIdentifier')->andReturn('clientId'); |
| 109 | + $accessToken->shouldReceive('getScopes')->andReturn([]); |
| 110 | + $accessToken->shouldReceive('getExpiryDateTime')->andReturn(CarbonImmutable::now()); |
| 111 | + |
| 112 | + $repository->persistNewAccessToken($accessToken); |
| 113 | + } |
| 114 | + |
| 115 | + private function authCodeRepository(): BridgeAuthCodeRepository |
| 116 | + { |
| 117 | + return new BridgeAuthCodeRepository; |
| 118 | + } |
| 119 | + |
| 120 | + private function persistNewAuthCode(BridgeAuthCodeRepository $repository, string $id): void |
| 121 | + { |
| 122 | + $authCode = m::mock(AuthCode::class); |
| 123 | + $authCode->shouldReceive('getIdentifier')->andReturn($id); |
| 124 | + $authCode->shouldReceive('getUserIdentifier')->andReturn('1'); |
| 125 | + $authCode->shouldReceive('getClient->getIdentifier')->andReturn('clientId'); |
| 126 | + $authCode->shouldReceive('getExpiryDateTime')->andReturn(CarbonImmutable::now()); |
| 127 | + $authCode->shouldReceive('getScopes')->andReturn([]); |
| 128 | + |
| 129 | + $repository->persistNewAuthCode($authCode); |
| 130 | + } |
| 131 | + |
| 132 | + private function refreshTokenRepository(): BridgeRefreshTokenRepository |
| 133 | + { |
| 134 | + $events = m::mock('Illuminate\Contracts\Events\Dispatcher'); |
| 135 | + $events->shouldReceive('dispatch'); |
| 136 | + |
| 137 | + return new BridgeRefreshTokenRepository(new RefreshTokenRepository, $events); |
| 138 | + } |
| 139 | + |
| 140 | + private function persistNewRefreshToken(BridgeRefreshTokenRepository $repository, string $id): void |
| 141 | + { |
| 142 | + $refreshToken = m::mock(RefreshToken::class); |
| 143 | + $refreshToken->shouldReceive('getIdentifier')->andReturn($id); |
| 144 | + $refreshToken->shouldReceive('getAccessToken->getIdentifier')->andReturn('accessTokenId'); |
| 145 | + $refreshToken->shouldReceive('getExpiryDateTime')->andReturn(CarbonImmutable::now()); |
| 146 | + |
| 147 | + $repository->persistNewRefreshToken($refreshToken); |
| 148 | + } |
| 149 | +} |
0 commit comments