|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AbterPhp\Framework\Bootstrappers\Authorization; |
|
4
|
|
|
|
|
5
|
|
|
use AbterPhp\Framework\Authorization\CombinedAdapter; |
|
6
|
|
|
use AbterPhp\Framework\Authorization\Constant\Role; |
|
7
|
|
|
use AbterPhp\Framework\Constant\Env; |
|
8
|
|
|
use AbterPhp\Framework\Environments\Environment; |
|
9
|
|
|
use Casbin\Enforcer; |
|
10
|
|
|
use Opulence\Ioc\Container; |
|
11
|
|
|
use Opulence\Sessions\ISession; |
|
12
|
|
|
use Opulence\Views\Compilers\Fortune\ITranspiler; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
|
|
15
|
|
|
class EnforcerBootstrapperTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var EnforcerBootstrapper */ |
|
18
|
|
|
protected EnforcerBootstrapper $sut; |
|
19
|
|
|
|
|
20
|
|
|
public function setUp(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$this->sut = new EnforcerBootstrapper(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
protected function tearDown(): void |
|
26
|
|
|
{ |
|
27
|
|
|
Environment::unsetVar(Env::DIR_AUTH_CONFIG); |
|
28
|
|
|
Environment::unsetVar(Env::ENV_NAME); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testRegisterBindingsWithoutSession() |
|
32
|
|
|
{ |
|
33
|
|
|
Environment::setVar(Env::DIR_AUTH_CONFIG, '/tmp'); |
|
34
|
|
|
Environment::setVar(Env::ENV_NAME, 'foo'); |
|
35
|
|
|
|
|
36
|
|
|
file_put_contents('/tmp/model.conf', ''); |
|
37
|
|
|
|
|
38
|
|
|
$mockAdapter = $this->getMockBuilder(CombinedAdapter::class)->disableOriginalConstructor()->getMock(); |
|
39
|
|
|
|
|
40
|
|
|
$container = new Container(); |
|
41
|
|
|
$container->bindInstance(CombinedAdapter::class, $mockAdapter); |
|
42
|
|
|
|
|
43
|
|
|
$this->sut->registerBindings($container); |
|
44
|
|
|
|
|
45
|
|
|
$actual = $container->resolve(Enforcer::class); |
|
46
|
|
|
$this->assertInstanceOf(Enforcer::class, $actual); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testRegisterBindingsWithSession() |
|
50
|
|
|
{ |
|
51
|
|
|
$usernameStub = 'Foo'; |
|
52
|
|
|
|
|
53
|
|
|
Environment::setVar(Env::DIR_AUTH_CONFIG, '/tmp'); |
|
54
|
|
|
Environment::setVar(Env::ENV_NAME, 'foo'); |
|
55
|
|
|
|
|
56
|
|
|
file_put_contents('/tmp/model.conf', ''); |
|
57
|
|
|
|
|
58
|
|
|
$mockAdapter = $this->getMockBuilder(CombinedAdapter::class)->disableOriginalConstructor()->getMock(); |
|
59
|
|
|
$sessionMock = $this->getMockBuilder(ISession::class)->getMock(); |
|
60
|
|
|
$sessionMock->expects($this->any())->method('get')->with()->willReturn($usernameStub); |
|
61
|
|
|
$mockTranspiler = $this->getMockBuilder(ITranspiler::class)->getMock(); |
|
62
|
|
|
|
|
63
|
|
|
$container = new Container(); |
|
64
|
|
|
$container->bindInstance(CombinedAdapter::class, $mockAdapter); |
|
65
|
|
|
$container->bindInstance(ISession::class, $sessionMock); |
|
66
|
|
|
$container->bindInstance(ITranspiler::class, $mockTranspiler); |
|
67
|
|
|
|
|
68
|
|
|
$this->sut->registerBindings($container); |
|
69
|
|
|
|
|
70
|
|
|
$actual = $container->resolve(Enforcer::class); |
|
71
|
|
|
$this->assertInstanceOf(Enforcer::class, $actual); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public function createCanViewFunctionProvider(): array |
|
78
|
|
|
{ |
|
79
|
|
|
return [ |
|
80
|
|
|
[true], |
|
81
|
|
|
[false], |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @dataProvider createCanViewFunctionProvider |
|
87
|
|
|
* |
|
88
|
|
|
* @param bool $expectedResult |
|
89
|
|
|
*/ |
|
90
|
|
|
public function testCreateCanViewViewFunctionChecksReadRole(bool $expectedResult) |
|
91
|
|
|
{ |
|
92
|
|
|
$usernameStub = 'foo'; |
|
93
|
|
|
$keyStub = 'bar'; |
|
94
|
|
|
|
|
95
|
|
|
$enforcerMock = $this->getMockBuilder(Enforcer::class)->disableOriginalConstructor()->getMock(); |
|
96
|
|
|
$enforcerMock |
|
97
|
|
|
->expects($this->once()) |
|
98
|
|
|
->method('enforce') |
|
99
|
|
|
->with($usernameStub, "admin_resource_$keyStub", Role::READ) |
|
100
|
|
|
->willReturn($expectedResult); |
|
101
|
|
|
|
|
102
|
|
|
$actual = $this->sut->createCanViewViewFunction($usernameStub, $enforcerMock)($keyStub); |
|
103
|
|
|
$this->assertEquals($expectedResult, $actual); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|