1 | <?php namespace Limoncello\Tests\Auth\Authorization\PolicyDecision; |
||
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 |