Failed Conditions
Push — master ( 9bceb4...42a7b7 )
by Guilherme
02:51 queued 10s
created

CompleteUserInfoTaskValidatorTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 178
rs 10
c 0
b 0
f 0
wmc 15
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\OpenIDBundle\Tests\Task;
12
13
use FOS\OAuthServerBundle\Event\OAuthEvent;
14
use LoginCidadao\CoreBundle\Entity\Person;
15
use LoginCidadao\OAuthBundle\Entity\Client;
16
use LoginCidadao\OpenIDBundle\Task\CompleteUserInfoTaskValidator;
17
use PHPUnit\Framework\TestCase;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
21
class CompleteUserInfoTaskValidatorTest extends TestCase
22
{
23
    public function testGetCompleteUserInfoTaskCantSkipNoTask()
24
    {
25
        $user = new Person();
26
        $client = new Client();
27
28
        $request = $this->getRequest();
29
        $request->expects($this->exactly(3))
30
            ->method('get')->willReturnMap([
31
                ['_route', null, false, '_authorize_validate'],
32
                ['scope', false, false, 'scope1'],
33
                ['prompt', null, false, false],
34
                ['nonce', null, false, false],
35
            ]);
36
37
        $dispatcher = $this->getEventDispatcherInterface();
38
        $validator = new CompleteUserInfoTaskValidator($dispatcher, false);
39
40
        $validator->getCompleteUserInfoTask($user, $client, $request);
41
    }
42
43
    public function testGetCompleteUserInfoTaskCantSkip()
44
    {
45
        $user = new Person();
46
        $client = new Client();
47
48
        $request = $this->getRequest();
49
        $request->expects($this->exactly(4))
50
            ->method('get')->willReturnMap([
51
                ['_route', null, false, '_authorize_validate'],
52
                ['scope', false, false, 'name mobile country state city birthdate email cpf other'],
53
                ['prompt', null, false, false],
54
                ['nonce', null, false, false],
55
            ]);
56
57
        $dispatcher = $this->getEventDispatcherInterface();
58
        $validator = new CompleteUserInfoTaskValidator($dispatcher, false);
59
60
        $task = $validator->getCompleteUserInfoTask($user, $client, $request);
61
62
        $this->assertInstanceOf('LoginCidadao\OpenIDBundle\Task\CompleteUserInfoTask', $task);
63
        $this->assertSame('name mobile country state city birthdate email cpf', $task->getScope());
64
    }
65
66
    public function testGetCompleteUserInfoTaskCanSkip()
67
    {
68
        $user = new Person();
69
        $client = new Client();
70
71
        $request = $this->getRequest();
72
        $request->expects($this->exactly(3))
73
            ->method('get')->willReturnMap([
74
                ['_route', null, false, '_authorize_validate'],
75
                ['scope', false, false, 'scope1'],
76
                ['prompt', null, false, false],
77
                ['nonce', null, false, false],
78
            ]);
79
80
        $dispatcher = $this->getEventDispatcherInterface();
81
        $this->dispatcherExpectAuthorized($dispatcher);
82
        $validator = new CompleteUserInfoTaskValidator($dispatcher, true);
83
84
        $validator->getCompleteUserInfoTask($user, $client, $request);
85
    }
86
87
    public function testShouldPromptConsent()
88
    {
89
        $request = $this->getRequest();
90
        $params = $this->logicalOr($this->equalTo('prompt'), $this->equalTo('nonce'));
91
        $request->expects($this->exactly(2))
92
            ->method('get')->with($params)
93
            ->willReturnCallback(function ($key) {
94
                switch ($key) {
95
                    case 'prompt':
96
                        return 'consent';
97
                    case 'nonce':
98
                        return 'nonce';
99
                }
100
101
                return null;
102
            });
103
104
        $validator = new CompleteUserInfoTaskValidator($this->getEventDispatcherInterface(), true);
105
106
        $this->assertTrue($validator->shouldPromptConsent($request));
107
    }
108
109
    public function testShouldNotPromptConsentWithoutPrompt()
110
    {
111
        $request = $this->getRequest();
112
        $params = $this->logicalOr($this->equalTo('prompt'), $this->equalTo('nonce'));
113
        $request->expects($this->exactly(2))
114
            ->method('get')->with($params)
115
            ->willReturnCallback(function ($key) {
116
                return $key === 'prompt' ? 'consent' : null;
117
            });
118
119
        $validator = new CompleteUserInfoTaskValidator($this->getEventDispatcherInterface(), true);
120
121
        $this->assertFalse($validator->shouldPromptConsent($request));
122
    }
123
124
    public function testShouldNotPromptConsentWithoutNonce()
125
    {
126
        $request = $this->getRequest();
127
        $request->expects($this->once())
128
            ->method('get')->with('prompt')
129
            ->willReturn(null);
130
131
        $validator = new CompleteUserInfoTaskValidator($this->getEventDispatcherInterface(), true);
132
133
        $this->assertFalse($validator->shouldPromptConsent($request));
134
    }
135
136
    public function testIsClientAuthorized()
137
    {
138
        $dispatcher = $this->getEventDispatcherInterface();
139
        $this->dispatcherExpectAuthorized($dispatcher);
140
        $validator = new CompleteUserInfoTaskValidator($dispatcher, true);
141
142
        $this->assertTrue(
143
            $validator->isClientAuthorized(new Person(), new Client())
144
        );
145
    }
146
147
    public function testRouteInvalid()
148
    {
149
        $request = $this->getRequest();
150
        $request->expects($this->exactly(2))
151
            ->method('get')->willReturn(false);
152
153
        $validator = new CompleteUserInfoTaskValidator($this->getEventDispatcherInterface(), true);
154
155
        $this->assertFalse($validator->isRouteValid($request));
156
    }
157
158
    public function testRouteValid()
159
    {
160
        $request = $this->getRequest();
161
        $request->expects($this->exactly(2))
162
            ->method('get')->willReturnMap([
163
                ['_route', null, false, '_authorize_validate'],
164
                ['scope', false, false, 'scope1'],
165
            ]);
166
167
        $validator = new CompleteUserInfoTaskValidator($this->getEventDispatcherInterface(), true);
168
169
        $this->assertTrue($validator->isRouteValid($request));
170
    }
171
172
    /**
173
     * @return Request|\PHPUnit_Framework_MockObject_MockObject
174
     */
175
    private function getRequest()
176
    {
177
        return $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
178
            ->disableOriginalConstructor()->getMock();
179
    }
180
181
    /**
182
     * @return EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
183
     */
184
    private function getEventDispatcherInterface()
185
    {
186
        return $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')
187
            ->disableOriginalConstructor()->getMock();
188
    }
189
190
    private function dispatcherExpectAuthorized(\PHPUnit_Framework_MockObject_MockObject $dispatcher)
191
    {
192
        $dispatcher->expects($this->once())
193
            ->method('dispatch')
194
            ->with(OAuthEvent::PRE_AUTHORIZATION_PROCESS, $this->isInstanceOf('FOS\OAuthServerBundle\Event\OAuthEvent'))
195
            ->willReturnCallback(function ($eventName, OAuthEvent $event) {
196
                $event->setAuthorizedClient(true);
197
198
                return $event;
199
            });
200
    }
201
}
202