2019-12-08 00:59:59 +02:00
|
|
|
package checkdomain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2023-05-05 09:49:38 +02:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/checkdomain/internal"
|
2019-12-08 00:59:59 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
const envDomain = envNamespace + "DOMAIN"
|
|
|
|
|
|
|
|
var envTest = tester.NewEnvTest(
|
|
|
|
EnvEndpoint,
|
|
|
|
EnvToken).
|
|
|
|
WithDomain(envDomain)
|
2019-12-08 00:59:59 +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
|
|
|
EnvToken: "dummy",
|
2019-12-08 00:59:59 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no token",
|
|
|
|
envVars: map[string]string{},
|
|
|
|
expected: "checkdomain: some credentials information are missing: CHECKDOMAIN_TOKEN",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "invalid endpoint",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvToken: "dummy",
|
|
|
|
EnvEndpoint: ":",
|
2019-12-08 00:59:59 +02:00
|
|
|
},
|
2020-02-26 01:02:29 +02:00
|
|
|
expected: `checkdomain: invalid CHECKDOMAIN_ENDPOINT: parse ":": missing protocol scheme`,
|
2019-12-08 00:59:59 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.envVars)
|
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
2021-03-04 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2019-12-08 00:59:59 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
token string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
token: "dummy",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing token",
|
|
|
|
token: "",
|
|
|
|
expected: "checkdomain: missing token",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
2023-05-05 09:49:38 +02:00
|
|
|
config.Endpoint, _ = url.Parse(internal.DefaultEndpoint)
|
2019-12-08 00:59:59 +02:00
|
|
|
|
|
|
|
if test.token != "" {
|
|
|
|
config.Token = test.token
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
2021-03-04 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2019-12-08 00:59:59 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLivePresent(t *testing.T) {
|
|
|
|
if !envTest.IsLiveTest() {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
|
|
|
envTest.RestoreEnv()
|
|
|
|
provider, err := NewDNSProvider()
|
2023-10-31 15:08:50 +02:00
|
|
|
require.NoError(t, err)
|
2019-12-08 00:59:59 +02:00
|
|
|
|
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2023-10-31 15:08:50 +02:00
|
|
|
require.NoError(t, err)
|
2019-12-08 00:59:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
|
|
|
if !envTest.IsLiveTest() {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
|
|
|
envTest.RestoreEnv()
|
|
|
|
provider, err := NewDNSProvider()
|
2023-10-31 15:08:50 +02:00
|
|
|
require.NoError(t, err)
|
2019-12-08 00:59:59 +02:00
|
|
|
|
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2023-10-31 15:08:50 +02:00
|
|
|
require.NoError(t, err)
|
2019-12-08 00:59:59 +02:00
|
|
|
}
|