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

Commit 86f6349

Browse files
author
Charles Ma
committed
ENH: refs #301 Added api tests
1 parent 3805989 commit 86f6349

File tree

4 files changed

+142
-17
lines changed

4 files changed

+142
-17
lines changed

modules/remoteprocessing/Notification.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function init()
1717
$this->addTask("TASK_REMOTEPROCESSING_ADD_JOB", 'addJob', "");
1818
$this->addCallBack('CALLBACK_REMOTEPROCESSING_IS_EXECUTABLE', 'isExecutable');
1919
$this->addCallBack('CALLBACK_REMOTEPROCESSING_EXECUTABLE_RESULTS', 'processProcessingResults');
20+
$this->addCallBack('CALLBACK_REMOTEPROCESSING_ADD_JOB', 'addJob');
2021
}//end init
2122

2223
/** check if item contains an executable */

modules/remoteprocessing/controllers/components/ApiComponent.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public function registerserver($args)
6666
$userDao->setPrivacy(MIDAS_USER_PRIVATE);
6767
$userDao->setCompany($os); //used to set operating system
6868
$userModel->save($userDao);
69-
7069
$serverGroup = $groupModel->load(MIDAS_GROUP_SERVER_KEY);
7170
$groupModel->addUser($serverGroup, $userDao);
7271
$userapiDao = $Api_UserapiModel->createKey($userDao, 'remoteprocessing', '100');
72+
7373
$apikey = $userapiDao->getApikey();
7474

7575
Zend_Registry::get('notifier')->callback('CALLBACK_REMOTEPROCESSING_CREATESERVER', $userDao->toArray());
@@ -184,10 +184,17 @@ public function keepaliveserver($args)
184184
*/
185185
public function resultsserver($args)
186186
{
187-
if($_SERVER['REQUEST_METHOD'] != 'POST')
187+
$testingmode = false;
188+
if($_GET['testingmode'] == 1)
189+
{
190+
$testingmode = true;
191+
}
192+
if(!$testingmode && $_SERVER['REQUEST_METHOD'] != 'POST')
188193
{
189194
throw new Exception('Should be a put request.', MIDAS_INVALID_PARAMETER);
190195
}
196+
197+
191198
$modelLoad = new MIDAS_ModelLoader();
192199
$componentLoader = new MIDAS_ComponentLoader();
193200
$authComponent = $componentLoader->loadComponent('Authentication', 'api');
@@ -226,7 +233,16 @@ public function resultsserver($args)
226233
$destionation = BASE_PATH.'/tmp/remoteprocessing/'.rand(1, 1000).time();
227234
}
228235
mkdir($destionation);
229-
move_uploaded_file($_FILES['file']['tmp_name'], $destionation."/results.zip");
236+
237+
if(!$testingmode)
238+
{
239+
move_uploaded_file($_FILES['file']['tmp_name'], $destionation."/results.zip");
240+
}
241+
242+
if($testingmode)
243+
{
244+
return array();
245+
}
230246

