Skip to content

Commit 94f71aa

Browse files
authored
Testing2 (#190)
* add functional tests * no die * only run unit tests in github * github action phpcs ignore test * test removing lint * put it back * args as array * recursive glob * replace lint with pre commit * pre commit install pbpcs phpcbf
1 parent e28c89c commit 94f71aa

File tree

11 files changed

+244
-63
lines changed

11 files changed

+244
-63
lines changed

.github/workflows/lint.yml

Lines changed: 0 additions & 40 deletions
This file was deleted.

.github/workflows/phpunit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ jobs:
1717
- name: Install dependencies
1818
run: composer install --prefer-dist --no-progress
1919
- name: Run PHPUnit tests
20-
run: vendor/bin/phpunit --colors=always
20+
run: vendor/bin/phpunit --colors=always --testsuite unit

.github/workflows/pre-commit.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: pre-commit
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
8+
jobs:
9+
pre-commit:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- uses: actions/setup-python@v3
14+
- name: setup PHP
15+
uses: shivammathur/setup-php@v2
16+
with:
17+
php-version: "8.3"
18+
tools: composer, phpcs, phpcbf
19+
- uses: pre-commit/[email protected]

.pre-commit-config.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
# pre-commit automatically excludes submodules
33
exclude: |
44
(?x)^(
5-
.*\.dist|
6-
roles/ood-head/files/auto-copy/var/www/ood/apps/common/common_attributes.yml|
7-
roles/ood-head/files/auto-copy/var/www/ood/apps/sys/dashboard/config/locales/en.yml|
8-
inventory.d/ipv4.py|
9-
files/shibboleth/filtered-incommon-metadata.xml.j2|
5+
test/.*|
106
)$
117
128
repos:

phpunit.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- restrictWarnings="true" -->
22
<phpunit
3-
bootstrap="test/unit/bootstrap.php"
3+
bootstrap="test/phpunit-bootstrap.php"
44
failOnWarning="true"
55
failOnDeprecation="true"
66
failOnNotice="true"
@@ -9,5 +9,8 @@
99
<testsuite name="unit">
1010
<directory>test/unit</directory>
1111
</testsuite>
12+
<testsuite name="functional">
13+
<directory>test/functional</directory>
14+
</testsuite>
1215
</testsuites>
1316
</phpunit>

resources/lib/UnityUser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ public function getSSHKeys($ignorecache = false)
446446
*/
447447
public function setLoginShell($shell, $operator = null, $send_mail = true)
448448
{
449+
// FIXME throw error if shell is not ascii
450+
// ldap schema syntax is "IA5 String (1.3.6.1.4.1.1466.115.121.1.26)"
449451
$ldapUser = $this->getLDAPUser();
450452
if ($ldapUser->exists()) {
451453
$ldapUser->setAttribute("loginshell", $shell);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
class AccountDeletionRequestTest extends TestCase
6+
{
7+
private function assertNumberAccountDeletionRequests(int $x)
8+
{
9+
global $USER, $SQL;
10+
if ($x == 0) {
11+
$this->assertFalse($USER->hasRequestedAccountDeletion());
12+
$this->assertFalse($SQL->accDeletionRequestExists($USER->getUID()));
13+
} elseif ($x > 0) {
14+
$this->assertTrue($USER->hasRequestedAccountDeletion());
15+
$this->assertTrue($SQL->accDeletionRequestExists($USER->getUID()));
16+
} else {
17+
throw new RuntimeError("x must not be negative");
18+
}
19+
$this->assertEquals($x, $this->getNumberAccountDeletionRequests());
20+
}
21+
22+
private function getNumberAccountDeletionRequests()
23+
{
24+
global $USER, $SQL;
25+
$stmt = $SQL->getConn()->prepare(
26+
"SELECT * FROM account_deletion_requests WHERE uid=:uid"
27+
);
28+
$uid = $USER->getUID();
29+
$stmt->bindParam(":uid", $uid);
30+
$stmt->execute();
31+
return count($stmt->fetchAll());
32+
}
33+
34+
public function testRequestAccountDeletionUserHasNoGroups()
35+
{
36+
global $USER, $SQL;
37+
switchUser(...getUserHasNotRequestedAccountDeletionHasNoGroups());
38+
$this->assertEmpty($USER->getGroups());
39+
$this->assertNumberAccountDeletionRequests(0);
40+
post(
41+
__DIR__ . "/../../webroot/panel/account.php",
42+
["form_type" => "account_deletion_request"]
43+
);
44+
$this->assertNumberAccountDeletionRequests(1);
45+
post(
46+
__DIR__ . "/../../webroot/panel/account.php",
47+
["form_type" => "account_deletion_request"]
48+
);
49+
$this->assertNumberAccountDeletionRequests(1);
50+
}
51+
52+
public function testRequestAccountDeletionUserHasGroup()
53+
{
54+
// FIXME this should be an error
55+
global $USER, $SQL;
56+
switchUser(...getUserHasNotRequestedAccountDeletionHasGroup());
57+
$this->assertNotEmpty($USER->getGroups());
58+
$this->assertNumberAccountDeletionRequests(0);
59+
post(
60+
__DIR__ . "/../../webroot/panel/account.php",
61+
["form_type" => "account_deletion_request"]
62+
);
63+
$this->assertNumberAccountDeletionRequests(0);
64+
}
65+
}

test/functional/LoginShellSetTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use PHPUnit\Framework\Attributes\DataProvider;
5+
6+
class LoginShellSetTest extends TestCase
7+
{
8+
private static $_initialLoginShell;
9+
10+
public static function setUpBeforeClass(): void
11+
{
12+
global $USER;
13+
switchUser(...getNormalUser());
14+
self::$_initialLoginShell = $USER->getLoginShell();
15+
}
16+
17+
public function tearDown(): void
18+
{
19+
global $USER;
20+
$USER->setLoginShell(self::$_initialLoginShell);
21+
}
22+
23+
public static function getShells()
24+
{
25+
global $HTTP_HEADER_TEST_INPUTS;
26+
// phpcs:disable
27+
return [["/bin/bash"]] + array_map(function($x){return [$x];}, $HTTP_HEADER_TEST_INPUTS);
28+
// phpcs:enable
29+
}
30+
31+
#[DataProvider("getShells")]
32+
public function testSetLoginShellCustom(string $shell): void
33+
{
34+
global $USER;
35+
// FIXME add check to avoid warning from ldap_modify
36+
if (!mb_check_encoding($shell, 'ASCII')) {
37+
$this->expectException("Exception");
38+
}
39+
// FIXME shell is not validated
40+
post(
41+
__DIR__ . "/../../webroot/panel/account.php",
42+
["form_type" => "loginshell", "shellSelect" => "custom", "shell" => $shell]
43+
);
44+
$this->assertEquals($shell, $USER->getLoginShell());
45+
}
46+
47+
#[DataProvider("getShells")]
48+
public function testSetLoginShellSelect(string $shell): void
49+
{
50+
global $USER;
51+
// FIXME add check to avoid warning from ldap_modify
52+
if (!mb_check_encoding($shell, 'ASCII')) {
53+
$this->expectException("Exception");
54+
}
55+
// FIXME shell is not validated
56+
post(
57+
__DIR__ . "/../../webroot/panel/account.php",
58+
["form_type" => "loginshell", "shellSelect" => $shell]
59+
);
60+
$this->assertEquals($shell, $USER->getLoginShell());
61+
}
62+
}

test/phpunit-bootstrap.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
require_once __DIR__ . "/../vendor/autoload.php";
4+
5+
require_once __DIR__ . "/../resources/lib/UnityLDAP.php";
6+
require_once __DIR__ . "/../resources/lib/UnityUser.php";
7+
require_once __DIR__ . "/../resources/lib/UnityGroup.php";
8+
require_once __DIR__ . "/../resources/lib/UnityOrg.php";
9+
require_once __DIR__ . "/../resources/lib/UnitySQL.php";
10+
require_once __DIR__ . "/../resources/lib/UnityMailer.php";
11+
require_once __DIR__ . "/../resources/lib/UnitySSO.php";
12+
require_once __DIR__ . "/../resources/lib/UnitySite.php";
13+
require_once __DIR__ . "/../resources/lib/UnityConfig.php";
14+
require_once __DIR__ . "/../resources/lib/UnityWebhook.php";
15+
require_once __DIR__ . "/../resources/lib/UnityRedis.php";
16+
17+
global $HTTP_HEADER_TEST_INPUTS;
18+
$HTTP_HEADER_TEST_INPUTS = [
19+
'',
20+
'a',
21+
'Hello, World!',
22+
' Some text ',
23+
' ',
24+
'12345',
25+
'abc123',
26+
'Hello@World!',
27+
str_repeat('a', 8190), // https://httpd.apache.org/docs/2.2/mod/core.html#limitrequestfieldsize
28+
'<p>This is a paragraph</p>',
29+
"'; DROP TABLE users; --",
30+
"<script>alert('XSS');</script>",
31+
'こんにちは世界',
32+
"Hello 👋 World 🌍",
33+
"Line 1\nLine 2",
34+
"Column1\tColumn2",
35+
'MiXeD cAsE',
36+
'https://www.example.com',
37+
38+
'{"key": "value"}',
39+
'SGVsbG8sIFdvcmxkIQ==',
40+
"Hello\x00World",
41+
mb_convert_encoding("Hello, World!", "UTF-16")
42+
];
43+
44+
function switchUser(string $eppn, string $given_name, string $sn, string $mail): void
45+
{
46+
global $CONFIG, $REDIS, $LDAP, $SQL, $MAILER, $WEBHOOK, $SITE, $SSO, $OPERATOR, $USER, $SEND_PIMESG_TO_ADMINS, $LOC_HEADER, $LOC_FOOTER;
47+
session_write_close();
48+
session_id(str_replace(["_", "@", "."], "-", $eppn));
49+
// session_start will be called on the first post()
50+
$_SERVER["REMOTE_USER"] = $eppn;
51+
$_SERVER["REMOTE_ADDR"] = "127.0.0.1";
52+
$_SERVER["eppn"] = $eppn;
53+
$_SERVER["givenName"] = $given_name;
54+
$_SERVER["sn"] = $sn;
55+
include __DIR__ . "/../resources/autoload.php";
56+
assert(!is_null($USER));
57+
}
58+
59+
function post(string $phpfile, array $post_data): void
60+
{
61+
global $CONFIG, $REDIS, $LDAP, $SQL, $MAILER, $WEBHOOK, $SITE, $SSO, $OPERATOR, $USER, $SEND_PIMESG_TO_ADMINS, $LOC_HEADER, $LOC_FOOTER;
62+
$_SERVER["REQUEST_METHOD"] = "POST";
63+
$_POST = $post_data;
64+
ob_start();
65+
try {
66+
include $phpfile;
67+
ob_get_clean(); // discard output
68+
} catch (Throwable $e) {
69+
error_log(ob_get_clean()); // don't discard output
70+
throw $e;
71+
} finally {
72+
unset($_POST);
73+
unset($_SERVER["REQUEST_METHOD"]);
74+
}
75+
}
76+
77+
function getNormalUser()
78+
{
79+
return ["[email protected]", "foo", "bar", "[email protected]"];
80+
}
81+
82+
function getUserHasNotRequestedAccountDeletionHasGroup()
83+
{
84+
return ["[email protected]", "foo", "bar", "[email protected]"];
85+
}
86+
87+
function getUserHasNotRequestedAccountDeletionHasNoGroups()
88+
{
89+
return ["[email protected]", "foo", "bar", "[email protected]"];
90+
}

test/unit/bootstrap.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

tools/docker-dev/identity/bootstrap.ldif

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10609,7 +10609,6 @@ memberuid: user1_org1_test
1060910609
memberuid: user3_org1_test
1061010610
memberuid: user264_org1_test
1061110611
memberuid: user10_org1_test
10612-
memberuid: user2_org1_test
1061310612
memberuid: user6_org1_test
1061410613
memberuid: user8_org1_test
1061510614
memberuid: user5_org2_test

0 commit comments

Comments
 (0)