Skip to content

Commit 88995b3

Browse files
committed
test: add whitelist_api_keys_test testing
test: add whitelist_api_keys_test testing
1 parent e981906 commit 88995b3

File tree

1 file changed

+311
-0
lines changed

1 file changed

+311
-0
lines changed
Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
package mongodbatlas
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"reflect"
8+
"testing"
9+
10+
"github.com/go-test/deep"
11+
)
12+
13+
func TestWhitelistAPIKeys_List(t *testing.T) {
14+
setup()
15+
defer teardown()
16+
17+
orgID := "ORG-ID"
18+
apiKeyID := "API-KEY-ID"
19+
20+
mux.HandleFunc(fmt.Sprintf("/"+whitelistAPIKeysPath, orgID, apiKeyID), func(w http.ResponseWriter, r *http.Request) {
21+
testMethod(t, r, http.MethodGet)
22+
fmt.Fprint(w, `{
23+
"links": [
24+
{
25+
"href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/599c510c80eef518f3b63fe1/apiKeys/5c49e72980eef544a218f8f8/whitelist/?pretty=true&pageNum=1&itemsPerPage=100",
26+
"rel": "self"
27+
}
28+
],
29+
"results": [
30+
{
31+
"cidrBlock": "147.58.184.16/32",
32+
"count": 0,
33+
"created": "2019-01-24T16:34:57Z",
34+
"ipAddress": "147.58.184.16",
35+
"lastUsed": "2019-01-24T20:18:25Z",
36+
"lastUsedAddress": "147.58.184.16",
37+
"links": [
38+
{
39+
"href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/147.58.184.16",
40+
"rel": "self"
41+
}
42+
]
43+
},
44+
{
45+
"cidrBlock": "84.255.48.125/32",
46+
"count": 0,
47+
"created": "2019-01-24T16:26:37Z",
48+
"ipAddress": "84.255.48.125",
49+
"lastUsed": "2019-01-24T20:18:25Z",
50+
"lastUsedAddress": "84.255.48.125",
51+
"links": [
52+
{
53+
"href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/206.252.195.126",
54+
"rel": "self"
55+
}
56+
]
57+
}
58+
],
59+
"totalCount": 2
60+
}`)
61+
})
62+
63+
whitelistAPIKeys, _, err := client.WhitelistAPIKeys.List(ctx, orgID, apiKeyID)
64+
if err != nil {
65+
t.Errorf("WhitelistAPIKeys.List returned error: %v", err)
66+
}
67+
68+
expected := &WhitelistAPIKeys{
69+
Links: []*Link{
70+
{
71+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/599c510c80eef518f3b63fe1/apiKeys/5c49e72980eef544a218f8f8/whitelist/?pretty=true&pageNum=1&itemsPerPage=100",
72+
Rel: "self",
73+
},
74+
},
75+
Results: []*WhitelistAPIKey{
76+
{
77+
CidrBlock: "147.58.184.16/32",
78+
Count: 0,
79+
Created: "2019-01-24T16:34:57Z",
80+
IPAddress: "147.58.184.16",
81+
LastUsed: "2019-01-24T20:18:25Z",
82+
LastUsedAddress: "147.58.184.16",
83+
Links: []*Link{
84+
{
85+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/147.58.184.16",
86+
Rel: "self",
87+
},
88+
},
89+
},
90+
{
91+
CidrBlock: "84.255.48.125/32",
92+
Count: 0,
93+
Created: "2019-01-24T16:26:37Z",
94+
IPAddress: "84.255.48.125",
95+
LastUsed: "2019-01-24T20:18:25Z",
96+
LastUsedAddress: "84.255.48.125",
97+
Links: []*Link{
98+
{
99+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/206.252.195.126",
100+
Rel: "self",
101+
},
102+
},
103+
},
104+
},
105+
TotalCount: 2,
106+
}
107+
108+
if diff := deep.Equal(whitelistAPIKeys, expected); diff != nil {
109+
t.Error(diff)
110+
}
111+
if !reflect.DeepEqual(whitelistAPIKeys, expected) {
112+
t.Errorf("WhitelistAPIKeys.List\n got=%#v\nwant=%#v", whitelistAPIKeys, expected)
113+
}
114+
}
115+
116+
func TestWhitelistAPIKeys_Get(t *testing.T) {
117+
setup()
118+
defer teardown()
119+
120+
orgID := "ORG-ID"
121+
apiKeyID := "API-KEY-ID"
122+
ipAddress := "IP-ADDRESS"
123+
124+
mux.HandleFunc(fmt.Sprintf("/"+whitelistAPIKeysPath+"/%s", orgID, apiKeyID, ipAddress), func(w http.ResponseWriter, r *http.Request) {
125+
testMethod(t, r, http.MethodGet)
126+
fmt.Fprint(w, `{
127+
"cidrBlock": "147.58.184.16/32",
128+
"count": 0,
129+
"created": "2019-01-24T16:34:57Z",
130+
"ipAddress": "147.58.184.16",
131+
"links": [
132+
{
133+
"href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/147.58.184.16",
134+
"rel": "self"
135+
}
136+
]
137+
}`)
138+
})
139+
140+
whitelistAPIKey, _, err := client.WhitelistAPIKeys.Get(ctx, orgID, apiKeyID, ipAddress)
141+
if err != nil {
142+
t.Errorf("WhitelistAPIKeys.Get returned error: %v", err)
143+
}
144+
145+
expected := &WhitelistAPIKey{
146+
CidrBlock: "147.58.184.16/32",
147+
Count: 0,
148+
Created: "2019-01-24T16:34:57Z",
149+
IPAddress: "147.58.184.16",
150+
Links: []*Link{
151+
{
152+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/147.58.184.16",
153+
Rel: "self",
154+
},
155+
},
156+
}
157+
158+
if diff := deep.Equal(whitelistAPIKey, expected); diff != nil {
159+
t.Error(diff)
160+
}
161+
if !reflect.DeepEqual(whitelistAPIKey, expected) {
162+
t.Errorf("WhitelistAPIKeys.Get\n got=%#v\nwant=%#v", whitelistAPIKey, expected)
163+
}
164+
}
165+
166+
func TestWhitelistAPIKeys_Create(t *testing.T) {
167+
setup()
168+
defer teardown()
169+
170+
orgID := "ORG-ID"
171+
apiKeyID := "API-KEY-ID"
172+
173+
createRequest := &[]WhitelistAPIKeysReq{
174+
{
175+
IPAddress: "77.54.32.11",
176+
CidrBlock: "77.54.32.11/32",
177+
},
178+
}
179+
180+
mux.HandleFunc(fmt.Sprintf("/"+whitelistAPIKeysPath, orgID, apiKeyID), func(w http.ResponseWriter, r *http.Request) {
181+
expected := []map[string]interface{}{
182+
{
183+
"ipAddress": "77.54.32.11",
184+
"cidrBlock": "77.54.32.11/32",
185+
},
186+
}
187+
188+
var v []map[string]interface{}
189+
err := json.NewDecoder(r.Body).Decode(&v)
190+
if err != nil {
191+
t.Fatalf("Decode json: %v", err)
192+
}
193+
194+
if diff := deep.Equal(v, expected); diff != nil {
195+
t.Error(diff)
196+
}
197+
if !reflect.DeepEqual(v, expected) {
198+
t.Errorf("Request body\n got=%#v\nwant=%#v", v, expected)
199+
}
200+
201+
fmt.Fprintf(w, `{
202+
"links": [
203+
{
204+
"href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/599c510c80eef518f3b63fe1/apiKeys/5c49e72980eef544a218f8f8/whitelist/?pretty=true&pageNum=1&itemsPerPage=100",
205+
"rel": "self"
206+
}
207+
],
208+
"results": [
209+
{
210+
"cidrBlock": "147.58.184.16/32",
211+
"count": 0,
212+
"created": "2019-01-24T16:34:57Z",
213+
"ipAddress": "147.58.184.16",
214+
"lastUsed": "2019-01-24T20:18:25Z",
215+
"lastUsedAddress": "147.58.184.16",
216+
"links": [
217+
{
218+
"href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/147.58.184.16",
219+
"rel": "self"
220+
}
221+
]
222+
},
223+
{
224+
"cidrBlock": "77.54.32.11/32",
225+
"count": 0,
226+
"created": "2019-01-24T16:26:37Z",
227+
"ipAddress": "77.54.32.11",
228+
"lastUsed": "2019-01-24T20:18:25Z",
229+
"lastUsedAddress": "77.54.32.11",
230+
"links": [
231+
{
232+
"href": "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/77.54.32.11",
233+
"rel": "self"
234+
}
235+
]
236+
}
237+
],
238+
"totalCount": 2
239+
}`)
240+
})
241+
242+
whitelistAPIKey, _, err := client.WhitelistAPIKeys.Create(ctx, orgID, apiKeyID, createRequest)
243+
if err != nil {
244+
t.Errorf("WhitelistAPIKeys.Create returned error: %v", err)
245+
}
246+
247+
expected := &WhitelistAPIKeys{
248+
Links: []*Link{
249+
{
250+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/599c510c80eef518f3b63fe1/apiKeys/5c49e72980eef544a218f8f8/whitelist/?pretty=true&pageNum=1&itemsPerPage=100",
251+
Rel: "self",
252+
},
253+
},
254+
Results: []*WhitelistAPIKey{
255+
{
256+
CidrBlock: "147.58.184.16/32",
257+
Count: 0,
258+
Created: "2019-01-24T16:34:57Z",
259+
IPAddress: "147.58.184.16",
260+
LastUsed: "2019-01-24T20:18:25Z",
261+
LastUsedAddress: "147.58.184.16",
262+
Links: []*Link{
263+
{
264+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/147.58.184.16",
265+
Rel: "self",
266+
},
267+
},
268+
},
269+
{
270+
CidrBlock: "77.54.32.11/32",
271+
Count: 0,
272+
Created: "2019-01-24T16:26:37Z",
273+
IPAddress: "77.54.32.11",
274+
LastUsed: "2019-01-24T20:18:25Z",
275+
LastUsedAddress: "77.54.32.11",
276+
Links: []*Link{
277+
{
278+
Href: "https://cloud.mongodb.com/api/atlas/v1.0/orgs/{ORG-ID}/apiKeys/{API-KEY-ID}/whitelist/77.54.32.11",
279+
Rel: "self",
280+
},
281+
},
282+
},
283+
},
284+
TotalCount: 2,
285+
}
286+
287+
if diff := deep.Equal(whitelistAPIKey, expected); diff != nil {
288+
t.Error(diff)
289+
}
290+
if !reflect.DeepEqual(whitelistAPIKey, expected) {
291+
t.Errorf("WhitelistAPIKeys.Create\n got=%#v\nwant=%#v", whitelistAPIKey, expected)
292+
}
293+
}
294+
295+
func TestWhitelistAPIKeys_Delete(t *testing.T) {
296+
setup()
297+
defer teardown()
298+
299+
orgID := "ORG-ID"
300+
apiKeyID := "API-KEY-ID"
301+
ipAddress := "IP-ADDRESS"
302+
303+
mux.HandleFunc(fmt.Sprintf("/"+whitelistAPIKeysPath+"/%s", orgID, apiKeyID, ipAddress), func(w http.ResponseWriter, r *http.Request) {
304+
testMethod(t, r, http.MethodDelete)
305+
})
306+
307+
_, err := client.WhitelistAPIKeys.Delete(ctx, orgID, apiKeyID, ipAddress)
308+
if err != nil {
309+
t.Errorf("WhitelistAPIKeys.Delete returned error: %v", err)
310+
}
311+
}

0 commit comments

Comments
 (0)