66
77class 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}
0 commit comments