summaryrefslogtreecommitdiffstats
path: root/tests/Psecio/Gatekeeper/GatekeeperTest.php
blob: a534eb85b8fa0306386cf2c7c9cd379c223f4264 (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
<?php

namespace Psecio\Gatekeeper;

class GatekeeperTest extends \Psecio\Gatekeeper\Base
{
    public function setUp()
    {
        // $config = array('test' => 1);
        // Gatekeeper::init(null, $config);
    }
    public function tearDown()
    {

    }

    /**
     * Test the getter/setter for datasources
     */
    public function testGetSetDatasource()
    {
        $ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Stub')
            ->disableOriginalConstructor()
            ->setMethods(array('find', 'delete'))
            ->getMock();

        Gatekeeper::setDatasource($ds);
        $this->assertEquals(Gatekeeper::getDatasource(), $ds);
    }

    /**
     * Test the enable/disable of throttling
     */
    public function testEnableDisableThrottle()
    {
        Gatekeeper::disableThrottle();
        $this->assertFalse(Gatekeeper::throttleStatus());

        Gatekeeper::enableThrottle();
        $this->assertTrue(Gatekeeper::throttleStatus());
    }

    /**
     * Test getting the user's throttle information (model instance)
     */
    // public function testGetUserThrottle()
    // {
    //     $userId = 42;

    //     // This is our model that will be returned
    //     $ds = $this->buildMock(null);
    //     $throttle1 = new ThrottleModel($ds, array('userId' => $userId));

    //     $ds = $this->buildMock($throttle1, 'find');
    //     $throttle = new ThrottleModel($ds);

    //     $gk = $this->getMockBuilder('\Psecio\Gatekeeper\Gatekeeper')
    //         ->setMethods(array('findThrottleByUserId'))
    //         ->getMock();

    //     $config = array('name' => 'test');
    //     $gk::init(null, $config, $ds);

    //     $gk->method('findThrottleByUserId')
    //         ->willReturn($throttle);

    //     $result = $gk::getUserThrottle($userId);
    //     $this->assertEquals(42, $result->userId);
    // }

    /**
     * Test that a restriction is correctly made
     */
    public function testCreateRestriction()
    {
        Gatekeeper::restrict('ip', array());
        $restrict = Gatekeeper::getRestrictions();
        $this->assertCount(1, $restrict);
        $this->assertTrue($restrict[0] instanceof \Psecio\Gatekeeper\Restrict\Ip);
    }

    /**
     * Test the creation of an invalid (not found) restriction
     *
     * @expectedException \InvalidArgumentException
     */
    public function testCreateInvalidRestriction()
    {
        Gatekeeper::restrict('foobar', array());
    }

    /**
     * Test the hash equality checking
     */
    public function testHashEqualsValid()
    {
        $hash = sha1(mt_rand());
        $this->assertTrue(Gatekeeper::hash_equals($hash, $hash));
    }

    /**
     * Test that false is returned when the hashes are different lengths
     */
    public function testHashEqualsDifferentLength()
    {
        $hash = sha1(mt_rand());
        $this->assertFalse(Gatekeeper::hash_equals($hash, md5(mt_rand())));
    }
}