Failed Conditions
Push — ng ( 17df75...6face8 )
by Florent
04:09
created

theEndpointDoesNotSupportResourceFromOtherHosts()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\IssuerDiscoveryEndpoint\Tests;
15
16
use Http\Message\MessageFactory\DiactorosMessageFactory;
17
use Http\Message\ResponseFactory;
18
use OAuth2Framework\Component\IssuerDiscoveryEndpoint\IdentifierResolver\Identifier;
19
use OAuth2Framework\Component\IssuerDiscoveryEndpoint\IdentifierResolver\IdentifierResolver;
20
use OAuth2Framework\Component\IssuerDiscoveryEndpoint\IdentifierResolver\IdentifierResolverManager;
21
use Psr\Http\Server\RequestHandlerInterface;
22
use OAuth2Framework\Component\IssuerDiscoveryEndpoint\IssuerDiscoveryEndpoint;
23
use OAuth2Framework\Component\IssuerDiscoveryEndpoint\ResourceObject;
24
use OAuth2Framework\Component\IssuerDiscoveryEndpoint\ResourceId;
25
use OAuth2Framework\Component\IssuerDiscoveryEndpoint\ResourceRepository;
26
use PHPUnit\Framework\TestCase;
27
use Prophecy\Argument;
28
use Psr\Http\Message\ServerRequestInterface;
29
30
/**
31
 * @group IssuerDiscoveryEndpoint
32
 */
