-
Notifications
You must be signed in to change notification settings - Fork 69
/
ip_allowlist_test.go
195 lines (187 loc) · 5.15 KB
/
ip_allowlist_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package iam
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
"github.com/tj/assert"
)
func TestIAM_DisableIPAllowlist(t *testing.T) {
tests := map[string]struct {
responseStatus int
expectedPath string
responseBody string
withError func(*testing.T, error)
}{
"204 no content": {
responseStatus: http.StatusNoContent,
expectedPath: "/identity-management/v3/user-admin/ip-acl/allowlist/disable",
},
"500 internal server error": {
responseStatus: http.StatusInternalServerError,
responseBody: `
{
"type": "internal_error",
"title": "Internal Server Error",
"detail": "Error making request",
"status": 500
}`,
expectedPath: "/identity-management/v3/user-admin/ip-acl/allowlist/disable",
withError: func(t *testing.T, e error) {
err := Error{
Type: "internal_error",
Title: "Internal Server Error",
Detail: "Error making request",
StatusCode: http.StatusInternalServerError,
}
assert.Equal(t, true, err.Is(e))
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, tc.expectedPath, r.URL.String())
assert.Equal(t, http.MethodPost, r.Method)
w.WriteHeader(tc.responseStatus)
if tc.responseBody != "" {
_, err := w.Write([]byte(tc.responseBody))
assert.NoError(t, err)
}
}))
client := mockAPIClient(t, mockServer)
err := client.DisableIPAllowlist(context.Background())
if tc.withError != nil {
tc.withError(t, err)
return
}
require.NoError(t, err)
})
}
}
func TestIAM_EnableIPAllowlist(t *testing.T) {
tests := map[string]struct {
responseStatus int
expectedPath string
responseBody string
withError func(*testing.T, error)
}{
"204 no content": {
responseStatus: http.StatusNoContent,
expectedPath: "/identity-management/v3/user-admin/ip-acl/allowlist/enable",
},
"500 internal server error": {
responseStatus: http.StatusInternalServerError,
responseBody: `
{
"type": "internal_error",
"title": "Internal Server Error",
"detail": "Error making request",
"status": 500
}`,
expectedPath: "/identity-management/v3/user-admin/ip-acl/allowlist/enable",
withError: func(t *testing.T, e error) {
err := Error{
Type: "internal_error",
Title: "Internal Server Error",
Detail: "Error making request",
StatusCode: http.StatusInternalServerError,
}
assert.Equal(t, true, err.Is(e))
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, tc.expectedPath, r.URL.String())
assert.Equal(t, http.MethodPost, r.Method)
w.WriteHeader(tc.responseStatus)
if tc.responseBody != "" {
_, err := w.Write([]byte(tc.responseBody))
assert.NoError(t, err)
}
}))
client := mockAPIClient(t, mockServer)
err := client.EnableIPAllowlist(context.Background())
if tc.withError != nil {
tc.withError(t, err)
return
}
require.NoError(t, err)
})
}
}
func TestIAM_GetIPAllowlistStatus(t *testing.T) {
tests := map[string]struct {
responseStatus int
responseBody string
expectedPath string
expectedResponse *GetIPAllowlistStatusResponse
withError func(*testing.T, error)
}{
"200 OK enabled true": {
responseStatus: 200,
expectedPath: "/identity-management/v3/user-admin/ip-acl/allowlist/status",
responseBody: `
{
"enabled": true
}`,
expectedResponse: &GetIPAllowlistStatusResponse{
Enabled: true,
},
},
"200 OK enabled false": {
responseStatus: 200,
expectedPath: "/identity-management/v3/user-admin/ip-acl/allowlist/status",
responseBody: `
{
"enabled": false
}`,
expectedResponse: &GetIPAllowlistStatusResponse{
Enabled: false,
},
},
"500 internal server error": {
responseStatus: 500,
expectedPath: "/identity-management/v3/user-admin/ip-acl/allowlist/status",
responseBody: `
{
"type": "internal_error",
"title": "Internal Server Error",
"detail": "Error making request",
"status": 500
}
`,
withError: func(t *testing.T, e error) {
err := &Error{
Type: "internal_error",
Title: "Internal Server Error",
StatusCode: 500,
Detail: "Error making request",
}
assert.Equal(t, true, err.Is(e))
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, tc.expectedPath, r.URL.String())
assert.Equal(t, http.MethodGet, r.Method)
w.WriteHeader(tc.responseStatus)
_, err := w.Write([]byte(tc.responseBody))
assert.NoError(t, err)
}))
client := mockAPIClient(t, mockServer)
result, err := client.GetIPAllowlistStatus(context.Background())
if tc.withError != nil {
tc.withError(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tc.expectedResponse, result)
})
}
}