WebFingerEndpointTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 128
dl 0
loc 218
rs 10
c 1
b 0
f 0

8 Methods

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