33
class IssuerDiscoveryEndpointTest extends TestCase
34
{
35
    /**
36
     * @test
37
     */
38
    public function theEndpointCannotFindTheRelParameter()
39
    {
40
        $request = $this->prophesize(ServerRequestInterface::class);
41
        $repository = $this->prophesize(ResourceRepository::class);
42
        $handler = $this->prophesize(RequestHandlerInterface::class);
43
        $identifierResolverManager = $this->prophesize(IdentifierResolverManager::class);
44
        $endpoint = new IssuerDiscoveryEndpoint(
45
            $repository->reveal(),
46
            $this->getResponseFactory(),
47
            $identifierResolverManager->reveal(),
48
        'www.foo.bar',
49
            8000
50
        );
51
52
        $response = $endpoint->process($request->reveal(), $handler->reveal());
53
54
        $response->getBody()->rewind();
55
        self::assertEquals('{"error":"invalid_request","error_description":"The parameter \"rel\" is mandatory."}', $response->getBody()->getContents());
56
        self::assertEquals(400, $response->getStatusCode());
57
    }
58
59
    /**
60
     * @test
61
     */
62
    public function theEndpointDoesNotSupportTheRelParameter()
63
    {
64
        $request = $this->prophesize(ServerRequestInterface::class);
65
        $request->getQueryParams()->willReturn([
66
            'rel' => 'http://foo.bar/specs/test/1.0/go',
67
        ]);
68
        $repository = $this->prophesize(ResourceRepository::class);
69
        $handler = $this->prophesize(RequestHandlerInterface::class);
70
        $identifierResolverManager = $this->prophesize(IdentifierResolverManager::class);
71
        $endpoint = new IssuerDiscoveryEndpoint(
72
            $repository->reveal(),
73
            $this->getResponseFactory(),
74
            $identifierResolverManager->reveal(),
75
        'www.foo.bar',
76
            8000
77
        );
78
79
        $response = $endpoint->process($request->reveal(), $handler->reveal());
80
81
        $response->getBody()->rewind();
82
        self::assertEquals('{"error":"invalid_request","error_description":"Unsupported \"rel\" parameter value."}', $response->getBody()->getContents());
83
        self::assertEquals(400, $response->getStatusCode());
84
    }
85
86
    /**
87
     * @test
88
     */
89
    public function theEndpointCannotFindTheResourceParameter()
90
    {
91
        $request = $this->prophesize(ServerRequestInterface::class);
92
        $request->getQueryParams()->willReturn([
93
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
94
            'resource' => '=Foo.Bar'
95
        ]);
96
        $repository = $this->prophesize(ResourceRepository::class);
97
        $handler = $this->prophesize(RequestHandlerInterface::class);
98
        $identifierResolverManager = new IdentifierResolverManager();
99
        $endpoint = new IssuerDiscoveryEndpoint(
100
            $repository->reveal(),
101
            $this->getResponseFactory(),
102
            $identifierResolverManager,
103
            'www.foo.bar',
104
            8000
105
        );
106
107
        $response = $endpoint->process($request->reveal(), $handler->reveal());
108
109
        $response->getBody()->rewind();
110
        self::assertEquals('{"error":"invalid_request","error_description":"The resource identified with \"=Foo.Bar\" does not exist or is not supported by this server."}', $response->getBody()->getContents());
111
        self::assertEquals(400, $response->getStatusCode());
112
    }
113
114
    /**
115
     * @test
116
     */
117
    public function theEndpointDoesNotSupportXri()
118
    {
119
        $request = $this->prophesize(ServerRequestInterface::class);
120
        $request->getQueryParams()->willReturn([
121
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
122
            'resource' => '@foo',
123
        ]);
124
        $repository = $this->prophesize(ResourceRepository::class);
125
        $handler = $this->prophesize(RequestHandlerInterface::class);
126
        $identifierResolverManager = new IdentifierResolverManager();
127
        $endpoint = new IssuerDiscoveryEndpoint(
128
            $repository->reveal(),
129
            $this->getResponseFactory(),
130
            $identifierResolverManager,
131
            'www.foo.bar',
132
            8000
133
        );
134
135
        $response = $endpoint->process($request->reveal(), $handler->reveal());
136
137
        $response->getBody()->rewind();
138
        self::assertEquals('{"error":"invalid_request","error_description":"The resource identified with \"@foo\" does not exist or is not supported by this server."}', $response->getBody()->getContents());
139
        self::assertEquals(400, $response->getStatusCode());
140
    }
141
142
    /**
143
     * @test
144
     */
145
    public function theEndpointDoesNotSupportResourceFromOtherHosts()
146
    {
147
        $request = $this->prophesize(ServerRequestInterface::class);
148
        $request->getQueryParams()->willReturn([
149
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
150
            'resource' => '[email protected]',
151
        ]);
152
        $repository = $this->prophesize(ResourceRepository::class);
153
        $handler = $this->prophesize(RequestHandlerInterface::class);
154
        $identifierResolverManager = new IdentifierResolverManager();
155
        $endpoint = new IssuerDiscoveryEndpoint(
156
            $repository->reveal(),
157
            $this->getResponseFactory(),
158
            $identifierResolverManager,
159
            'www.foo.bar',
160
            8000
161
        );
162
163
        $response = $endpoint->process($request->reveal(), $handler->reveal());
164
165
        $response->getBody()->rewind();
166
        self::assertEquals('{"error":"invalid_request","error_description":"The resource identified with \"[email protected]\" does not exist or is not supported by this server."}', $response->getBody()->getContents());
167
        self::assertEquals(400, $response->getStatusCode());
168
    }
169
170
    /**
171
     * @test
172
     */
173
    public function theResourceIsNotKnown()
174
    {
175
        $request = $this->prophesize(ServerRequestInterface::class);
176
        $request->getQueryParams()->willReturn([
177
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
178
            'resource' => '[email protected]:8000',
179
        ]);
180
        $repository = $this->prophesize(ResourceRepository::class);
181
        $handler = $this->prophesize(RequestHandlerInterface::class);
182
        $identifierResolverManager = new IdentifierResolverManager();
183
        $endpoint = new IssuerDiscoveryEndpoint(
184
            $repository->reveal(),
185
            $this->getResponseFactory(),
186
            $identifierResolverManager,
187
            'www.foo.bar',
188
            8000
189
        );
190
191
        $response = $endpoint->process($request->reveal(), $handler->reveal());
192
193
        $response->getBody()->rewind();
194
        self::assertEquals('{"error":"invalid_request","error_description":"The resource identified with \"[email protected]:8000\" does not exist or is not supported by this server."}', $response->getBody()->getContents());
195
        self::assertEquals(400, $response->getStatusCode());
196
    }
197
198
    /**
199
     * @test
200
     */
201
    public function theResourceIsAValidResourceFromEmail()
202
    {
203
        $request = $this->prophesize(ServerRequestInterface::class);
204
        $request->getQueryParams()->willReturn([
205
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
206
            'resource' => '[email protected]:8000',
207
        ]);
208
        $resource = $this->prophesize(ResourceObject::class);
209
        $resource->getIssuer()->willReturn('https://my.server.com/hello');
210
        $repository = $this->prophesize(ResourceRepository::class);
211
        $repository->find(Argument::type(ResourceId::class))->willReturn($resource->reveal());
212
        $handler = $this->prophesize(RequestHandlerInterface::class);
213
        $resolver = $this->prophesize(IdentifierResolver::class);
214
        $resolver->supports('[email protected]:8000')->willReturn(true);
215
        $resolver->resolve('[email protected]:8000')->willReturn(new Identifier('hello', 'www.foo.bar', 8000));
216
        $identifierResolverManager = new IdentifierResolverManager();
217
        $identifierResolverManager->add($resolver->reveal());
218
        $endpoint = new IssuerDiscoveryEndpoint(
219
            $repository->reveal(),
220
            $this->getResponseFactory(),
221
            $identifierResolverManager,
222
            'www.foo.bar',
223
            8000
224
        );
225
226
        $response = $endpoint->process($request->reveal(), $handler->reveal());
227
228
        $response->getBody()->rewind();
229
        self::assertEquals('{"subject":"[email protected]:8000","links":[{"rel":"http://openid.net/specs/connect/1.0/issuer","href":"https://my.server.com/hello"}]}', $response->getBody()->getContents());
230
        self::assertEquals(200, $response->getStatusCode());
231
    }
232
233
    /**
234
     * @test
235
     */
236
    public function theResourceIsAValidResourceFromAccount()
237
    {
238
        $request = $this->prophesize(ServerRequestInterface::class);
239
        $request->getQueryParams()->willReturn([
240
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
241
            'resource' => 'acct:hello%[email protected]:8000',
242
        ]);
243
        $resource = $this->prophesize(ResourceObject::class);
244
        $resource->getIssuer()->willReturn('https://my.server.com/hello');
245
        $repository = $this->prophesize(ResourceRepository::class);
246
        $repository->find(Argument::type(ResourceId::class))->willReturn($resource->reveal());
247
        $handler = $this->prophesize(RequestHandlerInterface::class);
248
        $resolver = $this->prophesize(IdentifierResolver::class);
249
        $resolver->supports('acct:hello%[email protected]:8000')->willReturn(true);
250
        $resolver->resolve('acct:hello%[email protected]:8000')->willReturn(new Identifier('hello', 'www.foo.bar', 8000));
251
        $identifierResolverManager = new IdentifierResolverManager();
252
        $identifierResolverManager->add($resolver->reveal());
253
        $endpoint = new IssuerDiscoveryEndpoint(
254
            $repository->reveal(),
255
            $this->getResponseFactory(),
256
            $identifierResolverManager,
257
            'www.foo.bar',
258
            8000
259
        );
260
261
        $response = $endpoint->process($request->reveal(), $handler->reveal());
262
263
        $response->getBody()->rewind();
264
        self::assertEquals('{"subject":"acct:hello%[email protected]:8000","links":[{"rel":"http://openid.net/specs/connect/1.0/issuer","href":"https://my.server.com/hello"}]}', $response->getBody()->getContents());
265
        self::assertEquals(200, $response->getStatusCode());
266
    }
267
268
    /**
269
     * @test
270
     */
271
    public function theResourceIsAValidResourceFromUri()
272
    {
273
        $request = $this->prophesize(ServerRequestInterface::class);
274
        $request->getQueryParams()->willReturn([
275
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
276
            'resource' => 'https://www.foo.bar:8000/+hello',
277
        ]);
278
        $resource = $this->prophesize(ResourceObject::class);
279
        $resource->getIssuer()->willReturn('https://my.server.com/hello');
280
        $repository = $this->prophesize(ResourceRepository::class);
281
        $repository->find(Argument::type(ResourceId::class))->willReturn($resource->reveal());
282
        $handler = $this->prophesize(RequestHandlerInterface::class);
283
        $resolver = $this->prophesize(IdentifierResolver::class);
284
        $resolver->supports('https://www.foo.bar:8000/+hello')->willReturn(true);
285
        $resolver->resolve('https://www.foo.bar:8000/+hello')->willReturn(new Identifier('hello', 'www.foo.bar', 8000));
286
        $identifierResolverManager = new IdentifierResolverManager();
287
        $identifierResolverManager->add($resolver->reveal());
288
        $endpoint = new IssuerDiscoveryEndpoint(
289
            $repository->reveal(),
290
            $this->getResponseFactory(),
291
            $identifierResolverManager,
292
            'www.foo.bar',
293
            8000
294
        );
295
296
        $response = $endpoint->process($request->reveal(), $handler->reveal());
297
298
        $response->getBody()->rewind();
299
        self::assertEquals('{"subject":"https://www.foo.bar:8000/+hello","links":[{"rel":"http://openid.net/specs/connect/1.0/issuer","href":"https://my.server.com/hello"}]}', $response->getBody()->getContents());
300
        self::assertEquals(200, $response->getStatusCode());
301
    }
302
303
    /**
304
     * @return ResponseFactory
305
     */
306
    private function getResponseFactory(): ResponseFactory
307
    {
308
        return new DiactorosMessageFactory();
309
    }
310
}
311