1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* OAuth2 Client tests.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Test OAuth2 Client.
|
10
|
*/
|
11
|
class OAuth2ClientTestCase extends DrupalWebTestCase {
|
12
|
protected $profile = 'testing';
|
13
|
|
14
|
public static function getInfo() {
|
15
|
return array(
|
16
|
'name' => 'OAuth2 Client',
|
17
|
'description' => 'Tests basic OAuth2 Client functionality.',
|
18
|
'group' => 'OAuth2',
|
19
|
);
|
20
|
}
|
21
|
|
22
|
public function setUp() {
|
23
|
parent::setUp(array('oauth2_client_test', 'libraries'));
|
24
|
}
|
25
|
|
26
|
public function testGetAccessToken() {
|
27
|
$this->clientCredentialsFlow();
|
28
|
$this->userPasswordFlow();
|
29
|
$this->serverSideFlow();
|
30
|
$this->clientIntegration();
|
31
|
$this->errorCases();
|
32
|
}
|
33
|
|
34
|
/**
|
35
|
* Get and return a token from the given test client.
|
36
|
*/
|
37
|
protected function getToken($client) {
|
38
|
$result = $this->drupalGet('oauth2/test/' . $client);
|
39
|
$this->assertPattern('/^access_token: /', $result);
|
40
|
$token = str_replace('access_token: ', '', $result);
|
41
|
$token = trim($token);
|
42
|
$this->assertNotEqual($token, '', 'Token is not empty.');
|
43
|
return $token;
|
44
|
}
|
45
|
|
46
|
/**
|
47
|
* Test the client-credentials flow.
|
48
|
*/
|
49
|
public function clientCredentialsFlow() {
|
50
|
$token1 = $this->getToken('client-credentials');
|
51
|
$token2 = $this->getToken('client-credentials');
|
52
|
$this->assertEqual($token1, $token2, 'The same cached token is used, while it has not expired yet.');
|
53
|
|
54
|
sleep(10); // wait for the token to expire
|
55
|
$token3 = $this->getToken('client-credentials');
|
56
|
$this->assertNotEqual($token1, $token3, 'Getting a new token, client-credential flow has no refresh token.');
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* Test the user-password flow.
|
61
|
*/
|
62
|
public function userPasswordFlow() {
|
63
|
$token1 = $this->getToken('user-password');
|
64
|
$token2 = $this->getToken('user-password');
|
65
|
$this->assertEqual($token1, $token2, 'The same cached token is used, while it has not expired yet.');
|
66
|
|
67
|
sleep(10); // wait for the token to expire
|
68
|
$token3 = $this->getToken('user-password');
|
69
|
$this->assertNotEqual($token1, $token3, 'Getting a new token from refresh_token.');
|
70
|
|
71
|
sleep(30); // wait for the refresh_token to expire
|
72
|
$token4 = $this->getToken('user-password');
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* Test the server-side flow.
|
77
|
*
|
78
|
* For this test we are using 'client2' which has
|
79
|
* automatic authorization enabled.
|
80
|
*/
|
81
|
public function serverSideFlow() {
|
82
|
$user = (object) array('name' => 'user1', 'pass_raw' => 'pass1');
|
83
|
$this->drupalLogin($user);
|
84
|
$token1 = $this->getToken('server-side-auto');
|
85
|
$token2 = $this->getToken('server-side-auto');
|
86
|
$this->assertEqual($token1, $token2, 'The same cached token is used, while it has not expired yet.');
|
87
|
|
88
|
sleep(10); // wait for the token to expire
|
89
|
$token3 = $this->getToken('server-side-auto');
|
90
|
$this->assertNotEqual($token1, $token3, 'Getting a new token from refresh_token.');
|
91
|
|
92
|
sleep(30); // wait for the refresh_token to expire
|
93
|
$token4 = $this->getToken('server-side-auto');
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* Test client integration.
|
98
|
*/
|
99
|
public function clientIntegration() {
|
100
|
$result = $this->drupalGet('oauth2/test-client-integration');
|
101
|
$this->assertText('access_token: ');
|
102
|
$this->assertText('extra_param: This will be appended to the request on redirect.');
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* Test error cases.
|
107
|
*/
|
108
|
public function errorCases() {
|
109
|
$error_cases = array(
|
110
|
'wrong-client-id',
|
111
|
'wrong-client-secret',
|
112
|
'wrong-token-endpoint',
|
113
|
'wrong-username',
|
114
|
'wrong-password',
|
115
|
'wrong-scope',
|
116
|
);
|
117
|
foreach ($error_cases as $error_case) {
|
118
|
$this->drupalGet('oauth2/test/' . $error_case);
|
119
|
$this->assertText('Failed to get an access token');
|
120
|
}
|
121
|
|
122
|
// wrong-auth-flow
|
123
|
$this->drupalGet('oauth2/test/wrong-auth-flow');
|
124
|
$this->assertText('Unknown authorization flow');
|
125
|
|
126
|
// wrong-authorization-endpoint
|
127
|
// wrong-redirect-uri
|
128
|
}
|
129
|
}
|