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

Commit ea02275

Browse files
author
Michael Grauer
committed
ENH: refs #953. Generalized invalid case testing; added group +/- and tests.
1 parent 22f19ec commit ea02275

File tree

2 files changed

+263
-58
lines changed

2 files changed

+263
-58
lines changed

modules/api/controllers/components/ApiComponent.php

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2905,8 +2905,9 @@ function metadataQualifiersList($args)
29052905
/**
29062906
* helper function to validate args of methods for adding or removing
29072907
* users from groups.
2908-
* @param type $args
2909-
* @return type
2908+
* @param group_id the group to add the user to
2909+
* @param user_id the user to add to the group
2910+
* @return an array of (groupModel, groupDao, groupUserDao)
29102911
*/
29112912
protected function _validateGroupUserChangeParams($args)
29122913
{
@@ -2948,6 +2949,7 @@ protected function _validateGroupUserChangeParams($args)
29482949
* admin privileges on the community associated with the group.
29492950
* @param group_id the group to add the user to
29502951
* @param user_id the user to add to the group
2952+
* @return success = true on success.
29512953
*/
29522954
function groupAddUser($args)
29532955
{
@@ -2961,6 +2963,7 @@ function groupAddUser($args)
29612963
* admin privileges on the community associated with the group.
29622964
* @param group_id the group to remove the user from
29632965
* @param user_id the user to remove from the group
2966+
* @return success = true on success.
29642967
*/
29652968
function groupRemoveUser($args)
29662969
{
@@ -2970,4 +2973,75 @@ function groupRemoveUser($args)
29702973
}
29712974

29722975

2976+
2977+
/**
2978+
* add a group associated with a community, requires admin privileges on the
2979+
* community.
2980+
* @param community_id the id of the community the group will associate with
2981+
* @param name the name of the new group
2982+
* @return group_id of the newly created group on success.
2983+
*/
2984+
function groupAdd($args)
2985+
{
2986+
$this->_validateParams($args, array('community_id', 'name'));
2987+
2988+
$userDao = $this->_getUser($args);
2989+
if(!$userDao)
2990+
{
2991+
throw new Exception('You must be logged in to add group', MIDAS_INVALID_POLICY);
2992+
}
2993+
2994+
$communityModel = MidasLoader::loadModel('Community');
2995+
$communityId = $args['community_id'];
2996+
$community = $communityModel->load($communityId);
2997+
if($community == false)
2998+
{
2999+
throw new Exception('This community does not exist', MIDAS_INVALID_PARAMETER);
3000+
}
3001+
if(!$communityModel->policyCheck($community, $userDao, MIDAS_POLICY_ADMIN))
3002+
{
3003+
throw new Zend_Exception("Community Admin permissions required.", MIDAS_INVALID_POLICY);
3004+
}
3005+
3006+
$name = $args['name'];
3007+
$groupModel = MidasLoader::loadModel('Group');
3008+
$group = $groupModel->createGroup($community, $name);
3009+
3010+
return array('group_id' => $group->getGroupId());
3011+
}
3012+
3013+
/**
3014+
* remove a group associated with a community, requires admin privileges on the
3015+
* community.
3016+
* @param group_id the id of the group to be removed
3017+
* @return success = true on success.
3018+
*/
3019+
function groupRemove($args)
3020+
{
3021+
$this->_validateParams($args, array('group_id'));
3022+
3023+
$userDao = $this->_getUser($args);
3024+
if(!$userDao)
3025+
{
3026+
throw new Exception('You must be logged in to remove a group', MIDAS_INVALID_POLICY);
3027+
}
3028+
3029+
$groupId = $args['group_id'];
3030+
$groupModel = MidasLoader::loadModel('Group');
3031+
$group = $groupModel->load($groupId);
3032+
if($group == false)
3033+
{
3034+
throw new Exception('This group does not exist', MIDAS_INVALID_PARAMETER);
3035+
}
3036+
3037+
$communityModel = MidasLoader::loadModel('Community');
3038+
if(!$communityModel->policyCheck($group->getCommunity(), $userDao, MIDAS_POLICY_ADMIN))
3039+
{
3040+
throw new Zend_Exception("Community Admin permissions required.", MIDAS_INVALID_POLICY);
3041+
}
3042+
3043+
$groupModel->delete($group);
3044+
return array('success' => 'true');
3045+
}
3046+
29733047
} // end class

modules/api/tests/controllers/ApiCallGroupMethodsTest.php

Lines changed: 187 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,111 @@ public function setUp()
2727
parent::setUp();
2828
}
2929

