Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 8e6f9be

Browse files
committed
ENH: refs #250. Add unit test for web api authentication using the session
1 parent 0d015de commit 8e6f9be

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

modules/api/controllers/components/AuthenticationComponent.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public function getUser($args, $sessionDao)
3737
return 0;
3838
}
3939
$token = $args['token'];
40-
$userapiDao = $this->Api_Userapi->getUserapiFromToken($token);
40+
$modelLoad = new MIDAS_ModelLoader();
41+
$userApiModel = $modelLoad->loadModel('Userapi', 'api');
42+
$userapiDao = $userApiModel->getUserapiFromToken($token);
4143
if(!$userapiDao)
4244
{
4345
throw new Exception('Invalid token', MIDAS_INVALID_TOKEN);
@@ -47,7 +49,8 @@ public function getUser($args, $sessionDao)
4749
{
4850
return false;
4951
}
50-
$userDao = $this->User->load($userid);
52+
$userModel = $modelLoad->loadModel('User');
53+
$userDao = $userModel->load($userid);
5154
return $userDao;
5255
}
5356
}

modules/api/tests/controllers/ApiIndexControllerTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ public function setUp()
2626
}
2727

2828
/** Invoke the JSON web API */
29-
private function _callJsonApi()
29+
private function _callJsonApi($sessionUser = null)
3030
{
31-
$this->dispatchUrI($this->webroot.'api/json');
32-
return json_decode($this->response->getBody());
31+
$this->dispatchUrI($this->webroot.'api/json', $sessionUser);
32+
return json_decode($this->getBody());
3333
}
3434

3535
/** Make sure we got a good response from a web API call */
3636
private function _assertStatusOk($resp)
3737
{
3838
$this->assertNotEquals($resp, false);
39+
$this->assertEquals($resp->message, '');
3940
$this->assertEquals($resp->stat, 'ok');
4041
$this->assertEquals($resp->code, 0);
4142
$this->assertTrue(isset($resp->data));
@@ -184,4 +185,30 @@ public function testUserApikeyDefault()
184185

185186
$this->assertEquals($resp->data->apikey, $apiKey);
186187
}
188+
189+
/** Test that we can authenticate to the web API using the user session */
190+
public function testSessionAuthentication()
191+
{
192+
$usersFile = $this->loadData('User', 'default');
193+
$userDao = $this->User->load($usersFile[0]->getKey());
194+
195+
$this->resetAll();
196+
$this->params = array();
197+
$this->params['method'] = 'midas.user.folders';
198+
$this->params['useSession'] = 'true';
199+
$this->request->setMethod('POST');
200+
$resp = $this->_callJsonApi($userDao);
201+
$this->_assertStatusOk($resp);
202+
203+
// We should see the user's folders
204+
$this->assertEquals(count($resp->data), 2);
205+
206+
foreach($resp->data as $folder)
207+
{
208+
$this->assertEquals($folder->_model, 'Folder');
209+
$this->assertEquals($folder->parent_id, 1000);
210+
}
211+
$this->assertEquals($resp->data[0]->name, 'User 1 name Folder 2');
212+
$this->assertEquals($resp->data[1]->name, 'User 1 name Folder 3');
213+
}
187214
}

0 commit comments

Comments
 (0)