Skip to content

Commit a0b4d7a

Browse files
authored
refactor tests 4 (#485)
1 parent a73bae0 commit a0b4d7a

File tree

8 files changed

+95
-81
lines changed

8 files changed

+95
-81
lines changed

CONTRIBUTING.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ The password is always `password`. `tools/docker-dev/web/htpasswd` contains all
6262
Notable users:
6363

6464
- `[email protected]` - admin, PI
65-
- `[email protected]` - not admin, not PI
66-
- `[email protected]` - does not yet have an account
67-
- `[email protected]` - regsitered but not qualified (not a PI or in a PI group)
65+
- `[email protected]` - blank user
66+
- `[email protected]` - normal user in PI group
67+
- `[email protected]` - PI with no group members
68+
- `[email protected]` - user never before seen
6869

6970
### Changes to Dev Environment
7071

phpstan.neon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ parameters:
1919
- '#Method SSHKeyAddTest::addSshKeysGithub\(\) is unused.#'
2020
paths:
2121
- test/functional/SSHKeyAddTest.php
22+
# these functions are called with call_user_func
23+
- messages:
24+
- '#Method PIMemberApproveTest::approveUserByPI\(\) is unused.#'
25+
- '#Method PIMemberApproveTest::approveUserByAdmin\(\) is unused.#'
26+
paths:
27+
- test/functional/PiMemberApproveTest.php
28+
# these functions are called with call_user_func
29+
- messages:
30+
- '#Method PIMemberDenyTest::denyRequestByPI\(\) is unused.#'
31+
- '#Method PIMemberDenyTest::denyRequestByAdmin\(\) is unused.#'
32+
paths:
33+
- test/functional/PiMemberDenyTest.php
2234
# assertTrue(true) makes phpunit shut up about tests doing no assertions
2335
- messages:
2436
- '#Call to method PHPUnit\\Framework\\Assert::assertTrue\(\) with true will always evaluate to true.#'

test/functional/PiMemberApproveTest.php

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,39 @@
88

99
class PIMemberApproveTest extends UnityWebPortalTestCase
1010
{
11-
// two different ways to accept a user into a PI group
12-
public static function approveUserProvider(): TRegxDataProvider
11+
private function approveUserByPI(string $uid, string $gid)
1312
{
14-
return TRegxDataProvider::list(
15-
function ($uid, $gid) {
16-
global $USER;
17-
assert($USER->getPIGroup()->gid === $gid, "signed in user must be the group owner");
18-
http_post(__DIR__ . "/../../webroot/panel/pi.php", [
19-
"form_type" => "userReq",
20-
"action" => "Approve",
21-
"uid" => $uid,
22-
]);
23-
},
24-
function ($uid, $gid) {
25-
http_post(__DIR__ . "/../../webroot/admin/pi-mgmt.php", [
26-
"form_type" => "reqChild",
27-
"action" => "Approve",
28-
"pi" => $gid,
29-
"uid" => $uid,
30-
]);
31-
},
32-
);
13+
global $USER;
14+
assert($USER->getPIGroup()->gid === $gid, "signed in user must be the group owner");
15+
http_post(__DIR__ . "/../../webroot/panel/pi.php", [
16+
"form_type" => "userReq",
17+
"action" => "Approve",
18+
"uid" => $uid,
19+
]);
20+
}
21+
22+
private function approveUserByAdmin(string $uid, string $gid)
23+
{
24+
$this->switchUser("Admin");
25+
try {
26+
http_post(__DIR__ . "/../../webroot/admin/pi-mgmt.php", [
27+
"form_type" => "reqChild",
28+
"action" => "Approve",
29+
"pi" => $gid,
30+
"uid" => $uid,
31+
]);
32+
} finally {
33+
$this->switchBackUser();
34+
}
35+
}
36+
37+
public static function provider(): TRegxDataProvider
38+
{
39+
return TRegxDataProvider::list("approveUserByPI", "approveUserByAdmin");
3340
}
3441

35-
#[DataProvider("approveUserProvider")]
36-
public function testApproveNonexistentRequest($approveUserFunc)
42+
#[DataProvider("provider")]
43+
public function testApproveNonexistentRequest($methodName)
3744
{
3845
global $USER;
3946
$this->switchUser("Blank");
@@ -42,15 +49,15 @@ public function testApproveNonexistentRequest($approveUserFunc)
4249
$piGroup = $USER->getPIGroup();
4350
try {
4451
$this->expectException(Exception::class); // FIXME more specific exception type
45-
$approveUserFunc($uid, $piGroup->gid);
52+
call_user_func([self::class, $methodName], $uid, $piGroup->gid);
4653
} finally {
4754
$this->switchUser("Blank", validate: false);
4855
ensureUserNotInPIGroup($piGroup);
4956
}
5057
}
5158

52-
#[DataProvider("approveUserProvider")]
53-
public function testApproveMember($func)
59+
#[DataProvider("provider")]
60+
public function testApproveMember($methodName)
5461
{
5562
global $USER, $SSO, $LDAP, $SQL, $MAILER, $WEBHOOK;
5663
$this->switchUser("EmptyPIGroupOwner");
@@ -65,7 +72,7 @@ public function testApproveMember($func)
6572

6673
$approve_uid = $SSO["user"];
6774
$this->switchUser("EmptyPIGroupOwner", validate: false);
68-
$func($approve_uid, $gid);
75+
call_user_func([self::class, $methodName], $approve_uid, $gid);
6976
$this->switchUser("Blank", validate: false);
7077

7178
$this->assertFalse($pi_group->requestExists($USER));
Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,59 @@
11
<?php
2-
32
use PHPUnit\Framework\Attributes\DataProvider;
4-
use UnityWebPortal\lib\UnityUser;
5-
use function PHPUnit\Framework\assertEquals;
3+
use TRegx\PhpUnit\DataProviders\DataProvider as TRegxDataProvider;
64

75
class PIMemberDenyTest extends UnityWebPortalTestCase
86
{
9-
static $requestUid;
7+
private function denyRequestByPI(string $uid)
8+
{
9+
http_post(__DIR__ . "/../../webroot/panel/pi.php", [
10+
"form_type" => "userReq",
11+
"action" => "Deny",
12+
"uid" => $uid,
13+
]);
14+
}
1015

11-
public function setUp(): void
16+
private function denyRequestByAdmin(string $uid)
1217
{
13-
parent::setUp();
1418
global $USER;
15-
$this->switchUser("Blank");
16-
self::$requestUid = $USER->uid;
19+
$gid = $USER->getPIGroup()->gid;
20+
$this->switchUser("Admin");
21+
try {
22+
http_post(__DIR__ . "/../../webroot/admin/pi-mgmt.php", [
23+
"form_type" => "reqChild",
24+
"action" => "Deny",
25+
"pi" => $gid,
26+
"uid" => $uid,
27+
]);
28+
} finally {
29+
$this->switchBackUser();
30+
}
1731
}
1832

19-
private function denyUser(string $uid)
33+
public static function provider()
2034
{
21-
http_post(__DIR__ . "/../../webroot/panel/pi.php", [
22-
"form_type" => "userReq",
23-
"action" => "approve",
24-
"uid" => $uid,
25-
]);
35+
return TRegxDataProvider::list("denyRequestByPI", "denyRequestByAdmin");
2636
}
2737

28-
public function testDenyRequest()
38+
#[DataProvider("provider")]
39+
public function testDenyRequest(string $methodName)
2940
{
3041
global $USER, $LDAP, $SQL, $MAILER, $WEBHOOK;
42+
$this->switchUser("Blank");
43+
$requestedUser = $USER;
3144
$this->switchUser("EmptyPIGroupOwner");
3245
$pi = $USER;
3346
$piGroup = $USER->getPIGroup();
34-
$requestedUser = new UnityUser(self::$requestUid, $LDAP, $SQL, $MAILER, $WEBHOOK);
47+
$this->assertEmpty($piGroup->getRequests());
48+
$this->assertEqualsCanonicalizing([$pi->uid], $piGroup->getMemberUIDs());
3549
try {
3650
$piGroup->newUserRequest($requestedUser);
37-
$this->assertFalse($piGroup->memberUIDExists($requestedUser->uid));
38-
39-
$piGroup->denyUser($requestedUser);
51+
$this->assertNotEmpty($piGroup->getRequests());
52+
call_user_func([self::class, $methodName], $requestedUser->uid);
4053
$this->assertEmpty($piGroup->getRequests());
4154
$this->assertEqualsCanonicalizing([$pi->uid], $piGroup->getMemberUIDs());
42-
$this->assertFalse($piGroup->memberUIDExists($requestedUser->uid));
4355
} finally {
44-
$SQL->removeRequest(self::$requestUid, $piGroup->gid);
56+
$SQL->removeRequest($requestedUser->uid, $piGroup->gid);
4557
}
4658
}
4759
}

test/functional/SSHKeyAddTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function testAddSshKeys(string $methodName, int $expectedKeysAdded, array
104104
$numKeysBefore = $this->getKeyCount();
105105
$this->assertEquals(0, $numKeysBefore);
106106
try {
107-
call_user_func([SSHKeyAddTest::class, $methodName], $keys);
107+
call_user_func([self::class, $methodName], $keys);
108108
// $method($keys);
109109
$numKeysAfter = $this->getKeyCount();
110110
$this->assertEquals($expectedKeysAdded, $numKeysAfter - $numKeysBefore);

test/phpunit-bootstrap.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ function ensurePIGroupDoesNotExist()
203203

204204
class UnityWebPortalTestCase extends TestCase
205205
{
206+
private ?string $last_user_nickname = null;
207+
private ?string $current_user_nickname = null;
206208
private array $uid_to_latest_session_id = [];
207209
// FIXME these names are wrong
208210
private static array $UID2ATTRIBUTES = [
@@ -445,6 +447,8 @@ function switchUser(
445447
} else {
446448
session_id($this->uid_to_latest_session_id[$uid]);
447449
}
450+
$this->last_user_nickname = $this->current_user_nickname;
451+
$this->current_user_nickname = $nickname;
448452
// session_start will be called on the first post()
449453
$_SERVER["REMOTE_USER"] = $eppn;
450454
$_SERVER["REMOTE_ADDR"] = "127.0.0.1";
@@ -458,4 +462,9 @@ function switchUser(
458462
$this->validateUser($nickname);
459463
}
460464
}
465+
466+
function switchBackUser(bool $validate = false)
467+
{
468+
$this->switchUser($this->last_user_nickname, validate: $validate);
469+
}
461470
}

tools/docker-dev/identity/bootstrap.ldif

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@ dn: cn=web_admins,dc=unityhpc,dc=test
3737
cn: web_admins
3838
gidnumber: 500
3939
memberuid: user1_org1_test
40-
memberuid: user3_org1_test
41-
memberuid: user4_org1_test
42-
memberuid: user5_org2_test
43-
memberuid: user6_org1_test
44-
memberuid: user7_org1_test
45-
memberuid: user8_org1_test
46-
memberuid: user9_org3_test
47-
memberuid: user10_org1_test
48-
memberuid: user11_org1_test
49-
memberuid: user12_org1_test
50-
memberuid: user13_org1_test
51-
memberuid: user14_org3_test
5240
objectclass: posixGroup
5341
objectclass: top
5442

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
<VirtualHost _default_:80>
2-
32
DocumentRoot /var/www/unity-web-portal/webroot
4-
5-
<Location /panel>
3+
<LocationMatch '^/(panel|admin)'>
64
AuthType Basic
75
AuthName "Unity User Panel"
86
AuthUserFile /etc/apache2/.htpasswd
97
Require valid-user
10-
118
SetEnv givenName "DevFirstname"
129
SetEnv sn "DevLastname"
1310
SetEnv mail "[email protected]"
14-
</Location>
15-
16-
<Location /admin>
17-
AuthType Basic
18-
AuthName "Unity Admin Panel"
19-
AuthUserFile /etc/apache2/.htpasswd
20-
21-
22-
SetEnv givenName "DevFirstname"
23-
SetEnv sn "DevLastname"
24-
SetEnv mail "[email protected]"
25-
</Location>
26-
11+
</LocationMatch>
2712
</VirtualHost>

0 commit comments

Comments
 (0)