2019-03-11 16:56:48 +00:00
|
|
|
package netcup
|
2018-09-08 12:08:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2018-10-12 17:29:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-09-08 12:08:07 +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
|
|
|
EnvCustomerNumber,
|
|
|
|
EnvAPIKey,
|
|
|
|
EnvAPIPassword).
|
|
|
|
WithDomain(envDomain)
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvCustomerNumber: "A",
|
|
|
|
EnvAPIKey: "B",
|
|
|
|
EnvAPIPassword: "C",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvCustomerNumber: "",
|
|
|
|
EnvAPIKey: "",
|
|
|
|
EnvAPIPassword: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
expected: "netcup: some credentials information are missing: NETCUP_CUSTOMER_NUMBER,NETCUP_API_KEY,NETCUP_API_PASSWORD",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing customer number",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvCustomerNumber: "",
|
|
|
|
EnvAPIKey: "B",
|
|
|
|
EnvAPIPassword: "C",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
expected: "netcup: some credentials information are missing: NETCUP_CUSTOMER_NUMBER",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing API key",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvCustomerNumber: "A",
|
|
|
|
EnvAPIKey: "",
|
|
|
|
EnvAPIPassword: "C",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
expected: "netcup: some credentials information are missing: NETCUP_API_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api password",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvCustomerNumber: "A",
|
|
|
|
EnvAPIKey: "B",
|
|
|
|
EnvAPIPassword: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
expected: "netcup: some credentials information are missing: NETCUP_API_PASSWORD",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2018-10-12 17:29:18 +00: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) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
customer string
|
|
|
|
key string
|
|
|
|
password string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
customer: "A",
|
|
|
|
key: "B",
|
|
|
|
password: "C",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
2018-10-23 00:01:13 +00:00
|
|
|
expected: "netcup: credentials missing",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing customer",
|
|
|
|
customer: "",
|
|
|
|
key: "B",
|
|
|
|
password: "C",
|
2018-10-23 00:01:13 +00:00
|
|
|
expected: "netcup: credentials missing",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing key",
|
|
|
|
customer: "A",
|
|
|
|
key: "",
|
|
|
|
password: "C",
|
2018-10-23 00:01:13 +00:00
|
|
|
expected: "netcup: credentials missing",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing password",
|
|
|
|
customer: "A",
|
|
|
|
key: "B",
|
|
|
|
password: "",
|
2018-10-23 00:01:13 +00:00
|
|
|
expected: "netcup: credentials missing",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Customer = test.customer
|
|
|
|
config.Key = test.key
|
|
|
|
config.Password = test.password
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2018-10-12 17:29:18 +00: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 TestLivePresentAndCleanup(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-09-08 12:08:07 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2018-09-08 12:08:07 +00:00
|
|
|
p, err := NewDNSProvider()
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-08 12:08:07 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
fqdn, _ := dns01.GetRecord(envTest.GetDomain(), "123d==")
|
2018-09-08 12:08:07 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
zone, err := dns01.FindZoneByFqdn(fqdn)
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err, "error finding DNSZone")
|
2018-09-08 12:08:07 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
zone = dns01.UnFqdn(zone)
|
2018-09-08 12:08:07 +00:00
|
|
|
|
|
|
|
testCases := []string{
|
|
|
|
zone,
|
|
|
|
"sub." + zone,
|
|
|
|
"*." + zone,
|
|
|
|
"*.sub." + zone,
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(fmt.Sprintf("domain(%s)", test), func(t *testing.T) {
|
|
|
|
err = p.Present(test, "987d", "123d==")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-08 12:08:07 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
err = p.CleanUp(test, "987d", "123d==")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err, "Did not clean up! Please remove record yourself.")
|
2018-09-08 12:08:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|