2019-08-19 21:41:10 +02:00
|
|
|
package liquidweb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-08-25 11:44:11 +02:00
|
|
|
"io"
|
2019-08-19 21:41:10 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2019-08-19 21:41:10 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
const envDomain = envNamespace + "DOMAIN"
|
|
|
|
|
2019-08-19 21:41:10 +02:00
|
|
|
var envTest = tester.NewEnvTest(
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvURL,
|
|
|
|
EnvUsername,
|
|
|
|
EnvPassword,
|
|
|
|
EnvZone).
|
|
|
|
WithDomain(envDomain)
|
2019-08-19 21:41:10 +02:00
|
|
|
|
2021-11-02 01:52:38 +02:00
|
|
|
func setupTest(t *testing.T) (*DNSProvider, *http.ServeMux) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
2019-08-19 21:41:10 +02:00
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Username = "blars"
|
|
|
|
config.Password = "tacoman"
|
|
|
|
config.BaseURL = server.URL
|
|
|
|
config.Zone = "tacoman.com"
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2021-11-02 01:52:38 +02:00
|
|
|
require.NoError(t, err)
|
2019-08-19 21:41:10 +02:00
|
|
|
|
2021-11-02 01:52:38 +02:00
|
|
|
return provider, mux
|
2019-08-19 21:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvURL: "https://storm.com",
|
|
|
|
EnvUsername: "blars",
|
|
|
|
EnvPassword: "tacoman",
|
|
|
|
EnvZone: "blars.com",
|
2019-08-19 21:41:10 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{},
|
|
|
|
expected: "liquidweb: some credentials information are missing: LIQUID_WEB_USERNAME,LIQUID_WEB_PASSWORD,LIQUID_WEB_ZONE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing username",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvPassword: "tacoman",
|
|
|
|
EnvZone: "blars.com",
|
2019-08-19 21:41:10 +02:00
|
|
|
},
|
|
|
|
expected: "liquidweb: some credentials information are missing: LIQUID_WEB_USERNAME",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing password",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvUsername: "blars",
|
|
|
|
EnvZone: "blars.com",
|
2019-08-19 21:41:10 +02:00
|
|
|
}, expected: "liquidweb: some credentials information are missing: LIQUID_WEB_PASSWORD",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing zone",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvUsername: "blars",
|
|
|
|
EnvPassword: "tacoman",
|
2019-08-19 21:41:10 +02:00
|
|
|
}, expected: "liquidweb: some credentials information are missing: LIQUID_WEB_ZONE",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2019-08-19 21:41:10 +02: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: "",
|
|
|
|
expected: "liquidweb: zone is missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing username",
|
|
|
|
username: "",
|
|
|
|
password: "secret",
|
|
|
|
zone: "example.com",
|
|
|
|
expected: "liquidweb: username is missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing password",
|
|
|
|
username: "acme",
|
|
|
|
password: "",
|
|
|
|
zone: "example.com",
|
|
|
|
expected: "liquidweb: password is missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing zone",
|
|
|
|
username: "acme",
|
|
|
|
password: "secret",
|
|
|
|
zone: "",
|
|
|
|
expected: "liquidweb: zone is missing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2019-08-19 21:41:10 +02: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) {
|
2021-11-02 01:52:38 +02:00
|
|
|
provider, mux := setupTest(t)
|
2019-08-19 21:41:10 +02:00
|
|
|
|
|
|
|
mux.HandleFunc("/v1/Network/DNS/Record/create", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodPost, r.Method)
|
|
|
|
|
|
|
|
username, password, ok := r.BasicAuth()
|
|
|
|
assert.Equal(t, "blars", username)
|
|
|
|
assert.Equal(t, "tacoman", password)
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
2021-08-25 11:44:11 +02:00
|
|
|
reqBody, err := io.ReadAll(r.Body)
|
2019-08-19 21:41:10 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedReqBody := `
|
|
|
|
{
|
|
|
|
"params": {
|
|
|
|
"name": "_acme-challenge.tacoman.com",
|
|
|
|
"rdata": "\"47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU\"",
|
|
|
|
"ttl": 300,
|
|
|
|
"type": "TXT",
|
|
|
|
"zone": "tacoman.com"
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
assert.JSONEq(t, expectedReqBody, string(reqBody))
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_, err = fmt.Fprintf(w, `{
|
|
|
|
"type": "TXT",
|
|
|
|
"name": "_acme-challenge.tacoman.com",
|
|
|
|
"rdata": "\"47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU\"",
|
|
|
|
"ttl": 300,
|
|
|
|
"id": 1234567,
|
|
|
|
"prio": null
|
|
|
|
}`)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2019-08-19 21:41:10 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
err := provider.Present("tacoman.com", "", "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_CleanUp(t *testing.T) {
|
2021-11-02 01:52:38 +02:00
|
|
|
provider, mux := setupTest(t)
|
2019-08-19 21:41:10 +02:00
|
|
|
|
|
|
|
mux.HandleFunc("/v1/Network/DNS/Record/delete", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodPost, r.Method)
|
|
|
|
|
|
|
|
username, password, ok := r.BasicAuth()
|
|
|
|
assert.Equal(t, "blars", username)
|
|
|
|
assert.Equal(t, "tacoman", password)
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
|
|
_, err := fmt.Fprintf(w, `{"deleted": "123"}`)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
provider.recordIDs["123"] = 1234567
|
|
|
|
|
|
|
|
err := provider.CleanUp("tacoman.com.", "123", "")
|
|
|
|
require.NoError(t, err, "fail to remove TXT record")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|