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

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