Completed
Push — master ( 9ab4b9...065cdd )
by Alejandro
09:44
created

someWhiteListedSituationsFallbackToNextMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 36
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 47
rs 9.0303
1
<?php
2
namespace ShlinkioTest\Shlink\Rest\Middleware;
3
4
use PHPUnit_Framework_TestCase as TestCase;
5
use Prophecy\Prophecy\ObjectProphecy;
6
use Shlinkio\Shlink\Rest\Authentication\JWTService;
7
use Shlinkio\Shlink\Rest\Middleware\CheckAuthenticationMiddleware;
8
use Zend\Diactoros\Response;
9
use Zend\Diactoros\ServerRequestFactory;
10
use Zend\Expressive\Router\RouteResult;
11
use Zend\I18n\Translator\Translator;
12
13
class CheckAuthenticationMiddlewareTest extends TestCase
14
{
15
    /**
16
     * @var CheckAuthenticationMiddleware
17
     */
18
    protected $middleware;
19
    /**
20
     * @var ObjectProphecy
21
     */
22
    protected $jwtService;
23
24
    public function setUp()
25
    {
26
        $this->jwtService = $this->prophesize(JWTService::class);
27
        $this->middleware = new CheckAuthenticationMiddleware($this->jwtService->reveal(), Translator::factory([]));
28
    }
29
30
    /**
31
     * @test
32
     */
33
    public function someWhiteListedSituationsFallbackToNextMiddleware()
34
    {
35
        $request = ServerRequestFactory::fromGlobals();
36
        $response = new Response();
37
        $isCalled = false;
38
        $this->assertFalse($isCalled);
39
        $this->middleware->__invoke($request, $response, function ($req, $resp) use (&$isCalled) {
0 ignored issues
show
Unused Code introduced by
The parameter $req is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $resp is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
            $isCalled = true;
41
        });
42
        $this->assertTrue($isCalled);
43
44
        $request = ServerRequestFactory::fromGlobals()->withAttribute(
45
            RouteResult::class,
46
            RouteResult::fromRouteFailure(['GET'])
47
        );
48
        $response = new Response();
49
        $isCalled = false;
50
        $this->assertFalse($isCalled);
51
        $this->middleware->__invoke($request, $response, function ($req, $resp) use (&$isCalled) {
0 ignored issues
show
Unused Code introduced by
The parameter $req is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $resp is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
            $isCalled = true;
53
        });
54
        $this->assertTrue($isCalled);
55
56
        $request = ServerRequestFactory::fromGlobals()->withAttribute(
57
            RouteResult::class,
58
            RouteResult::fromRouteMatch('rest-authenticate', 'foo', [])
59
        );
60
        $response = new Response();
61
        $isCalled = false;
62
        $this->assertFalse($isCalled);
63
        $this->middleware->__invoke($request, $response, function ($req, $resp) use (&$isCalled) {
0 ignored issues
show
Unused Code introduced by
The parameter $req is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $resp is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
            $isCalled = true;
65
        });
66
        $this->assertTrue($isCalled);
67
68
        $request = ServerRequestFactory::fromGlobals()->withAttribute(
69
            RouteResult::class,
70
            RouteResult::fromRouteMatch('bar', 'foo', [])
71
        )->withMethod('OPTIONS');
72
        $response = new Response();
73
        $isCalled = false;
74
        $this->assertFalse($isCalled);
75
        $this->middleware->__invoke($request, $response, function ($req, $resp) use (&$isCalled) {
0 ignored issues
show
Unused Code introduced by
The parameter $req is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $resp is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
            $isCalled = true;
77
        });
78
        $this->assertTrue($isCalled);
79
    }
80
81
    /**
82
     * @test
83
     */
84
    public function noHeaderReturnsError()
85
    {
86
        $request = ServerRequestFactory::fromGlobals()->withAttribute(
87
            RouteResult::class,
88
            RouteResult::fromRouteMatch('bar', 'foo', [])
89
        );
90
        $response = $this->middleware->__invoke($request, new Response());
91
        $this->assertEquals(401, $response->getStatusCode());
92
    }
93
94
    /**
95
     * @test
96
     */
97 View Code Duplication
    public function provideAnAuthorizationWithoutTypeReturnsError()
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...
98
    {
99
        $authToken = 'ABC-abc';
100
        $request = ServerRequestFactory::fromGlobals()->withAttribute(
101
            RouteResult::class,
102
            RouteResult::fromRouteMatch('bar', 'foo', [])
103
        )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, $authToken);
104
105
        $response = $this->middleware->__invoke($request, new Response());
106
        $this->assertEquals(401, $response->getStatusCode());
107
        $this->assertTrue(strpos($response->getBody()->getContents(), 'You need to provide the Bearer type') > 0);
108
    }
109
110
    /**
111
     * @test
112
     */
113 View Code Duplication
    public function provideAnAuthorizationWithWrongTypeReturnsError()
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...
114
    {
115
        $authToken = 'ABC-abc';
116
        $request = ServerRequestFactory::fromGlobals()->withAttribute(
117
            RouteResult::class,
118
            RouteResult::fromRouteMatch('bar', 'foo', [])
119
        )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Basic ' . $authToken);
120
121
        $response = $this->middleware->__invoke($request, new Response());
122
        $this->assertEquals(401, $response->getStatusCode());
123
        $this->assertTrue(
124
            strpos($response->getBody()->getContents(), 'Provided authorization type Basic is not supported') > 0
125
        );
126
    }
127
128
    /**
129
     * @test
130
     */
131 View Code Duplication
    public function provideAnExpiredTokenReturnsError()
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...
132
    {
133
        $authToken = 'ABC-abc';
134
        $request = ServerRequestFactory::fromGlobals()->withAttribute(
135
            RouteResult::class,
136
            RouteResult::fromRouteMatch('bar', 'foo', [])
137
        )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Bearer ' . $authToken);
138
        $this->jwtService->verify($authToken)->willReturn(false)->shouldBeCalledTimes(1);
139
140
        $response = $this->middleware->__invoke($request, new Response());
141
        $this->assertEquals(401, $response->getStatusCode());
142
    }
143
144
    /**
145
     * @test
146
     */
147
    public function provideCorrectTokenUpdatesExpirationAndFallbacksToNextMiddleware()
148
    {
149
        $authToken = 'ABC-abc';
150
        $request = ServerRequestFactory::fromGlobals()->withAttribute(
151
            RouteResult::class,
152
            RouteResult::fromRouteMatch('bar', 'foo', [])
153
        )->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'bearer ' . $authToken);
154
        $this->jwtService->verify($authToken)->willReturn(true)->shouldBeCalledTimes(1);
155
        $this->jwtService->refresh($authToken)->willReturn($authToken)->shouldBeCalledTimes(1);
156
157
        $isCalled = false;
158
        $this->assertFalse($isCalled);
159
        $this->middleware->__invoke($request, new Response(), function ($req, $resp) use (&$isCalled) {
160
            $isCalled = true;
161
            return $resp;
162
        });
163
        $this->assertTrue($isCalled);
164
    }
165
}
166