2019-04-28 12:33:50 +00:00
|
|
|
package joker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2019-04-28 12:33:50 +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(EnvAPIKey, EnvUsername, EnvPassword).
|
|
|
|
WithDomain(envDomain)
|
2019-04-28 12:33:50 +00:00
|
|
|
|
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2019-07-17 00:39:17 +00:00
|
|
|
desc: "success API key",
|
2019-04-28 12:33:50 +00:00
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIKey: "123",
|
2019-04-28 12:33:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-07-17 00:39:17 +00:00
|
|
|
desc: "success username password",
|
2019-04-28 12:33:50 +00:00
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvUsername: "123",
|
|
|
|
EnvPassword: "123",
|
2019-04-28 12:33:50 +00:00
|
|
|
},
|
2019-07-17 00:39:17 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIKey: "",
|
|
|
|
EnvUsername: "",
|
|
|
|
EnvPassword: "",
|
2019-07-17 00:39:17 +00:00
|
|
|
},
|
|
|
|
expected: "joker: some credentials information are missing: JOKER_USERNAME,JOKER_PASSWORD or some credentials information are missing: JOKER_API_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing password",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIKey: "",
|
|
|
|
EnvUsername: "123",
|
|
|
|
EnvPassword: "",
|
2019-07-17 00:39:17 +00:00
|
|
|
},
|
|
|
|
expected: "joker: some credentials information are missing: JOKER_PASSWORD or some credentials information are missing: JOKER_API_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing username",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIKey: "",
|
|
|
|
EnvUsername: "",
|
|
|
|
EnvPassword: "123",
|
2019-07-17 00:39:17 +00:00
|
|
|
},
|
|
|
|
expected: "joker: some credentials information are missing: JOKER_USERNAME or some credentials information are missing: JOKER_API_KEY",
|
2019-04-28 12:33:50 +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)
|
|
|
|
assert.NotNil(t, p.config)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2019-07-17 00:39:17 +00:00
|
|
|
apiKey string
|
|
|
|
username string
|
|
|
|
password string
|
2019-04-28 12:33:50 +00:00
|
|
|
baseURL string
|
|
|
|
expected string
|
|
|
|
expectedBaseURL string
|
|
|
|
}{
|
|
|
|
{
|
2019-07-17 00:39:17 +00:00
|
|
|
desc: "success api key",
|
|
|
|
apiKey: "123",
|
|
|
|
expectedBaseURL: defaultBaseURL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "success username and password",
|
|
|
|
username: "123",
|
|
|
|
password: "123",
|
2019-04-28 12:33:50 +00:00
|
|
|
expectedBaseURL: defaultBaseURL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "joker: credentials missing",
|
|
|
|
expectedBaseURL: defaultBaseURL,
|
|
|
|
},
|
2019-07-17 00:39:17 +00:00
|
|
|
{
|
|
|
|
desc: "missing credentials: username",
|
|
|
|
expected: "joker: credentials missing",
|
|
|
|
username: "123",
|
|
|
|
expectedBaseURL: defaultBaseURL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials: password",
|
|
|
|
expected: "joker: credentials missing",
|
|
|
|
password: "123",
|
|
|
|
expectedBaseURL: defaultBaseURL,
|
|
|
|
},
|
2019-04-28 12:33:50 +00:00
|
|
|
{
|
|
|
|
desc: "Base URL should ends with /",
|
2019-07-17 00:39:17 +00:00
|
|
|
apiKey: "123",
|
2019-04-28 12:33:50 +00:00
|
|
|
baseURL: "http://example.com",
|
|
|
|
expectedBaseURL: "http://example.com/",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Base URL already ends with /",
|
2019-07-17 00:39:17 +00:00
|
|
|
apiKey: "123",
|
2019-04-28 12:33:50 +00:00
|
|
|
baseURL: "http://example.com/",
|
|
|
|
expectedBaseURL: "http://example.com/",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
2019-07-17 00:39:17 +00:00
|
|
|
config.APIKey = test.apiKey
|
|
|
|
config.Username = test.username
|
|
|
|
config.Password = test.password
|
|
|
|
|
2019-04-28 12:33:50 +00:00
|
|
|
if test.baseURL != "" {
|
|
|
|
config.BaseURL = test.baseURL
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
|
|
|
if len(test.expected) == 0 {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
assert.NotNil(t, p.config)
|
|
|
|
assert.Equal(t, test.expectedBaseURL, p.config.BaseURL)
|
|
|
|
} 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()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
|
|
|
if !envTest.IsLiveTest() {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
|
|
|
envTest.RestoreEnv()
|
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|