@@ -2,6 +2,9 @@ package cmd
2
2
3
3
import (
4
4
"fmt"
5
+ "net"
6
+ "net/http"
7
+ "net/http/httptest"
5
8
"os"
6
9
"path/filepath"
7
10
"testing"
@@ -20,7 +23,7 @@ var initCmdTestCases = []struct {
20
23
// https://github.com/go-survey/survey/issues/394
21
24
// This flag is used to enable Pipe-based, and not PTY-based, tests.
22
25
pipe bool
23
- beforeTest func (t * testing.T , conjurrcInTmpDir string )
26
+ beforeTest func (t * testing.T , conjurrcInTmpDir string ) func ()
24
27
assert func (t * testing.T , conjurrcInTmpDir string , stdout string )
25
28
}{
26
29
{
@@ -94,8 +97,9 @@ appliance_url: http://conjur
94
97
},
95
98
},
96
99
pipe : true ,
97
- beforeTest : func (t * testing.T , conjurrcInTmpDir string ) {
100
+ beforeTest : func (t * testing.T , conjurrcInTmpDir string ) func () {
98
101
os .WriteFile (conjurrcInTmpDir , []byte ("something" ), 0644 )
102
+ return nil
99
103
},
100
104
assert : func (t * testing.T , conjurrcInTmpDir string , stdout string ) {
101
105
// Assert that file is not overwritten
@@ -113,8 +117,9 @@ appliance_url: http://conjur
113
117
},
114
118
},
115
119
pipe : true ,
116
- beforeTest : func (t * testing.T , conjurrcInTmpDir string ) {
120
+ beforeTest : func (t * testing.T , conjurrcInTmpDir string ) func () {
117
121
os .WriteFile (conjurrcInTmpDir , []byte ("something" ), 0644 )
122
+ return nil
118
123
},
119
124
assert : func (t * testing.T , conjurrcInTmpDir string , stdout string ) {
120
125
// Assert that file is overwritten
@@ -142,8 +147,9 @@ credential_storage: file
142
147
{
143
148
name : "force overwrite" ,
144
149
args : []string {"init" , "-u=http://host" , "-a=yet-another-test-account" , "--force" , "-i" },
145
- beforeTest : func (t * testing.T , conjurrcInTmpDir string ) {
150
+ beforeTest : func (t * testing.T , conjurrcInTmpDir string ) func () {
146
151
os .WriteFile (conjurrcInTmpDir , []byte ("something" ), 0644 )
152
+ return nil
147
153
},
148
154
assert : func (t * testing.T , conjurrcInTmpDir string , stdout string ) {
149
155
// Assert that file is overwritten
@@ -205,23 +211,30 @@ appliance_url: http://host
205
211
},
206
212
{
207
213
name : "fails for self-signed certificate" ,
208
- args : []string {"init" , "-u=https://self-signed.badssl.com" , "-a=test-account" },
214
+ args : []string {"init" , "-u=https://localhost:8080" , "-a=test-account" },
215
+ beforeTest : func (t * testing.T , conjurrcInTmpDir string ) func () {
216
+ return startSelfSignedServer (t , 8080 )
217
+ },
209
218
assert : func (t * testing.T , conjurrcInTmpDir string , stdout string ) {
210
219
assert .Contains (t , stdout , "Unable to retrieve and validate certificate" )
220
+ assert .Contains (t , stdout , "x509" )
211
221
assert .Contains (t , stdout , "If you're attempting to use a self-signed certificate, re-run the init command with the `--self-signed` flag" )
212
222
assertFetchCertFailed (t , conjurrcInTmpDir )
213
223
},
214
224
},
215
225
{
216
226
name : "succeeds for self-signed certificate with --self-signed flag" ,
217
- args : []string {"init" , "-u=https://self-signed.badssl.com " , "-a=test-account" , "--self-signed" },
227
+ args : []string {"init" , "-u=https://localhost:8080 " , "-a=test-account" , "--self-signed" },
218
228
promptResponses : []promptResponse {
219
229
{
220
230
prompt : "Trust this certificate?" ,
221
231
response : "y" ,
222
232
},
223
233
},
224
234
pipe : true ,
235
+ beforeTest : func (t * testing.T , conjurrcInTmpDir string ) func () {
236
+ return startSelfSignedServer (t , 8080 )
237
+ },
225
238
assert : func (t * testing.T , conjurrcInTmpDir string , stdout string ) {
226
239
assert .Contains (t , stdout , "Warning: Using self-signed certificates is not recommended and could lead to exposure of sensitive data" )
227
240
assertCertWritten (t , conjurrcInTmpDir , stdout )
@@ -307,7 +320,10 @@ func TestInitCmd(t *testing.T) {
307
320
conjurrcInTmpDir := tempDir + "/.conjurrc"
308
321
309
322
if tc .beforeTest != nil {
310
- tc .beforeTest (t , conjurrcInTmpDir )
323
+ cleanup := tc .beforeTest (t , conjurrcInTmpDir )
324
+ if cleanup != nil {
325
+ defer cleanup ()
326
+ }
311
327
}
312
328
313
329
// --file default to conjurrcInTmpDir. It can always be overwritten in each test case
@@ -389,3 +405,18 @@ func assertCertWritten(t *testing.T, conjurrcInTmpDir string, stdout string) {
389
405
data , _ = os .ReadFile (expectedCertPath )
390
406
assert .Contains (t , string (data ), "-----BEGIN CERTIFICATE-----" )
391
407
}
408
+
409
+ func startSelfSignedServer (t * testing.T , port int ) func () {
410
+ server := httptest .NewUnstartedServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
411
+ fmt .Fprintln (w , "Hello, client" )
412
+ }))
413
+ l , err := net .Listen ("tcp" , "localhost:8080" )
414
+ if err != nil {
415
+ assert .NoError (t , err , "unabled to start test server" )
416
+ }
417
+
418
+ server .Listener = l
419
+ server .StartTLS ()
420
+
421
+ return func () { server .Close () }
422
+ }
0 commit comments