|
| 1 | +<?php |
| 2 | +/*========================================================================= |
| 3 | + MIDAS Server |
| 4 | + Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE |
| 5 | + All rights reserved. |
| 6 | + More information http://www.kitware.com |
| 7 | +
|
| 8 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + you may not use this file except in compliance with the License. |
| 10 | + You may obtain a copy of the License at |
| 11 | +
|
| 12 | + http://www.apache.org/licenses/LICENSE-2.0.txt |
| 13 | +
|
| 14 | + Unless required by applicable law or agreed to in writing, software |
| 15 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + See the License for the specific language governing permissions and |
| 18 | + limitations under the License. |
| 19 | +=========================================================================*/ |
| 20 | +require_once BASE_PATH . '/modules/api/tests/controllers/ApiCallMethodsTest.php'; |
| 21 | +/** Tests the functionality of the web API Group methods */ |
| 22 | +class ApiCallGroupMethodsTest extends ApiCallMethodsTest |
| 23 | + { |
| 24 | + /** set up tests */ |
| 25 | + public function setUp() |
| 26 | + { |
| 27 | + parent::setUp(); |
| 28 | + } |
| 29 | + |
| 30 | + /** Test adding and removing a user from a group */ |
| 31 | + public function testGroupUserAddRemove() |
| 32 | + { |
| 33 | + $addMethod = "midas.group.add.user"; |
| 34 | + $removeMethod = "midas.group.remove.user"; |
| 35 | + $methods = array($addMethod, $removeMethod); |
| 36 | + |
| 37 | + $communityModel = MidasLoader::loadModel('Community'); |
| 38 | + $comm2001 = $communityModel->load('2001'); |
| 39 | + $userModel = MidasLoader::loadModel('User'); |
| 40 | + $commMember = $userModel->load('4'); |
| 41 | + $commModerator = $userModel->load('5'); |
| 42 | + $commAdmin = $userModel->load('6'); |
| 43 | + $nonModerators = array($commMember); |
| 44 | + $nonAdmins = array($commMember, $commModerator); |
| 45 | + $moderators = array($commModerator, $commAdmin); |
| 46 | + |
| 47 | + $validGroupId = '3004'; |
| 48 | + $invalidGroupId = '-10'; |
| 49 | + $validUserId = '2'; |
| 50 | + $invalidUserId = '-10'; |
| 51 | + |
| 52 | + // test all the failure cases |
| 53 | + foreach($methods as $method) |
| 54 | + { |
| 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); |
| 62 | + |
| 63 | + // an invalid group |
| 64 | + $this->resetAll(); |
| 65 | + $this->params['token'] = $this->_loginAsUser($commAdmin); |
| 66 | + $this->params['method'] = $method; |
| 67 | + $this->params['group_id'] = $invalidGroupId; |
| 68 | + $this->params['user_id'] = $validUserId; |
| 69 | + $resp = $this->_callJsonApi(); |
| 70 | + $this->_assertStatusFail($resp, MIDAS_INVALID_PARAMETER); |
| 71 | + |
| 72 | + // an invalid user |
| 73 | + $this->resetAll(); |
| 74 | + $this->params['token'] = $this->_loginAsUser($commAdmin); |
| 75 | + $this->params['method'] = $method; |
| 76 | + $this->params['group_id'] = $validGroupId; |
| 77 | + $this->params['user_id'] = $invalidUserId; |
| 78 | + $resp = $this->_callJsonApi(); |
| 79 | + $this->_assertStatusFail($resp, MIDAS_INVALID_PARAMETER); |
| 80 | + |
| 81 | + // as a non admin |
| 82 | + foreach($nonAdmins as $nonAdmin) |
| 83 | + { |
| 84 | + $this->resetAll(); |
| 85 | + $this->params['token'] = $this->_loginAsUser($nonAdmin); |
| 86 | + $this->params['method'] = $method; |
| 87 | + $this->params['group_id'] = $validGroupId; |
| 88 | + $this->params['user_id'] = $validUserId; |
| 89 | + $resp = $this->_callJsonApi(); |
| 90 | + $this->_assertStatusFail($resp, MIDAS_INVALID_POLICY); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // ensure the user isn't already in the group |
| 95 | + $groupModel = MidasLoader::loadModel('Group'); |
| 96 | + $changedUser = $userModel->load($validUserId); |
| 97 | + $group = $groupModel->load($validGroupId); |
| 98 | + $this->assertFalse($groupModel->userInGroup($changedUser, $group), "This user is not expected to be in the group"); |
| 99 | + |
| 100 | + // add the user to the group |
| 101 | + $this->resetAll(); |
| 102 | + $this->params['token'] = $this->_loginAsUser($commAdmin); |
| 103 | + $this->params['method'] = $addMethod; |
| 104 | + $this->params['group_id'] = $validGroupId; |
| 105 | + $this->params['user_id'] = $validUserId; |
| 106 | + $resp = $this->_callJsonApi(); |
| 107 | + $this->_assertStatusOk($resp); |
| 108 | + |
| 109 | + // ensure the user is now in the group |
| 110 | + $this->assertTrue($groupModel->userInGroup($changedUser, $group), "This user is expected to be in the group"); |
| 111 | + |
| 112 | + // remove the user from the group |
| 113 | + $this->resetAll(); |
| 114 | + $this->params['token'] = $this->_loginAsUser($commAdmin); |
| 115 | + $this->params['method'] = $removeMethod; |
| 116 | + $this->params['group_id'] = $validGroupId; |
| 117 | + $this->params['user_id'] = $validUserId; |
| 118 | + $resp = $this->_callJsonApi(); |
| 119 | + $this->_assertStatusOk($resp); |
| 120 | + |
| 121 | + $this->assertFalse($groupModel->userInGroup($changedUser, $group), "This user is not expected to be in the group"); |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + } |
0 commit comments