groupstest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Tom Needham <tom@owncloud.com>
  6. *
  7. * @copyright Copyright (c) 2015, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Provisioning_API\Tests;
  24. class GroupsTest extends TestCase {
  25. public function testGetGroupAsUser() {
  26. $users = $this->generateUsers(2);
  27. \OC_User::setUserId($users[0]);
  28. $group = $this->getUniqueID();
  29. \OC_Group::createGroup($group);
  30. \OC_Group::addToGroup($users[1], $group);
  31. $result = \OCA\provisioning_api\Groups::getGroup(array(
  32. 'groupid' => $group,
  33. ));
  34. $this->assertInstanceOf('OC_OCS_Result', $result);
  35. $this->assertFalse($result->succeeded());
  36. $this->assertEquals(\OC_API::RESPOND_UNAUTHORISED, $result->getStatusCode());
  37. }
  38. public function testGetGroupAsSubadmin() {
  39. $users = $this->generateUsers(2);
  40. \OC_User::setUserId($users[0]);
  41. $group = $this->getUniqueID();
  42. \OC_Group::createGroup($group);
  43. \OC_Group::addToGroup($users[0], $group);
  44. \OC_Group::addToGroup($users[1], $group);
  45. \OC_SubAdmin::createSubAdmin($users[0], $group);
  46. $result = \OCA\provisioning_api\Groups::getGroup(array(
  47. 'groupid' => $group,
  48. ));
  49. $this->assertInstanceOf('OC_OCS_Result', $result);
  50. $this->assertTrue($result->succeeded());
  51. $this->assertEquals(1, sizeof($result->getData()), 'Asserting the result data array only has the "users" key');
  52. $this->assertArrayHasKey('users', $result->getData());
  53. $resultData = $result->getData();
  54. $resultData = $resultData['users'];
  55. sort($users);
  56. sort($resultData);
  57. $this->assertEquals($users, $resultData);
  58. }
  59. public function testGetGroupAsIrrelevantSubadmin() {
  60. $users = $this->generateUsers(2);
  61. \OC_User::setUserId($users[0]);
  62. $group = $this->getUniqueID();
  63. \OC_Group::createGroup($group);
  64. $group2 = $this->getUniqueID();
  65. \OC_Group::createGroup($group2);
  66. \OC_Group::addToGroup($users[1], $group);
  67. \OC_Group::addToGroup($users[0], $group2);
  68. \OC_SubAdmin::createSubAdmin($users[0], $group2);
  69. $result = \OCA\provisioning_api\Groups::getGroup(array(
  70. 'groupid' => $group,
  71. ));
  72. $this->assertInstanceOf('OC_OCS_Result', $result);
  73. $this->assertFalse($result->succeeded());
  74. $this->assertEquals(\OC_API::RESPOND_UNAUTHORISED, $result->getStatusCode());
  75. }
  76. public function testGetGroupAsAdmin() {
  77. $users = $this->generateUsers(2);
  78. \OC_User::setUserId($users[0]);
  79. $group = $this->getUniqueID();
  80. \OC_Group::createGroup($group);
  81. \OC_Group::addToGroup($users[1], $group);
  82. \OC_Group::addToGroup($users[0], 'admin');
  83. $result = \OCA\provisioning_api\Groups::getGroup(array(
  84. 'groupid' => $group,
  85. ));
  86. $this->assertInstanceOf('OC_OCS_Result', $result);
  87. $this->assertTrue($result->succeeded());
  88. $this->assertEquals(array('users' => array($users[1])), $result->getData());
  89. }
  90. public function testGetSubAdminsOfGroup() {
  91. $user1 = $this->generateUsers();
  92. $user2 = $this->generateUsers();
  93. \OC_User::setUserId($user1);
  94. \OC_Group::addToGroup($user1, 'admin');
  95. $group1 = $this->getUniqueID();
  96. \OC_Group::createGroup($group1);
  97. \OC_SubAdmin::createSubAdmin($user2, $group1);
  98. $result = \OCA\provisioning_api\Groups::getSubAdminsOfGroup(array(
  99. 'groupid' => $group1,
  100. ));
  101. $this->assertInstanceOf('OC_OCS_Result', $result);
  102. $this->assertTrue($result->succeeded());
  103. $data = $result->getData();
  104. $this->assertEquals($user2, reset($data));
  105. \OC_Group::deleteGroup($group1);
  106. $user1 = $this->generateUsers();
  107. \OC_User::setUserId($user1);
  108. \OC_Group::addToGroup($user1, 'admin');
  109. $result = \OCA\provisioning_api\Groups::getSubAdminsOfGroup(array(
  110. 'groupid' => $this->getUniqueID(),
  111. ));
  112. $this->assertInstanceOf('OC_OCS_Result', $result);
  113. $this->assertFalse($result->succeeded());
  114. $this->assertEquals(101, $result->getStatusCode());
  115. }
  116. }