Skip to content

Commit 1e05710

Browse files
authored
Fix user init in pi-mgmt.php (#274)
* extend tests * remove extra call to init * change test name * LDAP is singleton
1 parent 4dd0868 commit 1e05710

File tree

3 files changed

+138
-23
lines changed

3 files changed

+138
-23
lines changed

resources/init.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,24 @@
3737
);
3838

3939
// Creates LDAP service
40-
$LDAP = new UnityLDAP(
41-
$CONFIG["ldap"]["uri"],
42-
$CONFIG["ldap"]["user"],
43-
$CONFIG["ldap"]["pass"],
44-
__DIR__ . "/../deployment/custom_user_mappings",
45-
$CONFIG["ldap"]["user_ou"],
46-
$CONFIG["ldap"]["group_ou"],
47-
$CONFIG["ldap"]["pigroup_ou"],
48-
$CONFIG["ldap"]["orggroup_ou"],
49-
$CONFIG["ldap"]["admin_group"],
50-
$CONFIG["ldap"]["user_group"],
51-
$CONFIG["ldap"]["def_user_shell"]
52-
);
40+
if (isset($GLOBALS["ldapconn"])) {
41+
$LDAP = $GLOBALS["ldapconn"];
42+
} else {
43+
$LDAP = new UnityLDAP(
44+
$CONFIG["ldap"]["uri"],
45+
$CONFIG["ldap"]["user"],
46+
$CONFIG["ldap"]["pass"],
47+
__DIR__ . "/../deployment/custom_user_mappings",
48+
$CONFIG["ldap"]["user_ou"],
49+
$CONFIG["ldap"]["group_ou"],
50+
$CONFIG["ldap"]["pigroup_ou"],
51+
$CONFIG["ldap"]["orggroup_ou"],
52+
$CONFIG["ldap"]["admin_group"],
53+
$CONFIG["ldap"]["user_group"],
54+
$CONFIG["ldap"]["def_user_shell"]
55+
);
56+
$GLOBALS["ldapconn"] = $LDAP;
57+
}
5358

5459
// Creates SQL service
5560
$SQL = new UnitySQL(

test/functional/NewUserTest.php

Lines changed: 120 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,43 @@ private function cancelAllRequests()
5050
);
5151
}
5252

53+
private function approveUserByAdmin($gid, $uid)
54+
{
55+
http_post(
56+
__DIR__ . "/../../webroot/admin/pi-mgmt.php",
57+
[
58+
"form_type" => "reqChild",
59+
"action" => "Approve",
60+
"pi" => $gid,
61+
"uid" => $uid,
62+
]
63+
);
64+
}
65+
66+
private function approveUserByPI($uid)
67+
{
68+
http_post(
69+
__DIR__ . "/../../webroot/panel/pi.php",
70+
[
71+
"form_type" => "userReq",
72+
"action" => "Approve",
73+
"uid" => $uid,
74+
]
75+
);
76+
}
77+
78+
private function approveGroup($uid)
79+
{
80+
http_post(
81+
__DIR__ . "/../../webroot/admin/pi-mgmt.php",
82+
[
83+
"form_type" => "req",
84+
"action" => "Approve",
85+
"uid" => $uid,
86+
]
87+
);
88+
}
89+
5390
// delete requests made by that user
5491
// delete user entry
5592
// remove user from org group
@@ -109,13 +146,80 @@ private function ensurePIGroupDoesNotExist()
109146
}
110147
}
111148

