Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 0 additions & 71 deletions test/functional/CancelRequestTest.php

This file was deleted.

217 changes: 217 additions & 0 deletions test/functional/NewUserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
<?php

use PHPUnit\Framework\TestCase;
use UnityWebPortal\lib\exceptions\PhpUnitNoDieException;
use UnityWebPortal\lib\UnityGroup;

class NewUserTest extends TestCase
{
private function assertNumberGroupRequests(int $x)
{
global $USER, $SQL;
$this->assertEquals($x, count($SQL->getRequestsByUser($USER->getUID())));
}

private function requestGroupCreation()
{
$redirectedOrDied = false;
try {
http_post(
__DIR__ . "/../../webroot/panel/new_account.php",
["new_user_sel" => "pi", "eula" => "agree", "confirm_pi" => "agree"]
);
} catch (\UnityWebPortal\lib\exceptions\PhpUnitNoDieException) {
$redirectedOrDied = true;
}
$this->assertTrue($redirectedOrDied);
}

private function requestGroupMembership(string $gid)
{
$redirectedOrDied = false;
try {
http_post(
__DIR__ . "/../../webroot/panel/new_account.php",
["new_user_sel" => "not_pi", "eula" => "agree", "pi" => $gid]
);
} catch (\UnityWebPortal\lib\exceptions\PhpUnitNoDieException) {
$redirectedOrDied = true;
}
$this->assertTrue($redirectedOrDied);
}

private function cancelAllRequests()
{
$redirectedOrDied = false;
try {
http_post(
__DIR__ . "/../../webroot/panel/new_account.php",
["cancel" => "true"] // value of cancel is arbitrary
);
} catch (\UnityWebPortal\lib\exceptions\PhpUnitNoDieException) {
$redirectedOrDied = true;
}
$this->assertTrue($redirectedOrDied);
}

// delete requests made by that user
// delete user entry
// remove user from org group
// remove user from "all users" group
// does not remove user from PI groups
private function ensureUserDoesNotExist()
{
global $USER, $SQL, $LDAP;
$SQL->deleteRequestsByUser($USER->getUID());
if ($USER->exists()) {
$USER->getLDAPUser()->delete();
assert(!$USER->exists());
}
$org = $USER->getOrgGroup();
if ($org->inOrg($USER)) {
$org->removeUser($USER);
assert(!$org->inOrg($USER));
}
$all_users_group = $LDAP->getUserGroup();
$all_member_uids = $all_users_group->getAttribute("memberuid");
$new_uids = array_diff($all_member_uids, [$USER->getUID()]);
if (in_array($USER->getUID(), $all_member_uids)) {
$all_users_group->setAttribute(
"memberuid",
array_diff($all_member_uids, [$USER->getUID()])
);
$all_users_group->write();
assert(!in_array($USER->getUID(), $all_users_group->getAttribute("memberuid")));
}
}

private function ensureOrgGroupDoesNotExist()
{
global $USER;
$org_group = $USER->getOrgGroup();
if ($org_group->exists()) {
$org_group->getLDAPOrgGroup()->delete();
assert(!$org_group->exists());
}
}

private function ensureUserNotInPIGroup(UnityGroup $pi_group)
{
global $USER;
if ($pi_group->userExists($USER)) {
$pi_group->removeUser($USER);
assert(!$pi_group->userExists($USER));
}
}

private function ensurePIGroupDoesNotExist()
{
global $USER;
if ($USER->getPIGroup()->exists()) {
$USER->getPIGroup()->getLDAPPIGroup()->delete();
assert(!$USER->getPIGroup()->exists());
}
}

public function testCreateUserByJoinGoup()
{
global $USER, $SQL, $LDAP;
switchUser(...getUserIsPIHasNoMembersNoMemberRequests());
$pi_group = $USER->getPIGroup();
switchUser(...getNonExistentUser());
$this->assertTrue(!$USER->exists());
$this->assertTrue(!$USER->getOrgGroup()->exists());
$this->assertTrue($pi_group->exists());
$this->assertTrue(!$pi_group->userExists($USER));
$this->assertNumberGroupRequests(0);
try {
$this->requestGroupMembership($pi_group->getPIUID());
$this->assertNumberGroupRequests(1);

// $second_request_failed = false;
// try {
$this->requestGroupMembership($pi_group->getPIUID());
// } catch(Exception) {
// $second_request_failed = true;
// }
// $this->assertTrue($second_request_failed);
$this->assertNumberGroupRequests(1);

$this->cancelAllRequests();
$this->assertNumberGroupRequests(0);

$this->requestGroupMembership($pi_group->getPIUID());
$this->assertTrue($pi_group->requestExists($USER));
$this->assertNumberGroupRequests(1);

$pi_group->approveUser($USER);
$this->assertTrue(!$pi_group->requestExists($USER));
$this->assertNumberGroupRequests(0);
$this->assertTrue($pi_group->userExists($USER));
$this->assertTrue($USER->exists());
$this->assertTrue($USER->getOrgGroup()->exists());

// $third_request_failed = false;
// try {
$this->requestGroupMembership($pi_group->getPIUID());
// } catch(Exception) {
// $third_request_failed = true;
// }
// $this->assertTrue($third_request_failed);
$this->assertNumberGroupRequests(0);
$this->assertTrue(!$pi_group->requestExists($USER));
} finally {
$this->ensureUserNotInPIGroup($pi_group);
$this->ensureUserDoesNotExist();
$this->ensureOrgGroupDoesNotExist();
}
}

public function testCreateUserByCreateGroup()
{
global $USER, $SQL, $LDAP;
switchuser(...getNonExistentUser());
$pi_group = $USER->getPIGroup();
$this->assertTrue(!$USER->exists());
$this->assertTrue(!$pi_group->exists());
$this->assertTrue(!$USER->getOrgGroup()->exists());
try {
$this->requestGroupCreation();
$this->assertNumberGroupRequests(1);

// $second_request_failed = false;
// try {
$this->requestGroupCreation();
// } catch(Exception) {
// $second_request_failed = true;
// }
// $this->assertTrue($second_request_failed);
$this->assertNumberGroupRequests(1);

$this->cancelAllRequests();
$this->assertNumberGroupRequests(0);

$this->requestGroupCreation();
$this->assertNumberGroupRequests(1);

$pi_group->approveGroup();
$this->assertNumberGroupRequests(0);
$this->assertTrue($pi_group->exists());
$this->assertTrue($USER->exists());
$this->assertTrue($USER->getOrgGroup()->exists());

// $third_request_failed = false;
// try {
$this->requestGroupCreation();
// } catch(Exception) {
// $third_request_failed = true;
// }
// $this->assertTrue($third_request_failed);
$this->assertNumberGroupRequests(0);
} finally {
$this->ensurePIGroupDoesNotExist();
$this->ensureUserDoesNotExist();
$this->ensureOrgGroupDoesNotExist();
}
}
}
7 changes: 1 addition & 6 deletions test/phpunit-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,10 @@ function getUserIsPIHasAtLeastOneMember()

