Debian Codegraph @

dnsimple/domains_records_test.go

raw
1 package dnsimple
2
3 import (
4 "fmt"
5 "net/http"
6 "reflect"
7 "strings"
8 "testing"
9 )
10
11 func TestRecords_recordPath(t *testing.T) {
12 var pathTest = []struct {
13 domainInput interface{}
14 recordInput interface{}
15 expected string
16 }{
17 {"example.com", nil, "domains/example.com/records"},
18 {"example.com", 2, "domains/example.com/records/2"},
19 {1, nil, "domains/1/records"},
20 {1, 2, "domains/1/records/2"},
21 }
22
23 for _, pt := range pathTest {
24 actual := recordPath(pt.domainInput, pt.recordInput)
25 if actual != pt.expected {
26 t.Errorf("recordPath(%+v, %+v): expected %s, actual %s", pt.domainInput, pt.recordInput, pt.expected, actual)
27 }
28 }
29 }
30
31 func TestDomainsService_ListRecords_all(t *testing.T) {
32 setup()
33 defer teardown()
34
35 mux.HandleFunc("/v1/domains/example.com/records", func(w http.ResponseWriter, r *http.Request) {
36 testMethod(t, r, "GET")
37 fmt.Fprint(w, `[{"record":{"id":1, "name":"foo.example.com"}}]`)
38 })
39
40 records, _, err := client.Domains.ListRecords("example.com", "", "")
41
42 if err != nil {
43 t.Errorf("Domains.ListRecords returned error: %v", err)
44 }
45
46 want := []Record{{Id: 1, Name: "foo.example.com"}}
47 if !reflect.DeepEqual(records, want) {
48 t.Fatalf("Domains.ListRecords returned %+v, want %+v", records, want)
49 }
50 }
51
52 func TestDomainsService_ListRecords_subdomain(t *testing.T) {
53 setup()
54 defer teardown()
55
56 mux.HandleFunc("/v1/domains/example.com/records", func(w http.ResponseWriter, r *http.Request) {
57 testMethod(t, r, "GET")
58 testFormValues(t, r, values{"name": "foo"})
59
60 fmt.Fprint(w, `[{"record":{"id":1, "name":"foo.example.com"}}]`)
61 })
62
63 records, _, err := client.Domains.ListRecords("example.com", "foo", "")
64
65 if err != nil {
66 t.Errorf("Domains.ListRecords returned error: %v", err)
67 }
68
69 want := []Record{{Id: 1, Name: "foo.example.com"}}
70 if !reflect.DeepEqual(records, want) {
71 t.Fatalf("Domains.ListRecords returned %+v, want %+v", records, want)
72 }
73 }
74
75 func TestDomainsService_ListRecords_type(t *testing.T) {
76 setup()
77 defer teardown()
78
79 mux.HandleFunc("/v1/domains/example.com/records", func(w http.ResponseWriter, r *http.Request) {
80 testMethod(t, r, "GET")
81 testFormValues(t, r, values{"name": "foo", "type": "CNAME"})
82
83 fmt.Fprint(w, `[{"record":{"id":1, "name":"foo.example.com"}}]`)
84 })
85
86 records, _, err := client.Domains.ListRecords("example.com", "foo", "CNAME")
87
88 if err != nil {
89 t.Errorf("Domains.ListRecords returned error: %v", err)
90 }
91
92 want := []Record{{Id: 1, Name: "foo.example.com"}}
93 if !reflect.DeepEqual(records, want) {
94 t.Fatalf("Domains.ListRecords returned %+v, want %+v", records, want)
95 }
96 }
97
98 func TestDomainsService_CreateRecord(t *testing.T) {
99 setup()
100 defer teardown()
101
102 mux.HandleFunc("/v1/domains/example.com/records", func(w http.ResponseWriter, r *http.Request) {
103 want := make(map[string]interface{})
104 want["record"] = map[string]interface{}{"name": "foo", "content": "192.168.0.10", "record_type": "A"}
105
106 testMethod(t, r, "POST")
107 testRequestJSON(t, r, want)
108
109 w.WriteHeader(http.StatusCreated)
110 fmt.Fprintf(w, `{"record":{"id":2, "domain_id":1, "name":"foo"}}`)
111 })
112
113 recordValues := Record{Name: "foo", Content: "192.168.0.10", Type: "A"}
114 record, _, err := client.Domains.CreateRecord("example.com", recordValues)
115
116 if err != nil {
117 t.Errorf("Domains.CreateRecord returned error: %v", err)
118 }
119
120 want := Record{Id: 2, DomainId: 1, Name: "foo"}
121 if !reflect.DeepEqual(record, want) {
122 t.Fatalf("Domains.CreateRecord returned %+v, want %+v", record, want)
123 }
124 }
125
126 func TestDomainsService_GetRecord(t *testing.T) {
127 setup()
128 defer teardown()
129
130 mux.HandleFunc("/v1/domains/example.com/records/1539", func(w http.ResponseWriter, r *http.Request) {
131 testMethod(t, r, "GET")
132 fmt.Fprintf(w, `{"record":{"id":2, "domain_id":1, "name":"foo"}}`)
133 })
134
135 record, _, err := client.Domains.GetRecord("example.com", 1539)
136
137 if err != nil {
138 t.Errorf("Domains.GetRecord returned error: %v", err)
139 }
140
141 want := Record{Id: 2, DomainId: 1, Name: "foo"}
142 if !reflect.DeepEqual(record, want) {
143 t.Fatalf("Domains.GetRecord returned %+v, want %+v", record, want)
144 }
145 }
146
147 func TestDomainsService_UpdateRecord(t *testing.T) {
148 setup()
149 defer teardown()
150
151 mux.HandleFunc("/v1/domains/example.com/records/2", func(w http.ResponseWriter, r *http.Request) {
152 want := make(map[string]interface{})
153 want["record"] = map[string]interface{}{"content": "192.168.0.10", "name": "bar"}
154
155 testMethod(t, r, "PUT")
156 testRequestJSON(t, r, want)
157
158 fmt.Fprint(w, `{"record":{"id":2, "domain_id":1, "name":"bar", "content": "192.168.0.10"}}`)
159 })
160
161 recordValues := Record{Name: "bar", Content: "192.168.0.10", Type: "A"}
162 record, _, err := client.Domains.UpdateRecord("example.com", 2, recordValues)
163
164 if err != nil {
165 t.Errorf("Domains.UpdateRecord returned error: %v", err)
166 }
167
168 want := Record{Id: 2, DomainId: 1, Name: "bar", Content: "192.168.0.10"}
169 if !reflect.DeepEqual(record, want) {
170 t.Fatalf("Domains.UpdateRecord returned %+v, want %+v", record, want)
171 }
172 }
173
174 func TestDomainsService_DeleteRecord(t *testing.T) {
175 setup()
176 defer teardown()
177
178 mux.HandleFunc("/v1/domains/example.com/records/2", func(w http.ResponseWriter, r *http.Request) {
179 testMethod(t, r, "DELETE")
180 // fmt.Fprint(w, `{}`)
181 })
182
183 _, err := client.Domains.DeleteRecord("example.com", 2)
184
185 if err != nil {
186 t.Errorf("Domains.DeleteRecord returned error: %v", err)
187 }
188 }
189
190 func TestDomainsService_DeleteRecord_failed(t *testing.T) {
191 setup()
192 defer teardown()
193
194 mux.HandleFunc("/v1/domains/example.com/records/2", func(w http.ResponseWriter, r *http.Request) {
195 testMethod(t, r, "DELETE")
196
197 w.WriteHeader(http.StatusBadRequest)
198 fmt.Fprint(w, `{"message":"Invalid request"}`)
199 })
200
201 _, err := client.Domains.DeleteRecord("example.com", 2)
202 if err == nil {
203 t.Errorf("Domains.DeleteRecord expected error to be returned")
204 }
205
206 if match := "400 Invalid request"; !strings.Contains(err.Error(), match) {
207 t.Errorf("Records.Delete returned %+v, should match %+v", err, match)
208 }
209 }
210
211 func TestRecord_UpdateIP(t *testing.T) {
212 setup()
213 defer teardown()
214
215 mux.HandleFunc("/v1/domains/24/records/42", func(w http.ResponseWriter, r *http.Request) {
216 want := make(map[string]interface{})
217 want["record"] = map[string]interface{}{"name": "foo", "content": "192.168.0.1"}
218
219 testMethod(t, r, "PUT")
220 testRequestJSON(t, r, want)
221
222 fmt.Fprint(w, `{"record":{"id":24, "domain_id":42}}`)
223 })
224
225 record := Record{Id: 42, DomainId: 24, Name: "foo"}
226 err := record.UpdateIP(client, "192.168.0.1")
227
228 if err != nil {
229 t.Errorf("UpdateIP returned error: %v", err)
230 }
231 }