|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/** |
| 4 | + * (c) Packagist Conductors GmbH <[email protected]> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PrivatePackagist\ApiClient\HttpClient\Plugin; |
| 11 | + |
| 12 | +use GuzzleHttp\Psr7\Request; |
| 13 | +use GuzzleHttp\Psr7\Response; |
| 14 | +use Http\Mock\Client; |
| 15 | +use Http\Promise\FulfilledPromise; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use PrivatePackagist\ApiClient\HttpClient\HttpPluginClientBuilder; |
| 18 | +use PrivatePackagist\OIDC\Identities\Token; |
| 19 | +use PrivatePackagist\OIDC\Identities\TokenGeneratorInterface; |
| 20 | + |
| 21 | +class TrustedPublishingTokenExchangeTest extends PluginTestCase |
| 22 | +{ |
| 23 | + /** @var TrustedPublishingTokenExchange */ |
| 24 | + private $plugin; |
| 25 | + /** @var Client */ |
| 26 | + private $httpClient; |
| 27 | + /** @var TokenGeneratorInterface&MockObject */ |
| 28 | + private $tokenGenerator; |
| 29 | + |
| 30 | + protected function setUp(): void |
| 31 | + { |
| 32 | + parent::setUp(); |
| 33 | + |
| 34 | + $this->plugin = new TrustedPublishingTokenExchange( |
| 35 | + 'organization', |
| 36 | + 'acme/package', |
| 37 | + new HttpPluginClientBuilder($this->httpClient = new Client()), |
| 38 | + $this->tokenGenerator = $this->createMock(TokenGeneratorInterface::class) |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + public function testTokenExchange(): void |
| 43 | + { |
| 44 | + $request = new Request('GET', '/api/packages/acme/package'); |
| 45 | + |
| 46 | + $this->tokenGenerator |
| 47 | + ->expects($this->once()) |
| 48 | + ->method('generate') |
| 49 | + ->with($this->identicalTo('test')) |
| 50 | + ->willReturn(Token::fromTokenString('test.test.test')); |
| 51 | + |
| 52 | + $this->httpClient->addResponse(new Response(200, [], json_encode(['audience' => 'test']))); |
| 53 | + $this->httpClient->addResponse(new Response(200, [], json_encode(['key' => 'key', 'secret' => 'secret']))); |
| 54 | + |
| 55 | + $this->plugin->handleRequest($request, function (Request $request) use (&$requestAfterPlugin) { |
| 56 | + $requestAfterPlugin = $request; |
| 57 | + |
| 58 | + return new FulfilledPromise($request); |
| 59 | + }, $this->first); |
| 60 | + |
| 61 | + $requests = $this->httpClient->getRequests(); |
| 62 | + $this->assertCount(2, $requests); |
| 63 | + $this->assertSame('/oidc/audience', (string) $requests[0]->getUri()); |
| 64 | + $this->assertSame('/oidc/token-exchange/organization/acme/package', (string) $requests[1]->getUri()); |
| 65 | + |
| 66 | + $this->assertStringContainsString('PACKAGIST-HMAC-SHA256 Key=key', $requestAfterPlugin->getHeader('Authorization')[0]); |
| 67 | + } |
| 68 | + |
| 69 | + public function testNoTokenGenerated(): void |
| 70 | + { |
| 71 | + $request = new Request('GET', '/api/packages/acme/package'); |
| 72 | + |
| 73 | + $this->tokenGenerator |
| 74 | + ->expects($this->once()) |
| 75 | + ->method('generate') |
| 76 | + ->with($this->identicalTo('test')) |
| 77 | + ->willReturn(null); |
| 78 | + |
| 79 | + $this->httpClient->addResponse(new Response(200, [], json_encode(['audience' => 'test']))); |
| 80 | + |
| 81 | + $this->expectException(\RuntimeException::class); |
| 82 | + $this->expectExceptionMessage('Unable to generate OIDC token'); |
| 83 | + |
| 84 | + $this->plugin->handleRequest($request, $this->next, $this->first); |
| 85 | + } |
| 86 | +} |
0 commit comments