|
1
|
|
|
<?php namespace Limoncello\Tests\Auth\Authorization\PolicyDecision; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2017 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Limoncello\Auth\Authorization\PolicyAdministration\AllOf; |
|
20
|
|
|
use Limoncello\Auth\Authorization\PolicyAdministration\AnyOf; |
|
21
|
|
|
use Limoncello\Auth\Authorization\PolicyAdministration\Policy; |
|
22
|
|
|
use Limoncello\Auth\Authorization\PolicyAdministration\PolicySet; |
|
23
|
|
|
use Limoncello\Auth\Authorization\PolicyAdministration\Rule; |
|
24
|
|
|
use Limoncello\Auth\Authorization\PolicyAdministration\Target; |
|
25
|
|
|
use Limoncello\Auth\Authorization\PolicyDecision\Algorithms\Encoder; |
|
26
|
|
|
use Limoncello\Auth\Authorization\PolicyDecision\PolicyAlgorithm; |
|
27
|
|
|
use Limoncello\Auth\Authorization\PolicyDecision\RuleAlgorithm; |
|
28
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\TargetInterface; |
|
29
|
|
|
use PHPUnit\Framework\TestCase; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @package Limoncello\Tests\Auth |
|
33
|
|
|
*/ |
|
34
|
|
|
class EncoderTest extends TestCase |
|
35
|
|
|
{ |
|
36
|
|
|
public function testEncodePolicy() |
|
37
|
|
|
{ |
|
38
|
|
|
$ruleIsAdmin = (new Rule())->setTarget($this->target('role', 'admin')); |
|
39
|
|
|
$createPolicy = |
|
40
|
|
|
(new Policy([$ruleIsAdmin], RuleAlgorithm::denyUnlessPermit()))->setTarget($this->target('op', 'create')); |
|
41
|
|
|
|
|
42
|
|
|
// Returned value represents policy in internal format. We won't check |
|
43
|
|
|
// that it has some specific structure but will test its validity later |
|
44
|
|
|
// in functional tests. |
|
45
|
|
|
$this->assertNotEmpty($encodedPolicy = Encoder::encodePolicy($createPolicy)); |
|
46
|
|
|
$this->assertNotEmpty(Encoder::policyTarget($encodedPolicy)); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertNotEmpty($encodedRule = Encoder::encodeRule($ruleIsAdmin)); |
|
49
|
|
|
$this->assertNotEmpty(Encoder::ruleTarget($encodedRule)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testEncodePolicySet() |
|
53
|
|
|
{ |
|
54
|
|
|
$ruleIsAdmin = (new Rule())->setTarget($this->target('role', 'admin')); |
|
55
|
|
|
$ruleIsEditor = (new Rule())->setTarget($this->target('role', 'editor')); |
|
56
|
|
|
$createPolicy = (new Policy([$ruleIsAdmin], RuleAlgorithm::denyUnlessPermit())) |
|
57
|
|
|
->setTarget($this->target('op', 'create')); |
|
58
|
|
|
$updatePolicy = (new Policy([$ruleIsEditor], RuleAlgorithm::denyUnlessPermit())) |
|
59
|
|
|
->setTarget($this->target('op', 'update')); |
|
60
|
|
|
$policySet = (new PolicySet([$createPolicy, $updatePolicy], PolicyAlgorithm::denyUnlessPermit())) |
|
61
|
|
|
->setTarget($this->target('type', 'some_resource')); |
|
62
|
|
|
|
|
63
|
|
|
// Returned value represents set in internal format. We won't check |
|
64
|
|
|
// that it has some specific structure but will test its validity later |
|
65
|
|
|
// in functional tests. |
|
66
|
|
|
$result = Encoder::encodePolicySet($policySet); |
|
67
|
|
|
$this->assertNotEmpty($result); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $key |
|
72
|
|
|
* @param string $value |
|
73
|
|
|
* |
|
74
|
|
|
* @return TargetInterface |
|
75
|
|
|
*/ |
|
76
|
|
|
private function target($key, $value) |
|
77
|
|
|
{ |
|
78
|
|
|
return new Target(new AnyOf([new AllOf([$key => $value])])); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|