-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathip_list_test.go
More file actions
169 lines (145 loc) · 5.34 KB
/
Copy pathip_list_test.go
File metadata and controls
169 lines (145 loc) · 5.34 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
package wallarm
import (
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIPListRead_GroupedEntries(t *testing.T) {
setup()
defer teardown()
requests := 0
mux.HandleFunc("/v1/blocklist/clients/17/groups", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
assert.Equal(t, "block", r.URL.Query().Get("filter[list]"))
assert.ElementsMatch(t, []string{"subnet", "proxy_type", "datacenter", "location"}, r.URL.Query()["filter[rule_type][]"])
assert.Equal(t, "1", r.URL.Query().Get("limit"))
assert.Equal(t, fmt.Sprintf("%d", requests), r.URL.Query().Get("offset"))
w.Header().Set("content-type", "application/json")
if requests == 0 {
fmt.Fprint(w, `{"body":{"objects":[{"id":7,"client_id":17,"rule_type":"subnet","list":"block","created_at":1770000000,"expired_at":0,"application_ids":[0],"reason":"manual block","author_user_id":11,"values":["1.2.3.4"],"status":"enabled"}]}}`)
} else if requests == 1 {
fmt.Fprint(w, `{"body":{"objects":[{"id":8,"client_id":17,"rule_type":"location","list":"block","created_at":1770000100,"expired_at":1771000000,"application_ids":[12],"reason":"geo block","author_user_id":12,"values":["RU"],"status":"enabled"}]}}`)
} else {
fmt.Fprint(w, `{"body":{"objects":[]}}`)
}
requests++
})
actual, err := client.IPListRead(DenylistType, 17, 1)
require.NoError(t, err)
require.Len(t, actual, 2)
assert.Equal(t, []IPRule{
{
ID: 7,
ClientID: 17,
RuleType: "subnet",
List: "block",
CreatedAt: 1770000000,
ExpiredAt: 0,
ApplicationIDs: []int{0},
Reason: "manual block",
AuthorUserID: 11,
Values: []string{"1.2.3.4"},
Status: "enabled",
},
{
ID: 8,
ClientID: 17,
RuleType: "location",
List: "block",
CreatedAt: 1770000100,
ExpiredAt: 1771000000,
ApplicationIDs: []int{12},
Reason: "geo block",
AuthorUserID: 12,
Values: []string{"RU"},
Status: "enabled",
},
}, actual)
assert.Equal(t, 3, requests)
}
func TestIPListSearch_UsesBracketedFilterQuery(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/blocklist/clients/42/groups", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
assert.Equal(t, []string{"location"}, r.URL.Query()["filter[rule_type][]"])
assert.Equal(t, "allow", r.URL.Query().Get("filter[list]"))
assert.Equal(t, "RU", r.URL.Query().Get("filter[query]"))
assert.Empty(t, r.URL.Query().Get("filter"))
assert.Equal(t, "1", r.URL.Query().Get("limit"))
assert.Equal(t, "0", r.URL.Query().Get("offset"))
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{"body":{"objects":[{"id":9,"client_id":42,"rule_type":"location","list":"allow","created_at":1770000200,"expired_at":0,"application_ids":[0],"reason":"allow geo","author_user_id":13,"values":["RU"],"status":"enabled"}]}}`)
})
actual, err := client.IPListSearch(AllowlistType, 42, "location", "RU")
require.NoError(t, err)
require.Len(t, actual, 1)
assert.Equal(t, IPRule{
ID: 9,
ClientID: 42,
RuleType: "location",
List: "allow",
CreatedAt: 1770000200,
ExpiredAt: 0,
ApplicationIDs: []int{0},
Reason: "allow geo",
AuthorUserID: 13,
Values: []string{"RU"},
Status: "enabled",
}, actual[0])
}
func TestIPListCreate(t *testing.T) {
setup()
defer teardown()
expected := AccessRuleCreateRequest{
List: GraylistType,
Force: true,
Reason: "temporary review",
ApplicationIDs: []int{0, 12},
ExpiredAt: 1772000000,
Rules: []AccessRuleEntry{{
RulesType: "subnet",
Values: []string{"10.0.0.0/8"},
}},
}
mux.HandleFunc("/v1/blocklist/clients/23/access_rules", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method)
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
var actual AccessRuleCreateRequest
require.NoError(t, json.Unmarshal(body, &actual))
assert.Equal(t, expected, actual)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{"status":200}`)
})
err := client.IPListCreate(23, expected)
require.NoError(t, err)
}
func TestIPListDelete(t *testing.T) {
setup()
defer teardown()
expected := AccessRuleDeleteRequest{
Rules: []AccessRuleDeleteEntry{
{RuleType: "subnet", IDs: []int{7, 8}},
{RuleType: "location", IDs: []int{9}},
},
}
mux.HandleFunc("/v1/blocklist/clients/23/groups", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodDelete, r.Method, "Expected method 'DELETE', got %s", r.Method)
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
var actual AccessRuleDeleteRequest
require.NoError(t, json.Unmarshal(body, &actual))
assert.Equal(t, expected, actual)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{"status":200}`)
})
err := client.IPListDelete(23, expected.Rules)
require.NoError(t, err)
}