Failed Conditions
Push — ng ( 75309d...bc6f2d )
by Florent
08:22
created

ClientCredentialsGrantTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 71
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 2
A theRequestHasNoGrantType() 0 7 1
A theClientIsNotAuthenticated() 0 8 1
A theClientIsNotKnown() 0 8 1
A theGrantTypeIsNotAllowedForTheClient() 0 8 1
A theClientIsNotConfidential() 0 8 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\Bundle\Tests\Functional\Grant\ClientCredentials;
15
16
use OAuth2Framework\Component\ClientCredentialsGrant\ClientCredentialsGrantType;
17
use OAuth2Framework\Component\Core\Client\Command;
18
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
19
20
/**
21
 * @group Bundle
22
 * @group Functional
23
 * @group Grant
24
 * @group ClientCredentials
25
 */
26
class ClientCredentialsGrantTest extends WebTestCase
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function setUp()
32
    {
33
        if (!class_exists(ClientCredentialsGrantType::class)) {
34
            $this->markTestSkipped('The component "client-credentials-grant" is not installed.');
35
        }
36
    }
37
38
    /**
39
     * @test
40
     */
41
    public function theRequestHasNoGrantType()
42
    {
43
        $client = static::createClient();
44
        $client->request('POST', '/token/get', [], [], ['HTTPS' => 'on'], null);
45
        $response = $client->getResponse();
46
        self::assertEquals('{"error":"invalid_request","error_description":"The \"grant_type\" parameter is missing."}', $response->getContent());
47
    }
48
49
    /**
50
     * @test
51
     */
52
    public function theClientIsNotAuthenticated()
53
    {
54
        $client = static::createClient();
55
        $client->request('POST', '/token/get', ['grant_type' => 'client_credentials'], [], ['HTTPS' => 'on'], null);
56
        $response = $client->getResponse();
57
        self::assertEquals(401, $response->getStatusCode());
58
        self::assertEquals('Basic realm="My OAuth2 Server",charset="UTF-8",error="invalid_client",error_description="Client authentication failed."', $response->headers->get('www-authenticate'));
59
    }
60
61
    /**
62
     * @test
63
     */
64
    public function theClientIsNotKnown()
65
    {
66
        $client = static::createClient();
67
        $client->request('POST', '/token/get', ['grant_type' => 'client_credentials', 'client_id' => 'UNKNOWN_CLIENT_ID'], [], ['HTTPS' => 'on'], null);
68
        $response = $client->getResponse();
69
        self::assertEquals(401, $response->getStatusCode());
70
        self::assertEquals('Basic realm="My OAuth2 Server",charset="UTF-8",error="invalid_client",error_description="Client authentication failed."', $response->headers->get('www-authenticate'));
71
    }
72
73
    /**
74
     * @test
75
     */
76
    public function theGrantTypeIsNotAllowedForTheClient()
77
    {
78
        $client = static::createClient();
79
        $client->request('POST', '/token/get', ['grant_type' => 'client_credentials', 'client_id' => 'CLIENT_ID_1'], [], ['HTTPS' => 'on'], null);
80
        $response = $client->getResponse();
81
        self::assertEquals(400, $response->getStatusCode());
82
        self::assertEquals('{"error":"unauthorized_client","error_description":"The grant type \"client_credentials\" is unauthorized for this client."}', $response->getContent());
83
    }
84
85
    /**
86
     * @test
87
     */
88
    public function theClientIsNotConfidential()
89
    {
90
        $client = static::createClient();
91
        $client->request('POST', '/token/get', ['grant_type' => 'client_credentials', 'client_id' => 'CLIENT_ID_2'], [], ['HTTPS' => 'on'], null);
92
        $response = $client->getResponse();
93
        self::assertEquals(400, $response->getStatusCode());
94
        self::assertEquals('{"error":"invalid_client","error_description":"The client is not a confidential client."}', $response->getContent());
95
    }
96
}
97