|
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\APIBundle\Tests\Event; |
|
12
|
|
|
|
|
13
|
|
|
use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken; |
|
14
|
|
|
use LoginCidadao\APIBundle\Event\LoggedInUserListener; |
|
15
|
|
|
use LoginCidadao\OAuthBundle\Entity\AccessToken; |
|
16
|
|
|
use LoginCidadao\OAuthBundle\Entity\AccessTokenRepository; |
|
17
|
|
|
use LoginCidadao\OAuthBundle\Entity\Client; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
use SimpleThings\EntityAudit\AuditConfiguration; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
21
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
22
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
23
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
24
|
|
|
|
|
25
|
|
|
class LoggedInUserListenerTest extends TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
public function testOnKernelRequest() |
|
28
|
|
|
{ |
|
29
|
|
|
$client = new Client(); |
|
30
|
|
|
$accessTokenToken = 'access_token'; |
|
31
|
|
|
$accessToken = new AccessToken(); |
|
32
|
|
|
$accessToken->setToken($accessTokenToken); |
|
33
|
|
|
$accessToken->setClient($client); |
|
34
|
|
|
|
|
35
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|AccessTokenRepository $accessTokenRepo */ |
|
36
|
|
|
$accessTokenRepo = $this->getMockBuilder(AccessTokenRepository::class) |
|
37
|
|
|
->disableOriginalConstructor()->getMock(); |
|
38
|
|
|
$accessTokenRepo->expects($this->once()) |
|
39
|
|
|
->method('findOneBy')->with(['token' => $accessTokenToken]) |
|
40
|
|
|
->willReturn($accessToken); |
|
41
|
|
|
|
|
42
|
|
|
$token = $this->createMock(OAuthToken::class); |
|
43
|
|
|
$token->expects($this->exactly(2)) |
|
44
|
|
|
->method('getToken')->willReturn($accessTokenToken); |
|
45
|
|
|
|
|
46
|
|
|
/** @var TokenStorageInterface|\PHPUnit_Framework_MockObject_MockObject $tokenStorage */ |
|
47
|
|
|
$tokenStorage = $this->createMock(TokenStorageInterface::class); |
|
48
|
|
|
$tokenStorage->expects($this->once())->method('getToken')->willReturn($token); |
|
49
|
|
|
|
|
50
|
|
|
$auditConfig = new AuditConfiguration(); |
|
51
|
|
|
|
|
52
|
|
|
/** @var GetResponseEvent|\PHPUnit_Framework_MockObject_MockObject $event */ |
|
53
|
|
|
$event = $this->getMockBuilder(GetResponseEvent::class)->disableOriginalConstructor()->getMock(); |
|
54
|
|
|
$event->expects($this->once()) |
|
55
|
|
|
->method('getRequestType')->willReturn(HttpKernelInterface::MASTER_REQUEST); |
|
56
|
|
|
|
|
57
|
|
|
$listener = new LoggedInUserListener($accessTokenRepo, $tokenStorage, $auditConfig); |
|
58
|
|
|
$listener->onKernelRequest($event); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testOnKernelRequestNotMaster() |
|
62
|
|
|
{ |
|
63
|
|
|
/** @var AccessTokenRepository|\PHPUnit_Framework_MockObject_MockObject $accessTokenRepo */ |
|
64
|
|
|
$accessTokenRepo = $this->getMockBuilder(AccessTokenRepository::class) |
|
65
|
|
|
->disableOriginalConstructor()->getMock(); |
|
66
|
|
|
|
|
67
|
|
|
/** @var TokenStorageInterface|\PHPUnit_Framework_MockObject_MockObject $tokenStorage */ |
|
68
|
|
|
$tokenStorage = $this->createMock(TokenStorageInterface::class); |
|
69
|
|
|
$tokenStorage->expects($this->never())->method('getToken'); |
|
70
|
|
|
|
|
71
|
|
|
$auditConfig = new AuditConfiguration(); |
|
72
|
|
|
|
|
73
|
|
|
/** @var GetResponseEvent|\PHPUnit_Framework_MockObject_MockObject $event */ |
|
74
|
|
|
$event = $this->getMockBuilder(GetResponseEvent::class)->disableOriginalConstructor()->getMock(); |
|
75
|
|
|
$event->expects($this->once()) |
|
76
|
|
|
->method('getRequestType')->willReturn(HttpKernelInterface::SUB_REQUEST); |
|
77
|
|
|
|
|
78
|
|
|
$listener = new LoggedInUserListener($accessTokenRepo, $tokenStorage, $auditConfig); |
|
79
|
|
|
$listener->onKernelRequest($event); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testOnKernelRequestNotOAuthToken() |
|
83
|
|
|
{ |
|
84
|
|
|
/** @var AccessTokenRepository|\PHPUnit_Framework_MockObject_MockObject $accessTokenRepo */ |
|
85
|
|
|
$accessTokenRepo = $this->getMockBuilder(AccessTokenRepository::class) |
|
86
|
|
|
->disableOriginalConstructor()->getMock(); |
|
87
|
|
|
|
|
88
|
|
|
$token = $this->createMock(TokenInterface::class); |
|
89
|
|
|
|
|
90
|
|
|
/** @var TokenStorageInterface|\PHPUnit_Framework_MockObject_MockObject $tokenStorage */ |
|
91
|
|
|
$tokenStorage = $this->createMock(TokenStorageInterface::class); |
|
92
|
|
|
$tokenStorage->expects($this->once())->method('getToken')->willReturn($token); |
|
93
|
|
|
|
|
94
|
|
|
$auditConfig = new AuditConfiguration(); |
|
95
|
|
|
|
|
96
|
|
|
/** @var GetResponseEvent|\PHPUnit_Framework_MockObject_MockObject $event */ |
|
97
|
|
|
$event = $this->getMockBuilder(GetResponseEvent::class)->disableOriginalConstructor()->getMock(); |
|
98
|
|
|
$event->expects($this->once()) |
|
99
|
|
|
->method('getRequestType')->willReturn(HttpKernelInterface::MASTER_REQUEST); |
|
100
|
|
|
|
|
101
|
|
|
$listener = new LoggedInUserListener($accessTokenRepo, $tokenStorage, $auditConfig); |
|
102
|
|
|
$listener->onKernelRequest($event); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|