Failed Conditions
Push — issue#702 ( 91bd46...0b5bf0 )
by Guilherme
19:37 queued 12:15
created

AuthorizationSubscriberTest::getPerson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\RemoteClaimsBundle\Tests\EventSubscriber;
12
13
use LoginCidadao\CoreBundle\Model\PersonInterface;
14
use LoginCidadao\OpenIDBundle\Event\AuthorizationEvent;
15
use LoginCidadao\OpenIDBundle\LoginCidadaoOpenIDEvents;
16
use LoginCidadao\RemoteClaimsBundle\EventSubscriber\AuthorizationSubscriber;
17
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimFetcherInterface;
18
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimManagerInterface;
19
use LoginCidadao\OAuthBundle\Model\ClientInterface;
20
use LoginCidadao\RemoteClaimsBundle\Model\TagUri;
21
use Psr\Log\LoggerInterface;
22
use Psr\Log\LogLevel;
23
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
24
25
class AuthorizationSubscriberTest extends \PHPUnit_Framework_TestCase
26
{
27
    public function testGetSubscribedEvents()
28
    {
29
        $this->assertEquals([
30
            LoginCidadaoOpenIDEvents::NEW_AUTHORIZATION_REQUEST => 'onNewAuthorizationRequest',
31
            LoginCidadaoOpenIDEvents::NEW_AUTHORIZATION => ['onNewAuthorization', 100],
32
            LoginCidadaoOpenIDEvents::UPDATE_AUTHORIZATION => ['onUpdateAuthorization', 100],
33
            LoginCidadaoOpenIDEvents::REVOKE_AUTHORIZATION => 'onRevokeAuthorization',
34
        ], AuthorizationSubscriber::getSubscribedEvents()
35
        );
36
    }
37
38
    public function testOnNewAuthorizationRequest()
39
    {
40
        $scope = [
41
            'openid',
42
            'simple_claim',
43
            'https://claim.provider.example.com/my-claim',
44
            'tag:example.com,2017:my_claim',
45
        ];
46
47
        $remoteClaims = [];
48
49
        $fetcher = $this->getRemoteClaimFetcher();
50
        $fetcher->expects($this->atLeastOnce())->method('getRemoteClaim')
51
            ->willReturnCallback(function ($scope) use (&$remoteClaims) {
52
                $remoteClaims[] = $scope;
53
54
                return $this->getRemoteClaim();
55
            });
56
57
        $event = $this->getEvent();
58
        $event->expects($this->atLeastOnce())->method('getScope')->willReturn($scope);
59
60
        $subscriber = new AuthorizationSubscriber($this->getRemoteClaimManager(), $fetcher, $this->getAuthChecker());
61
        $subscriber->onNewAuthorizationRequest($event);
62
63
        $this->assertCount(2, $remoteClaims);
64
    }
65
66
    public function testOnRemoteClaimNotFound()
67
    {
68
        $scope = [
69
            'openid',
70
            'simple_claim',
71
            'https://not.actually.a.remote.claim/fake',
72
        ];
73
74
        $remoteClaims = [];
75
76
        $fetcher = $this->getRemoteClaimFetcher();
77
        $fetcher->expects($this->atLeastOnce())->method('getRemoteClaim')
78
            ->willThrowException(new \RuntimeException('Random error'));
79
80
        /** @var \PHPUnit_Framework_MockObject_MockObject|LoggerInterface $logger */
81
        $logger = $this->getMock('Psr\Log\LoggerInterface');
82
        $logger->expects($this->once())->method('log')->with(LogLevel::ERROR);
83
84
        $event = $this->getEvent();
85
        $event->expects($this->atLeastOnce())->method('getScope')->willReturn($scope);
86
87
        $subscriber = new AuthorizationSubscriber($this->getRemoteClaimManager(), $fetcher, $this->getAuthChecker());
88
        $subscriber->setLogger($logger);
89
        $subscriber->onNewAuthorizationRequest($event);
90
91
        $this->assertEmpty($remoteClaims);
92
    }
93
94
    public function testOnNewAuthorization()
95
    {
96
        $test = $this->prepareEnforceRemoteClaimsTest();
97
98
        /** @var AuthorizationSubscriber $subscriber */
99
        $subscriber = $test['subscriber'];
100
101
        /** @var AuthorizationEvent $event */
102
        $event = $test['event'];
103
104
        $subscriber->onNewAuthorization($event);
105
    }
106
107
    public function testOnUpdateAuthorization()
108
    {
109
        $test = $this->prepareEnforceRemoteClaimsTest();
110
111
        /** @var AuthorizationSubscriber $subscriber */
112
        $subscriber = $test['subscriber'];
113
114
        /** @var AuthorizationEvent $event */
115
        $event = $test['event'];
116
117
        $subscriber->onUpdateAuthorization($event);
118
    }
119
120
    public function testOnRevokeAuthorizationWithRemoteClaims()
121
    {
122
        $remoteClaims = [$this->getCompleteRemoteClaim(), $this->getCompleteRemoteClaim()];
123
124
        $authorization = $this->getMock('LoginCidadao\CoreBundle\Entity\Authorization');
125
126
        $event = $this->getEvent();
127
        $event->expects($this->once())->method('getRemoteClaims')->willReturn($remoteClaims);
128
        $event->expects($this->once())->method('getAuthorization')->willReturn($authorization);
129
130
        $manager = $this->getRemoteClaimManager();
131
        $manager->expects($this->once())
132
            ->method('revokeAllAuthorizations')
133
            ->with($this->isInstanceOf('LoginCidadao\CoreBundle\Entity\Authorization'));
134
135
        $subscriber = new AuthorizationSubscriber($manager, $this->getRemoteClaimFetcher(), $this->getAuthChecker());
136
        $subscriber->onRevokeAuthorization($event);
137
    }
138
139
    public function testOnRevokeAuthorizationWithoutRemoteClaims()
140
    {
141
        $event = $this->getEvent();
142
        $event->expects($this->once())->method('getRemoteClaims')->willReturn(null);
143
144
        $manager = $this->getRemoteClaimManager();
145
        $manager->expects($this->never())->method('revokeAllAuthorizations');
146
147
        $subscriber = new AuthorizationSubscriber($manager, $this->getRemoteClaimFetcher(), $this->getAuthChecker());
148
        $subscriber->onRevokeAuthorization($event);
149
    }
150
151
    public function testFeatureFlagOff()
152
    {
153
        $event = $this->getEvent();
154
155
        $claimManager = $this->getRemoteClaimManager();
156
        $claimFetcher = $this->getRemoteClaimFetcher();
157
        $subscriber = new AuthorizationSubscriber($claimManager, $claimFetcher, $this->getAuthChecker(false));
158
159
        $subscriber->onNewAuthorizationRequest($event);
160
        $subscriber->onNewAuthorization($event);
161
        $subscriber->onUpdateAuthorization($event);
162
    }
163
164
    private function prepareEnforceRemoteClaimsTest()
165
    {
166
        $remoteClaims = [$this->getCompleteRemoteClaim(), $this->getCompleteRemoteClaim()];
167
168
        $event = $this->getEvent();
169
        $event->expects($this->once())->method('getRemoteClaims')->willReturn($remoteClaims);
170
        $event->expects($this->exactly(2))->method('getClient')->willReturn($this->getClient());
171
        $event->expects($this->exactly(2))->method('getPerson')->willReturn($this->getPerson());
172
173
        $manager = $this->getRemoteClaimManager();
174
        $manager->expects($this->exactly(2))
175
            ->method('enforceAuthorization')
176
            ->with($this->isInstanceOf('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimAuthorizationInterface'));
177
178
        $subscriber = new AuthorizationSubscriber($manager, $this->getRemoteClaimFetcher(), $this->getAuthChecker());
179
180
        return [
181
            'subscriber' => $subscriber,
182
            'event' => $event,
183
        ];
184
    }
185
186
    private function getRemoteClaim()
187
    {
188
        $remoteClaim = $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface');
189
190
        return $remoteClaim;
191
    }
192
193
    private function getCompleteRemoteClaim($name = null)
194
    {
195
        $provider = $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\ClaimProviderInterface');
196
197
        $remoteClaim = $this->getRemoteClaim();
198
        $remoteClaim->expects($this->any())->method('getProvider')
199
            ->willReturn($provider);
200
        $remoteClaim->expects($this->any())->method('getName')
201
            ->willReturn($name ?: TagUri::createFromString('tag:example.com,2017:my_claim'));
202
203
        return $remoteClaim;
204
    }
205
206
    /**
207
     * @return \PHPUnit_Framework_MockObject_MockObject|RemoteClaimManagerInterface
208
     */
209
    private function getRemoteClaimManager()
210
    {
211
        return $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimManagerInterface');
212
    }
213
214
    /**
215
     * @return \PHPUnit_Framework_MockObject_MockObject|RemoteClaimFetcherInterface
216
     */
217
    private function getRemoteClaimFetcher()
218
    {
219
        return $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimFetcherInterface');
220
    }
221
222
    /**
223
     * @return \PHPUnit_Framework_MockObject_MockObject|ClientInterface
224
     */
225
    private function getClient()
226
    {
227
        return $this->getMock('LoginCidadao\OAuthBundle\Model\ClientInterface');
228
    }
229
230
    /**
231
     * @return AuthorizationEvent|\PHPUnit_Framework_MockObject_MockObject
232
     */
233
    private function getEvent()
234
    {
235
        return $this->getMockBuilder('LoginCidadao\OpenIDBundle\Event\AuthorizationEvent')
236
            ->disableOriginalConstructor()->getMock();
237
    }
238
239
    /**
240
     * @return \PHPUnit_Framework_MockObject_MockObject|PersonInterface
241
     */
242
    private function getPerson()
243
    {
244
        return $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface');
245
    }
246
247
    /**
248
     * @return \PHPUnit_Framework_MockObject_MockObject|AuthorizationCheckerInterface
249
     */
250
    private function getAuthChecker($isGranted = true)
251
    {
252
        $checker = $this->getMock('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface');
253
        $checker->expects($this->any())->method('isGranted')->willReturn($isGranted);
254
255
        return $checker;
256
    }
257
}
258