Passed
Pull Request — master (#11)
by Pol
02:31
created

testUnpackAggregatedClaims()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 62
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 44
nc 1
nop 0
dl 0
loc 62
rs 9.216
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OpenIDClientTest\Claims;
6
7
use function Facile\OpenIDClient\base64url_encode;
8
use Facile\OpenIDClient\Claims\DistributedParser;
9
use Facile\OpenIDClient\Client\ClientInterface;
10
use Facile\OpenIDClient\Issuer\IssuerBuilderInterface;
11
use Facile\OpenIDClient\Issuer\IssuerInterface;
12
use function implode;
13
use Jose\Component\Core\AlgorithmManager;
14
use Jose\Component\Signature\JWSVerifier;
15
use function json_encode;
16
use Facile\OpenIDClientTest\TestCase;
17
use Psr\Http\Client\ClientInterface as HttpClient;
18
use Psr\Http\Message\RequestFactoryInterface;
19
use Psr\Http\Message\RequestInterface;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\StreamInterface;
22
23
class DistributedClaimsTest extends TestCase
24
{
25
    public function testUnpackAggregatedClaimsWithNoClaimSources(): void
26
    {
27
        $algorithmManager = $this->prophesize(AlgorithmManager::class);
28
        $JWSVerifier = $this->prophesize(JWSVerifier::class);
29
        $issuerBuilder = $this->prophesize(IssuerBuilderInterface::class);
30
        $httpClient = $this->prophesize(HttpClient::class);
31
        $requestFactory = $this->prophesize(RequestFactoryInterface::class);
32
        $client = $this->prophesize(ClientInterface::class);
33
34
        $service = new DistributedParser(
35
            $issuerBuilder->reveal(),
36
            $httpClient->reveal(),
37
            $requestFactory->reveal(),
38
            $algorithmManager->reveal(),
39
            $JWSVerifier->reveal()
40
        );
41
42
        $claims = [
43
            'sub' => 'foo',
44
            '_claim_names' => [
45
                'age' => 'src1',
46
            ],
47
        ];
48
49
        $distributed = $service->fetch($client->reveal(), $claims);
50
51
        static::assertSame($claims, $distributed);
52
    }
53
54
    public function testUnpackAggregatedClaimsWithNoClaimNames(): void
55
    {
56
        $algorithmManager = $this->prophesize(AlgorithmManager::class);
57
        $JWSVerifier = $this->prophesize(JWSVerifier::class);
58
        $issuerBuilder = $this->prophesize(IssuerBuilderInterface::class);
59
        $httpClient = $this->prophesize(HttpClient::class);
60
        $requestFactory = $this->prophesize(RequestFactoryInterface::class);
61
        $client = $this->prophesize(ClientInterface::class);
62
63
        $service = new DistributedParser(
64
            $issuerBuilder->reveal(),
65
            $httpClient->reveal(),
66
            $requestFactory->reveal(),
67
            $algorithmManager->reveal(),
68
            $JWSVerifier->reveal()
69
        );
70
71
        $claims = [
72
            'sub' => 'foo',
73
            '_claim_sources' => [
74
                'src1' => [
75
                    'endpoint' => 'https://endpoint.url/claims',
76
                    'access_token' => 'access-token',
77
                ],
78
            ],
79
        ];
80
81
        $distributed = $service->fetch($client->reveal(), $claims);
82
83
        static::assertSame($claims, $distributed);
84
    }
85
86
    public function testUnpackAggregatedClaims(): void
87
    {
88
        $jwt = implode('.', [
89
            base64url_encode((string) json_encode(['alg' => 'none'])),
90
            base64url_encode((string) json_encode(['age' => 30])),
91
            '.',
92
        ]);
93
94
        $algorithmManager = $this->prophesize(AlgorithmManager::class);
95
        $JWSVerifier = $this->prophesize(JWSVerifier::class);
96
        $issuerBuilder = $this->prophesize(IssuerBuilderInterface::class);
97
        $httpClient = $this->prophesize(HttpClient::class);
98
        $requestFactory = $this->prophesize(RequestFactoryInterface::class);
99
        $client = $this->prophesize(ClientInterface::class);
100
        $issuer = $this->prophesize(IssuerInterface::class);
101
        $request = $this->prophesize(RequestInterface::class);
102
        $response = $this->prophesize(ResponseInterface::class);
103
        $stream = $this->prophesize(StreamInterface::class);
104
105
        $requestFactory->createRequest('GET', 'https://endpoint.url/claims')
106
            ->willReturn($request->reveal());
107
108
        $request->withHeader('accept', 'application/jwt')
109
            ->willReturn($request->reveal());
110
        $request->withHeader('authorization', 'Bearer ' . 'access-token')
111
            ->willReturn($request->reveal());
112
113
        $response->getStatusCode()->willReturn(201);
114
        $response->getBody()->willReturn($stream->reveal());
115
        $stream->__toString()->willReturn($jwt);
116
117
        $httpClient->sendRequest($request->reveal())
118
            ->willReturn($response->reveal());
119
120
        $client->getIssuer()->willReturn($issuer->reveal());
121
122
        $service = new DistributedParser(
123
            $issuerBuilder->reveal(),
124
            $httpClient->reveal(),
125
            $requestFactory->reveal(),
126
            $algorithmManager->reveal(),
127
            $JWSVerifier->reveal()
128
        );
129
130
        $claims = [
131
            'sub' => 'foo',
132
            '_claim_names' => [
133
                'age' => 'src1',
134
            ],
135
            '_claim_sources' => [
136
                'src1' => [
137
                    'endpoint' => 'https://endpoint.url/claims',
138
                    'access_token' => 'access-token',
139
                ],
140
            ],
141
        ];
142
143
        $unpacked = $service->fetch($client->reveal(), $claims);
144
145
        static::assertSame(30, $unpacked['age'] ?? null);
146
        static::assertArrayNotHasKey('_claim_names', $unpacked);
147
        static::assertArrayNotHasKey('_claim_sources', $unpacked);
148
    }
149
150
    public function testUnpackAggregatedClaimsWithResourceError(): void
151
    {
152
        $jwt = implode('.', [
153
            base64url_encode((string) json_encode(['alg' => 'none'])),
154
            base64url_encode((string) json_encode(['age' => 30])),
155
            '.',
156
        ]);
157
158
        $algorithmManager = $this->prophesize(AlgorithmManager::class);
159
        $JWSVerifier = $this->prophesize(JWSVerifier::class);
160
        $issuerBuilder = $this->prophesize(IssuerBuilderInterface::class);
161
        $httpClient = $this->prophesize(HttpClient::class);
162
        $requestFactory = $this->prophesize(RequestFactoryInterface::class);
163
        $client = $this->prophesize(ClientInterface::class);
164
        $issuer = $this->prophesize(IssuerInterface::class);
165
        $request = $this->prophesize(RequestInterface::class);
166
        $response = $this->prophesize(ResponseInterface::class);
167
        $stream = $this->prophesize(StreamInterface::class);
168
169
        $requestFactory->createRequest('GET', 'https://endpoint.url/claims')
170
            ->willReturn($request->reveal());
171
172
        $request->withHeader('accept', 'application/jwt')
173
            ->willReturn($request->reveal());
174
        $request->withHeader('authorization', 'Bearer ' . 'access-token')
175
            ->willReturn($request->reveal());
176
177
        $response->getReasonPhrase()->willReturn('foo');
178
        $response->getStatusCode()->willReturn(401);
179
        $response->getBody()->willReturn($stream->reveal());
180
        $stream->__toString()->willReturn($jwt);
181
182
        $httpClient->sendRequest($request->reveal())
183
            ->willReturn($response->reveal());
184
185
        $client->getIssuer()->willReturn($issuer->reveal());
186
187
        $service = new DistributedParser(
188
            $issuerBuilder->reveal(),
189
            $httpClient->reveal(),
190
            $requestFactory->reveal(),
191
            $algorithmManager->reveal(),
192
            $JWSVerifier->reveal()
193
        );
194
195
        $claims = [
196
            'sub' => 'foo',
197
            '_claim_names' => [
198
                'age' => 'src1',
199
            ],
200
            '_claim_sources' => [
201
                'src1' => [
202
                    'endpoint' => 'https://endpoint.url/claims',
203
                    'access_token' => 'access-token',
204
                ],
205
            ],
206
        ];
207
208
        $unpacked = $service->fetch($client->reveal(), $claims);
209
210
        static::assertSame($claims, $unpacked);
211
    }
212
}
213