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

Commit 9391f9f

Browse files
committed
ENH: refs #0377. Awesome test for server side size quota management
1 parent ec55770 commit 9391f9f

File tree

6 files changed

+247
-4
lines changed

6 files changed

+247
-4
lines changed

core/tests/databaseDataset/default.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,6 @@
113113
<!-- MIDAS_FEED_CREATE_REVISION -->
114114
<feed feed_id="10" date="2011-01-27 12:09:02" user_id='1' type="40"
115115
ressource="1" />
116+
117+
<setting setting_id="1000" name="dummy" module="" value="dummy" />
116118
</dataset>

modules/sizequota/controllers/ConfigController.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,14 @@ public function getfreespaceAction()
169169
$this->disableLayout();
170170
$this->_helper->viewRenderer->setNoRender();
171171

172-
$folder = $this->Folder->load($this->_getParam('folderId'));
172+
$folderId = $this->_getParam('folderId');
173+
if(!isset($folderId))
174+
{
175+
echo JsonComponent::encode(array('status' => false, 'message' => 'Missing folderId parameter'));
176+
return;
177+
}
178+
179+
$folder = $this->Folder->load($folderId);
173180
if(!$folder)
174181
{
175182
echo JsonComponent::encode(array('status' => false, 'message' => 'Invalid folder'));
@@ -180,7 +187,15 @@ public function getfreespaceAction()
180187
echo JsonComponent::encode(array('status' => false, 'message' => 'Invalid policy'));
181188
return;
182189
}
183-
$rootFolder = $this->Folder->getRoot($folder);
190+
191+
if($folder->getParentId() < 0)
192+
{
193+
$rootFolder = $folder;
194+
}
195+
else
196+
{
197+
$rootFolder = $this->Folder->getRoot($folder);
198+
}
184199
$quota = $this->Sizequota_FolderQuota->getFolderQuota($rootFolder);
185200
if($quota == '')
186201
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
add_midas_test( SizequotaConfigController ConfigControllerTest.php )
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
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+
/** Test management of the size quotas behavior */
14+
class PerformTest extends ControllerTestCase
15+
{
16+
/** set up tests*/
17+
public function setUp()
18+
{
19+
$this->setupDatabase(array('default'));
20+
$this->setupDatabase(array('default'), 'sizequota');
21+
$this->enabledModules = array('api', 'sizequota');
22+
$this->_models = array('Community', 'Setting', 'User');
23+
24+
parent::setUp();
25+
}
26+
27+
/** Test the functionality of the main module config page */
28+
public function testAdminConfig()
29+
{
30+
// Make sure we don't have existing settings for defaults
31+
$this->assertTrue($this->Setting->getValueByName('defaultuserquota', 'sizequota') === null);
32+
$this->assertTrue($this->Setting->getValueByName('defaultcommunityquota', 'sizequota') === null);
33+
34+
// Use the admin user so we can configure the module
35+
$usersFile = $this->loadData('User', 'default');
36+
$userDao = $this->User->load($usersFile[2]->getKey());
37+
38+
// Make sure we can render the config controller
39+
$this->dispatchUrI('/sizequota/config/index', $userDao);
40+
$this->assertController('config');
41+
$this->assertAction('index');
42+
43+
// Set some default settings for user and community quota
44+
$this->resetAll();
45+
$this->request->setMethod('POST');
46+
$this->params['defaultuserquota'] = '';
47+
$this->params['defaultcommunityquota'] = '10000';
48+
$this->params['submitConfig'] = 'true';
49+
$this->dispatchUrI('/sizequota/config/index', $userDao);
50+
$resp = JsonComponent::decode($this->getBody());
51+
$this->assertTrue($resp[0] == true);
52+
53+
// Make sure our settings were set correctly
54+
$this->assertTrue($this->Setting->getValueByName('defaultuserquota', 'sizequota') === '');
55+
$this->assertTrue($this->Setting->getValueByName('defaultcommunityquota', 'sizequota') === '10000');
56+
}
57+
58+
/** Test management of user- and community-specific quotas */
59+
public function testFolderConfig()
60+
{
61+
// Set some defaults
62+
$this->Setting->setConfig('defaultuserquota', '', 'sizequota');
63+
$this->Setting->setConfig('defaultcommunityquota', '10000', 'sizequota');
64+
65+
$usersFile = $this->loadData('User', 'default');
66+
$user1 = $this->User->load($usersFile[0]->getKey());
67+
$adminUser = $this->User->load($usersFile[2]->getKey());
68+
69+
// Exception if no folder id is set
70+
$this->resetAll();
71+
$this->dispatchUrI('/sizequota/config/folder', null, true);
72+
73+
// Exception if invalid folder id is set
74+
$this->resetAll();
75+
$this->dispatchUrI('/sizequota/config/folder?folderId=foo', null, true);
76+
77+
// Exception if invalid policy
78+
$this->resetAll();
79+
$this->dispatchUrI('/sizequota/config/folder?folderId='.$user1->getFolderId(), null, true);
80+
81+
// Exception if non-root folder is passed
82+
$this->resetAll();
83+
$this->dispatchUrI('/sizequota/config/folder?folderId='.$user1->getPublicfolderId(), $user1, true);
84+
85+
// User 1 should be able to view their own root folder's quota info, but not see the form to change it
86+
$this->resetAll();
87+
$this->dispatchUrI('/sizequota/config/folder?folderId='.$user1->getFolderId(), $user1);
88+
$this->assertController('config');
89+
$this->assertAction('folder');
90+
$this->assertQueryContentRegex('span#hUsedSpaceValue', '/^0.0 KB$/');
91+
$this->assertQueryContentRegex('span#hQuotaValue', '/^Unlimited$/');
92+
$this->assertQueryContentRegex('span#hFreeSpaceValue', '/^$/');
93+
$this->assertQueryContentRegex('span#quotaValue', '/^$/');
94+
$this->assertQueryContentRegex('span#usedSpaceValue', '/^0$/');
95+
$this->assertNotQuery('form.quotaConfigForm');
96+
$this->assertNotQuery('input[type="submit"][name="submitQuota"]');
97+
98+
// Admin user should be able to view user 1's quota info, and also see the form to change it
99+
$this->resetAll();
100+
$this->dispatchUrI('/sizequota/config/folder?folderId='.$user1->getFolderId(), $adminUser);
101+
$this->assertController('config');
102+
$this->assertAction('folder');
103+
$this->assertQueryContentRegex('span#hUsedSpaceValue', '/^0.0 KB$/');
104+
$this->assertQueryContentRegex('span#hQuotaValue', '/^Unlimited$/');
105+
$this->assertQueryContentRegex('span#hFreeSpaceValue', '/^$/');
106+
$this->assertQueryContentRegex('span#quotaValue', '/^$/');
107+
$this->assertQueryContentRegex('span#usedSpaceValue', '/^0$/');
108+
$this->assertQuery('form.quotaConfigForm');
109+
$this->assertQuery('input[type="submit"][name="submitQuota"]');
110+
111+
$modelLoader = new MIDAS_ModelLoader();
112+
$folderQuotaModel = $modelLoader->loadModel('FolderQuota', 'sizequota');
113+
114+
// User 1 should not be able to change their own quota
115+
$this->resetAll();
116+
$this->dispatchUrI('/sizequota/config/foldersubmit?quota=1234&usedefault='.MIDAS_USE_SPECIFIC_QUOTA.
117+
'&folderId='.$user1->getFolderId(), $user1, true);
118+
$this->assertEquals($folderQuotaModel->getUserQuota($user1), '');
119+
120+
// Admin user should be able to change quota for a user
121+
$this->resetAll();
122+
$this->dispatchUrI('/sizequota/config/foldersubmit?quota=1234&usedefault='.MIDAS_USE_SPECIFIC_QUOTA.
123+
'&folderId='.$user1->getFolderId(), $adminUser);
124+
$this->assertEquals($folderQuotaModel->getUserQuota($user1), '1234');
125+
126+
$this->resetAll();
127+
$this->dispatchUrI('/sizequota/config/foldersubmit?quota=1234&usedefault='.MIDAS_USE_DEFAULT_QUOTA.
128+
'&folderId='.$user1->getFolderId(), $adminUser);
129+
$this->assertEquals($folderQuotaModel->getUserQuota($user1), '');
130+
131+
$commFile = $this->loadData('Community', 'default');
132+
$comm = $this->Community->load($commFile[0]->getKey());
133+
134+
// User 1 should not be able to see community quota (no privileges on the root folder)
135+
$this->resetAll();
136+
$this->dispatchUrI('/sizequota/config/folder?folderId='.$comm->getFolderId(), $user1, true);
137+
138+
// Admin user should be able to see community quota
139+
$this->resetAll();
140+
$this->dispatchUrI('/sizequota/config/folder?folderId='.$comm->getFolderId(), $adminUser);
141+
$this->assertController('config');
142+
$this->assertAction('folder');
143+
$this->assertQueryContentRegex('span#hUsedSpaceValue', '/^0.0 KB$/');
144+
$this->assertQueryContentRegex('span#hQuotaValue', '/^9.8 KB$/');
145+
$this->assertQueryContentRegex('span#hFreeSpaceValue', '/^9.8 KB$/');
146+
$this->assertQueryContentRegex('span#quotaValue', '/^10000$/');
147+
$this->assertQueryContentRegex('span#usedSpaceValue', '/^$/');
148+
$this->assertQuery('form.quotaConfigForm');
149+
$this->assertQuery('input[type="submit"][name="submitQuota"]');
150+
151+
// Admin should be able to set new community quota
152+
$this->resetAll();
153+
$this->dispatchUrI('/sizequota/config/foldersubmit?quota=&usedefault='.MIDAS_USE_SPECIFIC_QUOTA.
154+
'&folderId='.$comm->getFolderId(), $adminUser);
155+
$this->assertEquals($folderQuotaModel->getCommunityQuota($comm), '');
156+
157+
$this->resetAll();
158+
$this->dispatchUrI('/sizequota/config/foldersubmit?quota=&usedefault='.MIDAS_USE_DEFAULT_QUOTA.
159+
'&folderId='.$comm->getFolderId(), $adminUser);
160+
$this->assertEquals($folderQuotaModel->getCommunityQuota($comm), '10000');
161+
}
162+
163+
/** Test the ajax getFreeSpace() call */
164+
public function testGetFreeSpace()
165+
{
166+
// Set some defaults
167+
$this->Setting->setConfig('defaultuserquota', '', 'sizequota');
168+
$this->Setting->setConfig('defaultcommunityquota', '10000', 'sizequota');
169+
170+
$usersFile = $this->loadData('User', 'default');
171+
$user1 = $this->User->load($usersFile[0]->getKey());
172+
$adminUser = $this->User->load($usersFile[2]->getKey());
173+
174+
$commFile = $this->loadData('Community', 'default');
175+
$comm = $this->Community->load($commFile[0]->getKey());
176+
177+
// Exception if no folder id is set
178+
$this->resetAll();
179+
$this->dispatchUrI('/sizequota/config/getfreespace', $adminUser);
180+
$resp = JsonComponent::decode($this->getBody());
181+
$this->assertTrue($resp['status'] == false);
182+
$this->assertEquals($resp['message'], 'Missing folderId parameter');
183+
184+
// Exception if invalid folder id is set
185+
$this->resetAll();
186+
$this->dispatchUrI('/sizequota/config/getfreespace?folderId=foo', $adminUser);
187+
$resp = JsonComponent::decode($this->getBody());
188+
$this->assertTrue($resp['status'] == false);
189+
$this->assertEquals($resp['message'], 'Invalid folder');
190+
191+
// Exception if no read privileges
192+
$this->resetAll();
193+
$this->dispatchUrI('/sizequota/config/getfreespace?folderId='.$user1->getFolderId(), null);
194+
$resp = JsonComponent::decode($this->getBody());
195+
$this->assertTrue($resp['status'] == false);
196+
$this->assertEquals($resp['message'], 'Invalid policy');
197+
198+
// User with read privileges should be able to get free space
199+
$this->resetAll();
200+
$this->dispatchUrI('/sizequota/config/getfreespace?folderId='.$user1->getFolderId(), $user1);
201+
$resp = JsonComponent::decode($this->getBody());
202+
$this->assertTrue($resp['status'] == true);
203+
$this->assertEquals($resp['freeSpace'], '');
204+
$this->assertEquals($resp['hFreeSpace'], 'Unlimited');
205+
206+
// This should also work on non-root folders
207+
$this->resetAll();
208+
$this->dispatchUrI('/sizequota/config/getfreespace?folderId='.$user1->getPublicfolderId(), $user1);
209+
$resp = JsonComponent::decode($this->getBody());
210+
$this->assertTrue($resp['status'] == true);
211+
$this->assertEquals($resp['freeSpace'], '');
212+
$this->assertEquals($resp['hFreeSpace'], 'Unlimited');
213+
214+
// Should also work for community folders
215+
$this->resetAll();
216+
$this->dispatchUrI('/sizequota/config/getfreespace?folderId='.$comm->getPublicfolderId(), $adminUser);
217+
$resp = JsonComponent::decode($this->getBody());
218+
$this->assertTrue($resp['status'] == true);
219+
$this->assertEquals($resp['freeSpace'], '10000');
220+
$this->assertEquals($resp['hFreeSpace'], '9.8 KB');
221+
}
222+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<dataset>
3+
<sizequota_folderquota folderquota_id="1000" folder_id="55182" quota="" />
4+
</dataset>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
foo;bar

0 commit comments

Comments
 (0)