| 1 |
package dnsmadeeasy |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| •5 |
. "github.com/go-check/check" |
| 6 |
"github.com/soniah/dnsmadeeasy/testutil" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func Test(t *testing.T) { |
| 11 |
TestingT(t) |
| 12 |
} |
| 13 |
|
| 14 |
type S struct { |
| 15 |
client *Client |
| 16 |
} |
| 17 |
|
| 18 |
var _ = Suite(&S{}) |
| 19 |
|
| 20 |
var testServer = testutil.NewHTTPServer() |
| 21 |
|
| 22 |
func (s *S) SetUpSuite(c *C) { |
| 23 |
testServer.Start() |
| 24 |
var err error |
| 25 |
s.client, err = NewClient("aaaaaa1a-11a1-1aa1-a101-11a1a11aa1aa", |
| 26 |
"11a0a11a-a1a1-111a-a11a-a11110a11111") |
| 27 |
s.client.URL = "http://localhost:4444" |
| 28 |
if err != nil { |
| 29 |
panic(err) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func (s *S) TearDownTest(c *C) { |
| 34 |
testServer.Flush() |
| 35 |
} |
| 36 |
|
| 37 |
func (s *S) Test_endpoint(c *C) { |
| 38 |
c.Assert(create.endpoint("1", ""), Equals, "/dns/managed/1/records/") |
| 39 |
c.Assert(retrieve.endpoint("1", ""), Equals, "/dns/managed/1/records/") |
| 40 |
c.Assert(update.endpoint("1", "2"), Equals, "/dns/managed/1/records/2/") |
| 41 |
c.Assert(destroy.endpoint("1", "2"), Equals, "/dns/managed/1/records/2/") |
| 42 |
} |
| 43 |
|
| 44 |
func (s *S) Test_CreateRecordGood(c *C) { |
| 45 |
testServer.Response(201, nil, recordCreate) |
| 46 |
cr := map[string]interface{}{ |
| 47 |
"Name": "test", |
| 48 |
"Value": "1.1.1.1", |
| 49 |
} |
| 50 |
id, err := s.client.CreateRecord("870073", cr) |
| 51 |
_ = testServer.WaitRequest() |
| 52 |
c.Assert(err, IsNil) |
| 53 |
c.Assert(id, Equals, "10022989") |
| 54 |
} |
| 55 |
|
| 56 |
func (s *S) Test_CreateRecordBad(c *C) { |
| 57 |
testServer.Response(404, nil, "") |
| 58 |
cr := map[string]interface{}{ |
| 59 |
"Name": "test", |
| 60 |
"Value": "1.1.1.1", |
| 61 |
} |
| 62 |
_, err := s.client.CreateRecord("70073", cr) |
| 63 |
_ = testServer.WaitRequest() |
| 64 |
c.Assert(err, NotNil) |
| 65 |
} |
| 66 |
|
| 67 |
func (s *S) Test_ReadRecordGood(c *C) { |
| 68 |
testServer.Response(200, nil, recordRead) |
| 69 |
record, err := s.client.ReadRecord("870073", "10039429") |
| 70 |
_ = testServer.WaitRequest() |
| 71 |
c.Assert(err, IsNil) |
| 72 |
c.Assert(record.RecordID, Equals, int64(10039429)) |
| 73 |
} |
| 74 |
|
| 75 |
func (s *S) Test_ReadRecordBad(c *C) { |
| 76 |
testServer.Response(200, nil, recordRead) |
| 77 |
record, err := s.client.ReadRecord("870073", "1003942") |
| 78 |
_ = testServer.WaitRequest() |
| 79 |
c.Assert(err, NotNil) |
| 80 |
c.Assert(record, IsNil) |
| 81 |
c.Assert(fmt.Sprintf("%s", err), Equals, "Unable to find record 1003942") |
| 82 |
} |
| 83 |
|
| 84 |
func (s *S) Test_UpdateRecordGood(c *C) { |
| 85 |
testServer.Response(200, nil, recordRead) |
| 86 |
testServer.Response(200, nil, "") |
| 87 |
cr := map[string]interface{}{ |
| 88 |
"Name": "test-update", |
| 89 |
} |
| 90 |
recordID, err := s.client.UpdateRecord("870073", "10039429", cr) |
| 91 |
_ = testServer.WaitRequest() |
| 92 |
c.Assert(err, IsNil) |
| 93 |
c.Assert(recordID, Equals, "10039429") |
| 94 |
} |
| 95 |
|
| 96 |
func (s *S) Test_UpdateRecordBad(c *C) { |
| 97 |
testServer.Response(200, nil, recordRead) |
| 98 |
cr := map[string]interface{}{ |
| 99 |
"Name": "test-update", |
| 100 |
} |
| 101 |
recordID, err := s.client.UpdateRecord("870073", "100394", cr) |
| 102 |
_ = testServer.WaitRequest() |
| 103 |
c.Assert(err, NotNil) |
| 104 |
c.Assert(recordID, Equals, "") |
| 105 |
c.Assert(fmt.Sprintf("%s", err), Equals, "Unable to find record 100394") |
| 106 |
} |
| 107 |
|
| 108 |
func (s *S) Test_DeleteRecordGood(c *C) { |
| 109 |
testServer.Response(200, nil, "") |
| 110 |
err := s.client.DeleteRecord("870073", "10039429") |
| 111 |
c.Assert(err, IsNil) |
| 112 |
} |
| 113 |
|
| 114 |
func (s *S) Test_DeleteRecordBad(c *C) { |
| 115 |
testServer.Response(404, nil, "") |
| 116 |
err := s.client.DeleteRecord("870073", "100394") |
| 117 |
c.Assert(err, NotNil) |
| 118 |
c.Assert(fmt.Sprintf("%s", err), Equals, "Unable to find record 100394") |
| 119 |
} |
| 120 |
|
| 121 |
var recordCreate = `{ |
| 122 |
"name":"test", |
| 123 |
"value":"1.1.1.1", |
| 124 |
"id":10022989, |
| 125 |
"type":"A", |
| 126 |
"source":1, |
| 127 |
"failover":false, |
| 128 |
"monitor":false, |
| 129 |
"sourceId":870073, |
| 130 |
"dynamicDns":false, |
| 131 |
"failed":false, |
| 132 |
"gtdLocation":"DEFAULT", |
| 133 |
"hardLink":false, |
| 134 |
"ttl":86400 |
| 135 |
}` |
| 136 |
|
| 137 |
var recordRead = `{ |
| 138 |
"data":[ |
| 139 |
{ |
| 140 |
"name":"test", |
| 141 |
"value":"1.1.1.1", |
| 142 |
"id":10039428, |
| 143 |
"type":"A", |
| 144 |
"source":1, |
| 145 |
"failover":false, |
| 146 |
"monitor":false, |
| 147 |
"sourceId":870073, |
| 148 |
"dynamicDns":false, |
| 149 |
"failed":false, |
| 150 |
"gtdLocation":"DEFAULT", |
| 151 |
"ha rdLink":false, |
| 152 |
"ttl":86400 |
| 153 |
}, |
| 154 |
{ |
| 155 |
"name":"test", |
| 156 |
"value":"1.1.1.2", |
| 157 |
"id":10039429, |
| 158 |
"type":"A", |
| 159 |
"source":1, |
| 160 |
"failover":false, |
| 161 |
"monitor":false, |
| 162 |
"sourceId":870073, |
| 163 |
"dynamicDns":false, |
| 164 |
"failed":false, |
| 165 |
"gtdLocation":"DEFAULT", |
| 166 |
"ha rdLink":false, |
| 167 |
"ttl":86400 |
| 168 |
} |
| 169 |
], |
| 170 |
"page":0, |
| 171 |
"totalPages":1, |
| 172 |
"totalRecords":2 |
| 173 |
}` |