30+
/**
31+
* helper function to test simple invalid cases:
32+
* will test all invalid users sending in all required valid params
33+
* will also test all combinations of invalid params with a valid user
34+
* for each required param
35+
* @param type $method
36+
* @param type $validUser
37+
* @param type $invalidUsers
38+
* @param type $requiredParams
39+
*/
40+
protected function exerciseInvalidCases($method, $validUser, $invalidUsers, $requiredParams)
41+
{
42+
// test all invalid users with valid params
43+
foreach($invalidUsers as $invalidUser)
44+
{
45+
$this->resetAll();
46+
if($invalidUser != null)
47+
{
48+
$this->params['token'] = $this->_loginAsUser($invalidUser);
49+
}
50+
$this->params['method'] = $method;
51+
foreach($requiredParams as $requiredParam)
52+
{
53+
$this->params[$requiredParam['name']] = $requiredParam['valid'];
54+
}
55+
$resp = $this->_callJsonApi();
56+
$this->_assertStatusFail($resp, MIDAS_INVALID_POLICY);
57+
}
58+
59+
// test valid user with all combinations of missing/invalid/valid params
60+
// will not test a case of valid user and all valid params
61+
62+
$numParams = sizeof($requiredParams);
63+
// create an int array that is initially all 0
64+
$requiredParamStates = array_fill(0, $numParams, 0);
65+
$allTwosSum = 2 * $numParams;
66+
67+
while(array_sum($requiredParamStates) < $allTwosSum)
68+
{
69+
$this->resetAll();
70+
$this->params['token'] = $this->_loginAsUser($validUser);
71+
$this->params['method'] = $method;
72+
$skipTestCase = false;
73+
foreach($requiredParams as $ind => $requiredParam)
74+
{
75+
// find the state corresponding to this param
76+
$state = $requiredParamStates[$ind];
77+
// 0s mean the param is missing (not sent)
78+
if($state == 1)
79+
{
80+
// 1s mean an invalid form of the param is sent
81+
if(!array_key_exists('invalid', $requiredParam))
82+
{
83+
// some params may not have an invalid form
84+
// skip this test case as it would repeat the case of the missing param
85+
$skipTestCase = true;
86+
break;
87+
}
88+
$this->params[$requiredParam['name']] = $requiredParam['invalid'];
89+
}
90+
elseif($state == 2)
91+
{
92+
// 2s mean a valid form of the param is sent
93+
$this->params[$requiredParam['name']] = $requiredParam['valid'];
94+
}
95+
elseif($state < 0 || $state > 2)
96+
{
97+
throw new Exception("left most param state is invalid value: ".$state);
98+
}
99+
}
100+
if(!$skipTestCase)
101+
{
102+
$resp = $this->_callJsonApi();
103+
$this->_assertStatusFail($resp, MIDAS_INVALID_PARAMETER);
104+
}
105+
106+
// now increment the parameter states
107+
// add 1 to the right most value
108+
$incrementIndex = $numParams - 1;
109+
$rightMost = $requiredParamStates[$incrementIndex];
110+
$rightMost += 1;
111+
$requiredParamStates[$incrementIndex] = $rightMost;
112+
while($rightMost == 3)
113+
{
114+
// if the right most goes to 3, set it to 0
115+
// and repeat the process one index to the left, stop moving
116+
// to the left when the last increment doesn't go to 3,
117+
// i.e. there are no more carry bits
118+
$rightMost = 0;
119+
$requiredParamStates[$incrementIndex] = $rightMost;
120+
if($incrementIndex > 0)
121+
{
122+
$incrementIndex -= 1;
123+
$rightMost = $requiredParamStates[$incrementIndex];
124+
$rightMost += 1;
125+
$requiredParamStates[$incrementIndex] = $rightMost;
126+
}
127+
else
128+
{
129+
throw new Exception("left most param state is 3");
130+
}
131+
}
132+
}
133+
}
134+
30135
/** Test adding and removing a user from a group */
31136
public function testGroupUserAddRemove()
32137
{
@@ -40,71 +145,23 @@ public function testGroupUserAddRemove()
40145
$commMember = $userModel->load('4');
41146
$commModerator = $userModel->load('5');
42147
$commAdmin = $userModel->load('6');
43-
$nonModerators = array($commMember);
44-
$nonAdmins = array($commMember, $commModerator);
45-
$moderators = array($commModerator, $commAdmin);
46148

47149
$validGroupId = '3004';
48150
$invalidGroupId = '-10';
49151
$validUserId = '2';
50152
$invalidUserId = '-10';
51153

52-
// test all the failure cases
154+
// add in an anonymous user to non admins
155+
$invalidUsers = array($commMember, $commModerator, false);
156+
157+
// test all the invalid cases
53158
foreach($methods as $method)
54159
{
55-
// Try anonymously first
56-
$this->resetAll();
57-
$this->params['method'] = $method;
58-
$this->params['group_id'] = $validGroupId;
59-
$this->params['user_id'] = $validUserId;
60-
$resp = $this->_callJsonApi();
61-
$this->_assertStatusFail($resp, MIDAS_INVALID_POLICY);
160+
$requiredParams = array(
161+
array('name' => 'group_id', 'valid' => $validGroupId, 'invalid' => $invalidGroupId),
162+
array('name' => 'user_id', 'valid' => $validUserId, 'invalid' => $invalidUserId));
62163

63-
// missing group_id
64-
$this->resetAll();
65-
$this->params['token'] = $this->_loginAsUser($commAdmin);
66-
$this->params['method'] = $method;
67-
$this->params['user_id'] = $validUserId;
68-
$resp = $this->_callJsonApi();
69-
$this->_assertStatusFail($resp, MIDAS_INVALID_PARAMETER);
70-
71-
// missing user_id
72-
$this->resetAll();
73-
$this->params['token'] = $this->_loginAsUser($commAdmin);
74-
$this->params['method'] = $method;
75-
$this->params['group_id'] = $validGroupId;
76-
$resp = $this->_callJsonApi();
77-
$this->_assertStatusFail($resp, MIDAS_INVALID_PARAMETER);
78-
79-
// an invalid group
80-
$this->resetAll();
81-
$this->params['token'] = $this->_loginAsUser($commAdmin);
82-
$this->params['method'] = $method;
83-
$this->params['group_id'] = $invalidGroupId;
84-
$this->params['user_id'] = $validUserId;
85-
$resp = $this->_callJsonApi();
86-
$this->_assertStatusFail($resp, MIDAS_INVALID_PARAMETER);
87-
88-
// an invalid user
89-
$this->resetAll();
90-
$this->params['token'] = $this->_loginAsUser($commAdmin);
91-
$this->params['method'] = $method;
92-
$this->params['group_id'] = $validGroupId;
93-
$this->params['user_id'] = $invalidUserId;
94-
$resp = $this->_callJsonApi();
95-
$this->_assertStatusFail($resp, MIDAS_INVALID_PARAMETER);
96-
97-
// as a non admin
98-
foreach($nonAdmins as $nonAdmin)
99-
{
100-
$this->resetAll();
101-
$this->params['token'] = $this->_loginAsUser($nonAdmin);
102-
$this->params['method'] = $method;
103-
$this->params['group_id'] = $validGroupId;
104-
$this->params['user_id'] = $validUserId;
105-
$resp = $this->_callJsonApi();
106-
$this->_assertStatusFail($resp, MIDAS_INVALID_POLICY);
107-
}
164+
$this->exerciseInvalidCases($method, $commAdmin, $invalidUsers, $requiredParams);
108165
}
109166

110167
// ensure the user isn't already in the group
@@ -137,5 +194,79 @@ public function testGroupUserAddRemove()
137194
$this->assertFalse($groupModel->userInGroup($changedUser, $group), "This user is not expected to be in the group");
138195
}
139196

