2016-06-28 23:00:05 +02:00
|
|
|
package dnsmadeeasy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2018-10-12 19:29:18 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-06-28 23:00:05 +02:00
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
const envDomain = envNamespace + "DOMAIN"
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
var envTest = tester.NewEnvTest(
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAPIKey,
|
|
|
|
EnvAPISecret).
|
|
|
|
WithDomain(envDomain)
|
2016-06-28 23:00:05 +02:00
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
2020-03-12 00:51:10 +02:00
|
|
|
os.Setenv(EnvSandbox, "true")
|
2018-10-12 19:29:18 +02:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAPIKey: "123",
|
|
|
|
EnvAPISecret: "456",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAPIKey: "",
|
|
|
|
EnvAPISecret: "",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
expected: "dnsmadeeasy: some credentials information are missing: DNSMADEEASY_API_KEY,DNSMADEEASY_API_SECRET",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing access key",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAPIKey: "",
|
|
|
|
EnvAPISecret: "456",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
expected: "dnsmadeeasy: some credentials information are missing: DNSMADEEASY_API_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing secret key",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAPIKey: "123",
|
|
|
|
EnvAPISecret: "",
|
2018-10-12 19:29:18 +02: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 17:52:57 +02:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.envVars)
|
2018-10-12 19:29:18 +02:00
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
2021-03-04 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2018-10-12 19:29:18 +02:00
|
|
|
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-12 00:51:10 +02:00
|
|
|
os.Setenv(EnvSandbox, "true")
|
2018-12-06 23:50:17 +02:00
|
|
|
|
2018-10-12 19:29:18 +02: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)
|
|
|
|
|
2021-03-04 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2018-10-12 19:29:18 +02:00
|
|
|
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 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestLivePresentAndCleanup(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
if !envTest.IsLiveTest() {
|
2016-06-28 23:00:05 +02:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
os.Setenv(EnvSandbox, "true")
|
2018-12-06 23:50:17 +02:00
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
envTest.RestoreEnv()
|
2016-06-28 23:00:05 +02:00
|
|
|
provider, err := NewDNSProvider()
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err)
|
2016-06-28 23:00:05 +02:00
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err)
|
2016-06-28 23:00:05 +02:00
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err)
|
2016-06-28 23:00:05 +02:00
|
|
|
}
|