Skip to content

Commit e614cdc

Browse files
authored
add tests for approving, denying PI membership requests (#198)
* add tests for approving, denying PI membership requests * use uids instead of objects for assertions * add tests for removing group members * unityLDAP use its own getEntry method * write test for PI group disband (requires changes in phpopenldaper) * remove print debugging * add comment * remove checks on write success * add phpopenldaper as a submodule * make sure github actions does recursive checkout * remove junk submodule * update submodule * phpcs:disable not needed in test/ * new exception type * no more delete return success * update submodule * ignore phpunit result cache * PI disbanding has been removed * revert redirect changes * remove old submodule * revert gitignore change * revert autoload change
1 parent 2e93b8f commit e614cdc

File tree

10 files changed

+214
-17
lines changed

10 files changed

+214
-17
lines changed

resources/lib/UnityGroup.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ public function denyGroup($operator = null, $send_mail = true)
218218
// // now we delete the ldap entry
219219
// $ldapPiGroupEntry = $this->getLDAPPiGroup();
220220
// if ($ldapPiGroupEntry->exists()) {
221-
// ldapPiGroupEntry->delete();
222-
221+
// $ldapPiGroupEntry->delete();
223222
// $this->REDIS->removeCacheArray("sorted_groups", "", $this->getPIUID());
224223
// foreach ($users as $user) {
225224
// $this->REDIS->removeCacheArray($user->getUID(), "groups", $this->getPIUID());

resources/lib/UnityLDAP.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,28 +312,24 @@ public function getAllOrgGroups($UnitySQL, $UnityMailer, $UnityRedis, $UnityWebh
312312
public function getUserEntry($uid)
313313
{
314314
$uid = ldap_escape($uid, LDAP_ESCAPE_DN);
315-
$ldap_entry = new LDAPEntry($this->getConn(), unityLDAP::RDN . "=$uid," . $this->STR_USEROU);
316-
return $ldap_entry;
315+
return $this->getEntry(unityLDAP::RDN . "=$uid," . $this->STR_USEROU);
317316
}
318317

319318
public function getGroupEntry($gid)
320319
{
321320
$uid = ldap_escape($gid, LDAP_ESCAPE_DN);
322-
$ldap_entry = new LDAPEntry($this->getConn(), unityLDAP::RDN . "=$gid," . $this->STR_GROUPOU);
323-
return $ldap_entry;
321+
return $this->getEntry(unityLDAP::RDN . "=$gid," . $this->STR_GROUPOU);
324322
}
325323

326324
public function getPIGroupEntry($gid)
327325
{
328326
$uid = ldap_escape($gid, LDAP_ESCAPE_DN);
329-
$ldap_entry = new LDAPEntry($this->getConn(), unityLDAP::RDN . "=$gid," . $this->STR_PIGROUPOU);
330-
return $ldap_entry;
327+
return $this->getEntry(unityLDAP::RDN . "=$gid," . $this->STR_PIGROUPOU);
331328
}
332329

333330
public function getOrgGroupEntry($gid)
334331
{
335332
$uid = ldap_escape($gid, LDAP_ESCAPE_DN);
336-
$ldap_entry = new LDAPEntry($this->getConn(), unityLDAP::RDN . "=$gid," . $this->STR_ORGGROUPOU);
337-
return $ldap_entry;
333+
return $this->getEntry(unityLDAP::RDN . "=$gid," . $this->STR_ORGGROUPOU);
338334
}
339335
}

test/functional/LoginShellSetTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ public function tearDown(): void
2323
public static function getShells()
2424
{
2525
global $HTTP_HEADER_TEST_INPUTS;
26-
// phpcs:disable
2726
return [["/bin/bash"]] + array_map(function($x){return [$x];}, $HTTP_HEADER_TEST_INPUTS);
28-
// phpcs:enable
2927
}
3028

3129
private function isShellValid(string $shell)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use PHPUnit\Framework\Attributes\DataProvider;
5+
use UnityWebPortal\lib\UnityUser;
6+
7+
class PiMemberApproveTest extends TestCase {
8+
static $requestUid;
9+
static $noRequestUid;
10+
11+
public static function setUpBeforeClass(): void{
12+
global $USER;
13+
switchUser(...getNormalUser());
14+
self::$requestUid = $USER->getUID();
15+
switchUser(...getNormalUser2());
16+
self::$noRequestUid = $USER->getUID();
17+
}
18+
19+
private function approveUser(string $uid)
20+
{
21+
post(
22+
__DIR__ . "/../../webroot/panel/pi.php",
23+
["form_type" => "userReq", "action" => "approve", "uid" => $uid]
24+
);
25+
}
26+
27+
public function testApproveRequest()
28+
{
29+
global $USER, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK;
30+
switchUser(...getUserIsPIHasNoMembersNoMemberRequests());
31+
$pi = $USER;
32+
$piGroup = $USER->getPIGroup();
33+
$this->assertTrue($piGroup->exists());
34+
$this->assertEquals([$pi->getUID()], $piGroup->getGroupMemberUIDs());
35+
$this->assertEmpty($piGroup->getRequests());
36+
$requestedUser = new UnityUser(self::$requestUid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
37+
try {
38+
$piGroup->newUserRequest($requestedUser);
39+
$this->assertFalse($piGroup->userExists($requestedUser));
40+
41+
$piGroup->approveUser($requestedUser);
42+
$this->assertEmpty($piGroup->getRequests());
43+
44+
$this->assertEquals([$pi->getUID(), self::$requestUid], $piGroup->getGroupMemberUIDs());
45+
$this->assertTrue($piGroup->userExists($requestedUser));
46+
} finally {
47+
$piGroup->removeUser($requestedUser);
48+
$SQL->removeRequest(self::$requestUid, $piGroup->getPIUID());
49+
}
50+
}
51+
52+
public function testApproveNonexistentRequest()
53+
{
54+
global $USER, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK;
55+
switchUser(...getUserIsPIHasNoMembersNoMemberRequests());
56+
$pi = $USER;
57+
$piGroup = $USER->getPIGroup();
58+
$this->assertTrue($piGroup->exists());
59+
$this->assertEquals([$pi->getUID()], $piGroup->getGroupMemberUIDs());
60+
$this->assertEmpty($piGroup->getRequests());
61+
62+
$notRequestedUser = new UnityUser(self::$noRequestUid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
63+
$this->assertFalse($piGroup->userExists($notRequestedUser));
64+
$this->assertEmpty($piGroup->getRequests());
65+
66+
try {
67+
$piGroup->approveUser($notRequestedUser);
68+
$this->assertEquals([$pi->getUID()], $piGroup->getGroupMemberUIDs());
69+
$this->assertFalse($piGroup->userExists($notRequestedUser));
70+
} finally {
71+
$piGroup->removeUser($notRequestedUser);
72+
}
73+
}
74+
}

test/functional/PiMemberDenyTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use PHPUnit\Framework\Attributes\DataProvider;
5+
use UnityWebPortal\lib\UnityUser;
6+
7+
class PiMemberDenyTest extends TestCase {
8+
static $requestUid;
9+
10+
public static function setUpBeforeClass(): void{
11+
global $USER;
12+
switchUser(...getNormalUser());
13+
self::$requestUid = $USER->getUID();
14+
}
15+
16+
private function denyUser(string $uid)
17+
{
18+
post(
19+
__DIR__ . "/../../webroot/panel/pi.php",
20+
["form_type" => "userReq", "action" => "approve", "uid" => $uid]
21+
);
22+
}
23+
24+
public function testDenyRequest()
25+
{
26+
global $USER, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK;
27+
switchUser(...getUserIsPIHasNoMembersNoMemberRequests());
28+
$pi = $USER;
29+
$piGroup = $USER->getPIGroup();
30+
$this->assertTrue($piGroup->exists());
31+
$this->assertEquals([$pi->getUID()], $piGroup->getGroupMemberUIDs());
32+
$this->assertEmpty($piGroup->getRequests());
33+
$requestedUser = new UnityUser(self::$requestUid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
34+
try {
35+
$piGroup->newUserRequest($requestedUser);
36+
$this->assertFalse($piGroup->userExists($requestedUser));
37+
38+
$piGroup->denyUser($requestedUser);
39+
$this->assertEmpty($piGroup->getRequests());
40+
$this->assertEquals([$pi->getUID()], $piGroup->getGroupMemberUIDs());
41+
$this->assertFalse($piGroup->userExists($requestedUser));
42+
} finally {
43+
$SQL->removeRequest(self::$requestUid, $piGroup->getPIUID());
44+
}
45+
}
46+
}

test/functional/PiRemoveUserTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use PHPUnit\Framework\Attributes\DataProvider;
5+
use UnityWebPortal\lib\UnityUser;
6+
7+
class PiRemoveUserTest extends TestCase {
8+
private function removeUser(string $uid)
9+
{
10+
post(
11+
__DIR__ . "/../../webroot/panel/pi.php",
12+
["form_name" => "remUser", "uid" => $uid]
13+
);
14+
}
15+
16+
public function testRemoveUser()
17+
{
18+
global $USER, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK;
19+
switchUser(...getUserIsPIHasAtLeastOneMember());
20+
$pi = $USER;
21+
$piUid = $USER->getUID();
22+
$piGroup = $USER->getPIGroup();
23+
$this->assertTrue($piGroup->exists());
24+
$memberUIDs = $piGroup->getGroupMemberUIDs();
25+
// the 0th member of the PI group is the PI
26+
$this->assertGreaterThan(1, count($memberUIDs));
27+
// the ordering of the uids in getGroupMemberUIDs is different each time
28+
// use a linear search to find a user who is not the PI
29+
$memberToDelete = null;
30+
foreach ($memberUIDs as $uid) {
31+
if ($uid != $piUid) {
32+
$memberToDelete = new UnityUser($uid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
33+
break;
34+
}
35+
}
36+
$this->assertNotEquals($pi->getUID(), $memberToDelete->getUID());
37+
$this->assertTrue($piGroup->userExists($memberToDelete));
38+
try {
39+
$this->removeUser($memberToDelete->getUID());
40+
$this->assertFalse($piGroup->userExists($memberToDelete));
41+
} finally {
42+
if (!$piGroup->userExists($memberToDelete)) {
43+
$piGroup->newUserRequest($memberToDelete);
44+
$piGroup->approveUser($memberToDelete);
45+
}
46+
}
47+
}
48+
49+
public function testRemovePIFromTheirOwnGroup()
50+
{
51+
global $USER, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK;
52+
switchUser(...getUserIsPIHasAtLeastOneMember());
53+
$pi = $USER;
54+
$piGroup = $USER->getPIGroup();
55+
$this->assertTrue($piGroup->exists());
56+
$this->assertTrue($piGroup->userExists($pi));
57+
$this->expectException(Exception::class);
58+
try {
59+
$this->removeUser($pi->getUID());
60+
$this->assertTrue($piGroup->userExists($pi));
61+
} finally {
62+
if (!$piGroup->userExists($pi)) {
63+
$piGroup->newUserRequest($pi);
64+
$piGroup->approveUser($pi);
65+
}
66+
}
67+
}
68+
}

test/phpunit-bootstrap.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ function getNormalUser()
106106
return ["[email protected]", "foo", "bar", "[email protected]"];
107107
}
108108

109+
function getNormalUser2()
110+
{
111+
return ["[email protected]", "foo", "bar", "[email protected]"];
112+
}
113+
109114
function getUserHasNotRequestedAccountDeletionHasGroup()
110115
{
111116
return ["[email protected]", "foo", "bar", "[email protected]"];
@@ -136,6 +141,16 @@ function getUserWithOneKey()
136141
return ["[email protected]", "foo", "bar", "[email protected]"];
137142
}
138143

144+
function getUserIsPIHasNoMembersNoMemberRequests()
145+
{
146+
return ["[email protected]", "foo", "bar", "[email protected]"];
147+
}
148+
149+
function getUserIsPIHasAtLeastOneMember()
150+
{
151+
return ["[email protected]", "foo", "bar", "[email protected]"];
152+
}
153+
139154
function getNonExistentUser()
140155
{
141156
return ["[email protected]", "foo", "bar", "[email protected]"];

test/unit/AjaxSshValidateTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ public static function providerTestSshValidate()
1212
// sanity check only, see UnitySiteTest for more comprehensive test cases
1313
return [
1414
[false, "foobar"],
15-
// phpcs:disable
1615
[true, "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a"],
17-
// phpcs:enable
1816
];
1917
}
2018

test/unit/UnityGithubTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ public static function providerTestGetGithubKeys()
1717
# user with no keys
1818
["sheldor1510", []],
1919
# user with 1 key
20-
//phpcs:disable
2120
["simonLeary42", ["ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDGRl6JWPj+Gq2Lz9GjYdUl4/unLoFOyfgeiII1CxutpabPByRJbeuonR0zTpn51tZeYuAUOJBOeKt+Lj4i4UDGl6igpdXXSwkBXl7jxfRPwJ6WuTkDx7Z8ynwnqlDV2q089q4OX/b/uuHgsIhIBwrouKsRQaZIqTbwNMfiqQ2zl14V0KMrTPzOiwR6Q+hqSaR5Z29WKE7ff/OWzSC3/0T6avCmcCbQaRPJdVM+QC17B0vl8FzPwRjorMngwZ0cImdQ/0Ww1d12YAL7UWp1c2egfnthKP3MuQZnNF8ixsAk1eIIwTRdiI87BOoorW8NXhxXmhyheRCsFwyP4LJBqyUVoZJ0UYyk0AO4G9EStnfpiz8YXGK+M1G4tUrWgzs1cdjlHtgCWUmITtgabnYCC4141m7n4GZTk2H/lSrJcvAs3JEiwLTj1lzeGgzeSsz/XKsnOJyzjEVr2Jp3iT+J9PbQpfS0SxTCIGgxMqllovv79pfsF/zc+vaxqSShyHW7oyn7hLMHM60LO/IIX1RWGL3rD9ecXx2pXXQ1RhIkVteIi13XkFt+KW00cstFlAd3EHCoY/XorShd2jeID7tpnYlmNfotYUs6IKefvpNC0PWkh5UXFEv3SUfw4Wd8O0DiHfhkrhxn1W/GajqSIlZ5DKgPzFg8EHexv8lSa7WJg0H3YQ=="]]
22-
//phpcs:enable
2321
];
2422
}
2523

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
INSERT INTO `account_deletion_requests` (`id`, `timestamp`, `uid`) VALUES (1, '1970-01-01 00:00:01', 'user4_org1_test');
1+
INSERT INTO `account_deletion_requests` (`id`, `timestamp`, `uid`) VALUES
2+
(1, '1970-01-01 00:00:01', 'user4_org1_test');
3+
4+
-- INSERT INTO `requests` (`id`, `request_for`, `uid`, `timestamp`) VALUES
5+
-- (1, 'pi_user1_org1_test', 'user6_org1_test', '1970-01-01 00:00:01'),
6+
-- (2, 'pi_user1_org1_test', 'user7_org1_test', '1970-01-01 00:00:01');

0 commit comments

Comments
 (0)