Failed Conditions
Push — ng ( fe3ccc...89ffb0 )
by Florent
09:19
created

IssuerDiscoveryEndpointTest::getUriFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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 Psr\Http\Server\RequestHandlerInterface;
19
use OAuth2Framework\Component\IssuerDiscoveryEndpoint\IssuerDiscoveryEndpoint;
20
use OAuth2Framework\Component\IssuerDiscoveryEndpoint\Resource;
21
use OAuth2Framework\Component\IssuerDiscoveryEndpoint\ResourceId;
22
use OAuth2Framework\Component\IssuerDiscoveryEndpoint\ResourceRepository;
23
use PHPUnit\Framework\TestCase;
24
use Prophecy\Argument;
25
use Psr\Http\Message\ServerRequestInterface;
26
27
/**
28
 * @group IssuerDiscoveryEndpoint
29
 */
30
class IssuerDiscoveryEndpointTest extends TestCase
31
{
32
    /**
33
     * @test
34
     */
35
    public function theEndpointCannotFindTheRelParameter()
36
    {
37
        $request = $this->prophesize(ServerRequestInterface::class);
38
        $repository = $this->prophesize(ResourceRepository::class);
39
        $handler = $this->prophesize(RequestHandlerInterface::class);
40
        $endpoint = new IssuerDiscoveryEndpoint(
41
            $repository->reveal(),
42
            $this->getResponseFactory(),
43
            'https://www.foo.bar:8000'
44
        );
45
46
        $response = $endpoint->process($request->reveal(), $handler->reveal());
47
48
        $response->getBody()->rewind();
49
        self::assertEquals('{"error":"invalid_request","error_description":"The parameter \"rel\" is mandatory."}', $response->getBody()->getContents());
50
        self::assertEquals(400, $response->getStatusCode());
51
    }
52
53
    /**
54
     * @test
55
     */
56
    public function theEndpointDoesNotSupportTheRelParameter()
57
    {
58
        $request = $this->prophesize(ServerRequestInterface::class);
59
        $request->getQueryParams()->willReturn([
60
            'rel' => 'http://foo.bar/specs/test/1.0/go',
61
        ]);
62
        $repository = $this->prophesize(ResourceRepository::class);
63
        $handler = $this->prophesize(RequestHandlerInterface::class);
64
        $endpoint = new IssuerDiscoveryEndpoint(
65
            $repository->reveal(),
66
            $this->getResponseFactory(),
67
            'https://www.foo.bar:8000'
68
        );
69
70
        $response = $endpoint->process($request->reveal(), $handler->reveal());
71
72
        $response->getBody()->rewind();
73
        self::assertEquals('{"error":"invalid_request","error_description":"Unsupported \"rel\" parameter value."}', $response->getBody()->getContents());
74
        self::assertEquals(400, $response->getStatusCode());
75
    }
76
77
    /**
78
     * @test
79
     */
80
    public function theEndpointCannotFindTheResourceParameter()
81
    {
82
        $request = $this->prophesize(ServerRequestInterface::class);
83
        $request->getQueryParams()->willReturn([
84
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
85
        ]);
86
        $repository = $this->prophesize(ResourceRepository::class);
87
        $handler = $this->prophesize(RequestHandlerInterface::class);
88
        $endpoint = new IssuerDiscoveryEndpoint(
89
            $repository->reveal(),
90
            $this->getResponseFactory(),
91
            'https://www.foo.bar:8000'
92
        );
93
94
        $response = $endpoint->process($request->reveal(), $handler->reveal());
95
96
        $response->getBody()->rewind();
97
        self::assertEquals('{"error":"invalid_request","error_description":"The parameter \"resource\" is mandatory."}', $response->getBody()->getContents());
98
        self::assertEquals(400, $response->getStatusCode());
99
    }
100
101
    /**
102
     * @test
103
     */
104
    public function theEndpointDoesNotSupportXri()
105
    {
106
        $request = $this->prophesize(ServerRequestInterface::class);
107
        $request->getQueryParams()->willReturn([
108
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
109
            'resource' => '@foo',
110
        ]);
111
        $repository = $this->prophesize(ResourceRepository::class);
112
        $handler = $this->prophesize(RequestHandlerInterface::class);
113
        $endpoint = new IssuerDiscoveryEndpoint(
114
            $repository->reveal(),
115
            $this->getResponseFactory(),
116
            'https://www.foo.bar:8000'
117
        );
118
119
        $response = $endpoint->process($request->reveal(), $handler->reveal());
120
121
        $response->getBody()->rewind();
122
        self::assertEquals('{"error":"invalid_request","error_description":"Unsupported Extensible Resource Identifier (XRI) resource value."}', $response->getBody()->getContents());
123
        self::assertEquals(400, $response->getStatusCode());
124
    }
125
126
    /**
127
     * @test
128
     */
129
    public function theEndpointDoesNotSupportResourceFromOtherHosts()
130
    {
131
        $request = $this->prophesize(ServerRequestInterface::class);
132
        $request->getQueryParams()->willReturn([
133
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
134
            'resource' => '[email protected]',
135
        ]);
136
        $repository = $this->prophesize(ResourceRepository::class);
137
        $handler = $this->prophesize(RequestHandlerInterface::class);
138
        $endpoint = new IssuerDiscoveryEndpoint(
139
            $repository->reveal(),
140
            $this->getResponseFactory(),
141
            'https://www.foo.bar:8000'
142
        );
143
144
        $response = $endpoint->process($request->reveal(), $handler->reveal());
145
146
        $response->getBody()->rewind();
147
        self::assertEquals('{"error":"invalid_request","error_description":"Unsupported domain."}', $response->getBody()->getContents());
148
        self::assertEquals(400, $response->getStatusCode());
149
    }
150
151
    /**
152
     * @test
153
     */
154
    public function theResourceIsNotKnown()
155
    {
156
        $request = $this->prophesize(ServerRequestInterface::class);
157
        $request->getQueryParams()->willReturn([
158
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
159
            'resource' => '[email protected]:8000',
160
        ]);
161
        $repository = $this->prophesize(ResourceRepository::class);
162
        $handler = $this->prophesize(RequestHandlerInterface::class);
163
        $endpoint = new IssuerDiscoveryEndpoint(
164
            $repository->reveal(),
165
            $this->getResponseFactory(),
166
            'https://www.foo.bar:8000'
167
        );
168
169
        $response = $endpoint->process($request->reveal(), $handler->reveal());
170
171
        $response->getBody()->rewind();
172
        self::assertEquals('{"error":"invalid_request","error_description":"The resource with name \"[email protected]:8000\" does not exist or is not supported by this server."}', $response->getBody()->getContents());
173
        self::assertEquals(400, $response->getStatusCode());
174
    }
175
176
    /**
177
     * @test
178
     */
179
    public function theResourceIsAValidResourceFromEmail()
180
    {
181
        $request = $this->prophesize(ServerRequestInterface::class);
182
        $request->getQueryParams()->willReturn([
183
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
184
            'resource' => '[email protected]:8000',
185
        ]);
186
        $resource = $this->prophesize(Resource::class);
187
        $resource->getIssuer()->willReturn('https://my.server.com/hello');
188
        $repository = $this->prophesize(ResourceRepository::class);
189
        $repository->find(Argument::type(ResourceId::class))->willReturn($resource->reveal());
190
        $handler = $this->prophesize(RequestHandlerInterface::class);
191
        $endpoint = new IssuerDiscoveryEndpoint(
192
            $repository->reveal(),
193
            $this->getResponseFactory(),
194
            'https://www.foo.bar:8000'
195
        );
196
197
        $response = $endpoint->process($request->reveal(), $handler->reveal());
198
199
        $response->getBody()->rewind();
200
        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());
201
        self::assertEquals(200, $response->getStatusCode());
202
    }