197+
/** Test adding and removing a group */
198+
public function testGroupAddRemove()
199+
{
200+
$validCommunityId = 2001;
201+
$invalidCommunityId = -10;
202+
203+
$communityModel = MidasLoader::loadModel('Community');
204+
$comm2001 = $communityModel->load('2001');
205+
$userModel = MidasLoader::loadModel('User');
206+
$commMember = $userModel->load('4');
207+
$commModerator = $userModel->load('5');
208+
$commAdmin = $userModel->load('6');
209+
210+
// add in an anonymous user to non admins
211+
$invalidUsers = array($commMember, $commModerator, false);
212+
213+
// group add
214+
215+
$addMethod = "midas.group.add";
216+
$newGroupName = 'new group';
217+
$addMethodRequiredParams = array(
218+
array('name' => 'community_id', 'valid' => $validCommunityId, 'invalid' => $invalidCommunityId),
219+
array('name' => 'name', 'valid' => $newGroupName)); // no invalid name
220+
221+
$this->exerciseInvalidCases($addMethod, $commAdmin, $invalidUsers, $addMethodRequiredParams);
222+
223+
$groupModel = MidasLoader::loadModel('Group');
224+
$existingGroups = $groupModel->findByCommunity($comm2001);
140225

226+
// add a group via the api call
227+
228+
$addedGroupName = 'ApiCallGroupMethodsTest';
229+
$this->resetAll();
230+
$this->params['token'] = $this->_loginAsUser($commAdmin);
231+
$this->params['method'] = $addMethod;
232+
$this->params['community_id'] = $validCommunityId;
233+
$this->params['name'] = $addedGroupName;
234+
$resp = $this->_callJsonApi();
235+
$this->_assertStatusOk($resp);
236+
237+
$addedGroupId = $resp->data->group_id;
238+
// check that the group didn't already exist for the community
239+
foreach($existingGroups as $existingGroup)
240+
{
241+
$this->assertNotEquals($addedGroupId, $existingGroup->getGroupId(), 'added group has the same id as an existing group');
242+
}
243+
$addedGroup = $groupModel->load($addedGroupId);
244+
// check that the added group has the correct values
245+
$this->assertEquals($addedGroup->getCommunityId(), $validCommunityId, 'added group has incorrect community id');
246+
$this->assertEquals($addedGroup->getName(), $addedGroupName, 'added group has incorrect community id');
247+
248+
// group remove
249+
250+
$invalidGroupId = -10;
251+
$removeMethod = "midas.group.remove";
252+
$removeMethodRequiredParams = array(
253+
array('name' => 'group_id', 'valid' => $addedGroupId, 'invalid' => $invalidGroupId));
254+
255+
$this->exerciseInvalidCases($removeMethod, $commAdmin, $invalidUsers, $removeMethodRequiredParams);
256+
257+
// remove the group via the api call
258+
259+
$this->resetAll();
260+
$this->params['token'] = $this->_loginAsUser($commAdmin);
261+
$this->params['method'] = $removeMethod;
262+
$this->params['group_id'] = $addedGroupId;
263+
$resp = $this->_callJsonApi();
264+
$this->_assertStatusOk($resp);
265+
$success = $resp->data->success;
266+
$this->assertEquals($success, 'true', 'success value should have been true');
267+
268+
// ensure that the group doesn't exist
269+
$addedGroup = $groupModel->load($addedGroupId);
270+
$this->assertFalse($addedGroup, "group should have been removed but remains");
271+
}
141272
}

0 commit comments

Comments
 (0)