Completed
Push — master ( 224127...d5dc6c )
by Alejandro
28s
created

AuthorizationHeaderPluginTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 116
Duplicated Lines 26.72 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 31
loc 116
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A verifyAnAuthorizationWithoutBearerTypeThrowsException() 16 16 1
A verifyAnAuthorizationWithWrongTypeThrowsException() 15 15 1
A verifyAnExpiredTokenThrowsException() 0 20 1
A verifyValidTokenDoesNotThrowException() 0 13 1
A updateReturnsAnUpdatedResponseWithNewJwt() 0 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Rest\Authentication\Plugin;
5
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Prophecy\ObjectProphecy;
8
use Shlinkio\Shlink\Rest\Authentication\JWTServiceInterface;
9
use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthorizationHeaderPlugin;
10
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException;
11
use Zend\Diactoros\Response;
12
use Zend\Diactoros\ServerRequestFactory;
13
use Zend\I18n\Translator\Translator;
14
15
class AuthorizationHeaderPluginTest extends TestCase
16
{
17
    /**
18
     * @var AuthorizationHeaderPlugin
19
     */
20
    private $plugin;
21
    /**
22
     * @var ObjectProphecy
23
     */
24
    protected $jwtService;
25
26
    public function setUp()
27
    {
28
        $this->jwtService = $this->prophesize(JWTServiceInterface::class);
29
        $this->plugin = new AuthorizationHeaderPlugin($this->jwtService->reveal(), Translator::factory([]));
30
    }
31
32
    /**
33
     * @test
34
     */
35 View Code Duplication
    public function verifyAnAuthorizationWithoutBearerTypeThrowsException()
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...
36
    {
37
        $authToken = 'ABC-abc';
38
        $request = ServerRequestFactory::fromGlobals()->withHeader(
39
            AuthorizationHeaderPlugin::HEADER_NAME,
40
            $authToken
41
        );
42
43
        $this->expectException(VerifyAuthenticationException::class);
44
        $this->expectExceptionMessage(sprintf(
45
            'You need to provide the Bearer type in the %s header.',
46
            AuthorizationHeaderPlugin::HEADER_NAME
47
        ));
48
49
        $this->plugin->verify($request);
50
    }
51
52
    /**
53
     * @test
54
     */
55 View Code Duplication
    public function verifyAnAuthorizationWithWrongTypeThrowsException()
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...
56
    {
57
        $authToken = 'Basic ABC-abc';
58
        $request = ServerRequestFactory::fromGlobals()->withHeader(
59
            AuthorizationHeaderPlugin::HEADER_NAME,
60
            $authToken
61
        );
62
63
        $this->expectException(VerifyAuthenticationException::class);
64
        $this->expectExceptionMessage(
65
            'Provided authorization type Basic is not supported. Use Bearer instead.'
66
        );
67
68
        $this->plugin->verify($request);
69
    }
70
71
    /**
72
     * @test
73
     */
74
    public function verifyAnExpiredTokenThrowsException()
75
    {
76
        $authToken = 'Bearer ABC-abc';
77
        $request = ServerRequestFactory::fromGlobals()->withHeader(
78
            AuthorizationHeaderPlugin::HEADER_NAME,
79
            $authToken
80
        );
81
        $jwtVerify = $this->jwtService->verify('ABC-abc')->willReturn(false);
82
83
        $this->expectException(VerifyAuthenticationException::class);
84
        $this->expectExceptionMessage(sprintf(
85
            'Missing or invalid auth token provided. Perform a new authentication request and send provided '
86
            . 'token on every new request on the %s header',
87
            AuthorizationHeaderPlugin::HEADER_NAME
88
        ));
89
90
        $this->plugin->verify($request);
91
92
        $jwtVerify->shouldHaveBeenCalledTimes(1);
93
    }
94
95
    /**
96
     * @test
97
     */
98
    public function verifyValidTokenDoesNotThrowException()
99
    {
100
        $authToken = 'Bearer ABC-abc';
101
        $request = ServerRequestFactory::fromGlobals()->withHeader(
102
            AuthorizationHeaderPlugin::HEADER_NAME,
103
            $authToken
104
        );
105
        $jwtVerify = $this->jwtService->verify('ABC-abc')->willReturn(true);
106
107
        $this->plugin->verify($request);
108
109
        $jwtVerify->shouldHaveBeenCalledTimes(1);
110
    }
111
112
    /**
113
     * @test
114
     */
115
    public function updateReturnsAnUpdatedResponseWithNewJwt()
116
    {
117
        $authToken = 'Bearer ABC-abc';
118
        $request = ServerRequestFactory::fromGlobals()->withHeader(
119
            AuthorizationHeaderPlugin::HEADER_NAME,
120
            $authToken
121
        );
122
        $jwtRefresh = $this->jwtService->refresh('ABC-abc')->willReturn('DEF-def');
123
124
        $response = $this->plugin->update($request, new Response());
125
126
        $this->assertTrue($response->hasHeader(AuthorizationHeaderPlugin::HEADER_NAME));
127
        $this->assertEquals('Bearer DEF-def', $response->getHeaderLine(AuthorizationHeaderPlugin::HEADER_NAME));
128
        $jwtRefresh->shouldHaveBeenCalledTimes(1);
129
    }
130
}
131