Skip to content

Commit 724c03e

Browse files
committed
tests for ssh key deletion (#194)
* add tests for deleting ssh keys * fix account page ssh key index validation
1 parent 98143e6 commit 724c03e

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

test/functional/SSHKeyDeleteTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use PHPUnit\Framework\Attributes\DataProvider;
5+
6+
class SSHKeyDeleteTest extends TestCase {
7+
static $initialKeys;
8+
9+
public static function setUpBeforeClass(): void{
10+
global $USER;
11+
switchUser(...getUserWithOneKey());
12+
self::$initialKeys = $USER->getSSHKeys(true);
13+
}
14+
15+
private function deleteKey(string $index): void {
16+
post(
17+
__DIR__ . "/../../webroot/panel/account.php",
18+
["form_type" => "delKey", "delIndex" => $index]
19+
);
20+
}
21+
22+
public static function getGarbageIndexArgs()
23+
{
24+
global $HTTP_HEADER_TEST_INPUTS;
25+
return array_map(function($x){return [$x];}, $HTTP_HEADER_TEST_INPUTS);
26+
}
27+
28+
#[DataProvider("getGarbageIndexArgs")]
29+
public function testDeleteKeyGarbageInput(string $index)
30+
{
31+
global $USER;
32+
try {
33+
$this->deleteKey($index);
34+
$this->assertEquals(self::$initialKeys, $USER->getSSHKeys(true));
35+
} finally {
36+
$USER->setSSHKeys(self::$initialKeys);
37+
}
38+
}
39+
40+
public function testDeleteKeyNegativeIndex()
41+
{
42+
global $USER;
43+
try {
44+
$this->deleteKey("-1");
45+
$this->assertEquals(self::$initialKeys, $USER->getSSHKeys(true));
46+
} finally {
47+
$USER->setSSHKeys(self::$initialKeys);
48+
}
49+
}
50+
51+
public function testDeleteKeyIndexTooLarge()
52+
{
53+
global $USER;
54+
try {
55+
$this->deleteKey("99");
56+
$this->assertEquals(self::$initialKeys, $USER->getSSHKeys(true));
57+
} finally {
58+
$USER->setSSHKeys(self::$initialKeys);
59+
}
60+
}
61+
62+
public function testDeleteKeyDecimal()
63+
{
64+
global $USER;
65+
try {
66+
$this->deleteKey("0.5");
67+
$this->assertEquals(self::$initialKeys, $USER->getSSHKeys(true));
68+
} finally {
69+
$USER->setSSHKeys(self::$initialKeys);
70+
}
71+
}
72+
73+
public function testDeleteKey()
74+
{
75+
global $USER;
76+
try {
77+
$this->deleteKey("0");
78+
$this->assertEquals([], $USER->getSSHKeys(true));
79+
} finally {
80+
$USER->setSSHKeys(self::$initialKeys);
81+
}
82+
}
83+
}

test/phpunit-bootstrap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,8 @@ function getUserNotPiNotRequestedBecomePiRequestedAccountDeletion()
104104
{
105105
return ["user4@org1.test", "foo", "bar", "user4@org1.test"];
106106
}
107+
108+
function getUserWithOneKey()
109+
{
110+
return ["user5@org2.test", "foo", "bar", "user5@org2.test"];
111+
}

tools/docker-dev/identity/bootstrap.ldif

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15313,9 +15313,6 @@ objectclass: top
1531315313
objectclass: ldapPublicKey
1531415314
sn: Surname
1531515315
sshpublickey: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQClA2207u2KjiExKhcx6luLt5JN0SRCHseeYJwb0MnpEZq5hHvwYsnE1DByjRAlZStbvCMHqrty4VYC7lCT3GEGhPwo7w8an6RaGWSWRvdi54rsa9Cb0uQdNoooBwKq548LQAIl+i54S/Qx+EQDvF4zyTUtSLm7zmC5JJJZ9KujFpAnoXajGd6G6HSvUYP38CIoOxjQoClF6Uz3jwWfysr4BrhdnfQBUqFAhYKFHDCP9HuKvwbHM9aO8fuH7MGPZvtEHjVGoZbt4zW9ROTUWYgo7/XPMmwVjLVAacXJQTK5nOjFD6+8FhWZysYqhaWM+43vRkKMA57mrZIj1LJklcbZ
15316-
sshpublickey: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDCuQCJmDArk8CSLtEqrlxzP348iAjodsn37Xt1cQ1XADac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: user5_org2_test
1532015317
uidnumber: 1130
1532115318

webroot/panel/account.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@
5555
break;
5656
case "delKey":
5757
$keys = $USER->getSSHKeys();
58-
unset($keys[intval($_POST["delIndex"])]); // remove key from array
58+
$indexStr = $_POST["delIndex"];
59+
if (!preg_match("/^[0-9]+$/", $indexStr)) {
60+
break;
61+
}
62+
$index = intval($indexStr);
63+
if ($index >= count($keys)) {
64+
break;
65+
}
66+
unset($keys[$index]); // remove key from array
5967
$keys = array_values($keys);
6068

6169
$USER->setSSHKeys($keys, $OPERATOR); // Update user keys

0 commit comments

Comments
 (0)