2019-03-11 18:56:48 +02:00
|
|
|
package hostingde
|
2018-09-15 23:25:14 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2018-09-15 23:25:14 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
EnvZoneName).
|
|
|
|
WithDomain(envDomain)
|
2018-09-15 23:25:14 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
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",
|
2021-05-14 18:03:01 +02:00
|
|
|
EnvZoneName: "example.org",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAPIKey: "",
|
|
|
|
EnvZoneName: "",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
2021-05-14 18:03:01 +02:00
|
|
|
expected: "hostingde: some credentials information are missing: HOSTINGDE_API_KEY",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing access key",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAPIKey: "",
|
|
|
|
EnvZoneName: "456",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
expected: "hostingde: some credentials information are missing: HOSTINGDE_API_KEY",
|
|
|
|
},
|
|
|
|
}
|
2018-09-15 23:25:14 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
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.recordIDs)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-09-15 23:25:14 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
apiKey string
|
|
|
|
zoneName string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
apiKey: "123",
|
2021-05-14 18:03:01 +02:00
|
|
|
zoneName: "example.org",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "hostingde: API key missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api key",
|
|
|
|
zoneName: "456",
|
|
|
|
expected: "hostingde: API key missing",
|
|
|
|
},
|
|
|
|
}
|
2018-09-15 23:25:14 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = test.apiKey
|
|
|
|
config.ZoneName = test.zoneName
|
|
|
|
|
|
|
|
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.recordIDs)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-09-15 23:25:14 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestLivePresent(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-09-15 23:25:14 +02:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
envTest.RestoreEnv()
|
2018-10-12 19:29:18 +02:00
|
|
|
provider, err := NewDNSProvider()
|
2018-09-15 23:25:14 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
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)
|
2018-09-15 23:25:14 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-09-15 23:25:14 +02:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
envTest.RestoreEnv()
|
2018-10-12 19:29:18 +02:00
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
require.NoError(t, err)
|
2018-09-15 23:25:14 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
time.Sleep(2 * time.Second)
|
2018-09-15 23:25:14 +02:00
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2018-09-15 23:25:14 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|