2016-06-28 21:00:05 +00:00
|
|
|
package dnsmadeeasy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2018-10-12 17:29:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-06-28 21:00:05 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
const envDomain = envNamespace + "DOMAIN"
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
var envTest = tester.NewEnvTest(
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIKey,
|
|
|
|
EnvAPISecret).
|
|
|
|
WithDomain(envDomain)
|
2016-06-28 21:00:05 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
2020-03-11 22:51:10 +00:00
|
|
|
os.Setenv(EnvSandbox, "true")
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIKey: "123",
|
|
|
|
EnvAPISecret: "456",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIKey: "",
|
|
|
|
EnvAPISecret: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
expected: "dnsmadeeasy: some credentials information are missing: DNSMADEEASY_API_KEY,DNSMADEEASY_API_SECRET",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing access key",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIKey: "",
|
|
|
|
EnvAPISecret: "456",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
expected: "dnsmadeeasy: some credentials information are missing: DNSMADEEASY_API_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing secret key",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIKey: "123",
|
|
|
|
EnvAPISecret: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
expected: "dnsmadeeasy: some credentials information are missing: DNSMADEEASY_API_SECRET",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.envVars)
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
|
|
|
if len(test.expected) == 0 {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.client)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
2020-03-11 22:51:10 +00:00
|
|
|
os.Setenv(EnvSandbox, "true")
|
2018-12-06 21:50:17 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
apiKey string
|
|
|
|
apiSecret string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
apiKey: "123",
|
|
|
|
apiSecret: "456",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "dnsmadeeasy: credentials missing: API key",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api key",
|
|
|
|
apiSecret: "456",
|
|
|
|
expected: "dnsmadeeasy: credentials missing: API key",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing secret key",
|
|
|
|
apiKey: "123",
|
|
|
|
expected: "dnsmadeeasy: credentials missing: API secret",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = test.apiKey
|
|
|
|
config.APISecret = test.apiSecret
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
|
|
|
if len(test.expected) == 0 {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.client)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestLivePresentAndCleanup(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2016-06-28 21:00:05 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
os.Setenv(EnvSandbox, "true")
|
2018-12-06 21:50:17 +00:00
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2016-06-28 21:00:05 +00:00
|
|
|
provider, err := NewDNSProvider()
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2016-06-28 21:00:05 +00:00
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2016-06-28 21:00:05 +00:00
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|