@@ -39,7 +39,10 @@ import (
39
39
"github.com/stretchr/testify/assert"
40
40
)
41
41
42
- var token string
42
+ var (
43
+ token string
44
+ jwt string
45
+ )
43
46
44
47
func inspectHTTPResponse (httpResponse * http.Response ) string {
45
48
/*
@@ -167,13 +170,12 @@ func TestMain(m *testing.M) {
167
170
return
168
171
}
169
172
secret2 := out2 .String ()
170
- secret3 := decodeBase64 (secret2 [1 : len (secret2 )- 1 ])
171
- if secret3 == "" {
173
+ jwt := decodeBase64 (secret2 [1 : len (secret2 )- 1 ])
174
+ if jwt == "" {
172
175
fmt .Println ("jwt cannot be empty string" )
173
176
os .Exit (- 1 )
174
177
}
175
-
176
- response , err := LoginOperator (secret3 )
178
+ response , err := LoginOperator ()
177
179
if err != nil {
178
180
log .Println (err )
179
181
return
@@ -777,7 +779,7 @@ func TestCreateNamespace(t *testing.T) {
777
779
}
778
780
}
779
781
780
- func LoginOperator (jwt string ) (* http.Response , error ) {
782
+ func LoginOperator () (* http.Response , error ) {
781
783
/*
782
784
Description: Login to Operator Console.
783
785
URL: /login/operator
@@ -804,3 +806,65 @@ func LoginOperator(jwt string) (*http.Response, error) {
804
806
response , err := client .Do (request )
805
807
return response , err
806
808
}
809
+
810
+ func LogoutOperator () (* http.Response , error ) {
811
+ /*
812
+ Description: Logout from Operator.
813
+ URL: /logout
814
+ */
815
+ request , err := http .NewRequest (
816
+ "POST" ,
817
+ "http://localhost:9090/api/v1/logout" ,
818
+ nil ,
819
+ )
820
+ request .Header .Add ("Cookie" , fmt .Sprintf ("token=%s" , token ))
821
+ request .Header .Add ("Content-Type" , "application/json" )
822
+ if err != nil {
823
+ log .Println (err )
824
+ }
825
+ request .Header .Add ("Content-Type" , "application/json" )
826
+ client := & http.Client {
827
+ Timeout : 2 * time .Second ,
828
+ }
829
+ response , err := client .Do (request )
830
+ return response , err
831
+ }
832
+
833
+ func TestLogout (t * testing.T ) {
834
+ // Vars
835
+ assert := assert .New (t )
836
+
837
+ // 1. Logout
838
+ response , err := LogoutOperator ()
839
+ if err != nil {
840
+ log .Println (err )
841
+ return
842
+ }
843
+ if response != nil {
844
+ assert .Equal (
845
+ 200 ,
846
+ response .StatusCode ,
847
+ inspectHTTPResponse (response ),
848
+ )
849
+ }
850
+
851
+ // 2. Login to recover token
852
+ response , err = LoginOperator ()
853
+ if err != nil {
854
+ log .Println (err )
855
+ return
856
+ }
857
+ if response != nil {
858
+ for _ , cookie := range response .Cookies () {
859
+ if cookie .Name == "token" {
860
+ token = cookie .Value
861
+ break
862
+ }
863
+ }
864
+ }
865
+
866
+ // Verify token
867
+ if token == "" {
868
+ assert .Fail ("authentication token not found in cookies response" )
869
+ }
870
+ }
0 commit comments