|
| 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 | +} |
0 commit comments