File: waf_test.go

package info (click to toggle)
golang-github-sethvargo-go-fastly 1.2.1%2Bgit20190805.5c6c8bd-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,584 kB
  • sloc: makefile: 71
file content (325 lines) | stat: -rw-r--r-- 7,702 bytes parent folder | download | duplicates (2)
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
package fastly

import (
	"bytes"
	"io/ioutil"
	"testing"
)

func TestClient_WAFs(t *testing.T) {
}

func TestClient_ListWAFs_validation(t *testing.T) {
	var err error
	_, err = testClient.ListWAFs(&ListWAFsInput{
		Service: "",
	})
	if err != ErrMissingService {
		t.Errorf("bad error: %s", err)
	}

	_, err = testClient.ListWAFs(&ListWAFsInput{
		Service: "foo",
		Version: 0,
	})
	if err != ErrMissingVersion {
		t.Errorf("bad error: %s", err)
	}
}

func TestClient_CreateWAF_validation(t *testing.T) {
	var err error
	_, err = testClient.CreateWAF(&CreateWAFInput{
		Service: "",
	})
	if err != ErrMissingService {
		t.Errorf("bad error: %s", err)
	}

	_, err = testClient.CreateWAF(&CreateWAFInput{
		Service: "foo",
		Version: 0,
	})
	if err != ErrMissingVersion {
		t.Errorf("bad error: %s", err)
	}
}

func TestClient_GetWAF_validation(t *testing.T) {
	var err error
	_, err = testClient.GetWAF(&GetWAFInput{
		Service: "",
	})
	if err != ErrMissingService {
		t.Errorf("bad error: %s", err)
	}

	_, err = testClient.GetWAF(&GetWAFInput{
		Service: "foo",
		Version: 0,
	})
	if err != ErrMissingVersion {
		t.Errorf("bad error: %s", err)
	}

	_, err = testClient.GetWAF(&GetWAFInput{
		Service: "foo",
		Version: 1,
		ID:      "",
	})
	if err != ErrMissingWAFID {
		t.Errorf("bad error: %s", err)
	}
}

//
// func TestClient_UpdateWAF_validation(t *testing.T) {
// 	var err error
// 	_, err = testClient.UpdateWAF(&UpdateWAFInput{
// 		Service: "",
// 	})
// 	if err != ErrMissingService {
// 		t.Errorf("bad error: %s", err)
// 	}
//
// 	_, err = testClient.UpdateWAF(&UpdateWAFInput{
// 		Service: "foo",
// 		Version: 0,
// 	})
// 	if err != ErrMissingVersion {
// 		t.Errorf("bad error: %s", err)
// 	}
//
// 	_, err = testClient.UpdateWAF(&UpdateWAFInput{
// 		Service: "foo",
// 		Version: 1,
// 		WAFID:   "",
// 	})
// 	if err != ErrMissingWAFID {
// 		t.Errorf("bad error: %s", err)
// 	}
// }
//
// func TestClient_DeleteWAF_validation(t *testing.T) {
// 	var err error
// 	err = testClient.DeleteWAF(&DeleteWAFInput{
// 		Service: "",
// 	})
// 	if err != ErrMissingService {
// 		t.Errorf("bad error: %s", err)
// 	}
//
// 	err = testClient.DeleteWAF(&DeleteWAFInput{
// 		Service: "foo",
// 		Version: 0,
// 	})
// 	if err != ErrMissingVersion {
// 		t.Errorf("bad error: %s", err)
// 	}
//
// 	err = testClient.DeleteWAF(&DeleteWAFInput{
// 		Service: "foo",
// 		Version: 1,
// 		WAFID:   "",
// 	})
// 	if err != ErrMissingWAFID {
// 		t.Errorf("bad error: %s", err)
// 	}
// }

func TestUpdateWAFRuleStatusesInput_validate(t *testing.T) {
	tests := []struct {
		description string
		input       UpdateWAFRuleStatusInput
		expected    error
	}{
		{
			description: "Accepts valid input",
			input: UpdateWAFRuleStatusInput{
				ID:      "as098k-8104",
				RuleID:  8104,
				Service: "108asj1",
				WAF:     "as098k",
				Status:  "block",
			},
			expected: nil,
		},
		{
			description: "Rejects input with missing int field",
			input: UpdateWAFRuleStatusInput{
				ID:      "as098k-8104",
				Service: "108asj1",
				WAF:     "as098k",
				Status:  "block",
			},
			expected: ErrMissingRuleID,
		},
		{
			description: "Rejects input with missing string field",
			input: UpdateWAFRuleStatusInput{
				ID:     "as098k-8104",
				RuleID: 8104,
				WAF:    "as098k",
				Status: "block",
			},
			expected: ErrMissingService,
		},
	}
	for _, testcase := range tests {
		err := testcase.input.validate()
		if err != testcase.expected {
			t.Errorf("In test %s: Expected %v,got %v", testcase.description, testcase.expected, err)
		}
	}
}

