|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\Auth; |
|
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\Yii\Web\Auth\AuthInterface; |
|
13
|
|
|
use Yiisoft\Yii\Web\Auth\AuthMiddleware; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Yiisoft\Yii\Web\User\IdentityInterface; |
|
16
|
|
|
|
|
17
|
|
|
class AuthMiddlewareTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var ResponseFactoryInterface */ |
|
20
|
|
|
private $responseFactory; |
|
21
|
|
|
|
|
22
|
|
|
/** @var AuthInterface|MockObject */ |
|
23
|
|
|
private $authenticator; |
|
24
|
|
|
|
|
25
|
|
|
protected function setUp() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->responseFactory = new Psr17Factory(); |
|
28
|
|
|
$this->authenticator = $this->createMock(AuthInterface::class); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @test |
|
33
|
|
|
*/ |
|
34
|
|
|
public function shouldAuthenticateAndSetAttribute() |
|
35
|
|
|
{ |
|
36
|
|
|
$request = new ServerRequest('GET', '/'); |
|
37
|
|
|
$identity = $this->createMock(IdentityInterface::class); |
|
38
|
|
|
$identityAttribute = 'identity'; |
|
39
|
|
|
|
|
40
|
|
|
$this->authenticator |
|
41
|
|
|
->expects($this->once()) |
|
42
|
|
|
->method('authenticate') |
|
43
|
|
|
->willReturn($identity); |
|
44
|
|
|
|
|
45
|
|
|
$handler = $this->createMock(RequestHandlerInterface::class); |
|
46
|
|
|
$handler |
|
47
|
|
|
->expects($this->once()) |
|
48
|
|
|
->method('handle') |
|
49
|
|
|
->willReturnCallback( |
|
50
|
|
|
function (ServerRequestInterface $request) use ($identityAttribute, $identity) { |
|
51
|
|
|
$this->assertEquals($identity, $request->getAttribute($identityAttribute)); |
|
52
|
|
|
|
|
53
|
|
|
return $this->responseFactory->createResponse(); |
|
54
|
|
|
} |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$auth = new AuthMiddleware($this->responseFactory, $this->authenticator); |
|
58
|
|
|
$auth->setRequestName($identityAttribute); |
|
59
|
|
|
$auth->process($request, $handler); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @test |
|
64
|
|
|
*/ |
|
65
|
|
|
public function shouldAuthenticateOptionalPath() |
|
66
|
|
|
{ |
|
67
|
|
|
$path = '/optional'; |
|
68
|
|
|
$request = new ServerRequest('GET', $path); |
|
69
|
|
|
|
|
70
|
|
|
$this->authenticator |
|
71
|
|
|
->expects($this->once()) |
|
72
|
|
|
->method('authenticate') |
|
73
|
|
|
->willReturn(null); |
|
74
|
|
|
|
|
75
|
|
|
$handler = $this->createMock(RequestHandlerInterface::class); |
|
76
|
|
|
$handler |
|
77
|
|
|
->expects($this->once()) |
|
78
|
|
|
->method('handle'); |
|
79
|
|
|
|
|
80
|
|
|
$auth = new AuthMiddleware($this->responseFactory, $this->authenticator); |
|
81
|
|
|
$auth->setOptional([$path]); |
|
82
|
|
|
$auth->process($request, $handler); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @test |
|
87
|
|
|
*/ |
|
88
|
|
|
public function shouldNotAuthenticate() |
|
89
|
|
|
{ |
|
90
|
|
|
$request = new ServerRequest('GET', '/'); |
|
91
|
|
|
$header = 'Authenticated'; |
|
92
|
|
|
$headerValue = 'false'; |
|
93
|
|
|
|
|
94
|
|
|
$this->authenticator |
|
95
|
|
|
->expects($this->once()) |
|
96
|
|
|
->method('authenticate') |
|
97
|
|
|
->willReturn(null); |
|
98
|
|
|
|
|
99
|
|
|
$this->authenticator |
|
100
|
|
|
->expects($this->once()) |
|
101
|
|
|
->method('challenge') |
|
102
|
|
|
->willReturnCallback( |
|
103
|
|
|
function (ResponseInterface $response) use ($header, $headerValue) { |
|
104
|
|
|
return $response->withHeader($header, $headerValue); |
|
105
|
|
|
} |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
$handler = $this->createMock(RequestHandlerInterface::class); |
|
109
|
|
|
$handler |
|
110
|
|
|
->expects($this->never()) |
|
111
|
|
|
->method('handle'); |
|
112
|
|
|
|
|
113
|
|
|
$auth = new AuthMiddleware($this->responseFactory, $this->authenticator); |
|
114
|
|
|
$response = $auth->process($request, $handler); |
|
115
|
|
|
$this->assertEquals(401, $response->getStatusCode()); |
|
116
|
|
|
$this->assertEquals($headerValue, $response->getHeaderLine($header)); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|