Issues (25)

tests/Authentication/AccessTokenTest.php (2 issues)

1
<?php
2
/**
3
 * Copyright 2017 Facebook, Inc.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 */
23
namespace Facebook\Tests\Authentication;
24
25
use Facebook\Authentication\AccessToken;
26
use PHPUnit\Framework\TestCase;
27
28
class AccessTokenTest extends TestCase
29
{
30
    public function testAnAccessTokenCanBeReturnedAsAString()
31
    {
32
        $accessToken = new AccessToken('foo_token');
33
34
        $this->assertEquals('foo_token', $accessToken->getValue());
35
        $this->assertEquals('foo_token', (string)$accessToken);
36
    }
37
38
    public function testAnAppSecretProofWillBeProperlyGenerated()
39
    {
40
        $accessToken = new AccessToken('foo_token');
41
42
        $appSecretProof = $accessToken->getAppSecretProof('shhhhh!is.my.secret');
43
44
        $this->assertEquals('796ba0d8a6b339e476a7b166a9e8ac0a395f7de736dc37de5f2f4397f5854eb8', $appSecretProof);
45
    }
46
47
    public function testAnAppAccessTokenCanBeDetected()
48
    {
49
        $normalToken = new AccessToken('foo_token');
50
        $isNormalToken = $normalToken->isAppAccessToken();
51
52
        $this->assertFalse($isNormalToken, 'Normal access token not expected to look like an app access token.');
53
54
        $appToken = new AccessToken('123|secret');
55
        $isAppToken = $appToken->isAppAccessToken();
56
57
        $this->assertTrue($isAppToken, 'App access token expected to look like an app access token.');
58
    }
59
60
    public function testShortLivedAccessTokensCanBeDetected()
61
    {
62
        $anHourAndAHalf = time() + (1.5 * 60);
63
        $accessToken = new AccessToken('foo_token', $anHourAndAHalf);
0 ignored issues
show
$anHourAndAHalf of type double is incompatible with the type integer expected by parameter $expiresAt of Facebook\Authentication\AccessToken::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        $accessToken = new AccessToken('foo_token', /** @scrutinizer ignore-type */ $anHourAndAHalf);
Loading history...
64
65
        $isLongLived = $accessToken->isLongLived();
66
67
        $this->assertFalse($isLongLived, 'Expected access token to be short lived.');
68
    }
69
70
    public function testLongLivedAccessTokensCanBeDetected()
71
    {
72
        $accessToken = new AccessToken('foo_token', $this->aWeekFromNow());
73
74
        $isLongLived = $accessToken->isLongLived();
75
76
        $this->assertTrue($isLongLived, 'Expected access token to be long lived.');
77
    }
78
79
    public function testAnAppAccessTokenDoesNotExpire()
80
    {
81
        $appToken = new AccessToken('123|secret');
82
        $hasExpired = $appToken->isExpired();
83
84
        $this->assertFalse($hasExpired, 'App access token not expected to expire.');
85
    }
86
87
    public function testAnAccessTokenCanExpire()
88
    {
89
        $expireTime = time() - 100;
90
        $appToken = new AccessToken('foo_token', $expireTime);
91
        $hasExpired = $appToken->isExpired();
92
93
        $this->assertTrue($hasExpired, 'Expected 100 second old access token to be expired.');
94
    }
95
96
    public function testAccessTokenCanBeSerialized()
97
    {
98
        $accessToken = new AccessToken('foo', time(), 'bar');
0 ignored issues
show
The call to Facebook\Authentication\AccessToken::__construct() has too many arguments starting with 'bar'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
        $accessToken = /** @scrutinizer ignore-call */ new AccessToken('foo', time(), 'bar');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
99
100
        $newAccessToken = unserialize(serialize($accessToken));
101
102
        $this->assertEquals((string)$accessToken, (string)$newAccessToken);
103
        $this->assertEquals($accessToken->getExpiresAt(), $newAccessToken->getExpiresAt());
104
    }
105
106
    private function aWeekFromNow()
107
    {
108
        return time() + (60 * 60 * 24 * 7);//a week from now
109
    }
110
}
111