-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
76 lines (67 loc) · 2.4 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"net/http"
"net/http/httptest"
"testing"
akeyless "github.com/akeylesslabs/akeyless-go/v2"
"github.com/stretchr/testify/assert"
)
func TestRetrieveListOfGatewaysUsingToken(t *testing.T) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.RequestURI {
case "/v2/gateways":
// return a successful response
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"clusters": [{"name": "test-gateway"}]}`))
case "/v2/gateways?token=expired":
// return a 401 error for expired tokens
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"error": "token expired"}`))
case "/v2/gateways?token=empty":
// return an empty list of gateways
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"clusters": []}`))
case "/v2/gateways?token=error":
// return an error response
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error": "internal server error"}`))
default:
http.NotFound(w, r)
}
}))
defer mockServer.Close()
// create a client with the URL of our mock server
client := akeyless.NewAPIClient(&akeyless.Configuration{
Servers: []akeyless.ServerConfiguration{
{
URL: mockServer.URL,
},
},
}).V2Api
t.Run("Successful call", func(t *testing.T) {
gatewayListResponse := retrieveListOfGatewaysUsingToken(client, "valid-token")
assert.NotNil(t, gatewayListResponse)
assert.NotEmpty(t, gatewayListResponse.Clusters)
})
t.Run("Expired token", func(t *testing.T) {
assert.PanicsWithValue(t, "Unable to to retrieve list of gateways with provided token:", func() {
retrieveListOfGatewaysUsingToken(client, "expired")
})
})
t.Run("Token not set", func(t *testing.T) {
assert.PanicsWithValue(t, "Akeyless token is not set. Please set the token using the -t or --token flag or set the AKEYLESS_TOKEN environment variable", func() {
retrieveListOfGatewaysUsingToken(client, "")
})
})
t.Run("Empty list of gateways", func(t *testing.T) {
gatewayListResponse := retrieveListOfGatewaysUsingToken(client, "empty")
assert.NotNil(t, gatewayListResponse)
assert.Empty(t, gatewayListResponse.Clusters)
})
t.Run("Error response from API Gateway", func(t *testing.T) {
assert.PanicsWithValue(t, "Unable to to retrieve list of gateways with provided token:", func() {
retrieveListOfGatewaysUsingToken(client, "error")
})
})
}