203
204
    /**
205
     * @test
206
     */
207
    public function theResourceIsAValidResourceFromAccount()
208
    {
209
        $request = $this->prophesize(ServerRequestInterface::class);
210
        $request->getQueryParams()->willReturn([
211
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
212
            'resource' => 'acct:hello%[email protected]:8000',
213
        ]);
214
        $resource = $this->prophesize(Resource::class);
215
        $resource->getIssuer()->willReturn('https://my.server.com/hello');
216
        $repository = $this->prophesize(ResourceRepository::class);
217
        $repository->find(Argument::type(ResourceId::class))->willReturn($resource->reveal());
218
        $handler = $this->prophesize(RequestHandlerInterface::class);
219
        $endpoint = new IssuerDiscoveryEndpoint(
220
            $repository->reveal(),
221
            $this->getResponseFactory(),
222
            'https://www.foo.bar:8000'
223
        );
224
225
        $response = $endpoint->process($request->reveal(), $handler->reveal());
226
227
        $response->getBody()->rewind();
228
        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());
229
        self::assertEquals(200, $response->getStatusCode());
230
    }
231
232
    /**
233
     * @test
234
     */
235
    public function theResourceIsAValidResourceFromUri()
236
    {
237
        $request = $this->prophesize(ServerRequestInterface::class);
238
        $request->getQueryParams()->willReturn([
239
            'rel' => 'http://openid.net/specs/connect/1.0/issuer',
240
            'resource' => 'https://www.foo.bar:8000/+hello',
241
        ]);
242
        $resource = $this->prophesize(Resource::class);
243
        $resource->getIssuer()->willReturn('https://my.server.com/hello');
244
        $repository = $this->prophesize(ResourceRepository::class);
245
        $repository->find(Argument::type(ResourceId::class))->willReturn($resource->reveal());
246
        $handler = $this->prophesize(RequestHandlerInterface::class);
247
        $endpoint = new IssuerDiscoveryEndpoint(
248
            $repository->reveal(),
249
            $this->getResponseFactory(),
250
            'https://www.foo.bar:8000'
251
        );
252
253
        $response = $endpoint->process($request->reveal(), $handler->reveal());
254
255
        $response->getBody()->rewind();
256
        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());
257
        self::assertEquals(200, $response->getStatusCode());
258
    }
259
260
    /**
261
     * @return ResponseFactory
262
     */
263
    private function getResponseFactory(): ResponseFactory
264
    {
265
        return new DiactorosMessageFactory();
266
    }
267
}
268