|
3 | 3 | use UnityWebPortal\lib\UnityGithub; |
4 | 4 | use PHPUnit\Framework\Attributes\DataProvider; |
5 | 5 | use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations; |
| 6 | +use UnityWebPortal\lib\UnityHTTPDMessageLevel; |
6 | 7 |
|
7 | 8 | #[AllowMockObjectsWithoutExpectations] |
8 | 9 | class SSHKeyAddTest extends UnityWebPortalTestCase |
9 | 10 | { |
10 | 11 | public static function keyProvider() |
11 | 12 | { |
12 | 13 | $validKey = |
13 | | - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a foobar"; |
| 14 | + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPUef6kU0/P0lTO5KBZq6aFVm7nBHhB85SaG4HB0nh7p foobar"; |
14 | 15 | $invalidKey = "foobar"; |
15 | 16 | return [[false, $invalidKey], [true, $validKey]]; |
16 | 17 | } |
17 | 18 |
|
18 | 19 | public static function keysProvider() |
19 | 20 | { |
20 | 21 | $validKey = |
21 | | - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a foobar"; |
| 22 | + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPUef6kU0/P0lTO5KBZq6aFVm7nBHhB85SaG4HB0nh7p foobar"; |
22 | 23 | $validKeyDuplicateDifferentComment = |
23 | | - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a foobar2"; |
| 24 | + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPUef6kU0/P0lTO5KBZq6aFVm7nBHhB85SaG4HB0nh7p foobar2"; |
24 | 25 | $validKey2 = |
25 | | - "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBF/dSI9/7YWeyB8wa4rEWRdeb9pQbrGxZwYFV2ulr0agXdbiJIApp0MWDYlIc9XI+4Y+cVAj66PQ2YaRz44BV+o="; |
| 26 | + "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBF5Ossk5huH48Gdyw1nuC+1TKajZzF+83rwbFhml0b915mWzYbKqFtjFze8+4uW+xBjLmwx4e+vGiZbNR4ucm6w="; |
26 | 27 | $invalidKey = "foobar"; |
27 | 28 | return [ |
28 | 29 | [0, []], |
@@ -161,4 +162,73 @@ public function testAddSshKeysGithub(int $expectedKeysAdded, array $keys) |
161 | 162 | callPrivateMethod($USER, "setSSHKeys", []); |
162 | 163 | } |
163 | 164 | } |
| 165 | + |
| 166 | + public function testShareKeysBetweenUsers() |
| 167 | + { |
| 168 | + global $USER; |
| 169 | + $key = |
| 170 | + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPUef6kU0/P0lTO5KBZq6aFVm7nBHhB85SaG4HB0nh7p foobar"; |
| 171 | + $this->switchUser("Admin"); |
| 172 | + $user1 = $USER; |
| 173 | + $this->switchUser("Blank"); |
| 174 | + $user2 = $USER; |
| 175 | + $user1_keys_before = $user1->getSSHKeys(); |
| 176 | + $user2_keys_before = $user2->getSSHKeys(); |
| 177 | + try { |
| 178 | + $user1->addSSHKey($key); |
| 179 | + // as user2, try to add the key that user1 already added |
| 180 | + $this->http_post( |
| 181 | + __DIR__ . "/../../webroot/panel/account.php", |
| 182 | + [ |
| 183 | + "form_type" => "addKey", |
| 184 | + "add_type" => "paste", |
| 185 | + "key" => $key, |
| 186 | + ], |
| 187 | + do_validate_messages: false, |
| 188 | + ); |
| 189 | + $this->assertMessageExists( |
| 190 | + UnityHTTPDMessageLevel::WARNING, |
| 191 | + "/.*/", |
| 192 | + "/This incident has been reported/", |
| 193 | + ); |
| 194 | + $this->assertEquals($user2_keys_before, $user2->getSSHKeys()); |
| 195 | + } finally { |
| 196 | + callPrivateMethod($user1, "setSSHKeys", $user1_keys_before); |
| 197 | + callPrivateMethod($user2, "setSSHKeys", $user2_keys_before); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + /* |
| 202 | + while attempting to share keys between users says "this incident has been reported" |
| 203 | + you should not see this message if you add the same key to your account twice |
| 204 | + */ |
| 205 | + public function testAddDuplicateKey() |
| 206 | + { |
| 207 | + global $USER; |
| 208 | + $key = |
| 209 | + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPUef6kU0/P0lTO5KBZq6aFVm7nBHhB85SaG4HB0nh7p foobar"; |
| 210 | + $this->switchUser("Blank"); |
| 211 | + $this->assertEmpty($USER->getSSHKeys()); |
| 212 | + try { |
| 213 | + $USER->addSSHKey($key); |
| 214 | + $this->assertEquals([$key], $USER->getSSHKeys()); |
| 215 | + $this->http_post( |
| 216 | + __DIR__ . "/../../webroot/panel/account.php", |
| 217 | + [ |
| 218 | + "form_type" => "addKey", |
| 219 | + "add_type" => "paste", |
| 220 | + "key" => $key, |
| 221 | + ], |
| 222 | + do_validate_messages: false, |
| 223 | + ); |
| 224 | + $this->assertMessageExists( |
| 225 | + UnityHTTPDMessageLevel::WARNING, |
| 226 | + "/Key Already Added/", |
| 227 | + "/.*/", |
| 228 | + ); |
| 229 | + $this->assertEquals([$key], $USER->getSSHKeys()); |
| 230 | + } finally { |
| 231 | + callPrivateMethod($USER, "setSSHKeys", []); |
| 232 | + } |
| 233 | + } |
164 | 234 | } |
0 commit comments