-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathadmin_egress_ips_test.go
More file actions
329 lines (311 loc) · 8.68 KB
/
Copy pathadmin_egress_ips_test.go
File metadata and controls
329 lines (311 loc) · 8.68 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package http
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log/slog"
nethttp "net/http"
"net/http/httptest"
"testing"
"time"
"database/sql"
"github.com/zanel1u/cloud-cli-proxy/internal/store/repository"
)
type stubEgressIPStore struct {
ips []repository.EgressIP
ip repository.EgressIP
err error
createIP repository.EgressIP
createErr error
updateIP repository.EgressIP
updateErr error
deleteErr error
}
func (s *stubEgressIPStore) ListEgressIPs(_ context.Context) ([]repository.EgressIP, error) {
return s.ips, s.err
}
func (s *stubEgressIPStore) GetEgressIP(_ context.Context, _ string) (repository.EgressIP, error) {
if s.err != nil {
return repository.EgressIP{}, s.err
}
return s.ip, nil
}
func (s *stubEgressIPStore) CreateEgressIP(_ context.Context, _ repository.CreateEgressIPParams) (repository.EgressIP, error) {
if s.createErr != nil {
return repository.EgressIP{}, s.createErr
}
return s.createIP, nil
}
func (s *stubEgressIPStore) UpdateEgressIP(_ context.Context, _ string, _ repository.UpdateEgressIPParams) (repository.EgressIP, error) {
if s.updateErr != nil {
return repository.EgressIP{}, s.updateErr
}
return s.updateIP, nil
}
func (s *stubEgressIPStore) DeleteEgressIP(_ context.Context, _ string) error {
return s.deleteErr
}
func (s *stubEgressIPStore) UpdateEgressIPDetectedAddress(_ context.Context, _ string, _ string) error {
return nil
}
func TestAdminEgressIPsHandler(t *testing.T) {
now := time.Now().Truncate(time.Second)
sampleIP := repository.EgressIP{
ID: "ip1", Label: "test-ip", IPAddress: "1.2.3.4",
Provider: "manual", Status: "available",
CreatedAt: now, UpdatedAt: now,
}
tests := []struct {
name string
method string
path string
body any
store *stubEgressIPStore
wantStatus int
wantField string
}{
{
name: "List egress IPs 200",
method: "GET",
path: "/v1/admin/egress-ips",
store: &stubEgressIPStore{ips: []repository.EgressIP{sampleIP}},
wantStatus: 200,
wantField: "egress_ips",
},
{
name: "List egress IPs store error 500",
method: "GET",
path: "/v1/admin/egress-ips",
store: &stubEgressIPStore{err: fmt.Errorf("db down")},
wantStatus: 500,
},
{
name: "Create egress IP 201",
method: "POST",
path: "/v1/admin/egress-ips",
body: map[string]any{
"label": "new-ip", "ip_address": "5.6.7.8",
"provider": "aws",
"proxy_config": map[string]any{
"type": "socks", "server": "proxy.example.com", "server_port": 1080,
},
},
store: &stubEgressIPStore{createIP: sampleIP},
wantStatus: 201,
wantField: "egress_ip",
},
{
name: "Create egress IP missing label 400",
method: "POST",
path: "/v1/admin/egress-ips",
body: map[string]string{"ip_address": "5.6.7.8"},
store: &stubEgressIPStore{},
wantStatus: 400,
},
{
name: "Create egress IP invalid IP 400",
method: "POST",
path: "/v1/admin/egress-ips",
body: map[string]string{"label": "bad", "ip_address": "not-an-ip"},
store: &stubEgressIPStore{},
wantStatus: 400,
},
{
name: "Create egress IP without detected address 201",
method: "POST",
path: "/v1/admin/egress-ips",
body: map[string]any{
"label": "pending-detection",
"ip_address": "",
"proxy_config": map[string]any{
"type": "socks", "server": "proxy.example.com", "server_port": 1080,
},
},
store: &stubEgressIPStore{createIP: sampleIP},
wantStatus: 201,
wantField: "egress_ip",
},
{
name: "Get egress IP 200",
method: "GET",
path: "/v1/admin/egress-ips/ip1",
store: &stubEgressIPStore{ip: sampleIP},
wantStatus: 200,
wantField: "egress_ip",
},
{
name: "Get egress IP 404",
method: "GET",
path: "/v1/admin/egress-ips/missing",
store: &stubEgressIPStore{err: sql.ErrNoRows},
wantStatus: 404,
},
{
name: "Update egress IP 200",
method: "PUT",
path: "/v1/admin/egress-ips/ip1",
body: map[string]any{
"label": "updated", "ip_address": "5.6.7.8",
"provider": "aws", "status": "available",
"proxy_config": map[string]any{
"type": "socks", "server": "proxy.example.com", "server_port": 1080,
},
},
store: &stubEgressIPStore{updateIP: sampleIP},
wantStatus: 200,
wantField: "egress_ip",
},
{
name: "Update egress IP 404",
method: "PUT",
path: "/v1/admin/egress-ips/missing",
body: map[string]any{
"label": "x",
"proxy_config": map[string]any{
"type": "socks", "server": "proxy.example.com", "server_port": 1080,
},
},
store: &stubEgressIPStore{updateErr: sql.ErrNoRows},
wantStatus: 404,
},
{
name: "Update egress IP invalid non-empty address 400",
method: "PUT",
path: "/v1/admin/egress-ips/ip1",
body: map[string]any{
"label": "updated",
"ip_address": "not-an-ip",
"provider": "manual",
"status": "available",
"proxy_config": map[string]any{
"type": "socks", "server": "proxy.example.com", "server_port": 1080,
},
},
store: &stubEgressIPStore{updateIP: sampleIP},
wantStatus: 400,
},
{
name: "Delete egress IP 204",
method: "DELETE",
path: "/v1/admin/egress-ips/ip1",
store: &stubEgressIPStore{},
wantStatus: 204,
},
{
name: "Delete egress IP 404",
method: "DELETE",
path: "/v1/admin/egress-ips/missing",
store: &stubEgressIPStore{deleteErr: sql.ErrNoRows},
wantStatus: 404,
},
{
name: "Create proxy egress IP 201",
method: "POST",
path: "/v1/admin/egress-ips",
body: map[string]any{
"label": "proxy-ip",
"ip_address": "5.6.7.8",
"provider": "manual",
"proxy_config": map[string]any{
"type": "socks", "server": "proxy.example.com", "server_port": 1080,
},
},
store: &stubEgressIPStore{createIP: repository.EgressIP{
ID: "ip2", Label: "proxy-ip", IPAddress: "5.6.7.8",
Provider: "manual", Status: "available",
CreatedAt: now, UpdatedAt: now,
}},
wantStatus: 201,
wantField: "egress_ip",
},
{
name: "Create missing proxy_config 400",
method: "POST",
path: "/v1/admin/egress-ips",
body: map[string]any{
"label": "no-config", "ip_address": "1.2.3.4",
},
store: &stubEgressIPStore{},
wantStatus: 400,
},
{
name: "Create unsupported outbound type 400",
method: "POST",
path: "/v1/admin/egress-ips",
body: map[string]any{
"label": "bad-type", "ip_address": "1.2.3.4",
"proxy_config": map[string]any{
"type": "unsupported", "server": "example.com", "server_port": 1080,
},
},
store: &stubEgressIPStore{},
wantStatus: 400,
},
{
name: "Create missing server 400",
method: "POST",
path: "/v1/admin/egress-ips",
body: map[string]any{
"label": "no-server", "ip_address": "1.2.3.4",
"proxy_config": map[string]any{
"type": "socks", "server_port": 1080,
},
},
store: &stubEgressIPStore{},
wantStatus: 400,
},
{
name: "Create vmess missing uuid 400",
method: "POST",
path: "/v1/admin/egress-ips",
body: map[string]any{
"label": "no-uuid", "ip_address": "1.2.3.4",
"proxy_config": map[string]any{
"type": "vmess", "server": "proxy.example.com", "server_port": 443,
},
},
store: &stubEgressIPStore{},
wantStatus: 400,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
events := &stubEventRecorder{}
router := adminTestRouter(t, Dependencies{
Logger: slog.Default(),
AdminEgressIPs: tt.store,
EventRecorder: events,
})
srv := httptest.NewServer(router)
defer srv.Close()
var body []byte
if tt.body != nil {
body, _ = json.Marshal(tt.body)
}
req, _ := nethttp.NewRequest(tt.method, srv.URL+tt.path, bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+validAdminToken(t))
req.Header.Set("Content-Type", "application/json")
resp, err := nethttp.DefaultClient.Do(req)
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != tt.wantStatus {
var respBody map[string]any
json.NewDecoder(resp.Body).Decode(&respBody)
t.Errorf("status = %d, want %d; body = %v", resp.StatusCode, tt.wantStatus, respBody)
return
}
if tt.wantField != "" && resp.StatusCode != 204 {
var respBody map[string]any
if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
t.Fatalf("decode response: %v", err)
}
if _, ok := respBody[tt.wantField]; !ok {
t.Errorf("response missing field %q: %v", tt.wantField, respBody)
}
}
})
}
}