ClientSecretPostAuthenticationMethodTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 51
dl 0
loc 107
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A genericCalls() 0 6 1
A theClientIdHasBeenFoundInTheRequestButNoClientSecret() 0 8 1
A theClientIsAuthenticated() 0 22 1
A theClientConfigurationCanBeChecked() 0 7 1
A theClientIdAndClientSecretHaveBeenFoundInTheRequest() 0 11 1
A theClientIdCannotBeFoundInTheRequest() 0 8 1
A buildRequest() 0 11 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\ClientAuthentication\Tests;
15
16
use OAuth2Framework\Component\ClientAuthentication\ClientSecretPost;
17
use OAuth2Framework\Component\Core\Client\Client;
18
use OAuth2Framework\Component\Core\Client\ClientId;
19
use OAuth2Framework\Component\Core\DataBag\DataBag;
20
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
21
use PHPUnit\Framework\TestCase;
22
use Prophecy\PhpUnit\ProphecyTrait;
23
use Prophecy\Prophecy\ObjectProphecy;
24
use Psr\Http\Message\ServerRequestInterface;
25
use Psr\Http\Message\StreamInterface;
26
27
/**
28
 * @group TokenEndpoint
29
 * @group ClientAuthentication
30
 *
31
 * @internal
32
 */
33
final class ClientSecretPostAuthenticationMethodTest extends TestCase
34
{
35
    use ProphecyTrait;
36
37
    /**
38
     * @test
39
     */
40
    public function genericCalls()
41
    {
42
        $method = new ClientSecretPost();
43
44
        static::assertEquals([], $method->getSchemesParameters());
45
        static::assertEquals(['client_secret_post'], $method->getSupportedMethods());
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function theClientIdCannotBeFoundInTheRequest()
52
    {
53
        $method = new ClientSecretPost();
54
        $request = $this->buildRequest([]);
55
56
        $clientId = $method->findClientIdAndCredentials($request->reveal(), $credentials);
57
        static::assertNull($clientId);
58
        static::assertNull($credentials);
59
    }
60
61
    /**
62
     * @test
63
     */
64
    public function theClientIdHasBeenFoundInTheRequestButNoClientSecret()
65
    {
66
        $method = new ClientSecretPost();
67
        $request = $this->buildRequest(['client_id' => 'CLIENT_ID']);
68
69
        $clientId = $method->findClientIdAndCredentials($request->reveal(), $credentials);
70
        static::assertNull($clientId);
71
        static::assertNull($credentials);
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function theClientIdAndClientSecretHaveBeenFoundInTheRequest()
78
    {
79
        $method = new ClientSecretPost();
80
        $request = $this->buildRequest([
81
            'client_id' => 'CLIENT_ID',
82
            'client_secret' => 'CLIENT_SECRET',
83
        ]);
84
85
        $clientId = $method->findClientIdAndCredentials($request->reveal(), $credentials);
86
        static::assertInstanceOf(ClientId::class, $clientId);
87
        static::assertEquals('CLIENT_SECRET', $credentials);
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function theClientIsAuthenticated()
94
    {
95
        $method = new ClientSecretPost();
96
        $request = $this->buildRequest([
97
            'client_id' => 'CLIENT_ID',
98
            'client_secret' => 'CLIENT_SECRET',
99
        ]);
100
101
        $client = $this->prophesize(Client::class);
102
        $client->isPublic()->willReturn(false);
103
        $client->getPublicId()->willReturn(new ClientId('CLIENT_ID'));
104
        $client->getClientId()->willReturn(new ClientId('CLIENT_ID'));
105
        $client->getOwnerId()->willReturn(new UserAccountId('USER_ACCOUNT_ID'));
106
        $client->has('token_endpoint_auth_method')->willReturn(true);
107
        $client->get('token_endpoint_auth_method')->willReturn('client_secret_basic');
108
        $client->getTokenEndpointAuthenticationMethod()->willReturn('client_secret_basic');
109
        $client->has('client_secret')->willReturn(true);
110
        $client->get('client_secret')->willReturn('CLIENT_SECRET');
111
        $client->isDeleted()->willReturn(false);
112
        $client->areClientCredentialsExpired()->willReturn(false);
113
114
        static::assertTrue($method->isClientAuthenticated($client->reveal(), 'CLIENT_SECRET', $request->reveal()));
115
    }
116
117
    /**
118
     * @test
119
     */
120
    public function theClientConfigurationCanBeChecked()
121
    {
122
        $method = new ClientSecretPost();
123
        $validatedParameters = $method->checkClientConfiguration(new DataBag([]), new DataBag([]));
124
125
        static::assertTrue($validatedParameters->has('client_secret'));
126
        static::assertTrue($validatedParameters->has('client_secret_expires_at'));
127
    }
128
129
    private function buildRequest(array $data): ObjectProphecy
130
    {
131
        $body = $this->prophesize(StreamInterface::class);
132
        $body->getContents()->willReturn(http_build_query($data));
133
        $request = $this->prophesize(ServerRequestInterface::class);
134
        $request->hasHeader('Content-Type')->willReturn(true);
135
        $request->getHeader('Content-Type')->willReturn(['application/x-www-form-urlencoded']);
136
        $request->getBody()->willReturn($body->reveal());
137
        $request->getParsedBody()->willReturn([]);
138
139
        return $request;
140
    }
141
}
142