forked from vultr/govultr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firewall_rule_test.go
139 lines (116 loc) · 3.59 KB
/
firewall_rule_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
package govultr
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestFireWallRuleServiceHandler_Create(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/firewalls/abc123/rules", func(writer http.ResponseWriter, request *http.Request) {
response := `{"firewall_rule":{"id":1,"type":"v4","ip_type":"v4","action":"accept","protocol":"tcp","port":"80","subnet":"127.0.0.1","subnet_size":32,"source":"","notes":"thisisanote"}}`
fmt.Fprint(writer, response)
})
rule := &FirewallRuleReq{
IPType: "v4",
Protocol: "tcp",
Subnet: "127.0.0.1",
SubnetSize: 30,
Port: "80",
Notes: "thisisanote",
}
firewallRule, _, err := client.FirewallRule.Create(ctx, "abc123", rule)
if err != nil {
t.Errorf("FirewallRule.Create returned error: %v", err)
}
expected := &FirewallRule{
ID: 1,
Action: "accept",
Type: "v4",
IPType: "v4",
Protocol: "tcp",
Port: "80",
Subnet: "127.0.0.1",
SubnetSize: 32,
Source: "",
Notes: "thisisanote",
}
if !reflect.DeepEqual(firewallRule, expected) {
t.Errorf("FirewallRule.Create returned %+v, expected %+v", firewallRule, expected)
}
}
func TestFireWallRuleServiceHandler_Delete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/firewalls/abc123/rules/1", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
})
err := client.FirewallRule.Delete(ctx, "abc123", 1)
if err != nil {
t.Errorf("FirewallRule.Delete returned error: %v", err)
}
}
func TestFireWallRuleServiceHandler_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/firewalls/abc123/rules", func(writer http.ResponseWriter, request *http.Request) {
response := `{"firewall_rules":[{"id":1,"type":"v4","ip_type":"v4","action":"accept","protocol":"tcp","port":"22","subnet":"0.0.0.0","subnet_size":0,"source":"","notes":""}],"meta":{"total":5,"links":{"next":"","prev":""}}}`
fmt.Fprint(writer, response)
})
firewallRule, meta, _, err := client.FirewallRule.List(ctx, "abc123", nil)
if err != nil {
t.Errorf("FirewallRule.List returned error: %v", err)
}
expectedRule := []FirewallRule{
{
ID: 1,
Action: "accept",
Type: "v4",
IPType: "v4",
Protocol: "tcp",
Port: "22",
Subnet: "0.0.0.0",
SubnetSize: 0,
Source: "",
Notes: "",
},
}
expectedMeta := &Meta{
Total: 5,
Links: &Links{},
}
if !reflect.DeepEqual(firewallRule, expectedRule) {
t.Errorf("FirewallRule.List rules returned %+v, expected %+v", firewallRule, expectedRule)
}
if !reflect.DeepEqual(meta, expectedMeta) {
t.Errorf("FirewallRule.List meta returned %+v, expected %+v", meta, expectedMeta)
}
}
func TestFireWallRuleServiceHandler_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/firewalls/abc123/rules/1", func(writer http.ResponseWriter, request *http.Request) {
response := `{"firewall_rule":{"id":1,"type":"v4","ip_type":"v4","action":"accept","protocol":"tcp","port":"22","subnet":"0.0.0.0","subnet_size":0,"source":"","notes":""}}`
fmt.Fprint(writer, response)
})
firewallRule, _, err := client.FirewallRule.Get(ctx, "abc123", 1)
if err != nil {
t.Errorf("FirewallRule.Get returned error: %v", err)
}
expectedRule := &FirewallRule{
ID: 1,
Action: "accept",
Type: "v4",
IPType: "v4",
Protocol: "tcp",
Port: "22",
Subnet: "0.0.0.0",
SubnetSize: 0,
Source: "",
Notes: "",
}
if !reflect.DeepEqual(firewallRule, expectedRule) {
t.Errorf("FirewallRule.Get returned %+v, expected %+v", firewallRule, expectedRule)
}
}