@@ -20,10 +20,12 @@ import (
2020 "bytes"
2121 "encoding/json"
2222 "fmt"
23+ "io"
2324 "log"
2425 "net/http"
26+ "net/url"
27+ "os"
2528 "os/exec"
26- "strconv"
2729 "strings"
2830 "testing"
2931 "time"
@@ -36,8 +38,12 @@ import (
3638
3739var token string
3840
39- func initConsoleServer () (* restapi.Server , error ) {
40- // os.Setenv("CONSOLE_MINIO_SERVER", "localhost:9000")
41+ func initConsoleServer (consoleIDPURL string ) (* restapi.Server , error ) {
42+ // Configure Console Server with vars to get the idp config from the container
43+ os .Setenv ("CONSOLE_IDP_URL" , consoleIDPURL )
44+ os .Setenv ("CONSOLE_IDP_CLIENT_ID" , "minio-client-app" )
45+ os .Setenv ("CONSOLE_IDP_SECRET" , "minio-client-app-secret" )
46+ os .Setenv ("CONSOLE_IDP_CALLBACK" , "http://127.0.0.1:9090/oauth_callback" )
4147
4248 swaggerSpec , err := loads .Embedded (restapi .SwaggerJSON , restapi .FlatSwaggerJSON )
4349 if err != nil {
@@ -58,10 +64,9 @@ func initConsoleServer() (*restapi.Server, error) {
5864 server := restapi .NewServer (api )
5965 // register all APIs
6066 server .ConfigureAPI ()
61- consolePort , _ := strconv .Atoi ("9090" )
6267
6368 server .Host = "0.0.0.0"
64- server .Port = consolePort
69+ server .Port = 9090
6570 restapi .Port = "9090"
6671 restapi .Hostname = "0.0.0.0"
6772
@@ -74,7 +79,7 @@ func TestMain(t *testing.T) {
7479 // start console server
7580 go func () {
7681 fmt .Println ("start server" )
77- srv , err := initConsoleServer ()
82+ srv , err := initConsoleServer ("http://dex:5556/dex/.well-known/openid-configuration" )
7883 if err != nil {
7984 log .Println (err )
8085 log .Println ("init fail" )
@@ -90,43 +95,79 @@ func TestMain(t *testing.T) {
9095 Timeout : 2 * time .Second ,
9196 }
9297
98+ // Let's move this API here to increment our coverage
99+ getRequest , getError := http .NewRequest ("GET" , "http://localhost:9090/api/v1/login" , nil )
100+ if getError != nil {
101+ log .Println (getError )
102+ return
103+ }
104+ getRequest .Header .Add ("Content-Type" , "application/json" )
105+ getResponse , getErr := client .Do (getRequest )
106+ // current value:
107+ // {"loginStrategy":"form"}
108+ // but we want our console server to provide loginStrategy = redirect for SSO
109+ if getErr != nil {
110+ log .Println (getErr )
111+ return
112+ }
113+
114+ body , err := io .ReadAll (getResponse .Body )
115+ getResponse .Body .Close ()
116+ if getResponse .StatusCode > 299 {
117+ log .Fatalf ("Response failed with status code: %d and\n body: %s\n " , getResponse .StatusCode , body )
118+ }
119+ if err != nil {
120+ log .Fatal (err )
121+ }
122+ var jsonMap map [string ]interface {}
123+ json .Unmarshal (body , & jsonMap )
124+ fmt .Println (jsonMap ["redirect" ])
125+ redirect := jsonMap ["redirect" ]
126+ redirectAsString := fmt .Sprint (redirect )
127+ fmt .Println (redirectAsString )
128+
93129 // execute script to get the code and state
94- cmd , err := exec .Command ("python3" , "dex-requests.py" ).Output ()
130+ cmd , err := exec .Command ("python3" , "dex-requests.py" , redirectAsString ).Output ()
95131 if err != nil {
96132 fmt .Printf ("error %s" , err )
97133 }
98- output := string (cmd )
99-
100- fmt .Println (" " )
101- fmt .Println (" " )
102- fmt .Println ("output:" )
103- fmt .Println (output )
104- fmt .Println (" " )
105- fmt .Println (" " )
106-
107- temp := strings .Split (output , "\n " )
108-
109- fmt .Println (" " )
110- fmt .Println (" " )
111- fmt .Println ("temp:" )
112- fmt .Println (temp )
113- fmt .Println (" " )
114- fmt .Println (" " )
115-
116- fmt .Println ("index0" )
117- fmt .Println (temp [0 ])
134+ urlOutput := string (cmd )
135+ requestLoginBody := bytes .NewReader ([]byte ("login=dillon%40example.io&password=dillon" ))
136+
137+ // parse url remove carriage return
138+ temp2 := strings .Split (urlOutput , "\n " )
139+ fmt .Println ("temp2: " , temp2 )
140+ urlOutput = temp2 [0 ] // remove carriage return to avoid invalid control character in url
141+
142+ // validate url
143+ urlParseResult , urlParseError := url .Parse (urlOutput )
144+ if urlParseError != nil {
145+ panic (urlParseError )
146+ }
147+ fmt .Println (urlParseResult )
118148
119- if len (temp ) >= 2 {
120- fmt .Println ("index 1" )
121- fmt .Println (temp [1 ])
122- } else {
123- assert .Fail ("temp len is less than 2" , len (temp ))
124- return
149+ // prepare for post
150+ httpRequestLogin , newRequestError := http .NewRequest (
151+ "POST" ,
152+ urlOutput ,
153+ requestLoginBody ,
154+ )
155+ fmt .Println (newRequestError )
156+ httpRequestLogin .Header .Add ("Content-Type" , "application/x-www-form-urlencoded" )
157+ responseLogin , errorLogin := client .Do (httpRequestLogin )
158+ if errorLogin != nil {
159+ log .Println (errorLogin )
125160 }
161+ rawQuery := responseLogin .Request .URL .RawQuery
162+ fmt .Println (rawQuery )
163+ splitRawQuery := strings .Split (rawQuery , "&state=" )
164+ codeValue := strings .ReplaceAll (splitRawQuery [0 ], "code=" , "" )
165+ stateValue := splitRawQuery [1 ]
166+ fmt .Println ("stop" , splitRawQuery , codeValue , stateValue )
126167
127168 // get login credentials
128- codeVarIable := strings .TrimSpace (temp [ 0 ] )
129- stateVarIabl := strings .TrimSpace (temp [ 1 ] )
169+ codeVarIable := strings .TrimSpace (codeValue )
170+ stateVarIabl := strings .TrimSpace (stateValue )
130171 requestData := map [string ]string {
131172 "code" : codeVarIable ,
132173 "state" : stateVarIabl ,
@@ -137,7 +178,7 @@ func TestMain(t *testing.T) {
137178
138179 request , _ := http .NewRequest (
139180 "POST" ,
140- "http://localhost:9001 /api/v1/login/oauth2/auth" ,
181+ "http://localhost:9090 /api/v1/login/oauth2/auth" ,
141182 requestDataBody ,
142183 )
143184 request .Header .Add ("Content-Type" , "application/json" )
@@ -146,7 +187,6 @@ func TestMain(t *testing.T) {
146187 if err != nil {
147188 log .Println (err )
148189 }
149-
150190 if response != nil {
151191 for _ , cookie := range response .Cookies () {
152192 if cookie .Name == "token" {
@@ -162,3 +202,49 @@ func TestMain(t *testing.T) {
162202 fmt .Println (token )
163203 }
164204}
205+
206+ func TestBadLogin (t * testing.T ) {
207+ assert := assert .New (t )
208+
209+ // start console server
210+ go func () {
211+ fmt .Println ("start server" )
212+ srv , err := initConsoleServer ("http://dex:5556" )
213+ if err != nil {
214+ log .Println (err )
215+ log .Println ("init fail" )
216+ return
217+ }
218+ srv .Serve ()
219+ }()
220+ fmt .Println ("sleeping" )
221+ time .Sleep (2 * time .Second )
222+
223+ client := & http.Client {
224+ Timeout : 2 * time .Second ,
225+ }
226+
227+ // get login credentials
228+ codeVarIable := "invalidCode"
229+ stateVarIabl := "invalidState"
230+ requestData := map [string ]string {
231+ "code" : codeVarIable ,
232+ "state" : stateVarIabl ,
233+ }
234+ requestDataJSON , _ := json .Marshal (requestData )
235+
236+ requestDataBody := bytes .NewReader (requestDataJSON )
237+
238+ request , _ := http .NewRequest (
239+ "POST" ,
240+ "http://localhost:9090/api/v1/login/oauth2/auth" ,
241+ requestDataBody ,
242+ )
243+ request .Header .Add ("Content-Type" , "application/json" )
244+
245+ response , err := client .Do (request )
246+ fmt .Println (response )
247+ fmt .Println (err )
248+ expectedError := response .Status
249+ assert .Equal ("500 Internal Server Error" , expectedError )
250+ }
0 commit comments