function getNonExistentUser()
{
return ["user2000@org2.test", "foo", "bar", "user2000@org2.test"];
return ["user2001@org998.test", "foo", "bar", "user2001@org998.test"];
}

function getAdminUser()
{
return ["user1@org1.test", "foo", "bar", "user1@org1.test"];
}

function getExistingPI()
{
return "pi_user1005_org3_test";
}
5 changes: 5 additions & 0 deletions tools/docker-dev/sql/bootstrap.sql
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ CREATE TABLE `sitevars` (
`value` varchar(768) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO `sitevars` (`id`, `name`, `value`) VALUES
(0, 'MAX_UID', '0'),
(1, 'MAX_GID', '0'),
(2, 'MAX_PIGID', '0');

--
-- Indexes for dumped tables
--
Expand Down
4 changes: 2 additions & 2 deletions tools/docker-dev/web/htpasswd
Original file line number Diff line number Diff line change
Expand Up @@ -1303,5 +1303,5 @@ user1313@org1.test:$apr1$Rgrex74Z$rgJx6sCnGQN9UVMmhVG2R1
user1322@org14.test:$apr1$Rgrex74Z$rgJx6sCnGQN9UVMmhVG2R1
user1326@org14.test:$apr1$Rgrex74Z$rgJx6sCnGQN9UVMmhVG2R1
user2000@org2.test:$apr1$Rgrex74Z$rgJx6sCnGQN9UVMmhVG2R1
user2001@org1.test:$apr1$Rgrex74Z$rgJx6sCnGQN9UVMmhVG2R1
user2002@org5.test:$apr1$Rgrex74Z$rgJx6sCnGQN9UVMmhVG2R1
user2001@org998.test:$apr1$Rgrex74Z$rgJx6sCnGQN9UVMmhVG2R1
user2002@org999.test:$apr1$Rgrex74Z$rgJx6sCnGQN9UVMmhVG2R1