Passed
Pull Request — master (#52)
by Matthieu
04:51
created

testItDoesNotSendAResponseOnAuthenticationSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace AtlassianConnectBundle\Tests\Security;
4
5
use AtlassianConnectBundle\Entity\Tenant;
6
use AtlassianConnectBundle\Security\JWTSecurityHelperInterface;
7
use AtlassianConnectBundle\Security\JWTUserProvider;
8
use AtlassianConnectBundle\Security\JWTUserProviderInterface;
9
use AtlassianConnectBundle\Security\LegacyJWTAuthenticator;
10
use PHPUnit\Framework\MockObject\MockObject;
11
use PHPUnit\Framework\TestCase;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
14
use Symfony\Component\Security\Core\Exception\AuthenticationException;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
use Symfony\Component\Security\Core\User\UserProviderInterface;
17
18
/**
19
 * Class LegacyJWTAuthenticatorTest
20
 */
21
final class LegacyJWTAuthenticatorTest extends TestCase
22
{
23
    /**
24
     * @var JWTSecurityHelperInterface|MockObject
25
     */
26
    private $securityHelper;
27
28
    /**
29
     * @var LegacyJWTAuthenticator
30
     */
31
    private $jwtAuthenticator;
32
33
    /**
34
     * Setup method
35
     */
36
    public function setUp(): void
37
    {
38
        $this->securityHelper = $this->createMock(JWTSecurityHelperInterface::class);
39
40
        $this->jwtAuthenticator = new LegacyJWTAuthenticator($this->securityHelper);
41
    }
42
43
    /**
44
     * Test start method
45
     */
46
    public function testItSendsA401WhenNoAuthenticationHeaderIsSet(): void
47
    {
48
        $response = $this->jwtAuthenticator->start(new Request());
49
50
        $this->assertEquals('Authentication header required', $response->getContent());
51
        $this->assertEquals(401, $response->getStatusCode());
52
    }
53
54
    /**
55
     * Tests if the request is supported
56
     */
57
    public function testSupportsRequest(): void
58
    {
59
        $this->securityHelper
60
            ->expects($this->once())
61
            ->method('supportsRequest')
62
            ->with($request = new Request())
63
            ->willReturn(true);
64
65
        $this->assertTrue($this->jwtAuthenticator->supports($request));
66
    }
67
68
    /**
69
     * Test if the getCredentials method returns a valid array
70
     */
71
    public function testGetsCredentials(): void
72
    {
73
        $request = new Request(['jwt' => 'token']);
74
        $this->securityHelper
75
            ->expects($this->once())
76
            ->method('getJWTToken')
77
            ->with($request)
78
            ->willReturn('token');
79
80
        $this->assertSame(['jwt' => 'token'], $this->jwtAuthenticator->getCredentials($request));
81
    }
82
83
    /**
84
     * Test if the getCredentials method returns null when no jwt token is passed
85
     */
86
    public function testGetsCredentialsTokenDoesNotExist(): void
87
    {
88
        $request = new Request();
89
        $this->securityHelper
90
            ->expects($this->once())
91
            ->method('getJWTToken')
92
            ->with($request)
93
            ->willReturn(null);
94
95
        $this->assertNull($this->jwtAuthenticator->getCredentials($request));
96
    }
97
98
    /**
99
     * Test get user gets invalid user provider
100
     */
101
    public function testGetUserGetsInvalidUserProvider(): void
102
    {
103
        $this->expectException(\InvalidArgumentException::class);
104
        $this->expectExceptionMessage('UserProvider must implement AtlassianConnectBundle\Security\JWTUserProviderInterface');
105
106
        $userProvider = $this->createMock(UserProviderInterface::class);
107
108
        $this->jwtAuthenticator->getUser('credentials', $userProvider);
109
    }
110
111
    /**
112
     * Test get user without client key throws exception
113
     */
114
    public function testGetUserWithoutClientKeyThrowsException(): void
115
    {
116
        $this->expectException(AuthenticationException::class);
117
        $this->expectExceptionMessage('API Key "token" does not exist.');
118
119
        $token = [
120
            'sub' => 'username',
121
            'iss' => null,
122
        ];
123
124
        $userProvider = $this->createMock(JWTUserProviderInterface::class);
125
        $userProvider
126
            ->expects($this->once())
127
            ->method('getDecodedToken')
128
            ->willReturn((object) $token);
129
130
        $this->jwtAuthenticator->getUser(['jwt' => 'token'], $userProvider);
131
    }
132
133
    /**
134
     * Test UserProvider with loadByIdentifier metdho
135
     */
136
    public function testUserProviderHasLoadMethod(): void
137
    {
138
        $token = [
139
            'iss' => 'iss',
140
            'sub' => 'username',
141
        ];
142
143
        $tenant = new Tenant();
144
145
        $userProvider = $this->createMock(JWTUserProvider::class);
146
        $userProvider
147
            ->expects($this->once())
148
            ->method('getDecodedToken')
149
            ->willReturn((object) $token);
150
151
        $userProvider
152
            ->expects($this->once())
153
            ->method('loadUserByIdentifier')
154
            ->with('iss')
155
            ->willReturn($tenant);
156
157
        $user = $this->jwtAuthenticator->getUser(['jwt' => 'token'], $userProvider);
158
159
        $this->assertInstanceOf(Tenant::class, $user);
160
        $this->assertEquals('username', $tenant->getUsername());
161
    }
162
163
    /**
164
     * Test if a user gets fetched
165
     */
166
    public function testGetsUser(): void
167
    {
168
        $token = [
169
            'iss' => 'iss',
170
            'sub' => 'username',
171
        ];
172
173
        $tenant = new Tenant();
174
175
        $userProvider = $this->createMock(JWTUserProviderInterface::class);
176
        $userProvider
177
            ->expects($this->once())
178
            ->method('getDecodedToken')
179
            ->willReturn((object) $token);
180
181
        $userProvider
182
            ->expects($this->once())
183
            ->method('loadUserByUsername')
184
            ->with('iss')
185
            ->willReturn($tenant);
186
187
        $user = $this->jwtAuthenticator->getUser(['jwt' => 'token'], $userProvider);
188
189
        $this->assertInstanceOf(Tenant::class, $user);
190
        $this->assertEquals('username', $tenant->getUsername());
191
    }
192
193
    /**
194
     * test checkCredentials method
195
     */
196
    public function testItChecksCredentials(): void
197
    {
198
        $this->assertTrue($this->jwtAuthenticator->checkCredentials(null, $this->createMock(UserInterface::class)));
199
    }
200
201
    /**
202
     * test onAuthenticationFailure Method
203
     */
204
    public function testItSendsAResponseOnAuthenticationFailure(): void
205
    {
206
        $response = $this->jwtAuthenticator->onAuthenticationFailure(new Request(), new AuthenticationException('Error'));
207
208
        $this->assertEquals('Authentication Failed: Error', $response->getContent());
209
        $this->assertEquals(403, $response->getStatusCode());
210
    }
211
212
    /**
213
     * test onAuthenticationSuccess method
214
     */
215
    public function testItDoesNotSendAResponseOnAuthenticationSuccess(): void
216
    {
217
        $this->assertNull($this->jwtAuthenticator->onAuthenticationSuccess(new Request(), $this->createMock(TokenInterface::class), 'main'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->jwtAuthenticator-...erface::class), 'main') targeting AtlassianConnectBundle\S...AuthenticationSuccess() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
218
    }
219
220
    /**
221
     * test supportsRememberMe method
222
     */
223
    public function testItDoesNotSupportRememberMeFunctionality(): void
224
    {
225
        $this->assertFalse($this->jwtAuthenticator->supportsRememberMe());
226
    }
227
}
228