2018-10-13 16:01:46 +02:00
|
|
|
package dreamhost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2018-10-13 16:01:46 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
const envDomain = envNamespace + "DOMAIN"
|
|
|
|
|
|
|
|
var envTest = tester.NewEnvTest(EnvAPIKey).
|
|
|
|
WithDomain(envDomain)
|
2018-10-13 16:01:46 +02:00
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
const (
|
2018-10-13 16:01:46 +02:00
|
|
|
fakeAPIKey = "asdf1234"
|
|
|
|
fakeChallengeToken = "foobar"
|
|
|
|
fakeKeyAuth = "w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI"
|
|
|
|
)
|
|
|
|
|
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)
|
2018-10-13 16:01:46 +02:00
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = fakeAPIKey
|
|
|
|
config.BaseURL = server.URL
|
2021-11-02 01:52:38 +02:00
|
|
|
config.HTTPClient = server.Client()
|
2018-10-13 16:01:46 +02:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2021-11-02 01:52:38 +02:00
|
|
|
require.NoError(t, err)
|
2018-10-13 16:01:46 +02:00
|
|
|
|
2021-11-02 01:52:38 +02:00
|
|
|
return provider, mux
|
2018-10-13 16:01:46 +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
|
|
|
EnvAPIKey: "123",
|
2018-10-13 16:01:46 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing API key",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAPIKey: "",
|
2018-10-13 16:01:46 +02:00
|
|
|
},
|
|
|
|
expected: "dreamhost: some credentials information are missing: DREAMHOST_API_KEY",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.envVars)
|
2018-10-13 16:01:46 +02:00
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
2021-03-04 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2018-10-13 16:01:46 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, p)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
apiKey string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
apiKey: "123",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "dreamhost: credentials missing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = test.apiKey
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
2021-03-04 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2018-10-13 16:01:46 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, p)
|
|
|
|
} 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)
|
2018-10-13 16:01:46 +02:00
|
|
|
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodGet, r.Method, "method")
|
|
|
|
|
|
|
|
q := r.URL.Query()
|
2023-10-31 15:08:50 +02:00
|
|
|
assert.Equal(t, fakeAPIKey, q.Get("key"))
|
|
|
|
assert.Equal(t, "dns-add_record", q.Get("cmd"))
|
|
|
|
assert.Equal(t, "json", q.Get("format"))
|
|
|
|
assert.Equal(t, "_acme-challenge.example.com", q.Get("record"))
|
|
|
|
assert.Equal(t, fakeKeyAuth, q.Get("value"))
|
|
|
|
assert.Equal(t, "Managed+By+lego", q.Get("comment"))
|
2018-10-13 16:01:46 +02:00
|
|
|
|
|
|
|
_, err := fmt.Fprintf(w, `{"data":"record_added","result":"success"}`)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2018-10-13 16:01:46 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
err := provider.Present("example.com", "", fakeChallengeToken)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_PresentFailed(t *testing.T) {
|
2021-11-02 01:52:38 +02:00
|
|
|
provider, mux := setupTest(t)
|
2018-10-13 16:01:46 +02:00
|
|
|
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodGet, r.Method, "method")
|
|
|
|
|
|
|
|
_, err := fmt.Fprintf(w, `{"data":"record_already_exists_remove_first","result":"error"}`)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2018-10-13 16:01:46 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
err := provider.Present("example.com", "", fakeChallengeToken)
|
|
|
|
require.EqualError(t, err, "dreamhost: add TXT record failed: record_already_exists_remove_first")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_Cleanup(t *testing.T) {
|
2021-11-02 01:52:38 +02:00
|
|
|
provider, mux := setupTest(t)
|
2018-10-13 16:01:46 +02:00
|
|
|
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodGet, r.Method, "method")
|
|
|
|
|
|
|
|
q := r.URL.Query()
|
2023-10-31 15:08:50 +02:00
|
|
|
assert.Equal(t, fakeAPIKey, q.Get("key"), "key mismatch")
|
|
|
|
assert.Equal(t, "dns-remove_record", q.Get("cmd"), "cmd mismatch")
|
|
|
|
assert.Equal(t, "json", q.Get("format"))
|
|
|
|
assert.Equal(t, "_acme-challenge.example.com", q.Get("record"))
|
|
|
|
assert.Equal(t, fakeKeyAuth, q.Get("value"), "value mismatch")
|
|
|
|
assert.Equal(t, "Managed+By+lego", q.Get("comment"))
|
2018-10-13 16:01:46 +02:00
|
|
|
|
|
|
|
_, err := fmt.Fprintf(w, `{"data":"record_removed","result":"success"}`)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2018-10-13 16:01:46 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
err := provider.CleanUp("example.com", "", fakeChallengeToken)
|
|
|
|
require.NoError(t, err, "failed to remove TXT record")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLivePresentAndCleanUp(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-10-13 16:01:46 +02:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
envTest.RestoreEnv()
|
2018-10-13 16:01:46 +02:00
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2018-10-13 16:01:46 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2018-10-13 16:01:46 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|