Skip to content

Commit 0879bc1

Browse files
committed
ssh key add testst
1 parent 28f17ff commit 0879bc1

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

test/functional/SSHKeyAddTest.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use UnityWebPortal\lib\UnityGithub;
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use PHPUnit\Framework\MockObject\MockBuilder;
7+
8+
class SSHKeyAddTest extends TestCase
9+
{
10+
private function addSshKeysPaste(array $keys): void {
11+
foreach ($keys as $key) {
12+
post(
13+
__DIR__ . "/../../webroot/panel/account.php",
14+
[
15+
"form_type" => "addKey",
16+
"add_type" => "paste",
17+
"key" => $key
18+
]
19+
);
20+
}
21+
}
22+
23+
private function addSshKeysImport(array $keys): void {
24+
foreach ($keys as $key) {
25+
$tmp = tmpfile();
26+
$tmp_path = stream_get_meta_data($tmp)["uri"];
27+
fwrite($tmp, $key);
28+
$_FILES["keyfile"] = ["tmp_name" => $tmp_path];
29+
try {
30+
post(
31+
__DIR__ . "/../../webroot/panel/account.php",
32+
["form_type" => "addKey", "add_type" => "import"]
33+
);
34+
} finally {
35+
unlink($tmp_path);
36+
unset($_FILES["keyfile"]);
37+
}
38+
}
39+
}
40+
41+
private function addSshKeysGenerate(array $keys): void {
42+
foreach ($keys as $key) {
43+
post(
44+
__DIR__ . "/../../webroot/panel/account.php",
45+
[
46+
"form_type" => "addKey",
47+
"add_type" => "generate",
48+
"gen_key" => $key
49+
]
50+
);
51+
}
52+
}
53+
54+
private function addSshKeysGithub(array $keys): void {
55+
global $GITHUB;
56+
$oldGithub = $GITHUB;
57+
$GITHUB = $this->createMock(UnityGithub::class);
58+
$GITHUB->method("getSshPublicKeys")->willReturn($keys);
59+
try {
60+
post(
61+
__DIR__ . "/../../webroot/panel/account.php",
62+
[
63+
"form_type" => "addKey",
64+
"add_type" => "github",
65+
"gh_user" => "foobar"
66+
]
67+
);
68+
} finally {
69+
$GITHUB = $oldGithub;
70+
}
71+
}
72+
73+
public static function provider() {
74+
$validKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a foobar";
75+
$invalidKey = "foobar";
76+
$methods = [
77+
"addSshKeysPaste",
78+
"addSshKeysImport",
79+
"addSshKeysGenerate",
80+
"addSshKeysGithub"
81+
];
82+
$output = [];
83+
foreach ($methods as $method) {
84+
$output = array_merge($output, [
85+
[$method, 0, []],
86+
[$method, 0, [$invalidKey]],
87+
[$method, 1, [$validKey]],
88+
[$method, 1, [$validKey, $invalidKey]],
89+
[$method, 1, [$validKey, $validKey]],
90+
]);
91+
}
92+
return $output;
93+
}
94+
95+
public function getKeyCount()
96+
{
97+
global $USER;
98+
return count($USER->getSSHKeys(true));
99+
}
100+
101+
#[DataProvider("provider")]
102+
public function testAddSshKeys(string $methodName, int $expectedKeysAdded, array $keys)
103+
{
104+
global $USER;
105+
switchUser(...getUserHasNoSshKeys());
106+
$numKeysBefore = $this->getKeyCount($USER);
107+
$this->assertEquals(0, $numKeysBefore);
108+
try {
109+
call_user_func([SSHKeyAddTest::class, $methodName], $keys);
110+
// $method($keys);
111+
$numKeysAfter = $this->getKeyCount($USER);
112+
$this->assertEquals($expectedKeysAdded, ($numKeysAfter - $numKeysBefore));
113+
} finally {
114+
$USER->setSSHKeys([]);
115+
}
116+
}
117+
}

tools/docker-dev/identity/bootstrap.ldif

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13980,7 +13980,6 @@ objectclass: posixAccount
1398013980
objectclass: top
1398113981
objectclass: ldapPublicKey
1398213982
sn: Surname
13983-
sshpublickey: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3Rk+JhKNK4BL6EfvBpFpAklfvbhQYu5F4T3MBS+SErUn//UgVoW9Mam1H8F3cA0pkYbzgCeak9qOTvgOAoG6TihZj5ltCBWTdVJzhoAARYuJQ9mERunOndIp69helEK/lXsl++zdUb+znX3QT+oyeyb28+l96gBc8yspgUAmp1UAgR9vxoonNR33/w9D0OGa7y7W6kAhZ/itqVbHtqemZ4GB99jcsJ1ZRXbEgIJxa2Dbi+LHV4zVku7nHiFi8VVzvxejGeK545yh/Ox7JDi1Cglv43bIHlWPEz3biU9F7tevaZDV85Il2NuXN+lLVC/j1Tm9P38emMRiNN7uGDr9v
1398413983
uid: user3_org1_test
1398513984
uidnumber: 1018
1398613985

0 commit comments

Comments
 (0)