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

Commit 8ccaf77

Browse files
committed
ENH: refs #256. Add a test for the web api help section
1 parent 3e15a13 commit 8ccaf77

File tree

3 files changed

+224
-190
lines changed

3 files changed

+224
-190
lines changed
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5+
69328 Lyon, FRANCE.
6+
7+
See Copyright.txt for details.
8+
This software is distributed WITHOUT ANY WARRANTY; without even
9+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10+
PURPOSE. See the above copyright notices for more information.
11+
=========================================================================*/
12+
13+
/** Tests the functionality of the web API methods */
14+
class ApiCallMethodsTest extends ControllerTestCase
15+
{
16+
/** set up tests */
17+
public function setUp()
18+
{
19+
$this->setupDatabase(array('default')); //core dataset
20+
$this->setupDatabase(array('default'), 'api'); // module dataset
21+
$this->enabledModules = array('api');
22+
$this->_models = array('User', 'Folder');
23+
$this->_daos = array('User', 'Folder');
24+
25+
parent::setUp();
26+
}
27+
28+
/** Invoke the JSON web API */
29+
private function _callJsonApi($sessionUser = null)
30+
{
31+
$this->dispatchUrI($this->webroot.'api/json', $sessionUser);
32+
return json_decode($this->getBody());
33+
}
34+
35+
/** Make sure we got a good response from a web API call */
36+
private function _assertStatusOk($resp)
37+
{
38+
$this->assertNotEquals($resp, false);
39+
$this->assertEquals($resp->message, '');
40+
$this->assertEquals($resp->stat, 'ok');
41+
$this->assertEquals($resp->code, 0);
42+
$this->assertTrue(isset($resp->data));
43+
}
44+
45+
/** Authenticate using the default api key */
46+
private function _loginUsingApiKey()
47+
{
48+
$usersFile = $this->loadData('User', 'default');
49+
$userDao = $this->User->load($usersFile[0]->getKey());
50+
51+
$modelLoad = new MIDAS_ModelLoader();
52+
$userApiModel = $modelLoad->loadModel('Userapi', 'api');
53+
$userApiModel->createDefaultApiKey($userDao);
54+
$apiKey = $userApiModel->getByAppAndUser('Default', $userDao)->getApikey();
55+
56+
$this->params['method'] = 'midas.login';
57+
$this->params['email'] = $usersFile[0]->getEmail();
58+
$this->params['appname'] = 'Default';
59+
$this->params['apikey'] = $apiKey;
60+
$this->request->setMethod('POST');
61+
62+
$resp = $this->_callJsonApi();
63+
$this->_assertStatusOk($resp);
64+
$this->assertEquals(strlen($resp->data->token), 40);
65+
66+
// **IMPORTANT** This will clear any params that were set before this function was called
67+
$this->resetAll();
68+
return $resp->data->token;
69+
}
70+
71+
/** Get the folders corresponding to the user */
72+
public function testUserFolders()
73+
{
74+
// Try anonymously first
75+
$this->params['method'] = 'midas.user.folders';
76+
$this->request->setMethod('POST');
77+
$resp = $this->_callJsonApi();
78+
$this->_assertStatusOk($resp);
79+
// No user folders should be visible anonymously
80+
$this->assertEquals(count($resp->data), 0);
81+
82+
$this->resetAll();
83+
$this->params['token'] = $this->_loginUsingApiKey();
84+
$this->params['method'] = 'midas.user.folders';
85+
$resp = $this->_callJsonApi();
86+
$this->_assertStatusOk($resp);
87+
$this->assertEquals(count($resp->data), 2);
88+
89+
foreach($resp->data as $folder)
90+
{
91+
$this->assertEquals($folder->_model, 'Folder');
92+
$this->assertEquals($folder->parent_id, 1000);
93+
}
94+
$this->assertEquals($resp->data[0]->name, 'User 1 name Folder 2');
95+
$this->assertEquals($resp->data[1]->name, 'User 1 name Folder 3');
96+
}
97+
98+
/** Test listing of visible communities */
99+
public function testCommunityList()
100+
{
101+
$this->resetAll();
102+
$this->params['token'] = $this->_loginUsingApiKey();
103+
$this->params['method'] = 'midas.community.list';
104+
105+
$this->request->setMethod('POST');
106+
$resp = $this->_callJsonApi();
107+
$this->_assertStatusOk($resp);
108+
109+
$this->assertEquals(count($resp->data), 1);
110+
$this->assertEquals($resp->data[0]->_model, 'Community');
111+
$this->assertEquals($resp->data[0]->community_id, 2000);
112+
$this->assertEquals($resp->data[0]->folder_id, 1003);
113+
$this->assertEquals($resp->data[0]->publicfolder_id, 1004);
114+
$this->assertEquals($resp->data[0]->privatefolder_id, 1005);
115+
$this->assertEquals($resp->data[0]->name, 'Community test User 1');
116+
117+
//TODO test that a private community is not returned (requires another community in the data set)
118+
}
119+
120+
/** Test listing of child folders */
121+
public function testFolderChildren()
122+
{
123+
$this->resetAll();
124+
$token = $this->_loginUsingApiKey();
125+
$this->params['token'] = $token;
126+
$this->params['method'] = 'midas.folder.children';
127+
$this->params['id'] = 1000;
128+
$this->request->setMethod('POST');
129+
$resp = $this->_callJsonApi();
130+
$this->_assertStatusOk($resp);
131+
132+
// Should contain 2 folders and 0 items
133+
$this->assertEquals(count($resp->data->folders), 2);
134+
$this->assertEquals(count($resp->data->items), 0);
135+
136+
$this->assertEquals($resp->data->folders[0]->_model, 'Folder');
137+
$this->assertEquals($resp->data->folders[1]->_model, 'Folder');
138+
$this->assertEquals($resp->data->folders[0]->folder_id, 1001);
139+
$this->assertEquals($resp->data->folders[1]->folder_id, 1002);
140+
$this->assertEquals($resp->data->folders[0]->name, 'User 1 name Folder 2');
141+
$this->assertEquals($resp->data->folders[1]->name, 'User 1 name Folder 3');
142+
$this->assertEquals($resp->data->folders[0]->description, 'Description Folder 2');
143+
$this->assertEquals($resp->data->folders[1]->description, 'Description Folder 3');
144+
145+
$this->resetAll();
146+
$this->params['token'] = $token;
147+
$this->params['method'] = 'midas.folder.children';
148+
$this->params['id'] = 1001;
149+
$this->request->setMethod('POST');
150+
$resp = $this->_callJsonApi();
151+
$this->_assertStatusOk($resp);
152+
153+
// Should contain 0 folders and 2 items
154+
$this->assertEquals(count($resp->data->folders), 0);
155+
$this->assertEquals(count($resp->data->items), 2);
156+
157+
$this->assertEquals($resp->data->items[0]->_model, 'Item');
158+
$this->assertEquals($resp->data->items[1]->_model, 'Item');
159+
$this->assertEquals($resp->data->items[0]->item_id, 1);
160+
$this->assertEquals($resp->data->items[1]->item_id, 2);
161+
$this->assertEquals($resp->data->items[0]->name, 'name 1');
162+
$this->assertEquals($resp->data->items[1]->name, 'name 2');
163+
$this->assertEquals($resp->data->items[0]->description, 'Description 1');
164+
$this->assertEquals($resp->data->items[1]->description, 'Description 2');
165+
}
166+
167+
/** Test get user's default API key using username and password */
168+
public function testUserApikeyDefault()
169+
{
170+
$this->resetAll();
171+
$usersFile = $this->loadData('User', 'default');
172+
$userDao = $this->User->load($usersFile[0]->getKey());
173+
$this->params['method'] = 'midas.user.apikey.default';
174+
$this->params['email'] = $userDao->getEmail();
175+
$this->params['password'] = 'test';
176+
$this->request->setMethod('POST');
177+
$resp = $this->_callJsonApi();
178+
$this->_assertStatusOk($resp);
179+
180+
// Expected API key
181+
$modelLoad = new MIDAS_ModelLoader();
182+
$userApiModel = $modelLoad->loadModel('Userapi', 'api');
183+
$userApiModel->createDefaultApiKey($userDao);
184+
$apiKey = $userApiModel->getByAppAndUser('Default', $userDao)->getApikey();
185+
186+
$this->assertEquals($resp->data->apikey, $apiKey);
187+
}
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+
}
214+
}

0 commit comments

Comments
 (0)