2019-03-11 16:56:48 +00:00
|
|
|
package namedotcom
|
2018-03-14 17:43:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-30 19:19:32 +00:00
|
|
|
"github.com/go-acme/lego/v3/platform/tester"
|
2018-09-24 19:07:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-03-14 17:43:09 +00:00
|
|
|
)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
var envTest = tester.NewEnvTest(
|
|
|
|
"NAMECOM_USERNAME",
|
|
|
|
"NAMECOM_API_TOKEN").
|
|
|
|
WithDomain("NAMEDOTCOM_DOMAIN")
|
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{
|
|
|
|
"NAMECOM_USERNAME": "A",
|
|
|
|
"NAMECOM_API_TOKEN": "B",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"NAMECOM_USERNAME": "",
|
|
|
|
"NAMECOM_API_TOKEN": "",
|
|
|
|
},
|
|
|
|
expected: "namedotcom: some credentials information are missing: NAMECOM_USERNAME,NAMECOM_API_TOKEN",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing username",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"NAMECOM_USERNAME": "",
|
|
|
|
"NAMECOM_API_TOKEN": "B",
|
|
|
|
},
|
|
|
|
expected: "namedotcom: some credentials information are missing: NAMECOM_USERNAME",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api token",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"NAMECOM_USERNAME": "A",
|
|
|
|
"NAMECOM_API_TOKEN": "",
|
|
|
|
},
|
|
|
|
expected: "namedotcom: some credentials information are missing: NAMECOM_API_TOKEN",
|
|
|
|
},
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
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-09-15 17:07:24 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
p, err := NewDNSProvider()
|
2018-03-14 17:43:09 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
if len(test.expected) == 0 {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
apiToken string
|
|
|
|
username string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
apiToken: "A",
|
|
|
|
username: "B",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "namedotcom: username is required",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing API token",
|
|
|
|
apiToken: "",
|
|
|
|
username: "B",
|
|
|
|
expected: "namedotcom: API token is required",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing username",
|
|
|
|
apiToken: "A",
|
|
|
|
username: "",
|
|
|
|
expected: "namedotcom: username is required",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Username = test.username
|
|
|
|
config.APIToken = test.apiToken
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
2018-03-14 17:43:09 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
if len(test.expected) == 0 {
|
|
|
|
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 TestLivePresent(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-03-14 17:43:09 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2018-10-12 17:29:18 +00:00
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2018-03-14 17:43:09 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-10-12 17:29:18 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2018-10-12 17:29:18 +00:00
|
|
|
provider, err := NewDNSProvider()
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-14 17:43:09 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|