func TestUpdateWAFRuleTagStatusInput_validate(t *testing.T) {
	tests := []struct {
		description string
		input       UpdateWAFRuleTagStatusInput
		expected    error
	}{
		{
			description: "Accepts valid input",
			input: UpdateWAFRuleTagStatusInput{
				Tag:     "lala tag la",
				Service: "108asj1",
				WAF:     "as098k",
				Status:  "block",
			},
			expected: nil,
		},
		{
			description: "Rejects input with missing string field",
			input: UpdateWAFRuleTagStatusInput{
				Service: "108asj1",
				WAF:     "as098k",
				Status:  "block",
			},
			expected: ErrMissingTag,
		},
	}
	for _, testcase := range tests {
		err := testcase.input.validate()
		if err != testcase.expected {
			t.Errorf("In test %s: Expected %v,got %v", testcase.description, testcase.expected, err)
		}
	}
}

func TestGetWAFRuleStatusesInput_formatFilters(t *testing.T) {
	tests := []struct {
		description string
		filters     GetWAFRuleStatusesFilters
		expected    map[string]string
	}{
		{
			description: "converts both strings and ints to strings",
			filters: GetWAFRuleStatusesFilters{
				Status:   "log",
				Accuracy: 10,
				Version:  "180ad",
			},
			expected: map[string]string{
				"filter[status]":         "log",
				"filter[rule][accuracy]": "10",
				"filter[rule][version]":  "180ad",
			},
		},
		{
			description: "converts arrays to strings",
			filters: GetWAFRuleStatusesFilters{
				Status:  "log",
				Version: "181ad",
				Tags:    []int{18, 1, 1093, 86308},
			},
			expected: map[string]string{
				"filter[status]":        "log",
				"filter[rule][version]": "181ad",
				"include":               "18,1,1093,86308",
			},
		},
	}
	for _, testcase := range tests {
		input := GetWAFRuleStatusesInput{
			Filters: testcase.filters,
		}
		answer := input.formatFilters()
		if len(answer) != len(testcase.expected) {
			t.Errorf("In test %s: Expected map with %d entries,got one with %d", testcase.description, len(testcase.expected), len(answer))
		}
		for key, value := range testcase.expected {
			if answer[key] != value {
				t.Errorf("In test %s: Expected %s key to have value %s, got %s", testcase.description, key, value, answer[key])
			}
		}
	}
}

func TestGetPages(t *testing.T) {
	tests := []struct {
		description   string
		input         string
		expectedPages paginationInfo
		expectedErr   error
	}{
		{
			description: "returns the next page",
			input:       `{"links": {"next": "https://google.com/2"}, "data": []}`,
			expectedPages: paginationInfo{
				Next: "https://google.com/2",
			},
		},
		{
			description: "returns multiple pages",
			input:       `{"links": {"next": "https://google.com/2", "first": "https://google.com/1"}, "data": []}`,
			expectedPages: paginationInfo{
				First: "https://google.com/1",
				Next:  "https://google.com/2",
			},
		},
		{
			description:   "returns no pages",
			input:         `{"data": []}`,
			expectedPages: paginationInfo{},
		},
	}
	for _, testcase := range tests {
		pages, reader, err := getPages(bytes.NewReader([]byte(testcase.input)))
		if pages != testcase.expectedPages {
			t.Errorf("Test %s: Expected pages %+v, got %+v", testcase.description, testcase.expectedPages, pages)
		}

		// we expect to be able to get the original input out again
		resultBytes, _ := ioutil.ReadAll(reader)
		if string(resultBytes) != testcase.input {
			t.Errorf("Test %s: Expected body %s, got %s", testcase.description, testcase.input, string(resultBytes))
		}
		if err != testcase.expectedErr {
			t.Errorf("Test %s: Expected error %v, got %v", testcase.description, testcase.expectedErr, err)
		}
	}
}

func TestUpdateWAFConfigSetInput_validate(t *testing.T) {
	tests := []struct {
		description string
		input       UpdateWAFConfigSetInput
		expected    error
	}{
		{
			description: "Accepts valid input",
			input: UpdateWAFConfigSetInput{
				WAFList:     []ConfigSetWAFs{{ID: "derpID"}},
				ConfigSetID: "derpConfigSet",
			},
			expected: nil,
		},
	}
	for _, testcase := range tests {
		err := testcase.input.validate()
		if err != testcase.expected {
			t.Errorf("In test %s: Expected %v,got %v", testcase.description, testcase.expected, err)
		}
	}
}