Completed
Push — master ( 3ef1d3...dea670 )
by John
02:57
created

willSkipAuthenticationIfCustomHeaderNotSetInRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\JwtBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\JwtBundle\Tests\Firewall;
9
10
use KleijnWeb\JwtBundle\Authentication\JwtAuthenticatedToken;
11
use KleijnWeb\JwtBundle\Authentication\JwtAuthenticationToken;
12
use KleijnWeb\JwtBundle\Firewall\JwtAuthenticationListener;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
15
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17
use Symfony\Component\Security\Core\User\UserInterface;
18
19
/**
20
 * @author John Kleijn <[email protected]>
21
 */
22
class JwtAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * @var AuthenticationManagerInterface
26
     */
27
    private $authenticationManagerMock;
28
29
    /**
30
     * @var TokenStorageInterface
31
     */
32
    private $tokenStorageMock;
33
34
    protected function setUp()
35
    {
36
        $this->tokenStorageMock          = $this->getMockForAbstractClass(TokenStorageInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockForAbstrac...torageInterface::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Symfony\Component...\TokenStorageInterface> of property $tokenStorageMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        $this->authenticationManagerMock = $this->getMockForAbstractClass(AuthenticationManagerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockForAbstrac...anagerInterface::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Symfony\Component...cationManagerInterface> of property $authenticationManagerMock.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
    }
39
40
    /**
41
     * @test
42
     */
43
    public function willSkipAuthenticationIfHeaderNotSetInRequest()
44
    {
45
        /** @var \PHPUnit_Framework_MockObject_MockObject $mock */
46
        $mock = $this->authenticationManagerMock;
47
        $mock->expects($this->never())->method('authenticate');
48
49
        (new JwtAuthenticationListener($this->tokenStorageMock, $this->authenticationManagerMock))
50
            ->handle($this->createKernelEventWithRequest(new Request()));
51
    }
52
53
    /**
54
     * @test
55
     */
56 View Code Duplication
    public function canCreateTokenFromBearerHeaderByDefault()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        $tokenString = 'TheJwtToken';
59
60
        /** @var \PHPUnit_Framework_MockObject_MockObject $mock */
61
        $mock = $this->authenticationManagerMock;
62
        $mock->expects($this->once())
63
            ->method('authenticate')
64
            ->with($this->callback(function (JwtAuthenticationToken $token) use ($tokenString) {
65
                return $token->getCredentials() === $tokenString;
66
            }));
67
68
        (new JwtAuthenticationListener($this->tokenStorageMock, $this->authenticationManagerMock))
69
            ->handle($this->createKernelEventWithRequest(
70
                $this->createRequestWithServerVar('HTTP_AUTHORIZATION', "Bearer $tokenString"))
71
            );
72
73
    }
74
75
    /**
76
     * @test
77
     */
78 View Code Duplication
    public function canCreateTokenFromAuthHeaderWithoutBearerPrefixByDefault()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        $tokenString = 'TheJwtToken';
81
82
        /** @var \PHPUnit_Framework_MockObject_MockObject $mock */
83
        $mock = $this->authenticationManagerMock;
84
        $mock->expects($this->once())
85
            ->method('authenticate')
86
            ->with($this->callback(function (JwtAuthenticationToken $token) use ($tokenString) {
87
                return $token->getCredentials() === $tokenString;
88
            }));
89
90
        (new JwtAuthenticationListener($this->tokenStorageMock, $this->authenticationManagerMock))
91
            ->handle($this->createKernelEventWithRequest(
92
                $this->createRequestWithServerVar('HTTP_AUTHORIZATION', $tokenString))
93
            );
94
    }
95
96
    /**
97
     * @test
98
     */
99 View Code Duplication
    public function canCreateTokenFromCustomHeader()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $tokenString = 'TheJwtToken';
102
103
        /** @var \PHPUnit_Framework_MockObject_MockObject $mock */
104
        $mock = $this->authenticationManagerMock;
105
        $mock->expects($this->once())
106
            ->method('authenticate')
107
            ->with($this->callback(function (JwtAuthenticationToken $token) use ($tokenString) {
108
                return $token->getCredentials() === $tokenString;
109
            }));
110
111
        (new JwtAuthenticationListener($this->tokenStorageMock, $this->authenticationManagerMock, 'X-Token'))
112
            ->handle($this->createKernelEventWithRequest(
113
                $this->createRequestWithServerVar('HTTP_X_TOKEN', $tokenString))
114
            );
115
    }
116
117
    /**
118
     * @test
119
     */
120
    public function willSkipAuthenticationIfCustomHeaderNotSetInRequest()
121
    {
122
        /** @var \PHPUnit_Framework_MockObject_MockObject $mock */
123
        $mock = $this->authenticationManagerMock;
124
        $mock->expects($this->never())->method('authenticate');
125
126
        (new JwtAuthenticationListener($this->tokenStorageMock, $this->authenticationManagerMock, 'X-Token'))
127
            ->handle($this->createKernelEventWithRequest(
128
                $this->createRequestWithServerVar('HTTP_AUTHORIZATION', 'something'))
129
            );
130
    }
131
132
    /**
133
     * @test
134
     */
135
    public function willSetTokenReturnedByAuthenticationHeaderOnStorage()
136
    {
137
138
        $userStub = $this->getMockForAbstractClass(UserInterface::class);
139
        $userStub->expects($this->any())->method('getRoles')->willReturn([]);
140
141
        $authenticatedToken = new JwtAuthenticatedToken($userStub);
142
143
        /** @var \PHPUnit_Framework_MockObject_MockObject $mock */
144
        $mock = $this->authenticationManagerMock;
145
        $mock->expects($this->once())
146
            ->method('authenticate')
147
            ->willReturnCallback(function () use($authenticatedToken) {
148
                return $authenticatedToken;
149
            });
150
151
        /** @var \PHPUnit_Framework_MockObject_MockObject $mock */
152
        $mock = $this->tokenStorageMock;
153
        $mock->expects($this->once())
154
            ->method('setToken')
155
            ->with($authenticatedToken);
156
157
        (new JwtAuthenticationListener($this->tokenStorageMock, $this->authenticationManagerMock))
158
            ->handle($this->createKernelEventWithRequest(
159
                $this->createRequestWithServerVar('HTTP_AUTHORIZATION', 'something'))
160
            );
161
    }
162
163
    private function createRequestWithServerVar(string $name, string $value): Request
164
    {
165
        return new Request([], [], [], [], [], [$name => $value]);
166
    }
167
168
    private function createKernelEventWithRequest(Request $request): GetResponseEvent
169
    {
170
        $mock = $this->getMockBuilder(GetResponseEvent::class)
171
            ->disableOriginalConstructor()
172
            ->getMock();
173
        $mock
174
            ->expects($this->any())
175
            ->method('getRequest')
176
            ->willReturn($request);
177
178
        return $mock;
179
    }
180
181
}
182