summaryrefslogtreecommitdiff
path: root/lib/facebook-graph-sdk/tests/Authentication/AccessTokenMetadata.php
blob: 1b5450386ec16f1a88a354af2f760d27c7e80ff3 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
 * Copyright 2014 Facebook, Inc.
 *
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
 * use, copy, modify, and distribute this software in source code or binary
 * form for use in connection with the web services and APIs provided by
 * Facebook.
 *
 * As with any software that integrates with the Facebook platform, your use
 * of this software is subject to the Facebook Developer Principles and
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
 * shall be included in all copies or substantial portions of the software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */
namespace Facebook\Tests\Authentication;

use Facebook\Authentication\AccessTokenMetadata;

class AccessTokenMetadataTest extends \PHPUnit_Framework_TestCase
{

    protected $graphResponseData = [
        'data' => [
            'app_id' => '123',
            'application' => 'Foo App',
            'error' => [
                'code' => 190,
                'message' => 'Foo error message.',
                'subcode' => 463,
            ],
            'issued_at' => 1422110200,
            'expires_at' => 1422115200,
            'is_valid' => false,
            'metadata' => [
                'sso' => 'iphone-sso',
                'auth_type' => 'rerequest',
                'auth_nonce' => 'no-replicatey',
            ],
            'scopes' => ['public_profile', 'basic_info', 'user_friends'],
            'profile_id' => '1000',
            'user_id' => '1337',
        ],
    ];

    public function testDatesGetCastToDateTime()
    {
        $metadata = new AccessTokenMetadata($this->graphResponseData);

        $expires = $metadata->getExpiresAt();
        $issuedAt = $metadata->getIssuedAt();

        $this->assertInstanceOf('DateTime', $expires);
        $this->assertInstanceOf('DateTime', $issuedAt);
    }

    public function testAllTheGettersReturnTheProperValue()
    {
        $metadata = new AccessTokenMetadata($this->graphResponseData);

        $this->assertEquals('123', $metadata->getAppId());
        $this->assertEquals('Foo App', $metadata->getApplication());
        $this->assertTrue($metadata->isError(), 'Expected an error');
        $this->assertEquals('190', $metadata->getErrorCode());
        $this->assertEquals('Foo error message.', $metadata->getErrorMessage());
        $this->assertEquals('463', $metadata->getErrorSubcode());
        $this->assertFalse($metadata->getIsValid(), 'Expected the access token to not be valid');
        $this->assertEquals('iphone-sso', $metadata->getSso());
        $this->assertEquals('rerequest', $metadata->getAuthType());
        $this->assertEquals('no-replicatey', $metadata->getAuthNonce());
        $this->assertEquals('1000', $metadata->getProfileId());
        $this->assertEquals(['public_profile', 'basic_info', 'user_friends'], $metadata->getScopes());
        $this->assertEquals('1337', $metadata->getUserId());
    }

    /**
     * @expectedException \Facebook\Exceptions\FacebookSDKException
     */
    public function testInvalidMetadataWillThrow()
    {
        new AccessTokenMetadata(['foo' => 'bar']);
    }

    public function testAnExpectedAppIdWillNotThrow()
    {
        $metadata = new AccessTokenMetadata($this->graphResponseData);
        $metadata->validateAppId('123');
    }

    /**
     * @expectedException \Facebook\Exceptions\FacebookSDKException
     */
    public function testAnUnexpectedAppIdWillThrow()
    {
        $metadata = new AccessTokenMetadata($this->graphResponseData);
        $metadata->validateAppId('foo');
    }

    public function testAnExpectedUserIdWillNotThrow()
    {
        $metadata = new AccessTokenMetadata($this->graphResponseData);
        $metadata->validateUserId('1337');
    }

    /**
     * @expectedException \Facebook\Exceptions\FacebookSDKException
     */
    public function testAnUnexpectedUserIdWillThrow()
    {
        $metadata = new AccessTokenMetadata($this->graphResponseData);
        $metadata->validateUserId('foo');
    }

    public function testAnActiveAccessTokenWillNotThrow()
    {
        $this->graphResponseData['data']['expires_at'] = time() + 1000;
        $metadata = new AccessTokenMetadata($this->graphResponseData);
        $metadata->validateExpiration();
    }

    /**
     * @expectedException \Facebook\Exceptions\FacebookSDKException
     */
    public function testAnExpiredAccessTokenWillThrow()
    {
        $this->graphResponseData['data']['expires_at'] = time() - 1000;
        $metadata = new AccessTokenMetadata($this->graphResponseData);
        $metadata->validateExpiration();
    }
}