Skip to content

Commit 062679e

Browse files
committed
Added unit tests
1 parent ac2c9c1 commit 062679e

File tree

2 files changed

+166
-4
lines changed

2 files changed

+166
-4
lines changed

src/Identity/v3/Service.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ public function authenticate(array $options): array
3131
{
3232
$authOptions = array_intersect_key($options, $this->api->postTokens()['params']);
3333

34-
if (!empty($options['cachedCredential'])) {
35-
$token = $this->generateTokenFromCache($options['cachedCredential']);
34+
if (!empty($options['cachedToken'])) {
35+
$token = $this->generateTokenFromCache($options['cachedToken']);
36+
37+
if ($token->hasExpired()) {
38+
throw new \RuntimeException(sprintf('Cached token has expired. You may need to re-generate a new token.'));
39+
}
3640
} else {
3741
$token = $this->generateToken($authOptions);
3842
}
@@ -50,9 +54,17 @@ public function authenticate(array $options): array
5054
$type, $name, $region, $interface));
5155
}
5256

53-
public function generateTokenFromCache(array $cache): Models\Token
57+
/**
58+
* Generates authentication token from cached token using `$token->export()`
59+
*
60+
* @param array $cachedToken {@see \OpenStack\Identity\v3\Models\Token::export}
61+
*
62+
* @return Models\Token
63+
*
64+
*/
65+
public function generateTokenFromCache(array $cachedToken): Models\Token
5466
{
55-
return $this->model(Models\Token::class)->populateFromArray($cache);
67+
return $this->model(Models\Token::class)->populateFromArray($cachedToken);
5668
}
5769

5870
/**

tests/unit/Identity/v3/ServiceTest.php

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,156 @@ public function test_it_authenticates()
6666
$this->assertEquals('http://example.org:8080/v1/AUTH_e00abf65afca49609eedd163c515cf10', $url);
6767
}
6868

69+
public function test_it_authenticates_using_cache_token()
70+
{
71+
$cachedToken = [
72+
'is_domain' => false,
73+
'methods' => [
74+
'password',
75+
],
76+
'roles' => [
77+
0 => [
78+
'id' => 'ce40dfb7a1b14f8a875194fe2944e00c',
79+
'name' => 'admin',
80+
],
81+
],
82+
'expires_at' => '2199-11-24T04:47:49.000000Z',
83+
'project' => [
84+
'domain' => [
85+
'id' => 'default',
86+
'name' => 'Default',
87+
],
88+
'id' => 'c41b19de8aac4ecdb0f04ede718206c5',
89+
'name' => 'admin',
90+
],
91+
'catalog' => [
92+
[
93+
'endpoints' => [
94+
[
95+
'region_id' => 'RegionOne',
96+
'url' => 'http://example.org:8080/v1/AUTH_e00abf65afca49609eedd163c515cf10',
97+
'region' => 'RegionOne',
98+
'interface' => 'public',
99+
'id' => 'hhh',
100+
]
101+
],
102+
'type' => 'object-store',
103+
'id' => 'aaa',
104+
'name' => 'swift',
105+
],
106+
],
107+
'user' => [
108+
'domain' => [
109+
'id' => 'default',
110+
'name' => 'Default',
111+
],
112+
'id' => '37a36374b074428985165e80c9ab28c8',
113+
'name' => 'admin',
114+
],
115+
'audit_ids' => [
116+
'X0oY7ouSQ32vEpbgDJTDpA',
117+
],
118+
'issued_at' => '2017-11-24T03:47:49.000000Z',
119+
'id' => 'bb4f74cfb73847ec9ca947fa61d799d3',
120+
];
121+
122+
$userOptions = [
123+
'user' => [
124+
'id' => '{userId}',
125+
'password' => '{userPassword}',
126+
'domain' => ['id' => '{domainId}']
127+
],
128+
'scope' => [
129+
'project' => ['id' => '{projectId}']
130+
],
131+
'catalogName' => 'swift',
132+
'catalogType' => 'object-store',
133+
'region' => 'RegionOne',
134+
'cachedToken' => $cachedToken
135+
];
136+
137+
list($token, $url) = $this->service->authenticate($userOptions);
138+
139+
$this->assertInstanceOf(Models\Token::class, $token);
140+
$this->assertEquals('http://example.org:8080/v1/AUTH_e00abf65afca49609eedd163c515cf10', $url);
141+
}
142+
143+
144+
/**
145+
* @expectedException \RuntimeException
146+
* @expectedExceptionMessage Cached token has expired
147+
*/
148+
public function test_it_authenticates_and_throws_exception_when_authenticate_with_expired_cached_token()
149+
{
150+
$cachedToken = [
151+
'is_domain' => false,
152+
'methods' => [
153+
'password',
154+
],
155+
'roles' => [
156+
0 => [
157+
'id' => 'ce40dfb7a1b14f8a875194fe2944e00c',
158+
'name' => 'admin',
159+
],
160+
],
161+
'expires_at' => '2000-11-24T04:47:49.000000Z',
162+
'project' => [
163+
'domain' => [
164+
'id' => 'default',
165+
'name' => 'Default',
166+
],
167+
'id' => 'c41b19de8aac4ecdb0f04ede718206c5',
168+
'name' => 'admin',
169+
],
170+
'catalog' => [
171+
[
172+
'endpoints' => [
173+
[
174+
'region_id' => 'RegionOne',
175+
'url' => 'http://example.org:8080/v1/AUTH_e00abf65afca49609eedd163c515cf10',
176+
'region' => 'RegionOne',
177+
'interface' => 'public',
178+
'id' => 'hhh',
179+
]
180+
],
181+
'type' => 'object-store',
182+
'id' => 'aaa',
183+
'name' => 'swift',
184+
],
185+
],
186+
'user' => [
187+
'domain' => [
188+
'id' => 'default',
189+
'name' => 'Default',
190+
],
191+
'id' => '37a36374b074428985165e80c9ab28c8',
192+
'name' => 'admin',
193+
],
194+
'audit_ids' => [
195+
'X0oY7ouSQ32vEpbgDJTDpA',
196+
],
197+
'issued_at' => '2017-11-24T03:47:49.000000Z',
198+
'id' => 'bb4f74cfb73847ec9ca947fa61d799d3',
199+
];
200+
201+
$userOptions = [
202+
'user' => [
203+
'id' => '{userId}',
204+
'password' => '{userPassword}',
205+
'domain' => ['id' => '{domainId}']
206+
],
207+
'scope' => [
208+
'project' => ['id' => '{projectId}']
209+
],
210+
'catalogName' => 'swift',
211+
'catalogType' => 'object-store',
212+
'region' => 'RegionOne',
213+
'cachedToken' => $cachedToken
214+
];
215+
216+
$this->service->authenticate($userOptions);
217+
}
218+
69219
/**
70220
* @expectedException \RuntimeException
71221
*/

0 commit comments

Comments
 (0)