Debian Codegraph @

fastly/waf_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package fastly
2
3 import (
4 "bytes"
5 "io/ioutil"
6 "testing"
7 )
8
9 func TestClient_WAFs(t *testing.T) {
10 }
11
12 func TestClient_ListWAFs_validation(t *testing.T) {
13 var err error
14 _, err = testClient.ListWAFs(&ListWAFsInput{
15 Service: "",
16 })
17 if err != ErrMissingService {
18 t.Errorf("bad error: %s", err)
19 }
20
21 _, err = testClient.ListWAFs(&ListWAFsInput{
22 Service: "foo",
23 Version: 0,
24 })
25 if err != ErrMissingVersion {
26 t.Errorf("bad error: %s", err)
27 }
28 }
29
30 func TestClient_CreateWAF_validation(t *testing.T) {
31 var err error
32 _, err = testClient.CreateWAF(&CreateWAFInput{
33 Service: "",
34 })
35 if err != ErrMissingService {
36 t.Errorf("bad error: %s", err)
37 }
38
39 _, err = testClient.CreateWAF(&CreateWAFInput{
40 Service: "foo",
41 Version: 0,
42 })
43 if err != ErrMissingVersion {
44 t.Errorf("bad error: %s", err)
45 }
46 }
47
48 func TestClient_GetWAF_validation(t *testing.T) {
49 var err error
50 _, err = testClient.GetWAF(&GetWAFInput{
51 Service: "",
52 })
53 if err != ErrMissingService {
54 t.Errorf("bad error: %s", err)
55 }
56
57 _, err = testClient.GetWAF(&GetWAFInput{
58 Service: "foo",
59 Version: 0,
60 })
61 if err != ErrMissingVersion {
62 t.Errorf("bad error: %s", err)
63 }
64
65 _, err = testClient.GetWAF(&GetWAFInput{
66 Service: "foo",
67 Version: 1,
68 ID: "",
69 })
70 if err != ErrMissingWAFID {
71 t.Errorf("bad error: %s", err)
72 }
73 }
74
75 //
76 // func TestClient_UpdateWAF_validation(t *testing.T) {
77 // var err error
78 // _, err = testClient.UpdateWAF(&UpdateWAFInput{
79 // Service: "",
80 // })
81 // if err != ErrMissingService {
82 // t.Errorf("bad error: %s", err)
83 // }
84 //
85 // _, err = testClient.UpdateWAF(&UpdateWAFInput{
86 // Service: "foo",
87 // Version: 0,
88 // })
89 // if err != ErrMissingVersion {
90 // t.Errorf("bad error: %s", err)
91 // }
92 //
93 // _, err = testClient.UpdateWAF(&UpdateWAFInput{
94 // Service: "foo",
95 // Version: 1,
96 // WAFID: "",
97 // })
98 // if err != ErrMissingWAFID {
99 // t.Errorf("bad error: %s", err)
100 // }
101 // }
102 //
103 // func TestClient_DeleteWAF_validation(t *testing.T) {
104 // var err error
105 // err = testClient.DeleteWAF(&DeleteWAFInput{
106 // Service: "",
107 // })
108 // if err != ErrMissingService {
109 // t.Errorf("bad error: %s", err)
110 // }
111 //
112 // err = testClient.DeleteWAF(&DeleteWAFInput{
113 // Service: "foo",
114 // Version: 0,
115 // })
116 // if err != ErrMissingVersion {
117 // t.Errorf("bad error: %s", err)
118 // }
119 //
120 // err = testClient.DeleteWAF(&DeleteWAFInput{
121 // Service: "foo",
122 // Version: 1,
123 // WAFID: "",
124 // })
125 // if err != ErrMissingWAFID {
126 // t.Errorf("bad error: %s", err)
127 // }
128 // }
129
130 func TestUpdateWAFRuleStatusesInput_validate(t *testing.T) {
131 tests := []struct {
132 description string
133 input UpdateWAFRuleStatusInput
134 expected error
135 }{
136 {
137 description: "Accepts valid input",
138 input: UpdateWAFRuleStatusInput{
139 ID: "as098k-8104",
140 RuleID: 8104,
141 Service: "108asj1",
142 WAF: "as098k",
143 Status: "block",
144 },
145 expected: nil,
146 },
147 {
148 description: "Rejects input with missing int field",
149 input: UpdateWAFRuleStatusInput{
150 ID: "as098k-8104",
151 Service: "108asj1",
152 WAF: "as098k",
153 Status: "block",
154 },
155 expected: ErrMissingRuleID,
156 },
157 {
158 description: "Rejects input with missing string field",
159 input: UpdateWAFRuleStatusInput{
160 ID: "as098k-8104",
161 RuleID: 8104,
162 WAF: "as098k",
163 Status: "block",
164 },
165 expected: ErrMissingService,
166 },
167 }
168 for _, testcase := range tests {
169 err := testcase.input.validate()
170 if err != testcase.expected {
171 t.Errorf("In test %s: Expected %v,got %v", testcase.description, testcase.expected, err)
172 }
173 }
174 }
175
176 func TestUpdateWAFRuleTagStatusInput_validate(t *testing.T) {
177 tests := []struct {
178 description string
179 input UpdateWAFRuleTagStatusInput
180 expected error
181 }{
182 {
183 description: "Accepts valid input",
184 input: UpdateWAFRuleTagStatusInput{
185 Tag: "lala tag la",
186 Service: "108asj1",
187 WAF: "as098k",
188 Status: "block",
189 },
190 expected: nil,
191 },
192 {
193 description: "Rejects input with missing string field",
194 input: UpdateWAFRuleTagStatusInput{
195 Service: "108asj1",
196 WAF: "as098k",
197 Status: "block",
198 },
199 expected: ErrMissingTag,
200 },
201 }
202 for _, testcase := range tests {
203 err := testcase.input.validate()
204 if err != testcase.expected {
205 t.Errorf("In test %s: Expected %v,got %v", testcase.description, testcase.expected, err)
206 }
207 }
208 }
209
210 func TestGetWAFRuleStatusesInput_formatFilters(t *testing.T) {
211 tests := []struct {
212 description string
213 filters GetWAFRuleStatusesFilters
214 expected map[string]string
215 }{
216 {
217 description: "converts both strings and ints to strings",
218 filters: GetWAFRuleStatusesFilters{
219 Status: "log",
220 Accuracy: 10,
221 Version: "180ad",
222 },
223 expected: map[string]string{
224 "filter[status]": "log",
225 "filter[rule][accuracy]": "10",
226 "filter[rule][version]": "180ad",
227 },
228 },
229 {
230 description: "converts arrays to strings",
231 filters: GetWAFRuleStatusesFilters{
232 Status: "log",
233 Version: "181ad",
234 Tags: []int{18, 1, 1093, 86308},
235 },
236 expected: map[string]string{
237 "filter[status]": "log",
238 "filter[rule][version]": "181ad",
239 "include": "18,1,1093,86308",
240 },
241 },
242 }
243 for _, testcase := range tests {
244 input := GetWAFRuleStatusesInput{
245 Filters: testcase.filters,
246 }
247 answer := input.formatFilters()
248 if len(answer) != len(testcase.expected) {
249 t.Errorf("In test %s: Expected map with %d entries,got one with %d", testcase.description, len(testcase.expected), len(answer))
250 }
251 for key, value := range testcase.expected {
252 if answer[key] != value {
253 t.Errorf("In test %s: Expected %s key to have value %s, got %s", testcase.description, key, value, answer[key])
254 }
255 }
256 }
257 }
258
259 func TestGetPages(t *testing.T) {
260 tests := []struct {
261 description string
262 input string
263 expectedPages paginationInfo
264 expectedErr error
265 }{
266 {
267 description: "returns the next page",
268 input: `{"links": {"next": "https://google.com/2"}, "data": []}`,
269 expectedPages: paginationInfo{
270 Next: "https://google.com/2",
271 },
272 },
273 {
274 description: "returns multiple pages",
275 input: `{"links": {"next": "https://google.com/2", "first": "https://google.com/1"}, "data": []}`,
276 expectedPages: paginationInfo{
277 First: "https://google.com/1",
278 Next: "https://google.com/2",
279 },
280 },
281 {
282 description: "returns no pages",
283 input: `{"data": []}`,
284 expectedPages: paginationInfo{},
285 },
286 }
287 for _, testcase := range tests {
288 pages, reader, err := getPages(bytes.NewReader([]byte(testcase.input)))
289 if pages != testcase.expectedPages {
290 t.Errorf("Test %s: Expected pages %+v, got %+v", testcase.description, testcase.expectedPages, pages)
291 }
292
293 // we expect to be able to get the original input out again
294 resultBytes, _ := ioutil.ReadAll(reader)
295 if string(resultBytes) != testcase.input {
296 t.Errorf("Test %s: Expected body %s, got %s", testcase.description, testcase.input, string(resultBytes))
297 }
298 if err != testcase.expectedErr {
299 t.Errorf("Test %s: Expected error %v, got %v", testcase.description, testcase.expectedErr, err)
300 }
301 }
302 }
303
304 func TestUpdateWAFConfigSetInput_validate(t *testing.T) {
305 tests := []struct {
306 description string
307 input UpdateWAFConfigSetInput
308 expected error
309 }{
310 {
311 description: "Accepts valid input",
312 input: UpdateWAFConfigSetInput{
313 WAFList: []ConfigSetWAFs{{ID: "derpID"}},
314 ConfigSetID: "derpConfigSet",
315 },
316 expected: nil,
317 },
318 }
319 for _, testcase := range tests {
320 err := testcase.input.validate()
321 if err != testcase.expected {
322 t.Errorf("In test %s: Expected %v,got %v", testcase.description, testcase.expected, err)
323 }
324 }
325 }