Passed
Push — master ( 1ecae2...d4e24c )
by Alexander
01:38
created

AuthMiddlewareTest::testShouldSkipCheckForOptionalPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.8333
1
<?php
2
3
namespace Yiisoft\Auth\Tests;
4
5
use Nyholm\Psr7\Factory\Psr17Factory;
6
use Nyholm\Psr7\ServerRequest;
7
use PHPUnit\Framework\MockObject\MockObject;
8
use Psr\Http\Message\ResponseFactoryInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Yiisoft\Auth\AuthInterface;
13
use Yiisoft\Auth\Middleware\Auth;
14
use Yiisoft\Auth\IdentityInterface;
15
use PHPUnit\Framework\TestCase;
16
17
final class AuthMiddlewareTest extends TestCase
18
{
19
    private ResponseFactoryInterface $responseFactory;
20
21
    /** @var AuthInterface|MockObject */
22
    private AuthInterface $authenticator;
23
24
    protected function setUp(): void
25
    {
26
        $this->responseFactory = new Psr17Factory();
27
        $this->authenticator = $this->createMock(AuthInterface::class);
28
    }
29
30
    public function testShouldAuthenticateAndSetAttribute(): void
31
    {
32
        $request = new ServerRequest('GET', '/');
33
        $identity = $this->createMock(IdentityInterface::class);
34
        $identityAttribute = 'identity';
35
36
        $this->authenticator
37
            ->expects($this->once())
38
            ->method('authenticate')
39
            ->willReturn($identity);
40
41
        $handler = $this->createMock(RequestHandlerInterface::class);
42
        $handler
43
            ->expects($this->once())
44
            ->method('handle')
45
            ->willReturnCallback(
46
                function (ServerRequestInterface $request) use ($identityAttribute, $identity) {
47
                    $this->assertEquals($identity, $request->getAttribute($identityAttribute));
48
49
                    return $this->responseFactory->createResponse();
50
                }
51
            );
52
53
        $auth = new Auth($this->responseFactory, $this->authenticator);
54
        $auth->setRequestName($identityAttribute);
55
        $auth->process($request, $handler);
56
    }
57
58
    public function testShouldSkipCheckForOptionalPath(): void
59
    {
60
        $path = '/optional';
61
        $request = new ServerRequest('GET', $path);
62
63
        $this->authenticator
64
            ->expects($this->once())
65
            ->method('authenticate')
66
            ->willReturn(null);
67
68
        $handler = $this->createMock(RequestHandlerInterface::class);
69
        $handler
70
            ->expects($this->once())
71
            ->method('handle');
72
73
        $auth = new Auth($this->responseFactory, $this->authenticator);
74
        $auth->setOptional([$path]);
75
        $auth->process($request, $handler);
76
    }
77
78
    public function testShouldNotExecuteHandlerAndReturn401OnAuthenticationFailure(): void
79
    {
80
        $request = new ServerRequest('GET', '/');
81
        $header = 'Authenticated';
82
        $headerValue = 'false';
83
84
        $this->authenticator
85
            ->expects($this->once())
86
            ->method('authenticate')
87
            ->willReturn(null);
88
89
        $this->authenticator
90
            ->expects($this->once())
91
            ->method('challenge')
92
            ->willReturnCallback(
93
                static function (ResponseInterface $response) use ($header, $headerValue) {
94
                    return $response->withHeader($header, $headerValue);
95
                }
96
            );
97
98
        $handler = $this->createMock(RequestHandlerInterface::class);
99
        $handler
100
            ->expects($this->never())
101
            ->method('handle');
102
103
        $auth = new Auth($this->responseFactory, $this->authenticator);
104
        $response = $auth->process($request, $handler);
105
        $this->assertEquals(401, $response->getStatusCode());
106
        $this->assertEquals($headerValue, $response->getHeaderLine($header));
107
    }
108
}
109