2019-08-19 19:41:10 +00:00
|
|
|
package liquidweb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2023-10-14 15:08:29 +00:00
|
|
|
"github.com/liquidweb/liquidweb-go/network"
|
2019-08-19 19:41:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
const envDomain = envNamespace + "DOMAIN"
|
|
|
|
|
2019-08-19 19:41:10 +00:00
|
|
|
var envTest = tester.NewEnvTest(
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvURL,
|
|
|
|
EnvUsername,
|
|
|
|
EnvPassword,
|
|
|
|
EnvZone).
|
|
|
|
WithDomain(envDomain)
|
2019-08-19 19:41:10 +00:00
|
|
|
|
2023-10-14 15:08:29 +00:00
|
|
|
func setupTest(t *testing.T, initRecs ...network.DNSRecord) *DNSProvider {
|
2021-11-01 23:52:38 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2023-10-14 15:08:29 +00:00
|
|
|
serverURL := mockAPIServer(t, initRecs)
|
2019-08-19 19:41:10 +00:00
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Username = "blars"
|
|
|
|
config.Password = "tacoman"
|
2023-10-14 15:08:29 +00:00
|
|
|
config.BaseURL = serverURL
|
2019-08-19 19:41:10 +00:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2021-11-01 23:52:38 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-19 19:41:10 +00:00
|
|
|
|
2023-10-14 15:08:29 +00:00
|
|
|
return provider
|
2019-08-19 19:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2023-10-14 15:08:29 +00:00
|
|
|
desc: "minimum-success",
|
|
|
|
envVars: map[string]string{
|
|
|
|
EnvUsername: "blars",
|
|
|
|
EnvPassword: "tacoman",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "set-everything",
|
2019-08-19 19:41:10 +00:00
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvURL: "https://storm.com",
|
|
|
|
EnvUsername: "blars",
|
|
|
|
EnvPassword: "tacoman",
|
|
|
|
EnvZone: "blars.com",
|
2019-08-19 19:41:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{},
|
2023-10-14 15:08:29 +00:00
|
|
|
expected: "liquidweb: some credentials information are missing: LIQUID_WEB_USERNAME,LIQUID_WEB_PASSWORD",
|
2019-08-19 19:41:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing username",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvPassword: "tacoman",
|
|
|
|
EnvZone: "blars.com",
|
2019-08-19 19:41:10 +00:00
|
|
|
},
|
|
|
|
expected: "liquidweb: some credentials information are missing: LIQUID_WEB_USERNAME",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing password",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvUsername: "blars",
|
|
|
|
EnvZone: "blars.com",
|
2023-10-14 15:08:29 +00:00
|
|
|
},
|
|
|
|
expected: "liquidweb: some credentials information are missing: LIQUID_WEB_PASSWORD",
|
2019-08-19 19:41:10 +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()
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2019-08-19 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.client)
|
|
|
|
require.NotNil(t, p.recordIDs)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
zone string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
username: "acme",
|
|
|
|
password: "secret",
|
|
|
|
zone: "example.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
username: "",
|
|
|
|
password: "",
|
|
|
|
zone: "",
|
2023-10-14 15:08:29 +00:00
|
|
|
expected: "liquidweb: could not create Liquid Web API client: provided username is empty",
|
2019-08-19 19:41:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing username",
|
|
|
|
username: "",
|
|
|
|
password: "secret",
|
|
|
|
zone: "example.com",
|
2023-10-14 15:08:29 +00:00
|
|
|
expected: "liquidweb: could not create Liquid Web API client: provided username is empty",
|
2019-08-19 19:41:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing password",
|
|
|
|
username: "acme",
|
|
|
|
password: "",
|
|
|
|
zone: "example.com",
|
2023-10-14 15:08:29 +00:00
|
|
|
expected: "liquidweb: could not create Liquid Web API client: provided password is empty",
|
2019-08-19 19:41:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Username = test.username
|
|
|
|
config.Password = test.password
|
|
|
|
config.Zone = test.zone
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2019-08-19 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.client)
|
|
|
|
require.NotNil(t, p.recordIDs)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
2023-10-14 15:08:29 +00:00
|
|
|
provider := setupTest(t)
|
2019-08-19 19:41:10 +00:00
|
|
|
|
|
|
|
err := provider.Present("tacoman.com", "", "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_CleanUp(t *testing.T) {
|
2023-10-14 15:08:29 +00:00
|
|
|
provider := setupTest(t, network.DNSRecord{
|
|
|
|
Name: "_acme-challenge.tacoman.com",
|
|
|
|
RData: "123d==",
|
|
|
|
Type: "TXT",
|
|
|
|
TTL: 300,
|
|
|
|
ID: 1234567,
|
|
|
|
ZoneID: 42,
|
|
|
|
})
|
2019-08-19 19:41:10 +00:00
|
|
|
|
2023-10-14 15:08:29 +00:00
|
|
|
provider.recordIDs["123d=="] = 1234567
|
2019-08-19 19:41:10 +00:00
|
|
|
|
2023-10-14 15:08:29 +00:00
|
|
|
err := provider.CleanUp("tacoman.com.", "123d==", "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2019-08-19 19:41:10 +00:00
|
|
|
|
2023-10-14 15:08:29 +00:00
|
|
|
func TestDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
initRecs []network.DNSRecord
|
|
|
|
domain string
|
|
|
|
token string
|
|
|
|
keyAuth string
|
|
|
|
present bool
|
|
|
|
expPresentErr string
|
|
|
|
cleanup bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "expected successful",
|
|
|
|
domain: "tacoman.com",
|
|
|
|
token: "123",
|
|
|
|
keyAuth: "456",
|
|
|
|
present: true,
|
|
|
|
cleanup: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "other successful",
|
|
|
|
domain: "banana.com",
|
|
|
|
token: "123",
|
|
|
|
keyAuth: "456",
|
|
|
|
present: true,
|
|
|
|
cleanup: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "zone not on account",
|
|
|
|
domain: "huckleberry.com",
|
|
|
|
token: "123",
|
|
|
|
keyAuth: "456",
|
|
|
|
present: true,
|
|
|
|
expPresentErr: "no valid zone in account for certificate '_acme-challenge.huckleberry.com'",
|
|
|
|
cleanup: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ssl for domain",
|
|
|
|
domain: "sundae.cherry.com",
|
|
|
|
token: "5847953",
|
|
|
|
keyAuth: "34872934",
|
|
|
|
present: true,
|
|
|
|
cleanup: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "complicated domain",
|
|
|
|
domain: "always.money.stand.banana.com",
|
|
|
|
token: "5847953",
|
|
|
|
keyAuth: "there is always money in the banana stand",
|
|
|
|
present: true,
|
|
|
|
cleanup: true,
|
|
|
|
},
|
|
|
|
}
|
2019-08-19 19:41:10 +00:00
|
|
|
|
2023-10-14 15:08:29 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
provider := setupTest(t, test.initRecs...)
|
|
|
|
|
|
|
|
if test.present {
|
|
|
|
err := provider.Present(test.domain, test.token, test.keyAuth)
|
|
|
|
if test.expPresentErr == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.ErrorContains(t, err, test.expPresentErr)
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 19:41:10 +00:00
|
|
|
|
2023-10-14 15:08:29 +00:00
|
|
|
if test.cleanup {
|
|
|
|
err := provider.CleanUp(test.domain, test.token, test.keyAuth)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-08-19 19:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|