2019-03-11 16:56:48 +00:00
|
|
|
package inwx
|
2018-11-06 10:49:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2024-01-18 20:51:57 +00:00
|
|
|
"time"
|
2018-11-06 10:49:50 +00:00
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2024-01-18 20:51:57 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-11-06 10:49:50 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
const envDomain = envNamespace + "DOMAIN"
|
|
|
|
|
2018-11-06 10:49:50 +00:00
|
|
|
var envTest = tester.NewEnvTest(
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvUsername,
|
|
|
|
EnvPassword,
|
2020-05-31 13:18:43 +00:00
|
|
|
EnvSharedSecret,
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvSandbox,
|
|
|
|
EnvTTL).
|
|
|
|
WithDomain(envDomain).
|
|
|
|
WithLiveTestRequirements(EnvUsername, EnvPassword, envDomain)
|
2018-11-06 10:49:50 +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
|
|
|
EnvUsername: "123",
|
|
|
|
EnvPassword: "456",
|
2018-11-06 10:49:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvUsername: "",
|
|
|
|
EnvPassword: "",
|
2018-11-06 10:49:50 +00:00
|
|
|
},
|
|
|
|
expected: "inwx: some credentials information are missing: INWX_USERNAME,INWX_PASSWORD",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing username",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvUsername: "",
|
|
|
|
EnvPassword: "456",
|
2018-11-06 10:49:50 +00:00
|
|
|
},
|
|
|
|
expected: "inwx: some credentials information are missing: INWX_USERNAME",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing password",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvUsername: "123",
|
|
|
|
EnvPassword: "",
|
2018-11-06 10:49:50 +00:00
|
|
|
},
|
|
|
|
expected: "inwx: some credentials information are missing: INWX_PASSWORD",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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 == "" {
|
2018-11-06 10:49:50 +00:00
|
|
|
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 TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
username: "123",
|
|
|
|
password: "456",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "inwx: credentials missing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Username = test.username
|
|
|
|
config.Password = test.password
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2018-11-06 10:49:50 +00:00
|
|
|
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 TestLivePresentAndCleanup(t *testing.T) {
|
|
|
|
if !envTest.IsLiveTest() {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
|
|
|
envTest.RestoreEnv()
|
|
|
|
envTest.Apply(map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvSandbox: "true",
|
|
|
|
EnvTTL: "3600", // In sandbox mode, the minimum allowed TTL is 3600
|
2018-11-06 10:49:50 +00:00
|
|
|
})
|
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
|
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Verify that no error is thrown if record already exists
|
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2024-01-18 20:51:57 +00:00
|
|
|
|
|
|
|
func Test_computeSleep(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2024-01-19 12:55:57 +00:00
|
|
|
now string
|
2024-01-18 20:51:57 +00:00
|
|
|
expected time.Duration
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "after 30s",
|
2024-01-19 12:55:57 +00:00
|
|
|
now: "2024-01-01T06:30:30Z",
|
2024-01-18 20:51:57 +00:00
|
|
|
expected: 0 * time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "0s",
|
2024-01-19 12:55:57 +00:00
|
|
|
now: "2024-01-01T06:30:00Z",
|
2024-01-18 20:51:57 +00:00
|
|
|
expected: 0 * time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "before 30s",
|
2024-01-19 12:55:57 +00:00
|
|
|
now: "2024-01-01T06:29:40Z", // 10 s
|
2024-01-18 20:51:57 +00:00
|
|
|
expected: 20 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-01-19 12:55:57 +00:00
|
|
|
previous, err := time.Parse(time.RFC3339, "2024-01-01T06:29:30Z")
|
2024-01-18 20:51:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-01-19 12:55:57 +00:00
|
|
|
now, err := time.Parse(time.RFC3339, test.now)
|
2024-01-18 20:51:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
d := &DNSProvider{previousUnlock: previous}
|
|
|
|
|
|
|
|
sleep := d.computeSleep(now)
|
|
|
|
assert.Equal(t, test.expected, sleep)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|