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