forked from ns1/ns1-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope_test.go
149 lines (127 loc) · 3.64 KB
/
scope_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
140
141
142
143
144
145
146
147
148
149
package rest_test
import (
"gopkg.in/ns1/ns1-go.v2/rest/model/dhcp"
"net/http"
"testing"
"gopkg.in/ns1/ns1-go.v2/mockns1"
api "gopkg.in/ns1/ns1-go.v2/rest"
)
func TestDHCPScope(t *testing.T) {
mock, doer, err := mockns1.New(t)
if err != nil {
t.Fatalf("Error creating mock service: %v", err)
}
defer mock.Shutdown()
client := api.NewClient(doer, api.SetEndpoint("https://"+mock.Address+"/v1/"))
t.Run("List", func(t *testing.T) {
defer mock.ClearTestCases()
client.FollowPagination = true
idAddr := 1
sgs := []dhcp.Scope{
{IDAddress: &idAddr},
{IDAddress: &idAddr},
{IDAddress: &idAddr},
{IDAddress: &idAddr},
}
err := mock.AddTestCase(http.MethodGet, "/dhcp/scope", http.StatusOK, nil, nil, "", sgs)
if err != nil {
t.Fatalf("error adding test case: %v", err)
}
respSgs, _, err := client.Scope.List()
if err != nil {
t.Fatalf("error listing scopes: %v", err)
}
if len(respSgs) != len(sgs) {
t.Errorf("wrong length: want=%d, got=%d", len(sgs), len(respSgs))
}
for i, sg := range respSgs {
if *sg.IDAddress != *sgs[i].IDAddress {
t.Errorf("Incorrect name for scope %d: want=%q, got=%q", i, *sgs[i].IDAddress, *sg.IDAddress)
}
}
})
t.Run("Get", func(t *testing.T) {
defer mock.ClearTestCases()
idAddr := 1
sg := dhcp.Scope{IDAddress: &idAddr}
err := mock.AddTestCase(http.MethodGet, "/dhcp/scope/1", http.StatusOK, nil, nil, "", sg)
if err != nil {
t.Fatalf("error adding test case: %v", err)
}
respAddr, _, err := client.Scope.Get(1)
if err != nil {
t.Fatalf("error getting scope: %v", err)
}
if *respAddr.IDAddress != *sg.IDAddress {
t.Errorf("wrong scope returned, want=%+v, got=%+v", sg, respAddr)
}
})
t.Run("Create", func(t *testing.T) {
defer mock.ClearTestCases()
t.Run("RequiredParams", func(t *testing.T) {
sg := &dhcp.Scope{}
_, _, err = client.Scope.Create(sg)
if err == nil {
t.Errorf("expected a missing address id to result in an error")
}
})
idAddr := 123
sg := &dhcp.Scope{
IDAddress: &idAddr,
}
err := mock.AddTestCase(http.MethodPut, "/dhcp/scope", http.StatusCreated, nil, nil, sg, sg)
if err != nil {
t.Fatalf("error adding test case: %v", err)
}
respSG, _, err := client.Scope.Create(sg)
if err != nil {
t.Fatalf("error creating scope: %v", err)
}
if *respSG.IDAddress != *sg.IDAddress {
t.Errorf("wrong scope returned: want=%+v, got=%+v", sg, respSG)
}
})
t.Run("Edit", func(t *testing.T) {
t.Run("RequiredParams", func(t *testing.T) {
idAddr := 123
sg := &dhcp.Scope{IDAddress: &idAddr}
_, _, err = client.Scope.Edit(sg)
if err == nil {
t.Errorf("expected a missing ID to result in an error")
}
sg = &dhcp.Scope{}
_, _, err = client.Scope.Edit(sg)
if err == nil {
t.Errorf("expected a missing address ID to result in an error")
}
})
defer mock.ClearTestCases()
idAddr := 123
sg := &dhcp.Scope{
ID: 1,
IDAddress: &idAddr,
}
err := mock.AddTestCase(http.MethodPost, "/dhcp/scope/1", http.StatusOK, nil, nil, sg, sg)
if err != nil {
t.Fatalf("error adding test case: %v", err)
}
respSG, _, err := client.Scope.Edit(sg)
if err != nil {
t.Fatalf("error editing scope: %v", err)
}
if respSG.IDAddress != sg.IDAddress {
t.Errorf("wrong address returned: want=%+v, got=%+v", sg, respSG)
}
})
t.Run("Delete", func(t *testing.T) {
defer mock.ClearTestCases()
err := mock.AddTestCase(http.MethodDelete, "/dhcp/scope/1", http.StatusNoContent, nil, nil, "", nil)
if err != nil {
t.Fatalf("error adding test case: %v", err)
}
_, err = client.Scope.Delete(1)
if err != nil {
t.Fatalf("error deleting scope: %v", err)
}
})
}