-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient_test.go
244 lines (197 loc) · 6.07 KB
/
client_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package goacmedns
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
const updateValue = "idkmybffjill"
var (
errBody = []byte(`{"error":"this is a test"}`)
testAcct = Account{
FullDomain: "lettuceencrypt.org",
SubDomain: "tossed.lettuceencrypt.org",
Username: "cpu",
Password: "hunter2",
}
)
func TestClient_RegisterAccount(t *testing.T) {
testAllowFrom := []string{"space", "earth"}
testCases := []struct {
Name string
RegisterHandler func(http.ResponseWriter, *http.Request)
AllowFrom []string
ExpectedErr *ClientError
ExpectedAccount *Account
}{
{
Name: "registration failure",
RegisterHandler: errHandler,
ExpectedErr: &ClientError{
HTTPStatus: http.StatusBadRequest,
Body: errBody,
Message: "response error",
},
},
{
Name: "registration success",
RegisterHandler: newRegHandler(t, nil),
ExpectedAccount: &testAcct,
},
{
Name: "registration success, allow from",
AllowFrom: testAllowFrom,
RegisterHandler: newRegHandler(t, testAllowFrom),
ExpectedAccount: &testAcct,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/register", tc.RegisterHandler)
acct, err := client.RegisterAccount(context.Background(), tc.AllowFrom)
if tc.ExpectedErr == nil && err != nil {
t.Errorf("expected no error, got %v", err)
return
}
if tc.ExpectedErr != nil && err == nil {
t.Errorf("expected error %v, got nil", tc.ExpectedErr)
return
}
if tc.ExpectedErr != nil && err != nil {
var cErr *ClientError
if ok := errors.As(errors.Unwrap(err), &cErr); !ok {
t.Fatalf("expected ClientError from RegisterAccount. Got %T", errors.Unwrap(err))
} else if !reflect.DeepEqual(cErr, tc.ExpectedErr) {
t.Errorf("got %#v,\n expected err %#v", errors.Unwrap(err), tc.ExpectedErr)
}
return
}
if tc.ExpectedErr == nil && err == nil {
// Needed to be able to assert equivalence, as the server addr is dynamic
tc.ExpectedAccount.ServerURL = acct.ServerURL
if !reflect.DeepEqual(acct, *tc.ExpectedAccount) {
t.Errorf("expected account %v, got %v\n", tc.ExpectedAccount, acct)
}
}
})
}
}
func TestClient_UpdateTXTRecord(t *testing.T) {
testCases := []struct {
Name string
UpdateHandler func(http.ResponseWriter, *http.Request)
Value string
ExpectedErr *ClientError
}{
{
Name: "update failure",
UpdateHandler: errHandler,
ExpectedErr: &ClientError{
HTTPStatus: http.StatusBadRequest,
Body: errBody,
Message: "response error",
},
},
{
Name: "update success",
UpdateHandler: updateTXTHandler(t),
ExpectedErr: nil,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/update", tc.UpdateHandler)
err := client.UpdateTXTRecord(context.Background(), testAcct, updateValue)
switch {
case tc.ExpectedErr == nil && err != nil:
t.Errorf("expected no error, got %v", err)
case tc.ExpectedErr != nil && err == nil:
t.Errorf("expected error %v, got nil", tc.ExpectedErr)
case tc.ExpectedErr != nil && err != nil:
var cErr *ClientError
if ok := errors.As(errors.Unwrap(err), &cErr); !ok {
t.Fatalf("expected ClientError from UpdateTXTRecord. Got %v", errors.Unwrap(err))
} else if !reflect.DeepEqual(cErr, tc.ExpectedErr) {
t.Errorf("expected err %#v, got %#v\n", tc.ExpectedErr, cErr)
}
}
})
}
}
func errHandler(resp http.ResponseWriter, _ *http.Request) {
resp.WriteHeader(http.StatusBadRequest)
_, _ = resp.Write(errBody)
}
func newRegHandler(t *testing.T, expectedAllowFrom []string) http.HandlerFunc {
t.Helper()
return func(resp http.ResponseWriter, req *http.Request) {
expectedCT := "application/json"
if ct := req.Header.Get("Content-Type"); ct != expectedCT {
t.Errorf("expected Content-Type %q got %q", expectedCT, ct)
}
if ua := req.Header.Get("User-Agent"); ua != userAgent() {
t.Errorf("expected User-Agent %q got %q", userAgent(), ua)
}
if len(expectedAllowFrom) > 0 {
decoder := json.NewDecoder(req.Body)
var regReq Register
err := decoder.Decode(®Req)
if err != nil {
t.Fatalf("error decoding request body JSON: %v", err)
}
if !reflect.DeepEqual(regReq.AllowFrom, expectedAllowFrom) {
t.Errorf("expected AllowFrom %#v, got %#v", expectedAllowFrom, regReq.AllowFrom)
}
}
resp.WriteHeader(http.StatusCreated)
newRegBody, _ := json.Marshal(testAcct)
_, _ = resp.Write(newRegBody)
}
}
func updateTXTHandler(t *testing.T) http.HandlerFunc {
t.Helper()
return func(resp http.ResponseWriter, req *http.Request) {
expectedCT := "application/json"
if ct := req.Header.Get("Content-Type"); ct != expectedCT {
t.Errorf("expected Content-Type %q got %q", expectedCT, ct)
}
if ua := req.Header.Get("User-Agent"); ua != userAgent() {
t.Errorf("expected User-Agent %q got %q", userAgent(), ua)
}
if key := req.Header.Get("X-Api-Key"); key != testAcct.Password {
t.Errorf("expected X-Api-Key %q got %q", testAcct.Password, key)
}
if user := req.Header.Get("X-Api-User"); user != testAcct.Username {
t.Errorf("expected X-Api-User %q got %q", testAcct.Username, user)
}
decoder := json.NewDecoder(req.Body)
var updateReq Update
err := decoder.Decode(&updateReq)
if err != nil {
t.Fatalf("error decoding request body JSON: %v", err)
}
if updateReq.SubDomain != testAcct.SubDomain {
t.Errorf("expected update req to have SubDomain %q, had %q",
testAcct.SubDomain, updateReq.SubDomain)
}
if updateReq.Txt != updateValue {
t.Errorf("expected update req to have Txt %q, had %q",
updateValue, updateReq.Txt)
}
resp.WriteHeader(http.StatusOK)
_, _ = resp.Write([]byte(`{}`))
}
}
func setupTest(t *testing.T) (*Client, *http.ServeMux) {
t.Helper()
mux := http.NewServeMux()
ts := httptest.NewServer(mux)
t.Cleanup(ts.Close)
client, _ := NewClient(ts.URL)
return client, mux
}