Skip to content

Commit fb2eb0e

Browse files
authored
Test logout in Operator API (#2085)
1 parent e7a36a1 commit fb2eb0e

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

.github/workflows/jobs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ jobs:
15361536
go tool cover -func=all.out | grep total > tmp2
15371537
result=`cat tmp2 | awk 'END {print $3}'`
15381538
result=${result%\%}
1539-
threshold=51.40
1539+
threshold=51.50
15401540
echo "Result:"
15411541
echo "$result%"
15421542
if (( $(echo "$result >= $threshold" |bc -l) )); then

operator-integration/tenant_test.go

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ import (
3939
"github.com/stretchr/testify/assert"
4040
)
4141

42-
var token string
42+
var (
43+
token string
44+
jwt string
45+
)
4346

4447
func inspectHTTPResponse(httpResponse *http.Response) string {
4548
/*
@@ -167,13 +170,12 @@ func TestMain(m *testing.M) {
167170
return
168171
}
169172
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 == "" {
172175
fmt.Println("jwt cannot be empty string")
173176
os.Exit(-1)
174177
}
175-
176-
response, err := LoginOperator(secret3)
178+
response, err := LoginOperator()
177179
if err != nil {
178180
log.Println(err)
179181
return
@@ -777,7 +779,7 @@ func TestCreateNamespace(t *testing.T) {
777779
}
778780
}
779781

780-
func LoginOperator(jwt string) (*http.Response, error) {
782+
func LoginOperator() (*http.Response, error) {
781783
/*
782784
Description: Login to Operator Console.
783785
URL: /login/operator
@@ -804,3 +806,65 @@ func LoginOperator(jwt string) (*http.Response, error) {
804806
response, err := client.Do(request)
805807
return response, err
806808
}
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

Comments
 (0)