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
|
package dnsimple
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2"
)
var (
dnsimpleLiveTest bool
dnsimpleToken string
dnsimpleBaseURL string
dnsimpleClient *Client
)
func init() {
dnsimpleToken = os.Getenv("DNSIMPLE_TOKEN")
dnsimpleBaseURL = os.Getenv("DNSIMPLE_BASE_URL")
// Prevent people from wiping out their entire production account by mistake
if dnsimpleBaseURL == "" {
dnsimpleBaseURL = "https://api.sandbox.dnsimple.com"
}
if len(dnsimpleToken) > 0 {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: dnsimpleToken})
tc := oauth2.NewClient(context.Background(), ts)
dnsimpleLiveTest = true
dnsimpleClient = NewClient(tc)
dnsimpleClient.BaseURL = dnsimpleBaseURL
dnsimpleClient.UserAgent = fmt.Sprintf("%v +livetest", dnsimpleClient.UserAgent)
}
}
func TestLive_Whoami(t *testing.T) {
if !dnsimpleLiveTest {
t.Skip("skipping live test")
}
whoamiResponse, err := dnsimpleClient.Identity.Whoami(context.Background())
assert.NoError(t, err)
fmt.Printf("RateLimit: %v/%v until %v\n", whoamiResponse.RateLimitRemaining(), whoamiResponse.RateLimit(), whoamiResponse.RateLimitReset())
whoami := whoamiResponse.Data
fmt.Printf("Account: %+v\n", whoami.Account)
fmt.Printf("User: %+v\n", whoami.User)
}
func TestLive_Domains(t *testing.T) {
if !dnsimpleLiveTest {
t.Skip("skipping live test")
}
whoami, err := Whoami(context.Background(), dnsimpleClient)
if err != nil {
t.Fatalf("Live Whoami() returned error: %v", err)
}
accountID := whoami.Account.ID
domainsResponse, err := dnsimpleClient.Domains.ListDomains(context.Background(), fmt.Sprintf("%v", accountID), nil)
assert.NoError(t, err)
fmt.Printf("RateLimit: %v/%v until %v\n", domainsResponse.RateLimitRemaining(), domainsResponse.RateLimit(), domainsResponse.RateLimitReset())
fmt.Printf("Domains: %+v\n", domainsResponse.Data)
}
func TestLive_Registration(t *testing.T) {
if !dnsimpleLiveTest {
t.Skip("skipping live test")
}
whoami, err := Whoami(context.Background(), dnsimpleClient)
assert.NoError(t, err)
accountID := whoami.Account.ID
// TODO: fetch the registrant randomly
registerRequest := &RegisterDomainInput{RegistrantID: 2}
registrationResponse, err := dnsimpleClient.Registrar.RegisterDomain(context.Background(), fmt.Sprintf("%v", accountID), fmt.Sprintf("example-%v.com", time.Now().Unix()), registerRequest)
assert.NoError(t, err)
fmt.Printf("RateLimit: %v/%v until %v\n", registrationResponse.RateLimitRemaining(), registrationResponse.RateLimit(), registrationResponse.RateLimitReset())
fmt.Printf("Domain: %+v\n", registrationResponse.Data)
}
func TestLive_Webhooks(t *testing.T) {
if !dnsimpleLiveTest {
t.Skip("skipping live test")
}
var err error
var webhook *Webhook
var webhookResponse *WebhookResponse
var webhooksResponse *WebhooksResponse
whoami, err := Whoami(context.Background(), dnsimpleClient)
assert.NoError(t, err)
accountID := whoami.Account.ID
webhooksResponse, err = dnsimpleClient.Webhooks.ListWebhooks(context.Background(), fmt.Sprintf("%v", accountID), nil)
assert.NoError(t, err)
fmt.Printf("RateLimit: %v/%v until %v\n", webhooksResponse.RateLimitRemaining(), webhooksResponse.RateLimit(), webhooksResponse.RateLimitReset())
fmt.Printf("Webhooks: %+v\n", webhooksResponse.Data)
webhookAttributes := Webhook{URL: "https://livetest.test"}
webhookResponse, err = dnsimpleClient.Webhooks.CreateWebhook(context.Background(), fmt.Sprintf("%v", accountID), webhookAttributes)
assert.NoError(t, err)
fmt.Printf("RateLimit: %v/%v until %v\n", webhooksResponse.RateLimitRemaining(), webhooksResponse.RateLimit(), webhooksResponse.RateLimitReset())
fmt.Printf("Webhook: %+v\n", webhookResponse.Data)
webhook = webhookResponse.Data
webhookResponse, err = dnsimpleClient.Webhooks.DeleteWebhook(context.Background(), fmt.Sprintf("%v", accountID), webhook.ID)
assert.NoError(t, err)
fmt.Printf("RateLimit: %v/%v until %v\n", webhooksResponse.RateLimitRemaining(), webhooksResponse.RateLimit(), webhooksResponse.RateLimitReset())
fmt.Printf("Webhook: %+v\n", webhookResponse.Data)
}
func TestLive_Zones(t *testing.T) {
if !dnsimpleLiveTest {
t.Skip("skipping live test")
}
whoami, err := Whoami(context.Background(), dnsimpleClient)
assert.NoError(t, err)
accountID := fmt.Sprintf("%v", whoami.Account.ID)
domainResponse, err := dnsimpleClient.Domains.CreateDomain(context.Background(), accountID, Domain{Name: fmt.Sprintf("example-%v.test", time.Now().Unix())})
assert.NoError(t, err)
zoneName := domainResponse.Data.Name
recordName := fmt.Sprintf("%v", time.Now().Unix())
recordResponse, err := dnsimpleClient.Zones.CreateRecord(context.Background(), accountID, zoneName, ZoneRecordAttributes{Name: &recordName, Type: "TXT", Content: "Test"})
assert.NoError(t, err)
fmt.Printf("ZoneRecord: %+v\n", recordResponse.Data)
}
func TestLive_Error(t *testing.T) {
if !dnsimpleLiveTest {
t.Skip("skipping live test")
}
whoami, err := Whoami(context.Background(), dnsimpleClient)
assert.NoError(t, err)
_, err = dnsimpleClient.Registrar.RegisterDomain(context.Background(), fmt.Sprintf("%v", whoami.Account.ID), fmt.Sprintf("example-%v.test", time.Now().Unix()), &RegisterDomainInput{})
assert.NoError(t, err)
e := err.(*ErrorResponse)
fmt.Println(e.Message)
}
|