2019-04-28 14:33:50 +02:00
|
|
|
package joker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-30 21:19:32 +02:00
|
|
|
"github.com/go-acme/lego/v3/platform/tester"
|
2019-04-28 14:33:50 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2019-07-17 02:39:17 +02:00
|
|
|
var envTest = tester.NewEnvTest("JOKER_API_KEY", "JOKER_USERNAME", "JOKER_PASSWORD").
|
|
|
|
WithDomain("JOKER_DOMAIN")
|
2019-04-28 14:33:50 +02:00
|
|
|
|
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2019-07-17 02:39:17 +02:00
|
|
|
desc: "success API key",
|
2019-04-28 14:33:50 +02:00
|
|
|
envVars: map[string]string{
|
|
|
|
"JOKER_API_KEY": "123",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-07-17 02:39:17 +02:00
|
|
|
desc: "success username password",
|
2019-04-28 14:33:50 +02:00
|
|
|
envVars: map[string]string{
|
2019-07-17 02:39:17 +02:00
|
|
|
"JOKER_USERNAME": "123",
|
|
|
|
"JOKER_PASSWORD": "123",
|
2019-04-28 14:33:50 +02:00
|
|
|
},
|
2019-07-17 02:39:17 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"JOKER_API_KEY": "",
|
|
|
|
"JOKER_USERNAME": "",
|
|
|
|
"JOKER_PASSWORD": "",
|
|
|
|
},
|
|
|
|
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{
|
|
|
|
"JOKER_API_KEY": "",
|
|
|
|
"JOKER_USERNAME": "123",
|
|
|
|
"JOKER_PASSWORD": "",
|
|
|
|
},
|
|
|
|
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{
|
|
|
|
"JOKER_API_KEY": "",
|
|
|
|
"JOKER_USERNAME": "",
|
|
|
|
"JOKER_PASSWORD": "123",
|
|
|
|
},
|
|
|
|
expected: "joker: some credentials information are missing: JOKER_USERNAME or some credentials information are missing: JOKER_API_KEY",
|
2019-04-28 14:33:50 +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()
|
|
|
|
|
|
|
|
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 02:39:17 +02:00
|
|
|
apiKey string
|
|
|
|
username string
|
|
|
|
password string
|
2019-04-28 14:33:50 +02:00
|
|
|
baseURL string
|
|
|
|
expected string
|
|
|
|
expectedBaseURL string
|
|
|
|
}{
|
|
|
|
{
|
2019-07-17 02:39:17 +02:00
|
|
|
desc: "success api key",
|
|
|
|
apiKey: "123",
|
|
|
|
expectedBaseURL: defaultBaseURL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "success username and password",
|
|
|
|
username: "123",
|
|
|
|
password: "123",
|
2019-04-28 14:33:50 +02:00
|
|
|
expectedBaseURL: defaultBaseURL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "joker: credentials missing",
|
|
|
|
expectedBaseURL: defaultBaseURL,
|
|
|
|
},
|
2019-07-17 02:39:17 +02: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 14:33:50 +02:00
|
|
|
{
|
|
|
|
desc: "Base URL should ends with /",
|
2019-07-17 02:39:17 +02:00
|
|
|
apiKey: "123",
|
2019-04-28 14:33:50 +02:00
|
|
|
baseURL: "http://example.com",
|
|
|
|
expectedBaseURL: "http://example.com/",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Base URL already ends with /",
|
2019-07-17 02:39:17 +02:00
|
|
|
apiKey: "123",
|
2019-04-28 14:33:50 +02: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 02:39:17 +02:00
|
|
|
config.APIKey = test.apiKey
|
|
|
|
config.Username = test.username
|
|
|
|
config.Password = test.password
|
|
|
|
|
2019-04-28 14:33:50 +02: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)
|
|
|
|
}
|