|
3 | 3 | namespace TheCodingMachine\GraphQLite\Middlewares;
|
4 | 4 |
|
5 | 5 | use GraphQL\Type\Definition\FieldDefinition;
|
6 |
| -use PHPUnit\Framework\TestCase; |
7 |
| -use ReflectionMethod; |
8 | 6 | use TheCodingMachine\GraphQLite\AbstractQueryProviderTest;
|
9 | 7 | use TheCodingMachine\GraphQLite\Annotations\Exceptions\IncompatibleAnnotationsException;
|
10 | 8 | use TheCodingMachine\GraphQLite\Annotations\FailWith;
|
11 | 9 | use TheCodingMachine\GraphQLite\Annotations\HideIfUnauthorized;
|
12 | 10 | use TheCodingMachine\GraphQLite\Annotations\Logged;
|
| 11 | +use TheCodingMachine\GraphQLite\Annotations\MiddlewareAnnotationInterface; |
| 12 | +use TheCodingMachine\GraphQLite\Annotations\MiddlewareAnnotations; |
| 13 | +use TheCodingMachine\GraphQLite\Annotations\Right; |
13 | 14 | use TheCodingMachine\GraphQLite\QueryFieldDescriptor;
|
| 15 | +use TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface; |
| 16 | +use TheCodingMachine\GraphQLite\Security\AuthorizationServiceInterface; |
14 | 17 | use TheCodingMachine\GraphQLite\Security\VoidAuthenticationService;
|
15 | 18 | use TheCodingMachine\GraphQLite\Security\VoidAuthorizationService;
|
16 | 19 |
|
17 | 20 | class AuthorizationFieldMiddlewareTest extends AbstractQueryProviderTest
|
18 | 21 | {
|
| 22 | + public function testReturnsResolversValueWhenAuthorized(): void |
| 23 | + { |
| 24 | + $authenticationService = $this->createMock(AuthenticationServiceInterface::class); |
| 25 | + $authenticationService->method('isLogged') |
| 26 | + ->willReturn(true); |
| 27 | + $authorizationService = $this->createMock(AuthorizationServiceInterface::class); |
| 28 | + $authorizationService->method('isAllowed') |
| 29 | + ->willReturn(true); |
| 30 | + $middleware = new AuthorizationFieldMiddleware($authenticationService, $authorizationService); |
| 31 | + |
| 32 | + $descriptor = $this->stubDescriptor([new Logged(), new Right('test')]); |
| 33 | + $descriptor->setResolver(fn () => 123); |
| 34 | + |
| 35 | + $field = $middleware->process($descriptor, $this->stubFieldHandler()); |
| 36 | + |
| 37 | + self::assertNotNull($field); |
| 38 | + self::assertSame(123, ($field->resolveFn)()); |
| 39 | + } |
| 40 | + |
19 | 41 |
|
20 |
| - public function testException(): void |
| 42 | + public function testFailsForHideIfUnauthorizedAndFailWith(): void |
21 | 43 | {
|
22 | 44 | $middleware = new AuthorizationFieldMiddleware(new VoidAuthenticationService(), new VoidAuthorizationService());
|
23 | 45 |
|
24 |
| - $descriptor = new QueryFieldDescriptor(); |
25 |
| - $descriptor->setMiddlewareAnnotations($this->getAnnotationReader()->getMiddlewareAnnotations(new ReflectionMethod(__CLASS__, 'stub'))); |
26 |
| - |
27 | 46 | $this->expectException(IncompatibleAnnotationsException::class);
|
28 |
| - $middleware->process($descriptor, new class implements FieldHandlerInterface { |
29 |
| - public function handle(QueryFieldDescriptor $fieldDescriptor): ?FieldDefinition |
30 |
| - { |
31 |
| - return FieldDefinition::create(['name'=>'foo']); |
32 |
| - } |
33 |
| - }); |
| 47 | + $middleware->process($this->stubDescriptor([new Logged(), new HideIfUnauthorized(), new FailWith(value: 123)]), $this->stubFieldHandler()); |
| 48 | + } |
| 49 | + |
| 50 | + public function testHidesFieldForHideIfUnauthorized(): void |
| 51 | + { |
| 52 | + $middleware = new AuthorizationFieldMiddleware(new VoidAuthenticationService(), new VoidAuthorizationService()); |
| 53 | + |
| 54 | + $field = $middleware->process($this->stubDescriptor([new Logged(), new HideIfUnauthorized()]), $this->stubFieldHandler()); |
| 55 | + |
| 56 | + self::assertNull($field); |
| 57 | + } |
| 58 | + |
| 59 | + public function testReturnsFailsWithValueWhenNotAuthorized(): void |
| 60 | + { |
| 61 | + $middleware = new AuthorizationFieldMiddleware(new VoidAuthenticationService(), new VoidAuthorizationService()); |
| 62 | + |
| 63 | + $field = $middleware->process($this->stubDescriptor([new Logged(), new FailWith(value: 123)]), $this->stubFieldHandler()); |
| 64 | + |
| 65 | + self::assertNotNull($field); |
| 66 | + self::assertSame(123, ($field->resolveFn)()); |
| 67 | + } |
| 68 | + |
| 69 | + public function testThrowsUnauthorizedExceptionWhenNotAuthorized(): void |
| 70 | + { |
| 71 | + $middleware = new AuthorizationFieldMiddleware(new VoidAuthenticationService(), new VoidAuthorizationService()); |
| 72 | + |
| 73 | + $field = $middleware->process($this->stubDescriptor([new Logged()]), $this->stubFieldHandler()); |
| 74 | + |
| 75 | + self::assertNotNull($field); |
| 76 | + |
| 77 | + $this->expectExceptionObject(MissingAuthorizationException::unauthorized()); |
| 78 | + |
| 79 | + ($field->resolveFn)(); |
| 80 | + } |
| 81 | + |
| 82 | + public function testThrowsForbiddenExceptionWhenNotAuthorized(): void |
| 83 | + { |
| 84 | + $authenticationService = $this->createMock(AuthenticationServiceInterface::class); |
| 85 | + $authenticationService->method('isLogged') |
| 86 | + ->willReturn(true); |
| 87 | + $middleware = new AuthorizationFieldMiddleware($authenticationService, new VoidAuthorizationService()); |
| 88 | + |
| 89 | + $field = $middleware->process($this->stubDescriptor([new Logged(), new Right('test')]), $this->stubFieldHandler()); |
| 90 | + |
| 91 | + self::assertNotNull($field); |
| 92 | + |
| 93 | + $this->expectExceptionObject(MissingAuthorizationException::forbidden()); |
| 94 | + |
| 95 | + ($field->resolveFn)(); |
34 | 96 | }
|
35 | 97 |
|
36 | 98 | /**
|
37 |
| - * @Logged() |
38 |
| - * @HideIfUnauthorized() |
39 |
| - * @FailWith(null) |
| 99 | + * @param MiddlewareAnnotationInterface[] $annotations |
40 | 100 | */
|
41 |
| - public function stub() |
| 101 | + private function stubDescriptor(array $annotations): QueryFieldDescriptor |
42 | 102 | {
|
| 103 | + $descriptor = new QueryFieldDescriptor(); |
| 104 | + $descriptor->setMiddlewareAnnotations(new MiddlewareAnnotations($annotations)); |
| 105 | + $descriptor->setResolver(fn () => self::fail('Should not be called.')); |
43 | 106 |
|
| 107 | + return $descriptor; |
| 108 | + } |
| 109 | + |
| 110 | + private function stubFieldHandler(): FieldHandlerInterface |
| 111 | + { |
| 112 | + return new class implements FieldHandlerInterface { |
| 113 | + public function handle(QueryFieldDescriptor $fieldDescriptor): FieldDefinition|null |
| 114 | + { |
| 115 | + return new FieldDefinition([ |
| 116 | + 'name' => 'foo', |
| 117 | + 'resolve' => $fieldDescriptor->getResolver(), |
| 118 | + ]); |
| 119 | + } |
| 120 | + }; |
44 | 121 | }
|
45 | 122 | }
|
0 commit comments