Debian Codegraph @

dnsimple/registrar_test.go

raw
1 package dnsimple
2
3 import (
4 "fmt"
5 "net/http"
6 "reflect"
7 "strings"
8 "testing"
9 "time"
10 )
11
12 func TestRegistrarService_IsAvailable_available(t *testing.T) {
13 setup()
14 defer teardown()
15
16 mux.HandleFunc("/v1/domains/example.com/check", func(w http.ResponseWriter, r *http.Request) {
17 testMethod(t, r, "GET")
18 w.WriteHeader(http.StatusNotFound)
19 fmt.Fprint(w, `{"name":"example.com", "status":"available"}`)
20 })
21
22 available, err := client.Registrar.IsAvailable("example.com")
23
24 if err != nil {
25 t.Errorf("Registrar.IsAvailable check returned %v", err)
26 }
27
28 if !available {
29 t.Errorf("Registrar.IsAvailable returned false, want true")
30 }
31 }
32
33 func TestRegistrarService_IsAvailable_unavailable(t *testing.T) {
34 setup()
35 defer teardown()
36
37 mux.HandleFunc("/v1/domains/example.com/check", func(w http.ResponseWriter, r *http.Request) {
38 testMethod(t, r, "GET")
39 w.WriteHeader(http.StatusOK)
40 fmt.Fprint(w, `{"name":"example.com", "status":"unavailable"}`)
41 })
42
43 available, err := client.Registrar.IsAvailable("example.com")
44
45 if err != nil {
46 t.Errorf("Registrar.IsAvailable check returned %v", err)
47 }
48
49 if available {
50 t.Errorf("Registrar.IsAvailable returned true, want false")
51 }
52 }
53
54 func TestRegistrarService_IsAvailable_failed400(t *testing.T) {
55 setup()
56 defer teardown()
57
58 mux.HandleFunc("/v1/domains/example.com/check", func(w http.ResponseWriter, r *http.Request) {
59 testMethod(t, r, "GET")
60 w.WriteHeader(http.StatusBadRequest)
61 fmt.Fprint(w, `{"message":"Invalid request"}`)
62 })
63
64 _, err := client.Registrar.IsAvailable("example.com")
65
66 if err == nil {
67 t.Errorf("Registrar.IsAvailable expected error to be returned")
68 }
69
70 if match := "400 Invalid request"; !strings.Contains(err.Error(), match) {
71 t.Errorf("Registrar.IsAvailable returned %+v, should match %+v", err, match)
72 }
73 }
74
75 func TestRegistrarService_Register(t *testing.T) {
76 setup()
77 defer teardown()
78
79 mux.HandleFunc("/v1/domain_registrations", func(w http.ResponseWriter, r *http.Request) {
80 want := make(map[string]interface{})
81 want["domain"] = map[string]interface{}{"name": "example.com", "registrant_id": float64(21)}
82
83 testMethod(t, r, "POST")
84 testRequestJSON(t, r, want)
85
86 w.WriteHeader(http.StatusCreated)
87 fmt.Fprint(w, `{"domain": {"id":1, "name":"example.com", "expires_on":"2015-01-15"}}`)
88 })
89
90 domain, _, err := client.Registrar.Register("example.com", 21, nil)
91
92 if err != nil {
93 t.Errorf("Registrar.Register returned %v", err)
94 }
95
96 want := Domain{Id: 1, Name: "example.com", ExpiresOn: &Date{Time: time.Date(2015, 01, 15, 0, 0, 0, 0, time.UTC)}}
97 if !reflect.DeepEqual(domain, want) {
98 t.Fatalf("Registrar.Register returned %+v, want %+v", domain, want)
99 }
100 }
101
102 func TestRegistrarService_Register_withExtendedAttributes(t *testing.T) {
103 setup()
104 defer teardown()
105
106 mux.HandleFunc("/v1/domain_registrations", func(w http.ResponseWriter, r *http.Request) {
107 want := make(map[string]interface{})
108 want["domain"] = map[string]interface{}{"name": "example.com", "registrant_id": float64(21)}
109 want["extended_attribute"] = map[string]interface{}{"us_nexus": "C11", "us_purpose": "P3"}
110
111 testMethod(t, r, "POST")
112 testRequestJSON(t, r, want)
113
114 w.WriteHeader(http.StatusCreated)
115 fmt.Fprint(w, `{"domain": {"id":1, "name":"example.com"}}`)
116 })
117
118 domain, _, err := client.Registrar.Register("example.com", 21, &ExtendedAttributes{"us_nexus": "C11", "us_purpose": "P3"})
119
120 if err != nil {
121 t.Errorf("Registrar.Register returned %v", err)
122 }
123
124 want := Domain{Id: 1, Name: "example.com"}
125 if !reflect.DeepEqual(domain, want) {
126 t.Fatalf("Registrar.Register returned %+v, want %+v", domain, want)
127 }
128 }
129
130 func TestRegistrarService_Transfer(t *testing.T) {
131 setup()
132 defer teardown()
133
134 mux.HandleFunc("/v1/domain_transfers", func(w http.ResponseWriter, r *http.Request) {
135 want := make(map[string]interface{})
136 want["domain"] = map[string]interface{}{"name": "example.com", "registrant_id": float64(21)}
137 want["transfer_order"] = map[string]interface{}{"authinfo": "xjfjfjvhc293"}
138
139 testMethod(t, r, "POST")
140 testRequestJSON(t, r, want)
141
142 w.WriteHeader(http.StatusCreated)
143 fmt.Fprint(w, `{"domain": {"id":1, "name":"example.com"}}`)
144 })
145
146 domain, _, err := client.Registrar.Transfer("example.com", 21, "xjfjfjvhc293", nil)
147
148 if err != nil {
149 t.Errorf("Registrar.Transfer returned %v", err)
150 }
151
152 want := Domain{Id: 1, Name: "example.com"}
153 if !reflect.DeepEqual(domain, want) {
154 t.Fatalf("Registrar.Transfer returned %+v, want %+v", domain, want)
155 }
156 }
157
158 func TestRegistrarService_Transfer_withExtendedAttributes(t *testing.T) {
159 setup()
160 defer teardown()
161
162 mux.HandleFunc("/v1/domain_transfers", func(w http.ResponseWriter, r *http.Request) {
163 want := make(map[string]interface{})
164 want["domain"] = map[string]interface{}{"name": "example.com", "registrant_id": float64(21)}
165 want["transfer_order"] = map[string]interface{}{"authinfo": "xjfjfjvhc293"}
166 want["extended_attribute"] = map[string]interface{}{"us_nexus": "C11", "us_purpose": "P3"}
167
168 testMethod(t, r, "POST")
169 testRequestJSON(t, r, want)
170
171 w.WriteHeader(http.StatusCreated)
172 fmt.Fprint(w, `{"domain": {"id":1, "name":"example.com"}}`)
173 })
174
175 domain, _, err := client.Registrar.Transfer("example.com", 21, "xjfjfjvhc293", &ExtendedAttributes{"us_nexus": "C11", "us_purpose": "P3"})
176
177 if err != nil {
178 t.Errorf("Registrar.Transfer returned %v", err)
179 }
180
181 want := Domain{Id: 1, Name: "example.com"}
182 if !reflect.DeepEqual(domain, want) {
183 t.Fatalf("Registrar.Transfer returned %+v, want %+v", domain, want)
184 }
185 }
186
187 func TestRegistrarService_Renew(t *testing.T) {
188 setup()
189 defer teardown()
190
191 mux.HandleFunc("/v1/domain_renewals", func(w http.ResponseWriter, r *http.Request) {
192 want := make(map[string]interface{})
193 want["domain"] = map[string]interface{}{"name": "example.com", "renew_whois_privacy": true}
194
195 testMethod(t, r, "POST")
196 testRequestJSON(t, r, want)
197
198 fmt.Fprint(w, `{"domain": {"id":1, "name":"example.com"}}`)
199 })
200
201 domain, _, err := client.Registrar.Renew("example.com", true)
202
203 if err != nil {
204 t.Errorf("Registrar.Renew returned %v", err)
205 }
206
207 want := Domain{Id: 1, Name: "example.com"}
208 if !reflect.DeepEqual(domain, want) {
209 t.Fatalf("Registrar.Renew returned %+v, want %+v", domain, want)
210 }
211 }
212
213 func TestRegistrarService_EnableAutoRenewal(t *testing.T) {
214 setup()
215 defer teardown()
216
217 mux.HandleFunc("/v1/domains/example.com/auto_renewal", func(w http.ResponseWriter, r *http.Request) {
218 testMethod(t, r, "POST")
219 })
220
221 _, err := client.Registrar.EnableAutoRenewal("example.com")
222
223 if err != nil {
224 t.Errorf("Domains.EnableAutoRenewal returned %v", err)
225 }
226 }
227
228 func TestRegistrarService_DisableAutoRenewal(t *testing.T) {
229 setup()
230 defer teardown()
231
232 mux.HandleFunc("/v1/domains/example.com/auto_renewal", func(w http.ResponseWriter, r *http.Request) {
233 testMethod(t, r, "DELETE")
234 })
235
236 _, err := client.Registrar.DisableAutoRenewal("example.com")
237
238 if err != nil {
239 t.Errorf("Domains.DisableAutoRenewal returned %v", err)
240 }
241 }