Failed Conditions
Push — master ( 7c3864...930f9b )
by Florent
14:15
created

AuthorizationEndpointTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 10
dl 0
loc 96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 2
B theRequestIsValidButNoAccountIsSelected() 0 60 1
A buildUri() 0 6 2
A logIn() 0 15 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\ServerBundle\Tests\Functional\Grant\AuthorizationCode;
15
16
use OAuth2Framework\Component\AuthorizationCodeGrant\AuthorizationCodeGrantType;
17
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
18
use OAuth2Framework\ServerBundle\Tests\TestBundle\Entity\User;
19
use OAuth2Framework\ServerBundle\Tests\TestBundle\Entity\UserAccount;
20
use Symfony\Bundle\FrameworkBundle\Client;
21
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
22
use Symfony\Component\BrowserKit\Cookie;
23
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
24
25
/**
26
 * @group ServerBundle
27
 * @group Functional
28
 * @group Grant
29
 * @group AuthorizationCode
30
 */
31
class AuthorizationEndpointTest extends WebTestCase
32
{
33
    protected function setUp()
34
    {
35
        if (!\class_exists(AuthorizationCodeGrantType::class)) {
36
            static::markTestSkipped('The component "oauth2-framework/authorization-code-grant" is not installed.');
37
        }
38
    }
39
40
    /**
41
     * @test
42
     */
43
    public function theRequestIsValidButNoAccountIsSelected()
44
    {
45
        $uri = $this->buildUri([
46
            'client_id' => 'CLIENT_ID_2',
47
            'redirect_uri' => 'https://example.com/cb/?foo=bar',
48
            'response_type' => 'code',
49
        ]);
50
        $client = static::createClient();
51
        $this->logIn(
52
            $client,
53
            new User(
54
                'admin',
55
                ['ROLE_ADMIN', 'ROLE_USER'],
56
                ['john.1'],
57
                new \DateTimeImmutable('now -25 hours'),
58
                new \DateTimeImmutable('now -15 days')
59
            ),
60
            new UserAccount(
61
                new UserAccountId('john.1'),
62
                [
63
                    'address', [
64
                        'street_address' => '5 rue Sainte Anne',
65
                        'region' => 'Île de France',
66
                        'postal_code' => '75001',
67
                        'locality' => 'Paris',
68
                        'country' => 'France',
69
                    ],
70
                    'name' => 'John Doe',
71
                    'given_name' => 'John',
72
                    'family_name' => 'Doe',
73
                    'middle_name' => 'Jack',
74
                    'nickname' => 'Little John',
75
                    'profile' => 'https://profile.doe.fr/john/',
76
                    'preferred_username' => 'j-d',
77
                    'gender' => 'M',
78
                    'phone_number' => '+0123456789',
79
                    'phone_number_verified' => true,
80
                    'zoneinfo' => 'Europe/Paris',
81
                    'locale' => 'en',
82
                    'picture' => 'https://www.google.com',
83
                    'birthdate' => '1950-01-01',
84
                    'email' => '[email protected]',
85
                    'email_verified' => false,
86
                    'website' => 'https://john.doe.com',
87
                    'website#fr_fr' => 'https://john.doe.fr',
88
                    'website#fr' => 'https://john.doe.fr',
89
                    'picture#de' => 'https://john.doe.de/picture',
90
                ]
91
            )
92
        );
93
        $client->request('GET', $uri, [], [], ['HTTPS' => 'on'], null);
94
        $response = $client->getResponse();
95
96
        static::assertEquals(303, $response->getStatusCode());
97
        static::assertTrue($response->headers->has('location'));
98
99
        $client->followRedirect();
100
        $response = $client->getResponse();
101
        dump($response->headers->get('location'));
102
    }
103
104
    private function buildUri(array $query): string
105
    {
106
        $query = http_build_query($query);
107
108
        return empty($query) ? '/authorize' : \Safe\sprintf('/authorize?%s', $query);
109
    }
110
111
    private function logIn(Client $client, User $user, ?UserAccount $userAccount): void
112
    {
113
        $session = $client->getContainer()->get('session');
114
115
        $firewallName = 'main';
116
        $firewallContext = 'main';
117
118
        $token = new UsernamePasswordToken($user, null, $firewallName, ['ROLE_ADMIN']);
119
        $session->set('_security_'.$firewallContext, serialize($token));
120
        $session->set('user_account', $userAccount);
121
        $session->save();
122
123
        $cookie = new Cookie($session->getName(), $session->getId());
124
        $client->getCookieJar()->set($cookie);
125
    }
126
}
127