231247
if(file_exists($destionation."/results.zip"))
232248
{

modules/remoteprocessing/tests/ApiTest.php

Lines changed: 114 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,135 @@ class ApiTest extends ControllerTestCase
1717
public function setUp()
1818
{
1919
$this->setupDatabase(array('default', 'adminUser'));
20-
$this->_models = array('User');
21-
$this->enabledModules = array('remoteprocessing');
20+
$this->_models = array('User', 'Item');
21+
$this->enabledModules = array('scheduler', 'remoteprocessing', 'api');
2222
parent::setUp();
2323
}
2424

25-
/** test config */
26-
public function testIndex()
25+
private function _getSecurityKey()
2726
{
27+
$this->resetAll();
2828
$usersFile = $this->loadData('User', 'adminUser');
2929
$userDao = $this->User->load($usersFile[0]->getKey());
30-
31-
$this->dispatchUrI('/remoteprocessing/config', $userDao);
32-
$this->assertQuery("input#securitykey");
33-
34-
$this->resetAll();
35-
3630
$this->params = array();
3731
$securityKey = uniqid();
3832
$this->params['securitykey'] = $securityKey;
3933
$this->params['submitConfig'] = 'true';
4034
$this->request->setMethod('POST');
4135
$this->dispatchUrI("/remoteprocessing/config", $userDao);
36+
$this->resetAll();
37+
return $securityKey;
38+
}
39+
40+
/** test manage */
41+
public function testAllApiSubmissionProcess()
42+
{
43+
$usersFile = $this->loadData('User', 'adminUser');
44+
$userDao = $this->User->load($usersFile[0]->getKey());
45+
$itemFile = $this->loadData('Item', 'default');
46+
47+
$revision = $this->Item->getLastRevision($itemFile[0]);
48+
49+
// register (create user)
50+
$this->params = array();
51+
$this->params['securitykey'] = $this->_getSecurityKey();
52+
$this->params['os'] = MIDAS_REMOTEPROCESSING_OS_WINDOWS;
53+
$this->request->setMethod('POST');
4254

43-
if(!file_exists(BASE_PATH."/core/configs/remoteprocessing.local.ini"))
55+
$this->dispatchUrI('/api/json?method=midas.remoteprocessing.registerserver');
56+
57+
$jsonResults = $this->getBody();
58+
$this->resetAll();
59+
if(strpos($jsonResults, '{"stat":"ok"') === false)
60+
{
61+
$this->fail('Error json');
62+
}
63+
$results = JsonComponent::decode($jsonResults);
64+
65+
$token = $results['data']['token'];
66+
$email = $results['data']['email'];
67+
$apikey = $results['data']['apikey'];
68+
69+
// authenticate
70+
$this->params = array();
71+
$this->params['securitykey'] = $this->_getSecurityKey();
72+
$this->params['email'] = $email;
73+
$this->params['apikey'] = $apikey;
74+
$this->request->setMethod('POST');
75+
76+
$this->dispatchUrI('/api/json?method=midas.remoteprocessing.registerserver');
77+
78+
$jsonResults = $this->getBody();
79+
$this->resetAll();
80+
if(strpos($jsonResults, '{"stat":"ok"') === false)
4481
{
45-
$this->fail('Unable to find config file');
82+
$this->fail('Error json');
4683
}
47-
$applicationConfig = parse_ini_file(BASE_PATH."/core/configs/remoteprocessing.local.ini", true);
84+
$results = JsonComponent::decode($jsonResults);
4885

49-
$this->assertEquals($securityKey, $applicationConfig['global']['securitykey']);
86+
$token = $results['data']['token'];
87+
88+
// ask action
89+
$this->params = array();
90+
$this->params['token'] = $token;
91+
$this->params['os'] = MIDAS_REMOTEPROCESSING_OS_WINDOWS;
92+
$this->request->setMethod('POST');
93+
94+
$this->dispatchUrI('/api/json?method=midas.remoteprocessing.keepaliveserver');
95+
96+
$jsonResults = $this->getBody();
97+
$this->resetAll();
98+
if(strpos($jsonResults, '{"stat":"ok"') === false)
99+
{
100+
$this->fail('Error json');
101+
}
102+
$results = JsonComponent::decode($jsonResults);
103+
104+
if($results['data']['action'] != 'wait')
105+
{
106+
$this->fail('Should be wait, was '.$results['data']['action']);
107+
}
108+
109+
// add a job
110+
$scriptParams['script'] = 'script';
111+
$scriptParams['os'] = MIDAS_REMOTEPROCESSING_OS_WINDOWS;
112+
$scriptParams['condition'] = '';
113+
$scriptParams['params'] = array();
114+
Zend_Registry::get('notifier')->callback("CALLBACK_REMOTEPROCESSING_ADD_JOB", $scriptParams);
115+
116+
$this->params = array();
117+
$this->params['token'] = $token;
118+
$this->params['os'] = MIDAS_REMOTEPROCESSING_OS_WINDOWS;
119+
$this->request->setMethod('POST');
120+
121+
$this->dispatchUrI('/api/json?method=midas.remoteprocessing.keepaliveserver');
122+
123+
$jsonResults = $this->getBody();
124+
$this->resetAll();
125+
if(strpos($jsonResults, '{"stat":"ok"') === false)
126+
{
127+
$this->fail('Error json');
128+
}
129+
$results = JsonComponent::decode($jsonResults);
130+
131+
if($results['data']['action'] != 'process')
132+
{
133+
$this->fail('Should be process, was '.$results['data']['action']);
134+
}
135+
136+
// send results
137+
$this->params = array();
138+
$this->params['token'] = $token;
139+
$this->params['os'] = MIDAS_REMOTEPROCESSING_OS_WINDOWS;
140+
$this->request->setMethod('POST');
141+
142+
$this->dispatchUrI('/api/json?method=midas.remoteprocessing.resultsserver&testingmode=1');
143+
$jsonResults = $this->getBody();
144+
$this->resetAll();
145+
if(strpos($jsonResults, '{"stat":"ok"') === false)
146+
{
147+
$this->fail('Error json');
148+
}
50149
}
150+
51151
}

tests/ControllerTestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ private function _initModule()
9090
foreach($paramsTestingModules as $p)
9191
{
9292
$modules[$p] = 1;
93+
if(file_exists(BASE_PATH . "/modules/".$p."/constant/module.php"))
94+
{
95+
require_once BASE_PATH . "/modules/".$p."/constant/module.php";
96+
}
97+
if(file_exists(BASE_PATH . "/privateModules/".$p."/constant/module.php"))
98+
{
99+
require_once BASE_PATH . "/privateModules/".$p."/constant/module.php";
100+
}
93101
}
94102
}
95103
else

0 commit comments

Comments
 (0)