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

Commit 6af8bf2

Browse files
committed
ENH: refs #236. Add a test for uploading via the web API using upload token
1 parent 19668e5 commit 6af8bf2

File tree

3 files changed

+101
-11
lines changed

3 files changed

+101
-11
lines changed

core/tests/databaseDataset/default.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
parent_id="-1"/>
99
<folder folder_id="3" name="name Folder 3" description="Description Folder 3" left_indice="5" right_indice="6" uuid='4e311fdf82007c245b07d8d6c4fcb4205f2621eb72802'
1010
parent_id="-1"/>
11-
11+
1212
<folder folder_id="1000" name="User 1 name Folder 1" uuid='4e311fdf82007c245b07d8d6c4fcb4205f2621eb72760'
1313
description="Description Folder 1" left_indice="1000" right_indice="1005"
1414
parent_id="-1" />
@@ -30,13 +30,13 @@
3030
<folder folder_id="1005" name="Community test User 1 name Folder 3" uuid='4e311fdf82007c245b07d8d6c4fcb4205f2621eb72765'
3131
description="Description Folder 3" left_indice="1009" right_indice="1010"
3232
parent_id="1003" />
33-
34-
33+
34+
3535
<community community_id="2000" name="Community test User 1"
3636
description="Community test User 1" creation="2011-01-27 12:09:02"
3737
privacy="0" folder_id="1003" publicfolder_id="1004" privatefolder_id="1005"
3838
admingroup_id="3000" moderatorgroup_id="3001" membergroup_id="3002" />
39-
39+
4040

4141
<group group_id="3000" community_id="2000" name="Community test User 1 Admin" />
4242
<group group_id="3001" community_id="2000"
@@ -66,24 +66,25 @@
6666
<item2folder item_id="2" folder_id='1001' />
6767
<item item_id="3" name="name 3" description="Description 3" type="3" uuid='4e311fdf82007c245b07d8d6c4fcb4205f2621eb72752' />
6868

69-
69+
<bitstream bitstream_id="1" itemrevision_id="3" name="foo.png" mimetype="image/png" sizebytes="64" checksum="f283bc88b24491ba85c65ba960642753"
70+
path="f2/83/f283bc88b24491ba85c65ba960642753" assetstore_id="1" date="2011-01-27 12:09:02" />
7071

7172
<!-- MIDAS_FEED_CREATE_COMMUNITY -->
7273
<feed feed_id="1" date="2011-01-27 12:09:02" user_id='1' type="0"
7374
ressource="2000" />
7475
<feedpolicygroup feed_id="1" group_id="0" policy='0' />
75-
76+
7677
<!-- MIDAS_FEED_DELETE_COMMUNITY -->
7778
<feed feed_id="2" date="2011-01-27 12:09:02" user_id='1' type="1"
7879
ressource="CommunityName" />
7980
<feedpolicygroup feed_id="2" group_id="3000" policy='1' />
80-
81+
8182
<!-- MIDAS_FEED_UPDATE_COMMUNITY -->
8283
<feed feed_id="3" date="2011-01-27 12:09:02" user_id='1' type="2"
8384
ressource="2000" />
8485
<feedpolicyuser feed_id="3" user_id="1" policy='2' />
8586
<feed2community feed_id="2" community_id="2000" />
86-
87+
8788
<!-- MIDAS_FEED_CREATE_USER -->
8889
<feed feed_id="4" date="2011-01-27 12:09:02" user_id='1' type="10"
8990
ressource="1" />
@@ -106,4 +107,3 @@
106107
<feed feed_id="10" date="2011-01-27 12:09:02" user_id='1' type="40"
107108
ressource="1" />
108109
</dataset>
109-

modules/api/controllers/components/ApiComponent.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ function resourceSearch($args)
189189

