Failed Conditions
Push — issue#666 ( f415d0...521a08 )
by Guilherme
12:02
created

CompleteUserInfoTaskValidatorTest::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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