|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace League\Bundle\OAuth2ServerBundle\Tests\Acceptance; |
| 6 | + |
| 7 | +use League\Bundle\OAuth2ServerBundle\Event\UserResolveEvent; |
| 8 | +use League\Bundle\OAuth2ServerBundle\Manager\AccessTokenManagerInterface; |
| 9 | +use League\Bundle\OAuth2ServerBundle\Manager\AuthorizationCodeManagerInterface; |
| 10 | +use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface; |
| 11 | +use League\Bundle\OAuth2ServerBundle\Manager\RefreshTokenManagerInterface; |
| 12 | +use League\Bundle\OAuth2ServerBundle\Model\AccessToken; |
| 13 | +use League\Bundle\OAuth2ServerBundle\Model\AuthorizationCode; |
| 14 | +use League\Bundle\OAuth2ServerBundle\Model\Client; |
| 15 | +use League\Bundle\OAuth2ServerBundle\Model\RefreshToken; |
| 16 | +use League\Bundle\OAuth2ServerBundle\OAuth2Events; |
| 17 | +use League\Bundle\OAuth2ServerBundle\Service\CredentialsRevokerInterface; |
| 18 | +use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeAccessTokenManager; |
| 19 | +use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeAuthorizationCodeManager; |
| 20 | +use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeClientManager; |
| 21 | +use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeCredentialsRevoker; |
| 22 | +use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FakeRefreshTokenManager; |
| 23 | +use League\Bundle\OAuth2ServerBundle\Tests\Fixtures\FixtureFactory; |
| 24 | +use League\Bundle\OAuth2ServerBundle\Tests\TestHelper; |
| 25 | +use League\Bundle\OAuth2ServerBundle\Tests\TestKernel; |
| 26 | +use League\Bundle\OAuth2ServerBundle\ValueObject\RedirectUri; |
| 27 | +use PHPUnit\Framework\MockObject\MockObject; |
| 28 | +use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 29 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 30 | + |
| 31 | +class CustomPersistenceManagerTest extends AbstractAcceptanceTest |
| 32 | +{ |
| 33 | + private AccessTokenManagerInterface&MockObject $accessTokenManager; |
| 34 | + private ClientManagerInterface&MockObject $clientManager; |
| 35 | + private RefreshTokenManagerInterface&MockObject $refreshTokenManager; |
| 36 | + private AuthorizationCodeManagerInterface&MockObject $authCodeManager; |
| 37 | + |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + $this->client = self::createClient(); |
| 41 | + $this->accessTokenManager = $this->createMock(AccessTokenManagerInterface::class); |
| 42 | + $this->clientManager = $this->createMock(ClientManagerInterface::class); |
| 43 | + $this->refreshTokenManager = $this->createMock(RefreshTokenManagerInterface::class); |
| 44 | + $this->authCodeManager = $this->createMock(AuthorizationCodeManagerInterface::class); |
| 45 | + $this->application = new Application($this->client->getKernel()); |
| 46 | + } |
| 47 | + |
| 48 | + public function testRegisteredServices(): void |
| 49 | + { |
| 50 | + static::assertInstanceOf(FakeAccessTokenManager::class, $this->client->getContainer()->get(AccessTokenManagerInterface::class)); |
| 51 | + static::assertInstanceOf(FakeAuthorizationCodeManager::class, $this->client->getContainer()->get(AuthorizationCodeManagerInterface::class)); |
| 52 | + static::assertInstanceOf(FakeClientManager::class, $this->client->getContainer()->get(ClientManagerInterface::class)); |
| 53 | + static::assertInstanceOf(FakeRefreshTokenManager::class, $this->client->getContainer()->get(RefreshTokenManagerInterface::class)); |
| 54 | + static::assertInstanceOf(FakeCredentialsRevoker::class, $this->client->getContainer()->get(CredentialsRevokerInterface::class)); |
| 55 | + } |
| 56 | + |
| 57 | + public function testSuccessfulClientCredentialsRequest(): void |
| 58 | + { |
| 59 | + $this->accessTokenManager->expects(self::atLeastOnce())->method('find')->willReturn(null); |
| 60 | + $this->accessTokenManager->expects(self::atLeastOnce())->method('save'); |
| 61 | + $this->client->getContainer()->set('test.access_token_manager', $this->accessTokenManager); |
| 62 | + |
| 63 | + $this->clientManager->expects(self::atLeastOnce())->method('find')->with('foo')->willReturn(new Client('name', 'foo', 'secret')); |
| 64 | + $this->client->getContainer()->set('test.client_manager', $this->clientManager); |
| 65 | + |
| 66 | + $this->client->request('POST', '/token', [ |
| 67 | + 'client_id' => 'foo', |
| 68 | + 'client_secret' => 'secret', |
| 69 | + 'grant_type' => 'client_credentials', |
| 70 | + ]); |
| 71 | + |
| 72 | + $this->client->getResponse(); |
| 73 | + static::assertResponseIsSuccessful(); |
| 74 | + } |
| 75 | + |
| 76 | + public function testSuccessfulPasswordRequest(): void |
| 77 | + { |
| 78 | + $this->accessTokenManager->expects(self::atLeastOnce())->method('find')->willReturn(null); |
| 79 | + $this->accessTokenManager->expects(self::atLeastOnce())->method('save'); |
| 80 | + $this->client->getContainer()->set('test.access_token_manager', $this->accessTokenManager); |
| 81 | + |
| 82 | + $this->clientManager->expects(self::atLeastOnce())->method('find')->with('foo')->willReturn(new Client('name', 'foo', 'secret')); |
| 83 | + $this->client->getContainer()->set('test.client_manager', $this->clientManager); |
| 84 | + |
| 85 | + $eventDispatcher = $this->client->getContainer()->get('event_dispatcher'); |
| 86 | + $eventDispatcher->addListener(OAuth2Events::USER_RESOLVE, static function (UserResolveEvent $event): void { |
| 87 | + $event->setUser(FixtureFactory::createUser()); |
| 88 | + }); |
| 89 | + |
| 90 | + $this->client->request('POST', '/token', [ |
| 91 | + 'client_id' => 'foo', |
| 92 | + 'client_secret' => 'secret', |
| 93 | + 'grant_type' => 'password', |
| 94 | + 'username' => 'user', |
| 95 | + 'password' => 'pass', |
| 96 | + ]); |
| 97 | + |
| 98 | + $this->client->getResponse(); |
| 99 | + static::assertResponseIsSuccessful(); |
| 100 | + } |
| 101 | + |
| 102 | + public function testSuccessfulRefreshTokenRequest(): void |
| 103 | + { |
| 104 | + $client = new Client('name', 'foo', 'secret'); |
| 105 | + $accessToken = new AccessToken('access_token', new \DateTimeImmutable('+1 hour'), $client, 'user', []); |
| 106 | + $refreshToken = new RefreshToken('refresh_token', new \DateTimeImmutable('+1 month'), $accessToken); |
| 107 | + |
| 108 | + $this->refreshTokenManager->expects(self::atLeastOnce())->method('find')->willReturn($refreshToken, null); |
| 109 | + $this->client->getContainer()->set('test.refresh_token_manager', $this->refreshTokenManager); |
| 110 | + |
| 111 | + $this->accessTokenManager->expects(self::atLeastOnce())->method('find')->willReturn($accessToken, null); |
| 112 | + $this->accessTokenManager->expects(self::atLeastOnce())->method('save'); |
| 113 | + $this->client->getContainer()->set('test.access_token_manager', $this->accessTokenManager); |
| 114 | + |
| 115 | + $this->clientManager->expects(self::atLeastOnce())->method('find')->with('foo')->willReturn($client); |
| 116 | + $this->client->getContainer()->set('test.client_manager', $this->clientManager); |
| 117 | + |
| 118 | + $this->client->request('POST', '/token', [ |
| 119 | + 'client_id' => 'foo', |
| 120 | + 'client_secret' => 'secret', |
| 121 | + 'grant_type' => 'refresh_token', |
| 122 | + 'refresh_token' => TestHelper::generateEncryptedPayload($refreshToken), |
| 123 | + ]); |
| 124 | + |
| 125 | + $this->client->getResponse(); |
| 126 | + static::assertResponseIsSuccessful(); |
| 127 | + } |
| 128 | + |
| 129 | + public function testSuccessfulAuthorizationCodeRequest(): void |
| 130 | + { |
| 131 | + $client = new Client('name', 'foo', 'secret'); |
| 132 | + $client->setRedirectUris(new RedirectUri('https://example.org/oauth2/redirect-uri')); |
| 133 | + $authCode = new AuthorizationCode('authorization_code', new \DateTimeImmutable('+2 minute'), $client, 'user', []); |
| 134 | + |
| 135 | + $this->authCodeManager->expects(self::atLeastOnce())->method('find')->willReturn($authCode, null); |
| 136 | + $this->client->getContainer()->set('test.authorization_code_manager', $this->authCodeManager); |
| 137 | + |
| 138 | + $this->accessTokenManager->expects(self::atLeastOnce())->method('find')->willReturn(null); |
| 139 | + $this->accessTokenManager->expects(self::atLeastOnce())->method('save'); |
| 140 | + $this->client->getContainer()->set('test.access_token_manager', $this->accessTokenManager); |
| 141 | + |
| 142 | + $this->clientManager->expects(self::atLeastOnce())->method('find')->with('foo')->willReturn($client); |
| 143 | + $this->client->getContainer()->set('test.client_manager', $this->clientManager); |
| 144 | + |
| 145 | + $this->client->request('POST', '/token', [ |
| 146 | + 'client_id' => 'foo', |
| 147 | + 'client_secret' => 'secret', |
| 148 | + 'grant_type' => 'authorization_code', |
| 149 | + 'redirect_uri' => 'https://example.org/oauth2/redirect-uri', |
| 150 | + 'code' => TestHelper::generateEncryptedAuthCodePayload($authCode), |
| 151 | + ]); |
| 152 | + |
| 153 | + $this->client->getResponse(); |
| 154 | + static::assertResponseIsSuccessful(); |
| 155 | + } |
| 156 | + |
| 157 | + protected static function createKernel(array $options = []): KernelInterface |
| 158 | + { |
| 159 | + return new TestKernel( |
| 160 | + 'test', |
| 161 | + false, |
| 162 | + [ |
| 163 | + 'custom' => [ |
| 164 | + 'access_token_manager' => 'test.access_token_manager', |
| 165 | + 'authorization_code_manager' => 'test.authorization_code_manager', |
| 166 | + 'client_manager' => 'test.client_manager', |
| 167 | + 'refresh_token_manager' => 'test.refresh_token_manager', |
| 168 | + 'credentials_revoker' => 'test.credentials_revoker', |
| 169 | + ], |
| 170 | + ] |
| 171 | + ); |
| 172 | + } |
| 173 | +} |
0 commit comments