-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigital_ocean_test.go
147 lines (132 loc) · 3.39 KB
/
digital_ocean_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
package homebase
import (
"bytes"
"errors"
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"net/http"
"testing"
"testing/iotest"
)
type fakeDoer struct {
statusCode int
body string
err string
bodyError bool
}
func (f *fakeDoer) Do(req *http.Request) (resp *http.Response, err error) {
if f.err != "" {
return nil, errors.New(f.err)
}
var reader io.Reader
if f.bodyError {
// When body error is specified, we want to simulate an error being returned while reading the body.
// TimeoutReader will error on the second read.
reader = iotest.TimeoutReader(iotest.OneByteReader(bytes.NewBufferString("AB")))
} else {
reader = bytes.NewBufferString(f.body)
}
body := ioutil.NopCloser(reader)
resp = &http.Response{StatusCode: f.statusCode, Body: body}
return resp, nil
}
func TestGetRecordByName(t *testing.T) {
c := &fakeDoer{statusCode: 200, body: `{
"domain_records": [
{
"id": 1,
"type": "A",
"name": "subdomain",
"data": "1.2.3.4"
}
]
}`}
record, err := GetRecordByName(c, "example.com", "subdomain", "12345")
if assert.NoError(t, err) {
assert.NotNil(t, record)
assert.Equal(t, 1, record.ID)
assert.Equal(t, "A", record.Type)
assert.Equal(t, "subdomain", record.Name)
assert.Equal(t, "1.2.3.4", record.Data)
}
record, err = GetRecordByName(c, "example.com", "notfound", "12345")
if assert.Error(t, err) {
assert.Equal(t, ErrRecordNotFound, err)
assert.Nil(t, record)
}
}
func TestGetRecordByNameErrors(t *testing.T) {
type testFixture struct {
fakeDoer
errorString string
}
testFixtures := []testFixture{
testFixture{
errorString: "Error in json: unexpected end of JSON input",
fakeDoer: fakeDoer{statusCode: 200, body: ""},
},
testFixture{
errorString: "Bad response requesting records: 404: Not Found",
fakeDoer: fakeDoer{statusCode: 404, body: "Not Found"},
},
testFixture{
errorString: "Error reading body: timeout",
fakeDoer: fakeDoer{statusCode: 200, bodyError: true},
},
testFixture{
errorString: "Error making the request: Not awesome error",
fakeDoer: fakeDoer{err: "Not awesome error"},
},
}
for _, tf := range testFixtures {
record, err := GetRecordByName(&tf.fakeDoer, "example.com", "subdomain", "12345")
if assert.Error(t, err) {
assert.Equal(t, tf.errorString, err.Error())
assert.Nil(t, record)
}
}
}
func TestRecordSave(t *testing.T) {
record := &Record{
ID: 1,
Type: "A",
Name: "subdomain",
Data: "1.2.3.4",
}
fc := &fakeDoer{statusCode: 200}
err := record.Save(fc, "example.com", "12345")
assert.NoError(t, err)
}
func TestRecordSaveErrors(t *testing.T) {
type testFixture struct {
fakeDoer
errorString string
}
testFixtures := []testFixture{
testFixture{
errorString: "Bad response saving record: 404: Not Found",
fakeDoer: fakeDoer{statusCode: 404, body: "Not Found"},
},
testFixture{
errorString: "Error reading body: timeout",
fakeDoer: fakeDoer{statusCode: 200, bodyError: true},
},
testFixture{
errorString: "Error making the request: Not awesome error",
fakeDoer: fakeDoer{err: "Not awesome error"},
},
}
record := &Record{
ID: 1,
Type: "A",
Name: "subdomain",
Data: "1.2.3.4",
}
for _, tf := range testFixtures {
err := record.Save(&tf.fakeDoer, "example.com", "12345")
if assert.Error(t, err) {
assert.Equal(t, tf.errorString, err.Error())
}
}
}