2016-02-29 02:48:41 +00:00
|
|
|
package dnsimple
|
2016-01-26 11:14:10 +00:00
|
|
|
|
|
|
|
import (
|
2016-01-26 11:42:44 +00:00
|
|
|
"os"
|
2016-01-26 11:14:10 +00:00
|
|
|
"testing"
|
2016-01-26 14:09:33 +00:00
|
|
|
"time"
|
2016-01-26 11:14:10 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2016-01-26 11:42:44 +00:00
|
|
|
var (
|
2017-03-17 18:40:51 +00:00
|
|
|
dnsimpleLiveTest bool
|
|
|
|
dnsimpleOauthToken string
|
|
|
|
dnsimpleDomain string
|
|
|
|
dnsimpleBaseUrl string
|
2016-01-26 11:42:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-03-17 18:40:51 +00:00
|
|
|
dnsimpleOauthToken = os.Getenv("DNSIMPLE_OAUTH_TOKEN")
|
2016-01-26 14:09:33 +00:00
|
|
|
dnsimpleDomain = os.Getenv("DNSIMPLE_DOMAIN")
|
2017-03-17 18:40:51 +00:00
|
|
|
dnsimpleBaseUrl = "https://api.sandbox.dnsimple.com"
|
|
|
|
|
|
|
|
if len(dnsimpleOauthToken) > 0 && len(dnsimpleDomain) > 0 {
|
|
|
|
baseUrl := os.Getenv("DNSIMPLE_BASE_URL")
|
|
|
|
|
|
|
|
if baseUrl != "" {
|
|
|
|
dnsimpleBaseUrl = baseUrl
|
|
|
|
}
|
|
|
|
|
2016-01-26 14:09:33 +00:00
|
|
|
dnsimpleLiveTest = true
|
|
|
|
}
|
2016-01-26 11:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func restoreDNSimpleEnv() {
|
2017-03-17 18:40:51 +00:00
|
|
|
os.Setenv("DNSIMPLE_OAUTH_TOKEN", dnsimpleOauthToken)
|
|
|
|
os.Setenv("DNSIMPLE_BASE_URL", dnsimpleBaseUrl)
|
2016-01-26 11:42:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 18:40:51 +00:00
|
|
|
//
|
|
|
|
// NewDNSProvider
|
|
|
|
//
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
func TestNewDNSProviderValid(t *testing.T) {
|
2017-03-17 18:40:51 +00:00
|
|
|
defer restoreDNSimpleEnv()
|
|
|
|
|
|
|
|
os.Setenv("DNSIMPLE_OAUTH_TOKEN", "123")
|
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
|
|
|
|
assert.NotNil(t, provider)
|
|
|
|
assert.Equal(t, "lego", provider.client.UserAgent)
|
2016-01-26 11:14:10 +00:00
|
|
|
assert.NoError(t, err)
|
2016-01-26 11:42:44 +00:00
|
|
|
}
|
2017-03-17 18:40:51 +00:00
|
|
|
|
|
|
|
func TestNewDNSProviderValidWithBaseUrl(t *testing.T) {
|
|
|
|
defer restoreDNSimpleEnv()
|
|
|
|
|
|
|
|
os.Setenv("DNSIMPLE_OAUTH_TOKEN", "123")
|
|
|
|
os.Setenv("DNSIMPLE_BASE_URL", "https://api.dnsimple.test")
|
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
|
|
|
|
assert.NotNil(t, provider)
|
2016-01-26 11:42:44 +00:00
|
|
|
assert.NoError(t, err)
|
2017-03-17 18:40:51 +00:00
|
|
|
|
|
|
|
assert.Equal(t, provider.client.BaseURL, "https://api.dnsimple.test")
|
2016-01-26 11:14:10 +00:00
|
|
|
}
|
2016-01-26 11:25:51 +00:00
|
|
|
|
2017-03-17 18:40:51 +00:00
|
|
|
func TestNewDNSProviderInvalidWithMissingOauthToken(t *testing.T) {
|
|
|
|
if dnsimpleLiveTest {
|
|
|
|
t.Skip("skipping test in live mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer restoreDNSimpleEnv()
|
|
|
|
|
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
|
|
|
|
assert.Nil(t, provider)
|
2017-04-03 23:41:49 +00:00
|
|
|
assert.EqualError(t, err, "DNSimple OAuth token is missing")
|
2016-01-26 11:25:51 +00:00
|
|
|
}
|
2016-01-26 14:09:33 +00:00
|
|
|
|
2017-03-17 18:40:51 +00:00
|
|
|
//
|
|
|
|
// NewDNSProviderCredentials
|
|
|
|
//
|
|
|
|
|
|
|
|
func TestNewDNSProviderCredentialsValid(t *testing.T) {
|
|
|
|
provider, err := NewDNSProviderCredentials("123", "")
|
|
|
|
|
|
|
|
assert.NotNil(t, provider)
|
|
|
|
assert.Equal(t, "lego", provider.client.UserAgent)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderCredentialsValidWithBaseUrl(t *testing.T) {
|
|
|
|
provider, err := NewDNSProviderCredentials("123", "https://api.dnsimple.test")
|
|
|
|
|
|
|
|
assert.NotNil(t, provider)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, provider.client.BaseURL, "https://api.dnsimple.test")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderCredentialsInvalidWithMissingOauthToken(t *testing.T) {
|
|
|
|
provider, err := NewDNSProviderCredentials("", "")
|
|
|
|
|
|
|
|
assert.Nil(t, provider)
|
2017-04-03 23:41:49 +00:00
|
|
|
assert.EqualError(t, err, "DNSimple OAuth token is missing")
|
2017-03-17 18:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Present
|
|
|
|
//
|
|
|
|
|
2016-01-26 14:09:33 +00:00
|
|
|
func TestLiveDNSimplePresent(t *testing.T) {
|
|
|
|
if !dnsimpleLiveTest {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2017-03-17 18:40:51 +00:00
|
|
|
provider, err := NewDNSProviderCredentials(dnsimpleOauthToken, dnsimpleBaseUrl)
|
2016-01-26 14:09:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = provider.Present(dnsimpleDomain, "", "123d==")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2017-03-17 18:40:51 +00:00
|
|
|
//
|
|
|
|
// Cleanup
|
|
|
|
//
|
|
|
|
|
2016-01-26 14:09:33 +00:00
|
|
|
func TestLiveDNSimpleCleanUp(t *testing.T) {
|
|
|
|
if !dnsimpleLiveTest {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
|
2017-03-17 18:40:51 +00:00
|
|
|
provider, err := NewDNSProviderCredentials(dnsimpleOauthToken, dnsimpleBaseUrl)
|
2016-01-26 14:09:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = provider.CleanUp(dnsimpleDomain, "", "123d==")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|