lego/providers/dns/checkdomain/checkdomain_test.go

130 lines
2.5 KiB
Go
Raw Normal View History

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