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\Interceptor; |
13
|
|
|
|
14
|
|
|
use Spiral\App\Controller\Demo2Controller; |
15
|
|
|
use Spiral\App\Controller\Demo3Controller; |
16
|
|
|
use Spiral\App\Controller\DemoController; |
17
|
|
|
use Spiral\Core\CoreInterface; |
18
|
|
|
use Spiral\Core\Exception\ControllerException; |
19
|
|
|
use Spiral\Core\Exception\InterceptorException; |
20
|
|
|
use Spiral\Security\Actor\Actor; |
21
|
|
|
use Spiral\Security\ActorInterface; |
22
|
|
|
use Spiral\Tests\Framework\ConsoleTest; |
23
|
|
|
|
24
|
|
|
class GuardedTest extends ConsoleTest |
25
|
|
|
{ |
26
|
|
|
public function testInvalidAnnotationConfiguration(): void |
27
|
|
|
{ |
28
|
|
|
/** @var CoreInterface $core */ |
29
|
|
|
$core = $this->app->get(CoreInterface::class); |
30
|
|
|
|
31
|
|
|
$this->expectException(InterceptorException::class); |
32
|
|
|
$core->callAction(DemoController::class, 'guardedButNoName', []); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testInvalidAnnotationConfigurationWithAttribute(): void |
36
|
|
|
{ |
37
|
|
|
/** @var CoreInterface $core */ |
38
|
|
|
$core = $this->app->get(CoreInterface::class); |
39
|
|
|
|
40
|
|
|
$this->expectException(InterceptorException::class); |
41
|
|
|
$core->callAction(DemoController::class, 'guardedButNoNameAttribute', []); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testInvalidAnnotationConfigurationIfEmptyGuarded(): void |
45
|
|
|
{ |
46
|
|
|
/** @var CoreInterface $core */ |
47
|
|
|
$core = $this->app->get(CoreInterface::class); |
48
|
|
|
|
49
|
|
|
$this->expectException(InterceptorException::class); |
50
|
|
|
$core->callAction(Demo3Controller::class, 'do', []); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testNotAllowed(): void |
54
|
|
|
{ |
55
|
|
|
/** @var CoreInterface $core */ |
56
|
|
|
$core = $this->app->get(CoreInterface::class); |
57
|
|
|
|
58
|
|
|
$this->expectException(ControllerException::class); |
59
|
|
|
$core->callAction(DemoController::class, 'do', []); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testNotAllowed2(): void |
63
|
|
|
{ |
64
|
|
|
/** @var CoreInterface $core */ |
65
|
|
|
$core = $this->app->get(CoreInterface::class); |
66
|
|
|
|
67
|
|
|
$this->expectException(ControllerException::class); |
68
|
|
|
$core->callAction(Demo2Controller::class, 'do1', []); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testNotAllowedError1(): void |
72
|
|
|
{ |
73
|
|
|
/** @var CoreInterface $core */ |
74
|
|
|
$core = $this->app->get(CoreInterface::class); |
75
|
|
|
|
76
|
|
|
$this->expectExceptionCode(ControllerException::FORBIDDEN); |
77
|
|
|
$core->callAction(Demo2Controller::class, 'do1', []); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testNotAllowedError2(): void |
81
|
|
|
{ |
82
|
|
|
/** @var CoreInterface $core */ |
83
|
|
|
$core = $this->app->get(CoreInterface::class); |
84
|
|
|
|
85
|
|
|
$this->expectExceptionCode(ControllerException::NOT_FOUND); |
86
|
|
|
$core->callAction(Demo2Controller::class, 'do2', []); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
public function testNotAllowedError3(): void |
91
|
|
|
{ |
92
|
|
|
/** @var CoreInterface $core */ |
93
|
|
|
$core = $this->app->get(CoreInterface::class); |
94
|
|
|
|
95
|
|
|
$this->expectExceptionCode(ControllerException::ERROR); |
96
|
|
|
$core->callAction(Demo2Controller::class, 'do3', []); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
public function testNotAllowedError4(): void |
101
|
|
|
{ |
102
|
|
|
/** @var CoreInterface $core */ |
103
|
|
|
$core = $this->app->get(CoreInterface::class); |
104
|
|
|
|
105
|
|
|
$this->expectExceptionCode(ControllerException::BAD_ACTION); |
106
|
|
|
$core->callAction(Demo2Controller::class, 'do4', []); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testAllowed(): void |
110
|
|
|
{ |
111
|
|
|
/** @var CoreInterface $core */ |
112
|
|
|
$core = $this->app->get(CoreInterface::class); |
113
|
|
|
|
114
|
|
|
$this->app->getContainer()->bind(ActorInterface::class, new Actor(['user'])); |
|
|
|
|
115
|
|
|
|
116
|
|
|
$this->assertSame('ok', $core->callAction(DemoController::class, 'do', [])); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testAllowedWithAttribute(): void |
120
|
|
|
{ |
121
|
|
|
/** @var CoreInterface $core */ |
122
|
|
|
$core = $this->app->get(CoreInterface::class); |
123
|
|
|
|
124
|
|
|
$this->app->getContainer()->bind(ActorInterface::class, new Actor(['user'])); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$this->assertSame('ok', $core->callAction(DemoController::class, 'doAttribute', [])); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testNotAllowed3(): void |
130
|
|
|
{ |
131
|
|
|
/** @var CoreInterface $core */ |
132
|
|
|
$core = $this->app->get(CoreInterface::class); |
133
|
|
|
|
134
|
|
|
$this->app->getContainer()->bind(ActorInterface::class, new Actor(['user'])); |
|
|
|
|
135
|
|
|
|
136
|
|
|
$this->expectExceptionCode(ControllerException::FORBIDDEN); |
137
|
|
|
$this->assertSame('ok', $core->callAction(Demo2Controller::class, 'do1', [])); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testAllowed2(): void |
141
|
|
|
{ |
142
|
|
|
/** @var CoreInterface $core */ |
143
|
|
|
$core = $this->app->get(CoreInterface::class); |
144
|
|
|
|
145
|
|
|
$this->app->getContainer()->bind(ActorInterface::class, new Actor(['demo'])); |
|
|
|
|
146
|
|
|
$this->assertSame('ok', $core->callAction(Demo2Controller::class, 'do1', [])); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testNotAllowed2WithAttribute(): void |
150
|
|
|
{ |
151
|
|
|
/** @var CoreInterface $core */ |
152
|
|
|
$core = $this->app->get(CoreInterface::class); |
153
|
|
|
|
154
|
|
|
$this->app->getContainer()->bind(ActorInterface::class, new Actor(['demo'])); |
|
|
|
|
155
|
|
|
$this->assertSame('ok', $core->callAction(Demo2Controller::class, 'do1Attribute', [])); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|