|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Http\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Framework\TestDouble\Session\MockSessionFactory; |
|
8
|
|
|
use Casbin\Enforcer; |
|
9
|
|
|
use Casbin\Exceptions\CasbinException; |
|
10
|
|
|
use Opulence\Http\Requests\Request; |
|
11
|
|
|
use Opulence\Http\Responses\RedirectResponse; |
|
12
|
|
|
use Opulence\Http\Responses\Response; |
|
13
|
|
|
use Opulence\Http\Responses\ResponseHeaders; |
|
14
|
|
|
use Opulence\Sessions\ISession; |
|
15
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
|
|
18
|
|
|
class AuthorizationTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
protected const RESOURCE = 'foo'; |
|
21
|
|
|
protected const ROLE = 'bar'; |
|
22
|
|
|
protected const USERNAME = 'baz'; |
|
23
|
|
|
|
|
24
|
|
|
/** @var Authorization - System Under Test */ |
|
25
|
|
|
protected $sut; |
|
26
|
|
|
|
|
27
|
|
|
/** @var MockObject|ISession */ |
|
28
|
|
|
protected $sessionMock; |
|
29
|
|
|
|
|
30
|
|
|
/** @var MockObject|Enforcer */ |
|
31
|
|
|
protected $enforcerMock; |
|
32
|
|
|
|
|
33
|
|
|
/** @var array<string,string> */ |
|
34
|
|
|
protected $parametersStub = ['resource' => self::RESOURCE, 'role' => self::ROLE]; |
|
35
|
|
|
|
|
36
|
|
|
/** @var array<string,string> */ |
|
37
|
|
|
protected $sessionDataStub = ['username' => self::USERNAME]; |
|
38
|
|
|
|
|
39
|
|
|
public function setUp(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$this->sessionMock = MockSessionFactory::create($this, $this->sessionDataStub); |
|
42
|
|
|
|
|
43
|
|
|
$this->enforcerMock = $this->createMock(Enforcer::class); |
|
44
|
|
|
|
|
45
|
|
|
$this->sut = new Authorization( |
|
46
|
|
|
$this->sessionMock, |
|
|
|
|
|
|
47
|
|
|
$this->enforcerMock |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$this->sut->setParameters($this->parametersStub); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testHandleCallsNextIfEnforcingCasbinRulesSucceed() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->enforcerMock->expects($this->once())->method('enforce')->willReturn(true); |
|
56
|
|
|
|
|
57
|
|
|
$requestStub = new Request([], [], [], [], [], [], null); |
|
58
|
|
|
$responseStub = new Response(); |
|
59
|
|
|
|
|
60
|
|
|
$next = function () use ($responseStub) { |
|
61
|
|
|
return $responseStub; |
|
62
|
|
|
}; |
|
63
|
|
|
|
|
64
|
|
|
$actualResult = $this->sut->handle($requestStub, $next); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertSame($responseStub, $actualResult); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testHandleRedirectsTo403OnCasbinRulesEnforcingFailure() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->enforcerMock |
|
72
|
|
|
->expects($this->once()) |
|
73
|
|
|
->method('enforce') |
|
74
|
|
|
->willReturn(false); |
|
75
|
|
|
|
|
76
|
|
|
$requestStub = new Request([], [], [], [], [], [], null); |
|
77
|
|
|
$responseStub = new Response(); |
|
78
|
|
|
|
|
79
|
|
|
$next = function () use ($responseStub) { |
|
80
|
|
|
return $responseStub; |
|
81
|
|
|
}; |
|
82
|
|
|
|
|
83
|
|
|
/** @var RedirectResponse $actualResult */ |
|
84
|
|
|
$actualResult = $this->sut->handle($requestStub, $next); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertNotSame($responseStub, $actualResult); |
|
87
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $actualResult); |
|
88
|
|
|
$this->assertSame(ResponseHeaders::HTTP_TEMPORARY_REDIRECT, $actualResult->getStatusCode()); |
|
89
|
|
|
$this->assertSame(Authorization::PATH_403, $actualResult->getTargetUrl()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testHandleRedirectsTo403OnCasbinRulesEnforcingError() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->enforcerMock |
|
95
|
|
|
->expects($this->once()) |
|
96
|
|
|
->method('enforce') |
|
97
|
|
|
->willThrowException(new CasbinException()); |
|
98
|
|
|
|
|
99
|
|
|
$requestStub = new Request([], [], [], [], [], [], null); |
|
100
|
|
|
$responseStub = new Response(); |
|
101
|
|
|
|
|
102
|
|
|
$next = function () use ($responseStub) { |
|
103
|
|
|
return $responseStub; |
|
104
|
|
|
}; |
|
105
|
|
|
|
|
106
|
|
|
/** @var RedirectResponse $actualResult */ |
|
107
|
|
|
$actualResult = $this->sut->handle($requestStub, $next); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertNotSame($responseStub, $actualResult); |
|
110
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $actualResult); |
|
111
|
|
|
$this->assertSame(ResponseHeaders::HTTP_TEMPORARY_REDIRECT, $actualResult->getStatusCode()); |
|
112
|
|
|
$this->assertSame(Authorization::PATH_403, $actualResult->getTargetUrl()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testHandleThrowsExceptionOnUnexpectedException() |
|
116
|
|
|
{ |
|
117
|
|
|
$this->expectException(\Exception::class); |
|
118
|
|
|
|
|
119
|
|
|
$this->enforcerMock |
|
120
|
|
|
->expects($this->once()) |
|
121
|
|
|
->method('enforce') |
|
122
|
|
|
->willThrowException(new \Exception()); |
|
123
|
|
|
|
|
124
|
|
|
$requestStub = new Request([], [], [], [], [], [], null); |
|
125
|
|
|
$responseStub = new Response(); |
|
126
|
|
|
|
|
127
|
|
|
$next = function () use ($responseStub) { |
|
128
|
|
|
return $responseStub; |
|
129
|
|
|
}; |
|
130
|
|
|
|
|
131
|
|
|
$this->sut->handle($requestStub, $next); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|