8000 Updated the tests for 0.2.0 · oauth-io/sdk-php@e6f2b6b · GitHub
[go: up one dir, main page]

Skip to content

Commit e6f2b6b

Browse files
committed
Updated the tests for 0.2.0
1 parent 6631d61 commit e6f2b6b

File tree

5 files changed

+266
-265
lines changed

5 files changed

+266
-265
lines changed

src/OAuth_io/OAuth.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ public function generateStateToken() {
9494

9595
public function refreshCredentials($credentials, $force = false) {
9696
$date = new \DateTime();
97+
$credentials['refreshed'] = false;
9798
if (isset($credentials['refresh_token']) && ((isset($credentials['expires']) && $date->getTimestamp() > $credentials['expires']) || $force)) {
9899
$request = $this->injector->getRequest();
99100
$response = $request->make_request(array(
100101
'method' => 'POST',
101102
'url' => $this->injector->config['oauthd_url'] . '/auth/refresh_token/' . $credentials['provider'],
102103
'body' => http_build_query(array(
103-
'token' => $options['refresh_token'],
104+
'token' => $credentials['refresh_token'],
104105
'key' => $this->injector->config['app_key'],
105106
'secret' => $this->injector->config['app_secret']
106107
)) ,
@@ -113,6 +114,8 @@ public function refreshCredentials($credentials, $force = false) {
113114
foreach ($refreshed as $k => $v) {
114115
$credentials[$k] = $v;
115116
}
117+
$credentials['refreshed'] = true;
118+
116119
}
117120
return $credentials;
118121
}
@@ -149,24 +152,15 @@ public function auth($provider, $options = array()) {
149152
} else if (isset($options['credentials'])) {
150153
$credentials = $options['credentials'];
151154
} else {
152-
$credentials = $this->injector->session['oauthio']['auth'][$provider];
155+
if (isset($this->injector->session['oauthio']['auth'][$provider])) {
156+
$credentials = $this->injector->session['oauthio']['auth'][$provider];
157+
} else {
158+
throw new NotAuthenticatedException('The user is not authenticated for that provider');
159+
}
153160
}
154161
$credentials = $this->refreshCredentials($credentials, isset($options['force_refresh']) ? $options['force_refresh'] : false);
155162
$request_object = new RequestObject($credentials);
156163

157164
return $request_object;
158165
}
159-
160-
public function create($provider) {
161-
if (!$this->initialized) {
162-
throw new NotInitializedException('You must initialize the OAuth instance.');
163-
}
164-
if (isset($this->injector->session['oauthio']['auth'][$provider])) {
165-
$request = new Request();
166-
$request->initialize($provider);
167-
return $request;
168-
} else {
169-
throw new NotAuthenticatedException('The user is not authenticated for that provider');
170-
}
171-
}
172166
}

src/OAuth_io/RequestObject.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public function __construct($credentials = array()) {
1414
public function getCredentials() {
1515
return $this->credentials;
1616
}
17+
18+
public function wasRefreshed() {
19+
return $this->credentials['refreshed'] == true;
20+
}
1721

1822
private function object_to_array($obj) {
1923
return json_decode(json_encode($obj), true);
@@ -116,7 +120,7 @@ public function del($url) {
116120
}
117121

118122
public function patch($url, $fields) {
119-
$response = $this->makeRequest('PATCH', $url, $fields)->body->data;
123+
$response = $this->makeRequest('PATCH', $url, $fields)->body;
120124
return $this->object_to_array($response);
121125
}
122126

tests/AuthTest.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public function testAuthMethodCallsOauthioWithCredentialsAndCode() {
4747

4848
$this->request_mock->expects($this->once())->method('make_request')->will($this->returnValue($response));
4949

50-
$result = $this->oauth->auth('somecode');
50+
$request_object = $this->oauth->auth('some_provider', array(
51+
'code' => 'some_code'
52+
));
53+
$result = $request_object->getCredentials();
5154
$this->assertEquals($result['access_token'], 'someaccesstoken');
5255
$this->assertEquals($result['state'], $this->token);
5356
} else {
@@ -71,7 +74,10 @@ public function testAuthMethodSetsProviderFieldInSessions() {
7174

7275
$this->request_mock->expects($this->once())->method('make_request')->will($this->returnValue($response));
7376

74-
$result = $this->oauth->auth('somecode');
77+
$request_object = $this->oauth->auth('some_provider', array(
78+
'code' => 'somecode'
79+
));
80+
$result = $request_object->getCredentials();
7581
$this->assertTrue(isset($this->injector->session['oauthio']['auth']['blabla']));
7682
$this->assertEquals('someaccesstoken', $this->injector->session['oauthio']['auth']['blabla']['access_token']);
7783
$this->assertEquals($this->token, $this->injector->session['oauthio']['auth']['blabla']['state']);
@@ -80,4 +86,61 @@ public function testAuthMethodSetsProviderFieldInSessions() {
8086
$this->fail('OAuth::auth() does not exist');
8187
}
8288
}
89+
90+
public function testTokenIsRefreshedWhenCredentialsAreExpired() {
91+
$res = new stdClass();
92+
$res->access_token = 'someaccesstoken';
93+
$res->state = $this->token;
94+
$res->provider = 'some_provider';
95+
$res->refresh_token = 'some_refresh_token';
96+
$res->expires_in = -50;
97+
$response = new StdClass();
98+
$response->body = $res;
99+
100+
$this->request_mock->expects($this->exactly(3))->method('make_request')->will($this->returnValue($response));
101+
102+
$this->oauth->auth('some_provider', array(
103+
'code' => 'somecode'
104+
));
105+
106+
$request_object = $this->oauth->auth('some_provider');
107+
108+
$credentials = $request_object->getCredentials();
109+
$this->assertTrue($request_object->wasRefreshed());
110+
$this->assertTrue($credentials['refreshed']);
111+
}
112+
113+
public function testTokenIsRefreshedWhenForced() {
114+
$res = new stdClass();
115+
$res->access_token = 'someaccesstoken';
116+
$res->state = $this->token;
117+
$res->provider = 'some_provider';
118+
$res->refresh_token = 'some_refresh_token';
119+
$res->expires_in = 10000;
120+
$response = new StdClass();
121+
$response->body = $res;
122+
123+
$this->request_mock->expects($this->exactly(2))->method('make_request')->will($this->returnValue($response));
124+
125+
$this->oauth->auth('some_provider', array(
126+
'code' => 'somecode'
127+
));
128+
129+
$res = new stdClass();
130+
$res->access_token = 'someaccesstoken';
131+
$res->expires_in = 3600;
132+
$res->refresh_token = 'some_refresh_token';
133+
$response = new StdClass();
134+
$response->body = $res;
135+
136+
$this->request_mock->expects($this->exactly(1))->method('make_request')->will($this->returnValue($response));
137+
$request_object = $this->oauth->auth('some_provider', array(
138+
'force_refresh' => true
139+
));
140+
141+
$credentials = $request_object->getCredentials();
142+
$this->assertTrue($request_object->wasRefreshed());
143+
$this->assertTrue($credentials['refreshed']);
144+
}
145+
83146
}

tests/InitialTest.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,4 @@ public function testSetOauthdUrlSetsUrlInObject() {
5454
$this->fail('methods are missing');
5555
}
5656
}
57-
58-
public function testCallingAuthOrCreateWhenNotInitializedThrowsAnException() {
59-
if (method_exists($this->oauth, 'initialize') ) {
60-
$passed_auth = false;
61-
$passed_create = false;
62-
try {
63-
$this->oauth->auth('somecode');
64-
} catch (\OAuth_io\NotInitializedException $e) {
65-
$passed_auth = true;
66-
}
67-
68-
try {
69-
$this->oauth->create('somecode');
70-
} catch (\OAuth_io\NotInitializedException $e) {
71-
$passed_create = true;
72-
}
73-
74-
75-
$this->assertTrue($passed_auth);
76-
$this->assertTrue($passed_create);
77-
} else {
78-
$this->fail('methods are missing');
79-
}
80-
}
8157
}

0 commit comments

Comments
 (0)
0