Failed Conditions
Push — master ( 393e29...5c5e5d )
by Florent
05:55
created

TokenIntrospectionEndpointTest::buildRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
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\TokenIntrospectionEndpoint\Tests;
15
16
use Http\Message\MessageFactory\DiactorosMessageFactory;
17
use Http\Message\ResponseFactory;
18
use Prophecy\Prophecy\ObjectProphecy;
19
use Psr\Http\Message\StreamInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
use OAuth2Framework\Component\Core\ResourceServer\ResourceServer;
22
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
23
use OAuth2Framework\Component\Core\Token\Token;
24
use OAuth2Framework\Component\TokenIntrospectionEndpoint\TokenIntrospectionEndpoint;
25
use OAuth2Framework\Component\TokenIntrospectionEndpoint\TokenTypeHint;
26
use OAuth2Framework\Component\TokenIntrospectionEndpoint\TokenTypeHintManager;
27
use PHPUnit\Framework\TestCase;
28
use Psr\Http\Message\ServerRequestInterface;
29
30
/**
31
 * @group TokenIntrospectionEndpoint
32
 */
33
final class TokenIntrospectionEndpointTest extends TestCase
34
{
35
    /**
36
     * @test
37
     */
38
    public function aTokenTypeHintManagerCanHandleTokenTypeHints()
39
    {
40
        self::assertNotEmpty($this->getTokenTypeHintManager()->getTokenTypeHints());
41
    }
42
43
    /**
44
     * @test
45
     */
46
    public function theTokenIntrospectionEndpointReceivesAValidRequest()
47
    {
48
        $endpoint = $this->getTokenIntrospectionEndpoint();
49
50
        $request = $this->buildRequest(['token' => 'VALID_TOKEN']);
51
        $request->getAttribute('resource_server')->willReturn($this->getResourceServer());
52
53
        $handler = $this->prophesize(RequestHandlerInterface::class);
54
55
        $response = $endpoint->process($request->reveal(), $handler->reveal());
56
57
        self::assertEquals(200, $response->getStatusCode());
58
        $response->getBody()->rewind();
59
        self::assertEquals('{"active":true}', $response->getBody()->getContents());
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function theTokenIntrospectionEndpointReceivesAValidRequestWithTokenTypeHint()
66
    {
67
        $endpoint = $this->getTokenIntrospectionEndpoint();
68
69
        $request = $this->buildRequest(['token' => 'VALID_TOKEN', 'token_type_hint' => 'foo']);
70
        $request->getAttribute('resource_server')->willReturn($this->getResourceServer());
71
72
        $handler = $this->prophesize(RequestHandlerInterface::class);
73
74
        $response = $endpoint->process($request->reveal(), $handler->reveal());
75
76
        self::assertEquals(200, $response->getStatusCode());
77
        $response->getBody()->rewind();
78
        self::assertEquals('{"active":true}', $response->getBody()->getContents());
79
    }
80
81
    /**
82
     * @test
83
     */
84
    public function theTokenIntrospectionEndpointReceivesARequestWithAnUnsupportedTokenHint()
85
    {
86
        $endpoint = $this->getTokenIntrospectionEndpoint();
87
88
        $request = $this->buildRequest(['token' => 'VALID_TOKEN', 'token_type_hint' => 'bar']);
89
        $request->getAttribute('resource_server')->willReturn($this->getResourceServer());
90
91
        $handler = $this->prophesize(RequestHandlerInterface::class);
92
93
        $response = $endpoint->process($request->reveal(), $handler->reveal());
94
95
        self::assertEquals(400, $response->getStatusCode());
96
        $response->getBody()->rewind();
97
        self::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());
98
    }
99
100
    /**
101
     * @var TokenTypeHintManager|null
102
     */
103
    private $tokenTypeHintManager = null;
104
105
    /**
106
     * @return TokenTypeHintManager
107
     */
108
    private function getTokenTypeHintManager(): TokenTypeHintManager
109
    {
110
        if (null === $this->tokenTypeHintManager) {
111
            $token = $this->prophesize(Token::class);
112
            $token->getResourceServerId()->willReturn(ResourceServerId::create('RESOURCE_SERVER_ID'));
113
114
            $tokenType = $this->prophesize(TokenTypeHint::class);
115
            $tokenType->find('VALID_TOKEN')->willReturn($token->reveal());
116
            $tokenType->find('BAD_TOKEN')->willReturn(null);
117
            $tokenType->hint()->willReturn('foo');
118
            $tokenType->introspect($token)->willReturn(['active' => true]);
119
120
            $this->tokenTypeHintManager = new TokenTypeHintManager();
121
            $this->tokenTypeHintManager->add($tokenType->reveal());
122
        }
123
124
        return $this->tokenTypeHintManager;
125
    }
126
127
    /**
128
     * @var TokenIntrospectionEndpoint|null
129
     */
130
    private $tokenIntrospectionEndpoint = null;
131
132
    /**
133
     * @return TokenIntrospectionEndpoint
134
     */
135
    private function getTokenIntrospectionEndpoint(): TokenIntrospectionEndpoint
136
    {
137
        if (null === $this->tokenIntrospectionEndpoint) {
138
            $this->tokenIntrospectionEndpoint = new TokenIntrospectionEndpoint(
139
                $this->getTokenTypeHintManager(),
140
                $this->getResponseFactory()
141
            );
142
        }
143
144
        return $this->tokenIntrospectionEndpoint;
145
    }
146
147
    /**
148
     * @var ResponseFactory|null
149
     */
150
    private $responseFactory = null;
151
152
    /**
153
     * @return ResponseFactory
154
     */
155
    private function getResponseFactory(): ResponseFactory
156
    {
157
        if (null === $this->responseFactory) {
158
            $this->responseFactory = new DiactorosMessageFactory();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Http\Message\Messag...actorosMessageFactory() of type object<Http\Message\Mess...iactorosMessageFactory> is incompatible with the declared type object<Http\Message\ResponseFactory>|null of property $responseFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
159
        }
160
161
        return $this->responseFactory;
162
    }
163
164
    /**
165
     * @var ResourceServer|null
166
     */
167
    private $resourceServer = null;
168
169
    /**
170
     * @return ResourceServer
171
     */
172
    private function getResourceServer(): ResourceServer
173
    {
174
        if (null === $this->resourceServer) {
175
            $this->resourceServer = $this->prophesize(ResourceServer::class);
176
            $this->resourceServer->getResourceServerId()->willReturn(ResourceServerId::create('RESOURCE_SERVER_ID'));
177
        }
178
179
        return $this->resourceServer->reveal();
180
    }
181
182
    private function buildRequest(array $data): ObjectProphecy
183
    {
184
        $body = $this->prophesize(StreamInterface::class);
185
        $body->getContents()->willReturn(http_build_query($data));
186
        $request = $this->prophesize(ServerRequestInterface::class);
187
        $request->hasHeader('Content-Type')->willReturn(true);
188
        $request->getHeader('Content-Type')->willReturn(['application/x-www-form-urlencoded']);
189
        $request->getBody()->willReturn($body->reveal());
190
        $request->getParsedBody()->willReturn([]);
191
192
        return $request;
193
    }
194
}
195