112-
public function testCreateUserByJoinGoup()
149+
public function testCreateUserByJoinGoupByPI()
150+
{
151+
global $USER, $SSO, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK;
152+
$pi_user_args = getUserIsPIHasNoMembersNoMemberRequests();
153+
switchUser(...$pi_user_args);
154+
$pi_group = $USER->getPIGroup();
155+
$gid = $pi_group->gid;
156+
$user_to_create_args = getNonExistentUser();
157+
switchUser(...$user_to_create_args);
158+
$this->assertTrue(!$USER->exists());
159+
$newOrg = new UnityOrg($SSO["org"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
160+
$this->assertTrue(!$newOrg->exists());
161+
$this->assertTrue($pi_group->exists());
162+
$this->assertTrue(!$pi_group->userExists($USER));
163+
$this->assertRequestedMembership(false, $gid);
164+
try {
165+
$this->requestGroupMembership($pi_group->gid);
166+
$this->assertRequestedMembership(true, $gid);
167+
168+
// $second_request_failed = false;
169+
// try {
170+
$this->requestGroupMembership($pi_group->gid);
171+
// } catch(Exception) {
172+
// $second_request_failed = true;
173+
// }
174+
// $this->assertTrue($second_request_failed);
175+
$this->assertRequestedMembership(true, $gid);
176+
177+
$this->cancelAllRequests();
178+
$this->assertRequestedMembership(false, $gid);
179+
180+
$this->requestGroupMembership($pi_group->gid);
181+
$this->assertTrue($pi_group->requestExists($USER));
182+
$this->assertRequestedMembership(true, $gid);
183+
184+
$REDIS->flushAll(); // regression test: flush used to break requests
185+
186+
$approve_uid = $SSO["user"];
187+
switchUser(...$pi_user_args);
188+
$this->approveUserByPI($approve_uid);
189+
switchUser(...$user_to_create_args);
190+
191+
$this->assertTrue(!$pi_group->requestExists($USER));
192+
$this->assertRequestedMembership(false, $gid);
193+
$this->assertTrue($pi_group->userExists($USER));
194+
$this->assertTrue($USER->exists());
195+
$this->assertTrue($newOrg->exists());
196+
197+
// $third_request_failed = false;
198+
// try {
199+
$this->requestGroupMembership($pi_group->gid);
200+
// } catch(Exception) {
201+
// $third_request_failed = true;
202+
// }
203+
// $this->assertTrue($third_request_failed);
204+
$this->assertRequestedMembership(false, $gid);
205+
$this->assertTrue(!$pi_group->requestExists($USER));
206+
} finally {
207+
switchUser(...$user_to_create_args);
208+
$this->ensureOrgGroupDoesNotExist();
209+
$this->ensureUserNotInPIGroup($pi_group);
210+
$this->ensureUserDoesNotExist();
211+
}
212+
}
213+
214+
215+
public function testCreateUserByJoinGoupByAdmin()
113216
{
114217
global $USER, $SSO, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK;
115218
switchUser(...getUserIsPIHasNoMembersNoMemberRequests());
116219
$pi_group = $USER->getPIGroup();
117220
$gid = $pi_group->gid;
118-
switchUser(...getNonExistentUser());
221+
$user_to_create_args = getNonExistentUser();
222+
switchUser(...$user_to_create_args);
119223
$this->assertTrue(!$USER->exists());
120224
$newOrg = new UnityOrg($SSO["org"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
121225
$this->assertTrue(!$newOrg->exists());
@@ -144,7 +248,11 @@ public function testCreateUserByJoinGoup()
144248

145249
$REDIS->flushAll(); // regression test: flush used to break requests
146250

147-
$pi_group->approveUser($USER);
251+
$approve_uid = $SSO["user"];
252+
switchUser(...getAdminUser());
253+
$this->approveUserByAdmin($gid, $approve_uid);
254+
switchUser(...$user_to_create_args);
255+
148256
$this->assertTrue(!$pi_group->requestExists($USER));
149257
$this->assertRequestedMembership(false, $gid);
150258
$this->assertTrue($pi_group->userExists($USER));
@@ -161,16 +269,19 @@ public function testCreateUserByJoinGoup()
161269
$this->assertRequestedMembership(false, $gid);
162270
$this->assertTrue(!$pi_group->requestExists($USER));
163271
} finally {
272+
switchUser(...$user_to_create_args);
164273
$this->ensureOrgGroupDoesNotExist();
165274
$this->ensureUserNotInPIGroup($pi_group);
166275
$this->ensureUserDoesNotExist();
167276
}
168277
}
169278

279+
170280
public function testCreateUserByCreateGroup()
171281
{
172282
global $USER, $SSO, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK;
173-
switchuser(...getNonExistentUser());
283+
$user_to_create_args = getNonExistentUser();
284+
switchuser(...$user_to_create_args);
174285
$pi_group = $USER->getPIGroup();
175286
$this->assertTrue(!$USER->exists());
176287
$this->assertTrue(!$pi_group->exists());
@@ -197,7 +308,11 @@ public function testCreateUserByCreateGroup()
197308

198309
$REDIS->flushAll(); // regression test: flush used to break requests
199310

200-
$pi_group->approveGroup();
311+
$approve_uid = $SSO["user"];
312+
switchUser(...getAdminUser());
313+
$this->approveGroup($approve_uid);
314+
switchUser(...$user_to_create_args);
315+
201316
$this->assertRequestedPIGroup(false);
202317
$this->assertTrue($pi_group->exists());
203318
$this->assertTrue($USER->exists());

webroot/admin/pi-mgmt.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@
3131
case "reqChild":
3232
$parent_group = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
3333
if ($_POST["action"] == "Approve") {
34-
// initialize user if not initialized
35-
if (!$form_user->exists()) {
36-
$form_user->init();
37-
}
38-
3934
// approve request button clicked
4035
$parent_group->approveUser($form_user); // Add to group (ldap and slurm)
4136
} elseif ($_POST["action"] == "Deny") {

0 commit comments

Comments
 (0)