Failed Conditions
Push — master ( b8d841...bc596e )
by Florent
28:20
created

theEndpointCannotFindTheResourceParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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