77
88class SSHKeyAddTest extends UnityWebPortalTestCase
99{
10- private function addSshKeysPaste ( array $ keys ): void
10+ public static function keyProvider ()
1111 {
12- foreach ($ keys as $ key ) {
12+ $ validKey =
13+ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a foobar " ;
14+ $ invalidKey = "foobar " ;
15+ return [[false , $ invalidKey ], [true , $ validKey ]];
16+ }
17+
18+ public static function keysProvider ()
19+ {
20+ $ validKey =
21+ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a foobar " ;
22+ $ validKey2 =
23+ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a foobar2 " ;
24+ $ invalidKey = "foobar " ;
25+ return [
26+ [0 , []],
27+ [0 , [$ invalidKey ]],
28+ [1 , [$ validKey ]],
29+ [0 , [$ validKey , $ invalidKey ]],
30+ [1 , [$ validKey , $ validKey ]],
31+ [2 , [$ validKey , $ validKey2 ]],
32+ ];
33+ }
34+
35+ public function getKeyCount ()
36+ {
37+ global $ USER ;
38+ return count ($ USER ->getSSHKeys ());
39+ }
40+
41+ #[DataProvider("keyProvider " )]
42+ public function testAddSshKeyPaste (bool $ expectedKeyAdded , string $ key )
43+ {
44+ global $ USER ;
45+ switchUser (...getUserHasNoSshKeys ());
46+ $ numKeysBefore = $ this ->getKeyCount ();
47+ $ this ->assertEquals (0 , $ numKeysBefore );
48+ try {
1349 http_post (__DIR__ . "/../../webroot/panel/account.php " , [
1450 "form_type " => "addKey " ,
1551 "add_type " => "paste " ,
1652 "key " => $ key ,
1753 ]);
54+ $ numKeysAfter = $ this ->getKeyCount ();
55+ if ($ expectedKeyAdded ) {
56+ $ this ->assertEquals (1 , $ numKeysAfter - $ numKeysBefore );
57+ } else {
58+ $ this ->assertEquals (0 , $ numKeysAfter - $ numKeysBefore );
59+ }
60+ } finally {
61+ $ USER ->setSSHKeys ([]);
1862 }
1963 }
2064
21- private function addSshKeysImport (array $ keys ): void
65+ #[DataProvider("keyProvider " )]
66+ public function testAddSshKeyImport (bool $ expectedKeyAdded , string $ key )
2267 {
23- foreach ($ keys as $ key ) {
68+ global $ USER ;
69+ switchUser (...getUserHasNoSshKeys ());
70+ $ numKeysBefore = $ this ->getKeyCount ();
71+ $ this ->assertEquals (0 , $ numKeysBefore );
72+ try {
2473 $ tmp = tmpfile ();
2574 $ tmp_path = stream_get_meta_data ($ tmp )["uri " ];
2675 fwrite ($ tmp , $ key );
@@ -34,82 +83,67 @@ private function addSshKeysImport(array $keys): void
3483 } finally {
3584 unset($ _FILES ["keyfile " ]);
3685 }
86+ $ numKeysAfter = $ this ->getKeyCount ();
87+ if ($ expectedKeyAdded ) {
88+ $ this ->assertEquals (1 , $ numKeysAfter - $ numKeysBefore );
89+ } else {
90+ $ this ->assertEquals (0 , $ numKeysAfter - $ numKeysBefore );
91+ }
92+ } finally {
93+ $ USER ->setSSHKeys ([]);
3794 }
3895 }
3996
40- private function addSshKeysGenerate (array $ keys ): void
97+ #[DataProvider("keyProvider " )]
98+ public function testAddSshKeyGenerate (bool $ expectedKeyAdded , string $ key )
4199 {
42- foreach ($ keys as $ key ) {
100+ global $ USER ;
101+ switchUser (...getUserHasNoSshKeys ());
102+ $ numKeysBefore = $ this ->getKeyCount ();
103+ $ this ->assertEquals (0 , $ numKeysBefore );
104+ try {
43105 http_post (__DIR__ . "/../../webroot/panel/account.php " , [
44106 "form_type " => "addKey " ,
45107 "add_type " => "generate " ,
46108 "gen_key " => $ key ,
47109 ]);
48- }
49- }
50-
51- private function addSshKeysGithub (array $ keys ): void
52- {
53- global $ GITHUB ;
54- $ oldGithub = $ GITHUB ;
55- $ GITHUB = $ this ->createMock (UnityGithub::class);
56- $ GITHUB ->method ("getSshPublicKeys " )->willReturn ($ keys );
57- try {
58- http_post (__DIR__ . "/../../webroot/panel/account.php " , [
59- "form_type " => "addKey " ,
60- "add_type " => "github " ,
61- "gh_user " => "foobar " ,
62- ]);
110+ $ numKeysAfter = $ this ->getKeyCount ();
111+ if ($ expectedKeyAdded ) {
112+ $ this ->assertEquals (1 , $ numKeysAfter - $ numKeysBefore );
113+ } else {
114+ $ this ->assertEquals (0 , $ numKeysAfter - $ numKeysBefore );
115+ }
63116 } finally {
64- $ GITHUB = $ oldGithub ;
65- }
66- }
67-
68- public static function provider ()
69- {
70- $ validKey =
71- "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIB+XqO25MUB9x/pS04I3JQ7rMGboWyGXh0GUzkOrTi7a foobar " ;
72- $ invalidKey = "foobar " ;
73- $ methods = [
74- "addSshKeysPaste " ,
75- "addSshKeysImport " ,
76- "addSshKeysGenerate " ,
77- "addSshKeysGithub " ,
78- ];
79- $ output = [];
80- foreach ($ methods as $ method ) {
81- $ output = array_merge ($ output , [
82- [$ method , 0 , []],
83- [$ method , 0 , [$ invalidKey ]],
84- [$ method , 1 , [$ validKey ]],
85- [$ method , 1 , [$ validKey , $ invalidKey ]],
86- [$ method , 1 , [$ validKey , $ validKey ]],
87- ]);
117+ $ USER ->setSSHKeys ([]);
88118 }
89- return $ output ;
90- }
91-
92- public function getKeyCount ()
93- {
94- global $ USER ;
95- return count ($ USER ->getSSHKeys ());
96119 }
97120
98121 #[AllowMockObjectsWithoutExpectations]
99- #[DataProvider("provider " )]
100- public function testAddSshKeys ( string $ methodName , int $ expectedKeysAdded , array $ keys )
122+ #[DataProvider("keysProvider " )]
123+ public function testAddSshKeysGithub ( int $ expectedKeysAdded , array $ keys )
101124 {
102- global $ USER ;
125+ global $ USER , $ GITHUB ;
103126 switchUser (...getUserHasNoSshKeys ());
104127 $ numKeysBefore = $ this ->getKeyCount ();
105128 $ this ->assertEquals (0 , $ numKeysBefore );
129+ $ oldGithub = $ GITHUB ;
106130 try {
107- call_user_func ([SSHKeyAddTest::class, $ methodName ], $ keys );
108- // $method($keys);
131+ $ GITHUB = $ this ->createMock (UnityGithub::class);
132+ $ GITHUB ->method ("getSshPublicKeys " )->willReturn ($ keys );
133+ try {
134+ http_post (__DIR__ . "/../../webroot/panel/account.php " , [
135+ "form_type " => "addKey " ,
136+ "add_type " => "github " ,
137+ "gh_user " => "foobar " ,
138+ ]);
139+ } finally {
140+ $ GITHUB = $ oldGithub ;
141+ }
109142 $ numKeysAfter = $ this ->getKeyCount ();
110143 $ this ->assertEquals ($ expectedKeysAdded , $ numKeysAfter - $ numKeysBefore );
111144 } finally {
112145 $ USER ->setSSHKeys ([]);
146+ $ GITHUB = $ oldGithub ;
113147 }
114148 }
115149}
0 commit comments