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

AuthenticationTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 61
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testProtectedRouteWithBearerToken() 0 6 1
A getTenantJWTCode() 0 9 1
A testProtectedRouteWithQueryToken() 0 6 1
A testProtectedRouteInDevEnvironment() 0 6 1
A testProtectedRouteWithoutAuthentication() 0 8 1
1
<?php declare(strict_types = 1);
2
3
namespace AtlassianConnectBundle\Tests\Functional;
4
5
use AtlassianConnectBundle\Service\QSHGenerator;
6
use Firebase\JWT\JWT;
7
8
/**
9
 * Class AuthenticationTest
10
 */
11
final class AuthenticationTest extends AbstractWebTestCase
12
{
13
    /**
14
     * test a protected route without any authentication headers
15
     * also test the entry point response
16
     */
17
    public function testProtectedRouteWithoutAuthentication(): void
18
    {
19
        $client = self::createClient(['environment' => 'prod']);
20
21
        $client->request('GET', '/protected/route');
22
23
        $this->assertResponseStatusCodeSame(401);
24
        $this->assertSame('Authentication header required', $client->getResponse()->getContent());
25
    }
26
27
    /**
28
     * test authentication with bearer endpoint
29
     */
30
    public function testProtectedRouteWithBearerToken(): void
31
    {
32
        $client = self::createClient(['environment' => 'prod'], ['HTTP_AUTHORIZATION' => 'Bearer '.$this->getTenantJWTCode()]);
33
34
        $client->request('GET', '/protected/route');
35
        $this->assertResponseIsSuccessful();
36
    }
37
38
    /**
39
     * test authentication with jwt endpoint
40
     */
41
    public function testProtectedRouteWithQueryToken(): void
42
    {
43
        $client = self::createClient(['environment' => 'prod']);
44
45
        $client->request('GET', '/protected/route?jwt='.$this->getTenantJWTCode());
46
        $this->assertResponseIsSuccessful();
47
    }
48
49
    /**
50
     * test authentication in dev mode
51
     */
52
    public function testProtectedRouteInDevEnvironment(): void
53
    {
54
        $client = self::createClient(['environment' => 'dev']);
55
56
        $client->request('GET', '/protected/route');
57
        $this->assertResponseIsSuccessful();
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getTenantJWTCode(): string
64
    {
65
        return JWT::encode([
66
            'iss' => 'client_key',
67
            'iat' => \time(),
68
            'exp' => \strtotime('+1 day'),
69
            'qsh' => QSHGenerator::generate('/protected_route', 'GET'),
70
            'sub' => 'admin',
71
        ], 'shared_secret');
72
    }
73
}
74