CapabilityTest.php 3.85 KB
Newer Older
Juliper committed
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
<?php

require_once 'Twilio/Capability.php';

class CapabilityTest extends PHPUnit_Framework_TestCase {

    public function testNoPermissions() {
        $token = new Services_Twilio_Capability('AC123', 'foo');
        $payload = JWT::decode($token->generateToken(), 'foo');
        $this->assertEquals($payload->iss, "AC123");
        $this->assertEquals($payload->scope, '');
    }

    public function testInboundPermissions() {
        $token = new Services_Twilio_Capability('AC123', 'foo');
        $token->allowClientIncoming("andy");
        $payload = JWT::decode($token->generateToken(), 'foo');

        $eurl = "scope:client:incoming?clientName=andy";
        $this->assertEquals($payload->scope, $eurl);
    }

    public function testOutboundPermissions() {
        $token = new Services_Twilio_Capability('AC123', 'foo');
        $token->allowClientOutgoing("AP123");
        $payload = JWT::decode($token->generateToken(), 'foo');;
        $eurl = "scope:client:outgoing?appSid=AP123";
        $this->assertContains($eurl, $payload->scope);
    }

    public function testOutboundPermissionsParams() {
        $token = new Services_Twilio_Capability('AC123', 'foo');
        $token->allowClientOutgoing("AP123", array("foobar" => 3));
        $payload = JWT::decode($token->generateToken(), 'foo');

        $eurl = "scope:client:outgoing?appSid=AP123&appParams=foobar%3D3";
        $this->assertEquals($payload->scope, $eurl);
    }

    public function testEvents() {
        $token = new Services_Twilio_Capability('AC123', 'foo');
        $token->allowEventStream();
        $payload = JWT::decode($token->generateToken(), 'foo');

        $event_uri = "scope:stream:subscribe?path=%2F2010"
            . "-04-01%2FEvents&params=";
        $this->assertEquals($payload->scope, $event_uri);
    }

    public function testEventsWithFilters() {
        $token = new Services_Twilio_Capability('AC123', 'foo');
        $token->allowEventStream(array("foobar" => "hey"));
        $payload = JWT::decode($token->generateToken(), 'foo');

        $event_uri = "scope:stream:subscribe?path=%2F2010-"
            . "04-01%2FEvents&params=foobar%3Dhey";
        $this->assertEquals($payload->scope, $event_uri);
    }


    public function testDecode() {
        $token = new Services_Twilio_Capability('AC123', 'foo');
        $token->allowClientOutgoing("AP123", array("foobar"=> 3));
        $token->allowClientIncoming("andy");
        $token->allowEventStream();

        $outgoing_uri = "scope:client:outgoing?appSid="
            . "AP123&appParams=foobar%3D3&clientName=andy";
        $incoming_uri = "scope:client:incoming?clientName=andy";
        $event_uri = "scope:stream:subscribe?path=%2F2010-04-01%2FEvents";

        $payload = JWT::decode($token->generateToken(), 'foo');
        $scope = $payload->scope;

        $this->assertContains($outgoing_uri, $scope);
        $this->assertContains($incoming_uri, $scope);
        $this->assertContains($event_uri, $scope);
    }


    function testDecodeWithAuthToken() {
        try {
            $token = new Services_Twilio_Capability('AC123', 'foo');
            $payload = JWT::decode($token->generateToken(), 'foo');
            $this->assertSame($payload->iss, 'AC123');
        } catch (UnexpectedValueException $e) {
            $this->assertTrue(false, "Could not decode with 'foo'");
        }
    }

    function testClientNameValidation() {
        $this->setExpectedException('InvalidArgumentException');
        $token = new Services_Twilio_Capability('AC123', 'foo');
        $token->allowClientIncoming('@');
        $this->fail('exception should have been raised');
    }

    function zeroLengthNameInvalid() {
        $this->setExpectedException('InvalidArgumentException');
        $token = new Services_Twilio_Capability('AC123', 'foo');
        $token->allowClientIncoming("");
        $this->fail('exception should have been raised');
    }


}