Passed
Pull Request — master (#52)
by Matthieu
05:30 queued 45s
created

xist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
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\JWTAuthenticator;
7
use AtlassianConnectBundle\Security\JWTSecurityHelperInterface;
8
use AtlassianConnectBundle\Security\JWTUserProvider;
9
use AtlassianConnectBundle\Security\JWTUserProviderInterface;
10
use PHPUnit\Framework\MockObject\MockObject;
11
use PHPUnit\Framework\TestCase;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Kernel;
14
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15
use Symfony\Component\Security\Core\Exception\AuthenticationException;
16
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
17
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
18
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
19
20
/**
21
 * Class JWTAuthenticatorTest
22
 */
23
final class JWTAuthenticatorTest extends TestCase
24
{
25
    /**
26
     * @var JWTUserProviderInterface|MockObject
27
     */
28
    private $userProvider;
29
30
    /**
31
     * @var JWTSecurityHelperInterface|MockObject
32
     */
33
    private $securityHelper;
34
35
    /**
36
     * @var JWTAuthenticator
37
     */
38
    private $jwtAuthenticator;
39
40
    /**
41
     * Setup function
42
     */
43
    protected function setUp(): void
44
    {
45
        if (Kernel::VERSION_ID < 50200) {
46
            $this->markTestSkipped('This test only works with the new authenticator mechanism');
47
        }
48
49
        $this->userProvider = $this->createMock(JWTUserProvider::class);
50
        $this->securityHelper = $this->createMock(JWTSecurityHelperInterface::class);
51
        $this->jwtAuthenticator = new JWTAuthenticator(
52
            $this->userProvider,
53
            $this->securityHelper
54
        );
55
    }
56
57
    /**
58
     * Tests if the request is supported
59
     */
60
    public function testSupportsRequest(): void
61
    {
62
        $this->securityHelper
63
            ->expects($this->once())
64
            ->method('supportsRequest')
65
            ->with($request = new Request())
66
            ->willReturn(true);
67
68
        $this->assertTrue($this->jwtAuthenticator->supports($request));
69
    }
70
71
    /**
72
     * Test the authenticate method
73
     */
74
    public function testAuthenticate(): void
75
    {
76
        $token = [
77
            'sub' => 'username',
78
            'iss' => 'key',
79
        ];
80
81
        $this->securityHelper
82
            ->expects($this->once())
83
            ->method('getJWTToken')
84
            ->with($request = new Request())
85
            ->willReturn('token');
86
87
        $this->userProvider
88
            ->expects($this->once())
89
            ->method('getDecodedToken')
90
            ->with('token')
91
            ->willReturn((object) $token);
92
93
        $this->userProvider
94
            ->expects($this->once())
95
            ->method('loadUserByIdentifier')
96
            ->with('key')
97
            ->willReturn(new Tenant());
98
99
        $result = $this->jwtAuthenticator->authenticate($request);
100
        $this->assertEquals(
101
            new SelfValidatingPassport(new UserBadge('username')),
102
            $result
103
        );
104
    }
105
106
    /**
107
     * Test if an exception is thrown when no jwt token is present
108
     */
109
    public function testAuthenticateHasNoJWTToken(): void
110
    {
111
        $this->expectException(CustomUserMessageAuthenticationException::class);
112
        $this->expectExceptionMessage('JWT Token not provided');
113
114
        $this->securityHelper
115
            ->expects($this->once())
116
            ->method('getJWTToken')
117
            ->with($request = new Request())
118
            ->willReturn(null);
119
120
        $this->jwtAuthenticator->authenticate($request);
121
    }
122
123
    /**
124
     * Test if an exception is thrown when no client key is present
125
     */
126
    public function testAuthenticateHasNoClientKey(): void
127
    {
128
        $this->expectException(CustomUserMessageAuthenticationException::class);
129
        $this->expectExceptionMessage('API Key token does not exist');
130
131
        $token = [
132
            'sub' => 'username',
133
            'iss' => null,
134
        ];
135
136
        $this->securityHelper
137
            ->expects($this->once())
138
            ->method('getJWTToken')
139
            ->with($request = new Request())
140
            ->willReturn('token');
141
142
        $this->userProvider
143
            ->expects($this->once())
144
            ->method('getDecodedToken')
145
            ->with('token')
146
            ->willReturn((object) $token);
147
148
        $this->jwtAuthenticator->authenticate($request);
149
    }
150
151
    /**
152
     * test onAuthenticationFailure Method
153
     */
154
    public function testItSendsAResponseOnAuthenticationFailure(): void
155
    {
156
        $response = $this->jwtAuthenticator->onAuthenticationFailure(new Request(), new AuthenticationException('Error'));
157
158
        $this->assertEquals('Authentication Failed: Error', $response->getContent());
159
        $this->assertEquals(403, $response->getStatusCode());
160
    }
161
162
    /**
163
     * test onAuthenticationSuccess method
164
     */
165
    public function testItDoesNotSendAResponseOnAuthenticationSuccess(): void
166
    {
167
        $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...
168
    }
169
}
170