lego/providers/dns/dnsimple/dnsimple_test.go

162 lines
3.4 KiB
Go
Raw Normal View History

2016-02-29 02:48:41 +00:00
package dnsimple
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/acme"
)
var (
2017-03-17 18:40:51 +00:00
dnsimpleLiveTest bool
dnsimpleOauthToken string
dnsimpleDomain string
2018-05-30 17:53:04 +00:00
dnsimpleBaseURL string
)
func init() {
2017-03-17 18:40:51 +00:00
dnsimpleOauthToken = os.Getenv("DNSIMPLE_OAUTH_TOKEN")
dnsimpleDomain = os.Getenv("DNSIMPLE_DOMAIN")
2018-05-30 17:53:04 +00:00
dnsimpleBaseURL = "https://api.sandbox.dnsimple.com"
2017-03-17 18:40:51 +00:00
if len(dnsimpleOauthToken) > 0 && len(dnsimpleDomain) > 0 {
2018-05-30 17:53:04 +00:00
baseURL := os.Getenv("DNSIMPLE_BASE_URL")
2017-03-17 18:40:51 +00:00
2018-05-30 17:53:04 +00:00
if baseURL != "" {
dnsimpleBaseURL = baseURL
2017-03-17 18:40:51 +00:00
}
dnsimpleLiveTest = true
}
}
func restoreEnv() {
2017-03-17 18:40:51 +00:00
os.Setenv("DNSIMPLE_OAUTH_TOKEN", dnsimpleOauthToken)
2018-05-30 17:53:04 +00:00
os.Setenv("DNSIMPLE_BASE_URL", dnsimpleBaseURL)
}
2017-03-17 18:40:51 +00:00
//
// NewDNSProvider
//
func TestNewDNSProviderValid(t *testing.T) {
defer restoreEnv()
2017-03-17 18:40:51 +00:00
os.Setenv("DNSIMPLE_OAUTH_TOKEN", "123")
acme.UserAgent = "lego"
2017-03-17 18:40:51 +00:00
provider, err := NewDNSProvider()
assert.NotNil(t, provider)
assert.Equal(t, "lego", provider.client.UserAgent)
assert.NoError(t, err)
}
2017-03-17 18:40:51 +00:00
func TestNewDNSProviderValidWithBaseUrl(t *testing.T) {
defer restoreEnv()
2017-03-17 18:40:51 +00:00
os.Setenv("DNSIMPLE_OAUTH_TOKEN", "123")
os.Setenv("DNSIMPLE_BASE_URL", "https://api.dnsimple.test")
2017-03-17 18:40:51 +00:00
provider, err := NewDNSProvider()
assert.NotNil(t, provider)
assert.NoError(t, err)
2017-03-17 18:40:51 +00:00
assert.Equal(t, provider.client.BaseURL, "https://api.dnsimple.test")
}
2017-03-17 18:40:51 +00:00
func TestNewDNSProviderInvalidWithMissingOauthToken(t *testing.T) {
defer restoreEnv()
os.Setenv("DNSIMPLE_OAUTH_TOKEN", "")
2017-03-17 18:40:51 +00:00
provider, err := NewDNSProvider()
assert.Nil(t, provider)
assert.EqualError(t, err, "dnsimple: OAuth token is missing")
}
2017-03-17 18:40:51 +00:00
//
// NewDNSProviderCredentials
//
func TestNewDNSProviderCredentialsValid(t *testing.T) {
config := NewDefaultConfig()
config.AccessToken = "123"
config.BaseURL = ""
provider, err := NewDNSProviderConfig(config)
require.NoError(t, err)
require.NotNil(t, provider)
2017-03-17 18:40:51 +00:00
assert.Equal(t, "lego", provider.client.UserAgent)
assert.NoError(t, err)
}
func TestNewDNSProviderCredentialsValidWithBaseUrl(t *testing.T) {
config := NewDefaultConfig()
config.AccessToken = "123"
config.BaseURL = "https://api.dnsimple.test"
2017-03-17 18:40:51 +00:00
provider, err := NewDNSProviderConfig(config)
require.NoError(t, err)
require.NotNil(t, provider)
2017-03-17 18:40:51 +00:00
assert.Equal(t, provider.client.BaseURL, "https://api.dnsimple.test")
}
func TestNewDNSProviderCredentialsInvalidWithMissingOauthToken(t *testing.T) {
config := NewDefaultConfig()
config.AccessToken = ""
config.BaseURL = ""
provider, err := NewDNSProviderConfig(config)
2017-03-17 18:40:51 +00:00
assert.Nil(t, provider)
assert.EqualError(t, err, "dnsimple: OAuth token is missing")
2017-03-17 18:40:51 +00:00
}
//
// Present
//
func TestLiveDNSimplePresent(t *testing.T) {
if !dnsimpleLiveTest {
t.Skip("skipping live test")
}
config := NewDefaultConfig()
config.AccessToken = dnsimpleOauthToken
config.BaseURL = dnsimpleBaseURL
provider, err := NewDNSProviderConfig(config)
assert.NoError(t, err)
err = provider.Present(dnsimpleDomain, "", "123d==")
assert.NoError(t, err)
}
2017-03-17 18:40:51 +00:00
//
// Cleanup
//
func TestLiveDNSimpleCleanUp(t *testing.T) {
if !dnsimpleLiveTest {
t.Skip("skipping live test")
}
time.Sleep(time.Second * 1)
config := NewDefaultConfig()
config.AccessToken = dnsimpleOauthToken
config.BaseURL = dnsimpleBaseURL
provider, err := NewDNSProviderConfig(config)
assert.NoError(t, err)
err = provider.CleanUp(dnsimpleDomain, "", "123d==")
assert.NoError(t, err)
}