|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ApiSkeletonsTest\Laravel\Doctrine\ApiKey\Feature\Http\Middleware; |
| 4 | + |
| 5 | +use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey; |
| 6 | +use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope; |
| 7 | +use ApiSkeletons\Laravel\Doctrine\ApiKey\Http\Middleware\AuthorizeApiKey; |
| 8 | +use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService; |
| 9 | +use ApiSkeletonsTest\Laravel\Doctrine\ApiKey\TestCase; |
| 10 | +use Illuminate\Http\Request; |
| 11 | + |
| 12 | +final class AuthorizeApiKeyTest extends TestCase |
| 13 | +{ |
| 14 | + public function testApiKeyAuthorizesRequest(): void |
| 15 | + { |
| 16 | + $entityManager = $this->createDatabase(app('em')); |
| 17 | + $apiKeyRepository = $entityManager->getRepository(ApiKey::class); |
| 18 | + |
| 19 | + $apiKey = $apiKeyRepository->generate('testing'); |
| 20 | + $entityManager->flush(); |
| 21 | + |
| 22 | + $request = Request::create('/apiresource', 'GET'); |
| 23 | + $request->headers->set('Authorization', 'Bearer ' . $apiKey->getKey()); |
| 24 | + |
| 25 | + $middleware = new AuthorizeApiKey(app(ApiKeyService::class)); |
| 26 | + |
| 27 | + $response = $middleware->handle($request, function() {}); |
| 28 | + $this->assertNull($response); |
| 29 | + } |
| 30 | + |
| 31 | + public function testApiKeyDoesNotAuthorizeRequest(): void |
| 32 | + { |
| 33 | + $entityManager = $this->createDatabase(app('em')); |
| 34 | + |
| 35 | + $request = Request::create('/apiresource', 'GET'); |
| 36 | + |
| 37 | + $middleware = new AuthorizeApiKey(app(ApiKeyService::class)); |
| 38 | + |
| 39 | + $response = $middleware->handle($request, function() {}); |
| 40 | + $this->assertNotNull($response); |
| 41 | + } |
| 42 | + |
| 43 | + public function testApiKeyAuthorizesRequestWithScope(): void |
| 44 | + { |
| 45 | + $entityManager = $this->createDatabase(app('em')); |
| 46 | + $apiKeyRepository = $entityManager->getRepository(ApiKey::class); |
| 47 | + $scopeRepository = $entityManager->getRepository(Scope::class); |
| 48 | + |
| 49 | + $apiKey = $apiKeyRepository->generate('testing'); |
| 50 | + $scope = $scopeRepository->generate('scopetest'); |
| 51 | + $entityManager->flush(); |
| 52 | + |
| 53 | + $apiKeyRepository->addScope($apiKey, $scope); |
| 54 | + $entityManager->flush(); |
| 55 | + |
| 56 | + $request = Request::create('/apiresource', 'GET'); |
| 57 | + $request->headers->set('Authorization', 'Bearer ' . $apiKey->getKey()); |
| 58 | + |
| 59 | + $middleware = new AuthorizeApiKey(app(ApiKeyService::class)); |
| 60 | + |
| 61 | + $response = $middleware->handle($request, function() {}, $scope->getName()); |
| 62 | + $this->assertNull($response); |
| 63 | + } |
| 64 | + |
| 65 | + public function testApiKeyDoesNotAuthorizeRequestWithInvalidScope(): void |
| 66 | + { |
| 67 | + $entityManager = $this->createDatabase(app('em')); |
| 68 | + $apiKeyRepository = $entityManager->getRepository(ApiKey::class); |
| 69 | + $scopeRepository = $entityManager->getRepository(Scope::class); |
| 70 | + |
| 71 | + $apiKey = $apiKeyRepository->generate('testing'); |
| 72 | + $scope = $scopeRepository->generate('scopetest'); |
| 73 | + $entityManager->flush(); |
| 74 | + |
| 75 | + $request = Request::create('/apiresource', 'GET'); |
| 76 | + $request->headers->set('Authorization', 'Bearer ' . $apiKey->getKey()); |
| 77 | + |
| 78 | + $middleware = new AuthorizeApiKey(app(ApiKeyService::class)); |
| 79 | + |
| 80 | + $response = $middleware->handle($request, function() {}, $scope->getName()); |
| 81 | + $this->assertNotNull($response); |
| 82 | + } |
| 83 | +} |
0 commit comments