2019-12-07 22:59:59 +00:00
|
|
|
package checkdomain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2019-12-07 22:59:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
const envDomain = envNamespace + "DOMAIN"
|
|
|
|
|
|
|
|
var envTest = tester.NewEnvTest(
|
|
|
|
EnvEndpoint,
|
|
|
|
EnvToken).
|
|
|
|
WithDomain(envDomain)
|
2019-12-07 22:59:59 +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
|
|
|
EnvToken: "dummy",
|
2019-12-07 22:59:59 +00: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-11 22:51:10 +00:00
|
|
|
EnvToken: "dummy",
|
|
|
|
EnvEndpoint: ":",
|
2019-12-07 22:59:59 +00:00
|
|
|
},
|
2020-02-25 23:02:29 +00:00
|
|
|
expected: `checkdomain: invalid CHECKDOMAIN_ENDPOINT: parse ":": missing protocol scheme`,
|
2019-12-07 22:59:59 +00: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()
|
|
|
|
|
|
|
|
if len(test.expected) == 0 {
|
|
|
|
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()
|
|
|
|
config.Endpoint, _ = url.Parse(defaultEndpoint)
|
|
|
|
|
|
|
|
if test.token != "" {
|
|
|
|
config.Token = test.token
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
|
|
|
if len(test.expected) == 0 {
|
|
|
|
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()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
|
|
|
if !envTest.IsLiveTest() {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
|
|
|
envTest.RestoreEnv()
|
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|