1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Framework\Controller; |
13
|
|
|
|
14
|
|
|
use Spiral\Core\Container; |
15
|
|
|
use Spiral\Core\CoreInterface; |
16
|
|
|
use Spiral\Core\Exception\ControllerException; |
17
|
|
|
use Spiral\Core\Exception\ScopeException; |
18
|
|
|
use Spiral\Security\Actor\Actor; |
19
|
|
|
use Spiral\Security\Actor\Guest; |
20
|
|
|
use Spiral\Security\ActorInterface; |
21
|
|
|
use Spiral\Security\GuardInterface; |
22
|
|
|
use Spiral\Security\GuardScope; |
23
|
|
|
use Spiral\App\Controller\AuthController; |
24
|
|
|
use Spiral\Tests\Framework\BaseTest; |
25
|
|
|
|
26
|
|
|
class AuthorizesTest extends BaseTest |
27
|
|
|
{ |
28
|
|
|
public function testAuthException(): void |
29
|
|
|
{ |
30
|
|
|
$this->expectException(ControllerException::class); |
31
|
|
|
$this->expectDeprecationMessage("Unauthorized permission 'do'"); |
32
|
|
|
|
33
|
|
|
$app = $this->makeApp(); |
34
|
|
|
$app->get(Container::class)->bind(ActorInterface::class, new Guest()); |
35
|
|
|
|
36
|
|
|
$r = $app->get(CoreInterface::class)->callAction(AuthController::class, 'do'); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testAuth(): void |
40
|
|
|
{ |
41
|
|
|
$app = $this->makeApp(); |
42
|
|
|
$app->get(Container::class)->bind(ActorInterface::class, new Actor(['user'])); |
43
|
|
|
|
44
|
|
|
$r = $app->get(CoreInterface::class)->callAction(AuthController::class, 'do'); |
45
|
|
|
$this->assertSame('ok', $r); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testAuthNoActor(): void |
49
|
|
|
{ |
50
|
|
|
$this->expectException(ScopeException::class); |
51
|
|
|
|
52
|
|
|
$app = $this->makeApp(); |
53
|
|
|
$app->getContainer()->removeBinding(ActorInterface::class); |
54
|
|
|
|
55
|
|
|
$app->get(CoreInterface::class)->callAction(AuthController::class, 'do'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testWithRoles(): void |
59
|
|
|
{ |
60
|
|
|
$app = $this->makeApp(); |
61
|
|
|
$g = $app->get(GuardInterface::class); |
62
|
|
|
$this->assertInstanceOf(GuardScope::class, $g); |
63
|
|
|
|
64
|
|
|
$this->assertSame(['guest'], $g->getRoles()); |
65
|
|
|
|
66
|
|
|
$g2 = $g->withRoles(['user']); |
67
|
|
|
|
68
|
|
|
$this->assertSame(['guest'], $g->getRoles()); |
69
|
|
|
$this->assertSame(['user', 'guest'], $g2->getRoles()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testWithActor(): void |
73
|
|
|
{ |
74
|
|
|
$app = $this->makeApp(); |
75
|
|
|
$g = $app->get(GuardInterface::class); |
76
|
|
|
$this->assertInstanceOf(GuardScope::class, $g); |
77
|
|
|
|
78
|
|
|
$this->assertSame(['guest'], $g->getRoles()); |
79
|
|
|
|
80
|
|
|
$g2 = $g->withActor(new Actor(['admin'])); |
81
|
|
|
|
82
|
|
|
$this->assertSame(['admin'], $g2->getRoles()); |
83
|
|
|
$this->assertSame(['guest'], $g->getRoles()); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|