summaryrefslogtreecommitdiffstats
path: root/tests/Psecio/Gatekeeper/PolicyModelTest.php
blob: 18909aa42ca948e604f58e6953db402c1818b57d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php

namespace Psecio\Gatekeeper;

class PolicyModelTest extends \Psecio\Gatekeeper\Base
{
    private $policy;

    public function setUp()
    {
        $ds = $this->buildMock(true, 'save');
        $this->policy = new PolicyModel($ds, ['id' => 1]);
    }
    public function tearDown()
    {
        unset($this->policy);
    }

    /**
     * Test that the evaluation passes for a single object
     *  with the type defined (array index)
     */
    public function testPolicyEvaluateSingleObject()
    {
        $this->policy->load(['expression' => 'user.test == "foo"']);

        $user = (object)['test' => 'foo'];
        $data = ['user' => $user];

        $this->assertTrue($this->policy->evaluate($data));
    }

    /**
     * Test that the evaluation passes with multiple objects
     *  with types defined (array index)
     */
    public function testPolicyEvaluateMultipleObject()
    {
        $this->policy->load([
            'expression' => 'user.test == "foo" and group.name == "test"'
        ]);

        $data = [
            'user' => (object)['test' => 'foo'],
            'group' => (object)['name' => 'test']
        ];
        $this->assertTrue($this->policy->evaluate($data));
    }

    /**
     * Test that the resolution of type by classname works
     *  in expression evaluation
     */
    public function testPolicyEvaluateObjectByClassname()
    {
        $ds = $this->buildMock(true);
        $user = new UserModel($ds, ['username' => 'ccornutt']);

        $this->policy->load([
            'expression' => 'user.username == "ccornutt"'
        ]);
        $data = [$user];
        $this->assertTrue($this->policy->evaluate($data));
    }

    /**
     * Test that an testInvalidExpressionFailure is thrown when
     *  the expression fails
     *
     * @expectedException \Psecio\Gatekeeper\Exception\InvalidExpressionException
     */
    public function testInvalidExpressionFailure()
    {
        $this->policy->load([
            'expression' => 'user.username == "ccornutt"'
        ]);
        $data = [];
        $this->policy->evaluate($data);
    }

    /**
     * Test that an exception is thrown when no policy expression (by ID)
     *  is currently loaded
     *
     * @expectedException \Psecio\Gatekeeper\Exception\InvalidExpressionException
     */
    public function testFailureWhenNoPolicyLoaded()
    {
        $ds = $this->buildMock(true);
        $policy = new PolicyModel($ds);

        $this->policy->evaluate([]);
    }

    /**
     * Test the expression matching when a method is involved
     *  In this case, get a User's groups list and return just
     *  the names to see if a group exists/doesn't exist
     */
    public function testPolicyEvaluateObjectWithFunction()
    {
        $ds = $this->buildMock(true);
        $groups = new GroupCollection($ds);
        $group = new GroupModel($ds, ['name' => 'group1']);
        $groups->add($group);

        $ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Stub')
            ->setConstructorArgs(array(array()))
            ->getMock();
        $ds->method('fetch')
            ->willReturn($groups->toArray(true));

        $user = new UserModel($ds, ['username' => 'ccornutt42']);

        // "group1" does exist
        $this->policy->load(['expression' => '"group1" in user.groups.getName()']);
        $this->assertTrue($this->policy->evaluate($user));

        // "group2" does not exist
        $this->policy->load(['expression' => '"group2" in user.groups.getName()']);
        $this->assertFalse($this->policy->evaluate($user));
    }
}