|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nord\Lumen\OAuth2\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Nord\Lumen\OAuth2\OAuth2Service; |
|
6
|
|
|
|
|
7
|
|
|
class OAuth2ServiceTest extends \Codeception\TestCase\Test |
|
8
|
|
|
{ |
|
9
|
|
|
use \Codeception\Specify; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var \UnitTester |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $tester; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected static $token = [ |
|
20
|
|
|
'access_token' => 'mF_9.B5f-4.1JqM', |
|
21
|
|
|
'token_type' => 'Bearer', |
|
22
|
|
|
'expires_in' => 3600, |
|
23
|
|
|
'refresh_token' => 'tGzv3JOkF0XG5Qx2TlKWIA' |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritdoc |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function setup() |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
$_SERVER['HTTP_AUTHORIZATION'] = 'Bearer mF_9.B5f-4.1JqM'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
*/ |
|
37
|
|
|
public function testAssertIssueAccessToken() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->specify('verify service issueAccessToken', function () { |
|
40
|
|
|
$authorizationServer = $this->createAuthorizationServer(); |
|
41
|
|
|
$authorizationServer->expects($this->once()) |
|
|
|
|
|
|
42
|
|
|
->method('issueAccessToken') |
|
43
|
|
|
->will($this->returnValue(self::$token)); |
|
44
|
|
|
|
|
45
|
|
|
$service = new OAuth2Service($authorizationServer, $this->createResourceServer()); |
|
|
|
|
|
|
46
|
|
|
verify($service->issueAccessToken())->equals(self::$token); |
|
47
|
|
|
}); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* |
|
52
|
|
|
*/ |
|
53
|
|
View Code Duplication |
public function testAssertValidateAccessToken() |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$this->specify('verify service validateAccessToken', function () { |
|
56
|
|
|
$service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer()); |
|
|
|
|
|
|
57
|
|
|
verify($service->validateAccessToken(true, self::$token['access_token']))->true(); |
|
58
|
|
|
}); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* |
|
63
|
|
|
*/ |
|
64
|
|
View Code Duplication |
public function testAssertGetResourceOwnerId() |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
$this->specify('verify service can getResourceOwnerId', function () { |
|
67
|
|
|
$service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer()); |
|
|
|
|
|
|
68
|
|
|
$service->validateAccessToken(true, self::$token['access_token']); |
|
69
|
|
|
verify($service->getResourceOwnerId())->equals('test'); |
|
70
|
|
|
}); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
public function testAssertGetResourceOwnerType() |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$this->specify('verify service can getResourceOwnerType', function () { |
|
79
|
|
|
$service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer()); |
|
|
|
|
|
|
80
|
|
|
$service->validateAccessToken(true, self::$token['access_token']); |
|
81
|
|
|
verify($service->getResourceOwnerType())->equals('test'); |
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* |
|
87
|
|
|
*/ |
|
88
|
|
View Code Duplication |
public function testAssertGetClientId() |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$this->specify('verify service can getClientId', function () { |
|
91
|
|
|
$service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer()); |
|
|
|
|
|
|
92
|
|
|
$service->validateAccessToken(true, self::$token['access_token']); |
|
93
|
|
|
verify($service->getClientId())->equals('test'); |
|
94
|
|
|
}); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return \League\OAuth2\Server\AuthorizationServer|\PHPUnit_Framework_MockObject_MockObject |
|
99
|
|
|
*/ |
|
100
|
|
|
private function createAuthorizationServer() |
|
101
|
|
|
{ |
|
102
|
|
|
$authorizationServer = $this->getMockBuilder(\League\OAuth2\Server\AuthorizationServer::class) |
|
103
|
|
|
->disableOriginalConstructor() |
|
104
|
|
|
->getMock(); |
|
105
|
|
|
|
|
106
|
|
|
return $authorizationServer; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return \League\OAuth2\Server\ResourceServer |
|
111
|
|
|
*/ |
|
112
|
|
|
private function createResourceServer() |
|
113
|
|
|
{ |
|
114
|
|
|
return new \League\OAuth2\Server\ResourceServer( |
|
115
|
|
|
new MockSessionStorage(), |
|
116
|
|
|
new MockAccessTokenStorage(), |
|
117
|
|
|
new MockClientStorage(), |
|
118
|
|
|
new MockScopeStorage() |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: