blob: b34e0bfc010144a2483b8f7b85abe4f0c7ae967f [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3namespace OAuth2\Storage;
4
5class AccessTokenTest extends BaseTest
6{
7 /** @dataProvider provideStorage */
8 public function testSetAccessToken(AccessTokenInterface $storage)
9 {
10 if ($storage instanceof NullStorage) {
11 $this->markTestSkipped('Skipped Storage: ' . $storage->getMessage());
12
13 return;
14 }
15
16 // assert token we are about to add does not exist
17 $token = $storage->getAccessToken('newtoken');
18 $this->assertFalse($token);
19
20 // add new token
21 $expires = time() + 20;
22 $success = $storage->setAccessToken('newtoken', 'client ID', 'SOMEUSERID', $expires);
23 $this->assertTrue($success);
24
25 $token = $storage->getAccessToken('newtoken');
26 $this->assertNotNull($token);
27 $this->assertArrayHasKey('access_token', $token);
28 $this->assertArrayHasKey('client_id', $token);
29 $this->assertArrayHasKey('user_id', $token);
30 $this->assertArrayHasKey('expires', $token);
31 $this->assertEquals($token['access_token'], 'newtoken');
32 $this->assertEquals($token['client_id'], 'client ID');
33 $this->assertEquals($token['user_id'], 'SOMEUSERID');
34 $this->assertEquals($token['expires'], $expires);
35
36 // change existing token
37 $expires = time() + 42;
38 $success = $storage->setAccessToken('newtoken', 'client ID2', 'SOMEOTHERID', $expires);
39 $this->assertTrue($success);
40
41 $token = $storage->getAccessToken('newtoken');
42 $this->assertNotNull($token);
43 $this->assertArrayHasKey('access_token', $token);
44 $this->assertArrayHasKey('client_id', $token);
45 $this->assertArrayHasKey('user_id', $token);
46 $this->assertArrayHasKey('expires', $token);
47 $this->assertEquals($token['access_token'], 'newtoken');
48 $this->assertEquals($token['client_id'], 'client ID2');
49 $this->assertEquals($token['user_id'], 'SOMEOTHERID');
50 $this->assertEquals($token['expires'], $expires);
51
52 // add token with scope having an empty string value
53 $expires = time() + 42;
54 $success = $storage->setAccessToken('newtoken', 'client ID', 'SOMEOTHERID', $expires, '');
55 $this->assertTrue($success);
56 }
57
58 /** @dataProvider provideStorage */
59 public function testUnsetAccessToken(AccessTokenInterface $storage)
60 {
61 if ($storage instanceof NullStorage || !method_exists($storage, 'unsetAccessToken')) {
62 $this->markTestSkipped('Skipped Storage: ' . $storage->getMessage());
63
64 return;
65 }
66
67 // assert token we are about to unset does not exist
68 $token = $storage->getAccessToken('revokabletoken');
69 $this->assertFalse($token);
70
71 // add new token
72 $expires = time() + 20;
73 $success = $storage->setAccessToken('revokabletoken', 'client ID', 'SOMEUSERID', $expires);
74 $this->assertTrue($success);
75
76 // assert unsetAccessToken returns true
77 $result = $storage->unsetAccessToken('revokabletoken');
78 $this->assertTrue($result);
79
80 // assert token we unset does not exist
81 $token = $storage->getAccessToken('revokabletoken');
82 $this->assertFalse($token);
83 }
84
85 /** @dataProvider provideStorage */
86 public function testUnsetAccessTokenReturnsFalse(AccessTokenInterface $storage)
87 {
88 if ($storage instanceof NullStorage || !method_exists($storage, 'unsetAccessToken')) {
89 $this->markTestSkipped('Skipped Storage: ' . $storage->getMessage());
90
91 return;
92 }
93
94 // assert token we are about to unset does not exist
95 $token = $storage->getAccessToken('nonexistanttoken');
96 $this->assertFalse($token);
97
98 // assert unsetAccessToken returns false
99 $result = $storage->unsetAccessToken('nonexistanttoken');
100 $this->assertFalse($result);
101 }
102}