theTokenRevocationEndpointReceivesFromAnotherClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 15
rs 9.9666
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\TokenRevocationEndpoint\Tests;
15
16
use Nyholm\Psr7\Factory\Psr17Factory;
17
use OAuth2Framework\Component\Core\AccessToken\AccessToken;
18
use OAuth2Framework\Component\Core\Client\Client;
19
use OAuth2Framework\Component\Core\Client\ClientId;
20
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
21
use OAuth2Framework\Component\TokenRevocationEndpoint\TokenRevocationGetEndpoint;
22
use OAuth2Framework\Component\TokenRevocationEndpoint\TokenTypeHint;
23
use OAuth2Framework\Component\TokenRevocationEndpoint\TokenTypeHintManager;
24
use PHPUnit\Framework\TestCase;
25
use Prophecy\PhpUnit\ProphecyTrait;
26
use Psr\Http\Message\ResponseFactoryInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
use Psr\Http\Server\RequestHandlerInterface;
29
30
/**
31
 * @group TokenRevocationEndpoint
32
 *
33
 * @internal
34
 */
35
final class TokenRevocationGetEndpointTest extends TestCase
36
{
37
    use ProphecyTrait;
38
39
    /**
40
     * @var null|TokenTypeHintManager
41
     */
42
    private $tokenTypeHintManager;
43
44
    /**
45
     * @var null|TokenRevocationGetEndpoint
46
     */
47
    private $tokenRevocationEndpoint;
48
49
    /**
50
     * @var null|ResponseFactoryInterface
51
     */
52
    private $responseFactory;
53
54
    /**
55
     * @var null|Client
56
     */
57
    private $client;
58
59
    /**
60
     * @test
61
     */
62
    public function aTokenTypeHintManagerCanHandleTokenTypeHints()
63
    {
64
        static::assertNotEmpty($this->getTokenTypeHintManager()->getTokenTypeHints());
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function theTokenRevocationEndpointReceivesAValidGetRequest()
71
    {
72
        $endpoint = $this->getTokenRevocationGetEndpoint();
73
74
        $request = $this->prophesize(ServerRequestInterface::class);
75
        $request->getQueryParams()->willReturn(['token' => 'VALID_TOKEN']);
76
        $request->getAttribute('client')->willReturn($this->getClient());
77
78
        $handler = $this->prophesize(RequestHandlerInterface::class);
79
80
        $response = $endpoint->process($request->reveal(), $handler->reveal());
81
82
        static::assertEquals(200, $response->getStatusCode());
83
        $response->getBody()->rewind();
84
        static::assertEquals('', $response->getBody()->getContents());
85
    }
86
87
    /**
88
     * @test
89
     */
90
    public function theTokenRevocationEndpointReceivesAValidGetRequestWithTokenTypeHint()
91
    {
92
        $endpoint = $this->getTokenRevocationGetEndpoint();
93
94
        $request = $this->prophesize(ServerRequestInterface::class);
95
        $request->getQueryParams()->willReturn(['token' => 'VALID_TOKEN', 'token_type_hint' => 'foo']);
96
        $request->getAttribute('client')->willReturn($this->getClient());
97
98
        $handler = $this->prophesize(RequestHandlerInterface::class);
99
100
        $response = $endpoint->process($request->reveal(), $handler->reveal());
101
102
        static::assertEquals(200, $response->getStatusCode());
103
        $response->getBody()->rewind();
104
        static::assertEquals('', $response->getBody()->getContents());
105
    }
106
107
    /**
108
     * @test
109
     */
110
    public function theTokenRevocationEndpointReceivesAValidGetRequestWithCallback()
111
    {
112
        $endpoint = $this->getTokenRevocationGetEndpoint();
113
114
        $request = $this->prophesize(ServerRequestInterface::class);
115
        $request->getQueryParams()->willReturn(['token' => 'VALID_TOKEN', 'callback' => 'callThisFunctionPlease']);
116
        $request->getAttribute('client')->willReturn($this->getClient());
117
118
        $handler = $this->prophesize(RequestHandlerInterface::class);
119
120
        $response = $endpoint->process($request->reveal(), $handler->reveal());
121
122
        static::assertEquals(200, $response->getStatusCode());
123
        $response->getBody()->rewind();
124
        static::assertEquals('callThisFunctionPlease()', $response->getBody()->getContents());
125
    }
126
127
    /**
128
     * @test
129
     */
130
    public function theTokenDoesNotExistAndCannotBeRevoked()
131
    {
132
        $endpoint = $this->getTokenRevocationGetEndpoint();
133
134
        $request = $this->prophesize(ServerRequestInterface::class);
135
        $request->getQueryParams()->willReturn(['token' => 'UNKNOWN_TOKEN', 'callback' => 'callThisFunctionPlease']);
136
        $request->getAttribute('client')->willReturn($this->getClient());
137
138
        $handler = $this->prophesize(RequestHandlerInterface::class);
139
140
        $response = $endpoint->process($request->reveal(), $handler->reveal());
141
142
        static::assertEquals(200, $response->getStatusCode());
143
        $response->getBody()->rewind();
144
        static::assertEquals('callThisFunctionPlease()', $response->getBody()->getContents());
145
    }
146
147
    /**
148
     * @test
149
     */
150
    public function theTokenRevocationEndpointReceivesFromAnotherClient()
151
    {
152
        $endpoint = $this->getTokenRevocationGetEndpoint();
153
154
        $request = $this->prophesize(ServerRequestInterface::class);
155
        $request->getQueryParams()->willReturn(['token' => 'TOKEN_FOR_ANOTHER_CLIENT', 'callback' => 'callThisFunctionPlease']);
156
        $request->getAttribute('client')->willReturn($this->getClient());
157
158
        $handler = $this->prophesize(RequestHandlerInterface::class);
159
160
        $response = $endpoint->process($request->reveal(), $handler->reveal());
161
162
        static::assertEquals(400, $response->getStatusCode());
163
        $response->getBody()->rewind();
164
        static::assertEquals('callThisFunctionPlease({"error":"invalid_request","error_description":"The parameter \"token\" is invalid."})', $response->getBody()->getContents());
165
    }
166
167
    /**
168
     * @test
169
     */
170
    public function theTokenRevocationEndpointReceivesARequestWithAnUnsupportedTokenHint()
171
    {
172
        $endpoint = $this->getTokenRevocationGetEndpoint();
173
174
        $request = $this->prophesize(ServerRequestInterface::class);
175
        $request->getQueryParams()->willReturn(['token' => 'VALID_TOKEN', 'token_type_hint' => 'bar']);
176
        $request->getAttribute('client')->willReturn($this->getClient());
177
178
        $handler = $this->prophesize(RequestHandlerInterface::class);
179
180
        $response = $endpoint->process($request->reveal(), $handler->reveal());
181
182
        static::assertEquals(400, $response->getStatusCode());
183
        $response->getBody()->rewind();
184
        static::assertEquals('{"error":"unsupported_token_type","error_description":"The token type hint \"bar\" is not supported. Please use one of the following values: foo."}', $response->getBody()->getContents());
185
    }
186
187
    private function getTokenTypeHintManager(): TokenTypeHintManager
188
    {
189
        if (null === $this->tokenTypeHintManager) {
190
            $token1 = $this->prophesize(AccessToken::class);
191
            $token1->getClientId()->willReturn(new ClientId('CLIENT_ID'));
192
193
            $token2 = $this->prophesize(AccessToken::class);
194
            $token2->getClientId()->willReturn(new ClientId('OTHER_CLIENT_ID'));
195
196
            $tokenType = $this->prophesize(TokenTypeHint::class);
197
            $tokenType->find('VALID_TOKEN')->willReturn($token1->reveal());
198
            $tokenType->find('TOKEN_FOR_ANOTHER_CLIENT')->willReturn($token2->reveal());
199
            $tokenType->find('UNKNOWN_TOKEN')->willReturn(null);
200
            $tokenType->hint()->willReturn('foo');
201
            $tokenType->revoke($token1)->will(function () {});
202
203
            $this->tokenTypeHintManager = new TokenTypeHintManager();
204
            $this->tokenTypeHintManager->add($tokenType->reveal());
205
        }
206
207
        return $this->tokenTypeHintManager;
208
    }
209
210
    private function getTokenRevocationGetEndpoint(): TokenRevocationGetEndpoint
211
    {
212
        if (null === $this->tokenRevocationEndpoint) {
213
            $this->tokenRevocationEndpoint = new TokenRevocationGetEndpoint(
214
                $this->getTokenTypeHintManager(),
215
                $this->getResponseFactory(),
216
                true
217
            );
218
        }
219
220
        return $this->tokenRevocationEndpoint;
221
    }
222
223
    private function getResponseFactory(): ResponseFactoryInterface
224
    {
225
        if (null === $this->responseFactory) {
226
            $this->responseFactory = new Psr17Factory();
227
        }
228
229
        return $this->responseFactory;
230
    }
231
232
    private function getClient(): Client
233
    {
234
        if (null === $this->client) {
235
            $client = $this->prophesize(Client::class);
236
            $client->isPublic()->willReturn(false);
237
            $client->getOwnerId()->willReturn(new UserAccountId('USER_ACCOUNT'));
238
            $client->getPublicId()->willReturn(new ClientId('CLIENT_ID'));
239
            $client->getClientId()->willReturn(new ClientId('CLIENT_ID'));
240
241
            $this->client = $client->reveal();
242
        }
243
244
        return $this->client;
245
    }
246
}
247