190190
/**
191191
* Generate a unique upload token
192+
* @param token Authentication token
192193
* @param itemid The id of the parent item to upload into
193194
* @param filename The filename of the bitstream you will upload
194195
* @return An upload token that can be used to upload a file
@@ -245,7 +246,6 @@ function uploadPerform($args)
245246
}
246247

247248
list($userid, $resourceid, ) = explode('/', $args['uploadtoken']);
248-
//TODO check if this upload token is valid
249249

250250
$modelLoader = new MIDAS_ModelLoader();
251251
$itemModel = $modelLoader->loadModel('Item');
@@ -333,6 +333,12 @@ function uploadPerform($args)
333333
$mode = array_key_exists('mode', $args) ? $args['mode'] : 'stream';
334334
$uploadApi = new KwUploadAPI($this->apiSetup);
335335

336+
if(array_key_exists('testingmode', $args))
337+
{
338+
$uploadApi->testing_enable = true;
339+
$args['localinput'] = $this->apiSetup['tmp_directory'].'/'.$args['filename'];
340+
}
341+
336342
// Use KWUploadApi to handle the actual file upload
337343
if($mode == 'stream')
338344
{

modules/api/tests/controllers/ApiCallMethodsTest.php

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function setUp()
1919
$this->setupDatabase(array('default')); //core dataset
2020
$this->setupDatabase(array('default'), 'api'); // module dataset
2121
$this->enabledModules = array('api');
22-
$this->_models = array('User', 'Folder', 'Item');
22+
$this->_models = array('User', 'Folder', 'Item', 'ItemRevision');
2323
$this->_daos = array('User', 'Folder', 'Item');
2424

2525
parent::setUp();
@@ -251,4 +251,88 @@ public function testSessionAuthentication()
251251
$this->assertEquals($resp->data[0]->name, 'User 1 name Folder 2');
252252
$this->assertEquals($resp->data[1]->name, 'User 1 name Folder 3');
253253
}
254+
255+
/** Test file upload */
256+
public function testUpload()
257+
{
258+
$this->resetAll();
259+
$usersFile = $this->loadData('User', 'default');
260+
$itemsFile = $this->loadData('Item', 'default');
261+
262+
$this->params['token'] = $this->_loginUsingApiKey();
263+
$this->params['method'] = 'midas.upload.generatetoken';
264+
$this->params['filename'] = 'test.txt';
265+
// call should fail for the first item since we don't have write permission
266+
$this->params['itemid'] = $itemsFile[0]->getKey();
267+
$this->request->setMethod('POST');
268+
$resp = $this->_callJsonApi();
269+
$this->assertEquals($resp->stat, 'fail');
270+
$this->assertEquals($resp->message, 'Invalid policy or itemid');
271+
$this->assertTrue($resp->code != 0);
272+
273+
$this->resetAll();
274+
$usersFile = $this->loadData('User', 'default');
275+
$itemsFile = $this->loadData('Item', 'default');
276+
277+
$this->params['token'] = $this->_loginUsingApiKey();
278+
$this->params['method'] = 'midas.upload.generatetoken';
279+
$this->params['filename'] = 'test.txt';
280+
// use the second item since it has write permission set for our user
281+
$this->params['itemid'] = $itemsFile[1]->getKey();
282+
$this->request->setMethod('POST');
283+
$resp = $this->_callJsonApi();
284+
285+
$this->_assertStatusOk($resp);
286+
$token = $resp->data->token;
287+
$this->assertTrue(
288+
preg_match('/^'.$usersFile[0]->getKey().'\/'.$itemsFile[1]->getKey().'\/.+\.tmp$/', $token) > 0,
289+
'Upload token is not of the form <userid>/<itemid>/*.tmp');
290+
$this->assertTrue(file_exists(BASE_PATH.'/tmp/misc/'.$token),
291+
"Token placeholder file $token was not created in the temp dir");
292+
293+
//now upload using our token
294+
$this->resetAll();
295+
296+
$string = '';
297+
$length = 100;
298+
for($i = 0; $i < $length; $i++)
299+
{
300+
$string .= 'a';
301+
}
302+
$fh = fopen(BASE_PATH.'/tmp/misc/test.txt', 'w');
303+
fwrite($fh, $string);
304+
fclose($fh);
305+
$md5 = md5($string);
306+
$assetstoreFile = BASE_PATH.'/data/assetstore/'.substr($md5, 0, 2).'/'.substr($md5, 2, 2).'/'.$md5;
307+
unlink($assetstoreFile);
308+
309+
$this->params['method'] = 'midas.upload.perform';
310+
$this->params['uploadtoken'] = $token;
311+
$this->params['filename'] = 'test.txt';
312+
$this->params['length'] = $length;
313+
$this->params['itemid'] = $itemsFile[1]->getKey();
314+
$this->params['revision'] = 'head'; //upload into head revision
315+
$this->params['testingmode'] = 'true';
316+
317+
$this->request->setMethod('POST');
318+
$resp = $this->_callJsonApi();
319+
320+
unlink(BASE_PATH.'/tmp/misc/test.txt');
321+
322+
$this->_assertStatusOk($resp);
323+
324+
$this->assertTrue(file_exists($assetstoreFile), 'File was not written to the assetstore');
325+
$this->assertEquals(filesize($assetstoreFile), $length, 'Assetstore file is the wrong length: '.filesize($assetstoreFile));
326+
$this->assertEquals(md5_file($assetstoreFile), $md5, 'Assetstore file had incorrect checksum');
327+
328+
// make sure it was uploaded to the head revision of the item
329+
$itemDao = $this->Item->load($itemsFile[1]->getKey());
330+
$revisions = $itemDao->getRevisions();
331+
$this->assertEquals(count($revisions), 1, 'Too many revisions in the item');
332+
$bitstreams = $revisions[0]->getBitstreams();
333+
$this->assertEquals(count($bitstreams), 1, 'Too many bitstreams in the revision');
334+
$this->assertEquals($bitstreams[0]->name, 'test.txt');
335+
$this->assertEquals($bitstreams[0]->sizebytes, $length);
336+
$this->assertEquals($bitstreams[0]->checksum, $md5);
337+
}
254338
}

0 commit comments

Comments
 (0)