forked from mezzio/mezzio-authentication-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthorizationMiddlewareTest.php
More file actions
210 lines (170 loc) · 6.96 KB
/
AuthorizationMiddlewareTest.php
File metadata and controls
210 lines (170 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
declare(strict_types=1);
namespace MezzioTest\Authentication\OAuth2;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
use Mezzio\Authentication\OAuth2\AuthorizationMiddleware;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use RuntimeException;
final class AuthorizationMiddlewareTest extends TestCase
{
private MockObject&AuthorizationRequestInterface $authRequest;
private MockObject&AuthorizationServer $authServer;
private MockObject&RequestHandlerInterface $handler;
private MockObject&ResponseInterface $response;
/** @var callable(): ResponseInterface */
private $responseFactory;
private MockObject&ServerRequestInterface $serverRequest;
protected function setUp(): void
{
$this->authServer = $this->createMock(AuthorizationServer::class);
$this->response = $this->createMock(ResponseInterface::class);
$this->serverRequest = $this->createMock(ServerRequestInterface::class);
$this->authRequest = $this->createMock(AuthorizationRequestInterface::class);
$this->handler = $this->createMock(RequestHandlerInterface::class);
$this->responseFactory = fn(): ResponseInterface => $this->response;
}
public function testConstructor(): void
{
$middleware = new AuthorizationMiddleware(
$this->authServer,
$this->responseFactory
);
self::assertInstanceOf(AuthorizationMiddleware::class, $middleware);
self::assertInstanceOf(MiddlewareInterface::class, $middleware);
}
public function testProcess(): void
{
$this->authRequest->expects(self::never())
->method('setUser'); // Ths middleware must not provide a user entity
$this->authRequest->expects(self::once())
->method('setAuthorizationApproved')
->with(false); // Expect approval to be set to false only
// Mock a valid authorization request
$this->authServer->expects(self::once())
->method('validateAuthorizationRequest')
->with($this->serverRequest)
->willReturn($this->authRequest);
// Mock a instance immutability when the authorization request
// is populated
$newRequest = $this->createMock(ServerRequestInterface::class);
$this->serverRequest->expects(self::once())
->method('withAttribute')
->with(AuthorizationRequestInterface::class, $this->authRequest)
->willReturn($newRequest);
// Expect the handler to be called with the new modified request,
// that contains the auth request attribute
$handlerResponse = $this->createMock(ResponseInterface::class);
$this->handler->expects(self::once())
->method('handle')
->with($newRequest)
->willReturn($handlerResponse);
$middleware = new AuthorizationMiddleware(
$this->authServer,
$this->responseFactory
);
$response = $middleware->process(
$this->serverRequest,
$this->handler
);
self::assertSame($handlerResponse, $response);
}
public function testAuthorizationRequestRaisingOAuthServerExceptionGeneratesResponseFromException(): void
{
$oauthServerException = $this->createMock(OAuthServerException::class);
$oauthServerException->expects(self::once())
->method('generateHttpResponse')
->with($this->response)
->willReturnArgument(0);
$this->authServer->expects(self::once())
->method('validateAuthorizationRequest')
->with($this->serverRequest)
->willThrowException($oauthServerException);
$middleware = new AuthorizationMiddleware(
$this->authServer,
$this->responseFactory
);
$this->response
->method('withStatus')
->willReturnSelf();
$result = $middleware->process(
$this->serverRequest,
$this->handler
);
self::assertSame($this->response, $result);
}
public function testAuthorizationRequestRaisingUnknownExceptionGeneratesResponseFromException(): void
{
$body = $this->createMock(StreamInterface::class);
$body->expects(self::once())
->method('write')
->with(self::stringContains('An internal error occurred'));
$this->response
->expects(self::once())
->method('getBody')
->willReturn($body);
$this->response
->expects(self::once())
->method('withHeader')
->willReturnSelf();
$this->response
->expects(self::exactly(2))
->method('withStatus')
->willReturnMap([
[200, '', $this->response],
[500, '', $this->response],
]);
$exception = new RuntimeException('An internal error occurred');
$this->authServer->expects(self::once())
->method('validateAuthorizationRequest')
->with($this->serverRequest)
->willThrowException($exception);
$middleware = new AuthorizationMiddleware(
$this->authServer,
$this->responseFactory
);
$response = $middleware->process(
$this->serverRequest,
$this->handler
);
self::assertSame($this->response, $response);
}
public function testAuthorizationRequestRaisingUnknownExceptionLogsError(): void
{
$exception = new RuntimeException('An internal error occurred');
$this->authServer->expects(self::once())
->method('validateAuthorizationRequest')
->with($this->serverRequest)
->willThrowException($exception);
$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::once())
->method('error')
->with(
'Authorization request error',
['exception' => $exception]
);
$body = $this->createMock(StreamInterface::class);
$body->method('write');
$this->response->method('getBody')->willReturn($body);
$this->response->method('withHeader')->willReturnSelf();
$this->response->method('withStatus')->willReturnSelf();
$middleware = new AuthorizationMiddleware(
$this->authServer,
$this->responseFactory,
$logger
);
$response = $middleware->process(
$this->serverRequest,
$this->handler
);
self::assertSame($this->response, $response);
}
}