Skip to content

Commit 9026317

Browse files
committed
simplify ssh key adding behavior
1 parent d24c734 commit 9026317

File tree

2 files changed

+101
-76
lines changed

2 files changed

+101
-76
lines changed

test/functional/SSHKeyAddTest.php

Lines changed: 91 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,69 @@
66

77
class SSHKeyAddTest extends UnityWebPortalTestCase
88
{
9-
private function addSshKeysPaste(array $keys): void
9+
public static function keyProvider()
1010
{
11-
foreach ($keys as $key) {
11+
$validKey =
12+
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a foobar";
13+
$invalidKey = "foobar";
14+
return [[false, $invalidKey], [true, $validKey]];
15+
}
16+
17+
public static function keysProvider()
18+
{
19+
$validKey =
20+
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a foobar";
21+
$validKey2 =
22+
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a foobar2";
23+
$invalidKey = "foobar";
24+
return [
25+
[0, []],
26+
[0, [$invalidKey]],
27+
[1, [$validKey]],
28+
[0, [$validKey, $invalidKey]],
29+
[1, [$validKey, $validKey]],
30+
[2, [$validKey, $validKey2]],
31+
];
32+
}
33+
34+
public function getKeyCount()
35+
{
36+
global $USER;
37+
return count($USER->getSSHKeys());
38+
}
39+
40+
#[DataProvider("keyProvider")]
41+
public function testAddSshKeyPaste(bool $expectedKeyAdded, string $key)
42+
{
43+
global $USER;
44+
switchUser(...getUserHasNoSshKeys());
45+
$numKeysBefore = $this->getKeyCount();
46+
$this->assertEquals(0, $numKeysBefore);
47+
try {
1248
http_post(__DIR__ . "/../../webroot/panel/account.php", [
1349
"form_type" => "addKey",
1450
"add_type" => "paste",
1551
"key" => $key,
1652
]);
53+
$numKeysAfter = $this->getKeyCount();
54+
if ($expectedKeyAdded) {
55+
$this->assertEquals(1, $numKeysAfter - $numKeysBefore);
56+
} else {
57+
$this->assertEquals(0, $numKeysAfter - $numKeysBefore);
58+
}
59+
} finally {
60+
$USER->setSSHKeys([]);
1761
}
1862
}
1963

20-
private function addSshKeysImport(array $keys): void
64+
#[DataProvider("keyProvider")]
65+
public function testAddSshKeyImport(bool $expectedKeyAdded, string $key)
2166
{
22-
foreach ($keys as $key) {
67+
global $USER;
68+
switchUser(...getUserHasNoSshKeys());
69+
$numKeysBefore = $this->getKeyCount();
70+
$this->assertEquals(0, $numKeysBefore);
71+
try {
2372
$tmp = tmpfile();
2473
$tmp_path = stream_get_meta_data($tmp)["uri"];
2574
fwrite($tmp, $key);
@@ -33,81 +82,66 @@ private function addSshKeysImport(array $keys): void
3382
} finally {
3483
unset($_FILES["keyfile"]);
3584
}
85+
$numKeysAfter = $this->getKeyCount();
86+
if ($expectedKeyAdded) {
87+
$this->assertEquals(1, $numKeysAfter - $numKeysBefore);
88+
} else {
89+
$this->assertEquals(0, $numKeysAfter - $numKeysBefore);
90+
}
91+
} finally {
92+
$USER->setSSHKeys([]);
3693
}
3794
}
3895

39-
private function addSshKeysGenerate(array $keys): void
96+
#[DataProvider("keyProvider")]
97+
public function testAddSshKeyGenerate(bool $expectedKeyAdded, string $key)
4098
{
41-
foreach ($keys as $key) {
99+
global $USER;
100+
switchUser(...getUserHasNoSshKeys());
101+
$numKeysBefore = $this->getKeyCount();
102+
$this->assertEquals(0, $numKeysBefore);
103+
try {
42104
http_post(__DIR__ . "/../../webroot/panel/account.php", [
43105
"form_type" => "addKey",
44106
"add_type" => "generate",
45107
"gen_key" => $key,
46108
]);
47-
}
48-
}
49-
50-
private function addSshKeysGithub(array $keys): void
51-
{
52-
global $GITHUB;
53-
$oldGithub = $GITHUB;
54-
$GITHUB = $this->createMock(UnityGithub::class);
55-
$GITHUB->method("getSshPublicKeys")->willReturn($keys);
56-
try {
57-
http_post(__DIR__ . "/../../webroot/panel/account.php", [
58-
"form_type" => "addKey",
59-
"add_type" => "github",
60-
"gh_user" => "foobar",
61-
]);
109+
$numKeysAfter = $this->getKeyCount();
110+
if ($expectedKeyAdded) {
111+
$this->assertEquals(1, $numKeysAfter - $numKeysBefore);
112+
} else {
113+
$this->assertEquals(0, $numKeysAfter - $numKeysBefore);
114+
}
62115
} finally {
63-
$GITHUB = $oldGithub;
64-
}
65-
}
66-
67-
public static function provider()
68-
{
69-
$validKey =
70-
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a foobar";
71-
$invalidKey = "foobar";
72-
$methods = [
73-
"addSshKeysPaste",
74-
"addSshKeysImport",
75-
"addSshKeysGenerate",
76-
"addSshKeysGithub",
77-
];
78-
$output = [];
79-
foreach ($methods as $method) {
80-
$output = array_merge($output, [
81-
[$method, 0, []],
82-
[$method, 0, [$invalidKey]],
83-
[$method, 1, [$validKey]],
84-
[$method, 1, [$validKey, $invalidKey]],
85-
[$method, 1, [$validKey, $validKey]],
86-
]);
116+
$USER->setSSHKeys([]);
87117
}
88-
return $output;
89-
}
90-
91-
public function getKeyCount()
92-
{
93-
global $USER;
94-
return count($USER->getSSHKeys());
95118
}
96119

97-
#[DataProvider("provider")]
98-
public function testAddSshKeys(string $methodName, int $expectedKeysAdded, array $keys)
120+
#[DataProvider("keysProvider")]
121+
public function testAddSshKeysGithub(int $expectedKeysAdded, array $keys)
99122
{
100-
global $USER;
123+
global $USER, $GITHUB;
101124
switchUser(...getUserHasNoSshKeys());
102125
$numKeysBefore = $this->getKeyCount();
103126
$this->assertEquals(0, $numKeysBefore);
127+
$oldGithub = $GITHUB;
104128
try {
105-
call_user_func([SSHKeyAddTest::class, $methodName], $keys);
106-
// $method($keys);
129+
$GITHUB = $this->createMock(UnityGithub::class);
130+
$GITHUB->method("getSshPublicKeys")->willReturn($keys);
131+
try {
132+
http_post(__DIR__ . "/../../webroot/panel/account.php", [
133+
"form_type" => "addKey",
134+
"add_type" => "github",
135+
"gh_user" => "foobar",
136+
]);
137+
} finally {
138+
$GITHUB = $oldGithub;
139+
}
107140
$numKeysAfter = $this->getKeyCount();
108141
$this->assertEquals($expectedKeysAdded, $numKeysAfter - $numKeysBefore);
109142
} finally {
110143
$USER->setSSHKeys([]);
144+
$GITHUB = $oldGithub;
111145
}
112146
}
113147
}

webroot/panel/account.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,33 @@
1313
UnityHTTPD::validatePostCSRFToken();
1414
switch (UnityHTTPD::getPostData("form_type")) {
1515
case "addKey":
16-
$keys = array();
1716
switch (UnityHTTPD::getPostData("add_type")) {
1817
case "paste":
19-
array_push($keys, UnityHTTPD::getPostData("key"));
18+
$keys = [UnityHTTPD::getPostData("key")];
2019
break;
2120
case "import":
2221
try {
23-
$key = UnityHTTPD::getUploadedFileContents("keyfile");
22+
$keys = [UnityHTTPD::getUploadedFileContents("keyfile")];
2423
} catch (EncodingUnknownException | EncodingConversionException $e) {
2524
UnityHTTPD::badRequest("uploaded key has bad encoding", error: $e);
2625
}
27-
array_push($keys, $key);
2826
break;
2927
case "generate":
30-
array_push($keys, UnityHTTPD::getPostData("gen_key"));
28+
$keys = [UnityHTTPD::getPostData("gen_key")];
3129
break;
3230
case "github":
3331
$githubUsername = UnityHTTPD::getPostData("gh_user");
34-
$githubKeys = $GITHUB->getSshPublicKeys($githubUsername);
35-
$keys = array_merge($keys, $githubKeys);
32+
$keys = $GITHUB->getSshPublicKeys($githubUsername);
3633
break;
3734
}
38-
if (!empty($keys)) {
39-
$keys = array_map("trim", $keys);
40-
$validKeys = array_filter($keys, "testValidSSHKey");
41-
$USER->setSSHKeys(array_merge($USER->getSSHKeys(), $validKeys));
42-
if (count($keys) != count($validKeys)) {
43-
UnityHTTPD::badRequest(
44-
"one more more invalid SSH keys were not added",
45-
data: [
46-
"keys_valid_added" => $validKeys,
47-
"keys_invalid_not_added" => array_diff($keys, $validKeys),
48-
],
49-
);
35+
$keys = array_map("trim", $keys);
36+
foreach ($keys as $key) {
37+
if (!testValidSSHKey($key)) {
38+
UnityHTTPD::messageError("Invalid SSH Key", $key);
39+
UnityHTTPD::redirect();
5040
}
5141
}
42+
$USER->setSSHKeys(array_merge($USER->getSSHKeys(), $keys));
5243
break;
5344
case "delKey":
5445
$keys = $USER->getSSHKeys();

0 commit comments

Comments
 (0)