| 1 |
package dynect |
| 2 |
|
| 3 |
import ( |
| 4 |
"log" |
| 5 |
"os" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/dnaeon/go-vcr/recorder" |
| 10 |
) |
| 11 |
|
| 12 |
var ( |
| 13 |
DynCustomerName string |
| 14 |
DynUsername string |
| 15 |
DynPassword string |
| 16 |
testZone string |
| 17 |
) |
| 18 |
|
| 19 |
func getenv(key, defaultValue string) string { |
| 20 |
if value, ok := os.LookupEnv(key); ok { |
| 21 |
return value |
| 22 |
} |
| 23 |
return defaultValue |
| 24 |
} |
| 25 |
|
| 26 |
func init() { |
| 27 |
DynCustomerName = getenv("DYNECT_CUSTOMER_NAME", "go-dynect") |
| 28 |
DynUsername = getenv("DYNECT_USER_NAME", "dynect-user") |
| 29 |
DynPassword = getenv("DYNECT_PASSWORD", "p@55w0rd") |
| 30 |
testZone = getenv("DYNECT_TEST_ZONE", "go-dynect.test") |
| 31 |
} |
| 32 |
|
| 33 |
// test helper to begin recording or playback of vcr cassette |
| 34 |
func withCassette(cassetteName string, f func(*recorder.Recorder)) { |
| 35 |
r, err := recorder.New(cassetteName) |
| 36 |
if err != nil { |
| 37 |
log.Fatal(err) |
| 38 |
} |
| 39 |
|
| 40 |
defer r.Stop() |
| 41 |
|
| 42 |
f(r) |
| 43 |
} |
| 44 |
|
| 45 |
// test helper to setup client with vcr cassette |
| 46 |
func withClient(cassetteName string, f func(*Client)) { |
| 47 |
withCassette(cassetteName, func(r *recorder.Recorder) { |
| 48 |
c := NewClient(DynCustomerName) |
| 49 |
c.SetTransport(r) |
| 50 |
c.Verbose(true) |
| 51 |
|
| 52 |
f(c) |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
// test helper to setup authenticated client with vcr cassette |
| 57 |
func testWithClientSession(cassetteName string, t *testing.T, f func(*Client)) { |
| 58 |
withClient(cassetteName, func(c *Client) { |
| 59 |
if err := c.Login(DynUsername, DynPassword); err != nil { |
| 60 |
t.Fatal(err) |
| 61 |
} |
| 62 |
|
| 63 |
defer func() { |
| 64 |
if err := c.Logout(); err != nil { |
| 65 |
t.Error(err) |
| 66 |
} |
| 67 |
}() |
| 68 |
|
| 69 |
f(c) |
| 70 |
}) |
| 71 |
} |
| 72 |
|
| 73 |
func TestLoginLogout(t *testing.T) { |
| 74 |
withClient("fixtures/login_logout", func(c *Client) { |
| 75 |
if err := c.Login(DynUsername, DynPassword); err != nil { |
| 76 |
t.Error(err) |
| 77 |
} |
| 78 |
|
| 79 |
if err := c.Logout(); err != nil { |
| 80 |
t.Error(err) |
| 81 |
} |
| 82 |
}) |
| 83 |
} |
| 84 |
|
| 85 |
func TestZonesRequest(t *testing.T) { |
| 86 |
testWithClientSession("fixtures/zones_request", t, func(c *Client) { |
| 87 |
var resp ZonesResponse |
| 88 |
|
| 89 |
if err := c.Do("GET", "Zone", nil, &resp); err != nil { |
| 90 |
t.Fatal(err) |
| 91 |
} |
| 92 |
|
| 93 |
nresults := len(resp.Data) |
| 94 |
for i, zone := range resp.Data { |
| 95 |
parts := strings.Split(zone, "/") |
| 96 |
t.Logf("(%d/%d) %q", i+1, nresults, parts[len(parts)-2]) |
| 97 |
} |
| 98 |
}) |
| 99 |
} |
| 100 |
|
| 101 |
func TestFetchingAllZoneRecords(t *testing.T) { |
| 102 |
testWithClientSession("fixtures/fetching_all_zone_records", t, func(c *Client) { |
| 103 |
var resp AllRecordsResponse |
| 104 |
|
| 105 |
if err := c.Do("GET", "AllRecord/"+testZone, nil, &resp); err != nil { |
| 106 |
t.Error(err) |
| 107 |
} |
| 108 |
|
| 109 |
for _, zr := range resp.Data { |
| 110 |
parts := strings.Split(zr, "/") |
| 111 |
uri := strings.Join(parts[2:], "/") |
| 112 |
t.Log(uri) |
| 113 |
|
| 114 |
var record RecordResponse |
| 115 |
|
| 116 |
if err := c.Do("GET", uri, nil, &record); err != nil { |
| 117 |
t.Fatal(err) |
| 118 |
} |
| 119 |
|
| 120 |
t.Log("OK") |
| 121 |
} |
| 122 |
}) |
| 123 |
} |