lego/providers/dns/easydns/easydns_test.go

415 lines
10 KiB
Go
Raw Normal View History

2019-04-29 00:03:04 +00:00
package easydns
import (
"fmt"
2021-08-25 09:44:11 +00:00
"io"
2019-04-29 00:03:04 +00:00
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/platform/tester"
2019-04-29 00:03:04 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2023-05-05 07:49:38 +00:00
const authorizationHeader = "Authorization"
const envDomain = envNamespace + "DOMAIN"
2019-04-29 00:03:04 +00:00
var envTest = tester.NewEnvTest(
EnvEndpoint,
EnvToken,
EnvKey).
WithDomain(envDomain)
2019-04-29 00:03:04 +00:00
func setupTest(t *testing.T) (*DNSProvider, *http.ServeMux) {
t.Helper()
mux := http.NewServeMux()
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
2019-04-29 00:03:04 +00:00
endpoint, err := url.Parse(server.URL)
require.NoError(t, err)
2019-04-29 00:03:04 +00:00
config := NewDefaultConfig()
config.Token = "TOKEN"
config.Key = "SECRET"
config.Endpoint = endpoint
config.HTTPClient = server.Client()
2019-04-29 00:03:04 +00:00
provider, err := NewDNSProviderConfig(config)
require.NoError(t, err)
2019-04-29 00:03:04 +00:00
return provider, mux
2019-04-29 00:03:04 +00:00
}
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
desc string
envVars map[string]string
expected string
}{
{
desc: "success",
envVars: map[string]string{
EnvToken: "TOKEN",
EnvKey: "SECRET",
2019-04-29 00:03:04 +00:00
},
},
{
desc: "missing token",
envVars: map[string]string{
EnvKey: "SECRET",
2019-04-29 00:03:04 +00:00
},
expected: "easydns: some credentials information are missing: EASYDNS_TOKEN",
},
{
desc: "missing key",
envVars: map[string]string{
EnvToken: "TOKEN",
2019-04-29 00:03:04 +00:00
},
expected: "easydns: some credentials information are missing: EASYDNS_KEY",
},
}
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-04-29 00:03:04 +00:00
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
} else {
require.EqualError(t, err, test.expected)
}
})
}
}
func TestNewDNSProviderConfig(t *testing.T) {
testCases := []struct {
desc string
config *Config
expected string
}{
{
desc: "success",
config: &Config{
Token: "TOKEN",
Key: "KEY",
},
},
{
desc: "nil config",
config: nil,
expected: "easydns: the configuration of the DNS provider is nil",
},
{
desc: "missing token",
config: &Config{
Key: "KEY",
},
expected: "easydns: the API token is missing",
},
{
desc: "missing key",
config: &Config{
Token: "TOKEN",
},
expected: "easydns: the API key is missing",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
p, err := NewDNSProviderConfig(test.config)
2021-03-04 19:16:59 +00:00
if test.expected == "" {
2019-04-29 00:03:04 +00:00
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
} else {
require.EqualError(t, err, test.expected)
}
})
}
}
func TestDNSProvider_Present(t *testing.T) {
provider, mux := setupTest(t)
2019-04-29 00:03:04 +00:00
2024-03-03 14:41:55 +00:00
mux.HandleFunc("/zones/records/all/example.com", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "method")
assert.Equal(t, "format=json", r.URL.RawQuery, "query")
assert.Equal(t, "Basic VE9LRU46U0VDUkVU", r.Header.Get(authorizationHeader), authorizationHeader)
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintf(w, `{
"msg": "string",
"status": 200,
"tm": 0,
"data": [{
"id": "60898922",
"domain": "example.com",
"host": "hosta",
"ttl": "300",
"prio": "0",
"geozone_id": "0",
"type": "A",
"rdata": "1.2.3.4",
"last_mod": "2019-08-28 19:09:50"
}],
"count": 0,
"total": 0,
"start": 0,
"max": 0
}
`)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
2019-04-29 00:03:04 +00:00
mux.HandleFunc("/zones/records/add/example.com/TXT", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method, "method")
assert.Equal(t, "format=json", r.URL.RawQuery, "query")
assert.Equal(t, "application/json", r.Header.Get("Content-Type"), "Content-Type")
2023-05-05 07:49:38 +00:00
assert.Equal(t, "Basic VE9LRU46U0VDUkVU", r.Header.Get(authorizationHeader), authorizationHeader)
2019-04-29 00:03:04 +00:00
2021-08-25 09:44:11 +00:00
reqBody, err := io.ReadAll(r.Body)
2019-04-29 00:03:04 +00:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
2021-11-03 19:39:12 +00:00
return
2019-04-29 00:03:04 +00:00
}
expectedReqBody := `{"domain":"example.com","host":"_acme-challenge","ttl":"120","prio":"0","type":"TXT","rdata":"pW9ZKG0xz_PCriK-nCMOjADy9eJcgGWIzkkj2fN4uZM"}
`
assert.Equal(t, expectedReqBody, string(reqBody))
w.WriteHeader(http.StatusCreated)
_, err = fmt.Fprintf(w, `{
"msg": "OK",
"tm": 1554681934,
"data": {
"host": "_acme-challenge",
"geozone_id": 0,
"ttl": "120",
"prio": "0",
"rdata": "pW9ZKG0xz_PCriK-nCMOjADy9eJcgGWIzkkj2fN4uZM",
"revoked": 0,
"id": "123456789",
"new_host": "_acme-challenge.example.com"
},
"status": 201
}`)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
2021-11-03 19:39:12 +00:00
return
2019-04-29 00:03:04 +00:00
}
})
err := provider.Present("example.com", "token", "keyAuth")
require.NoError(t, err)
require.Contains(t, provider.recordIDs, "_acme-challenge.example.com.|pW9ZKG0xz_PCriK-nCMOjADy9eJcgGWIzkkj2fN4uZM")
}
func TestDNSProvider_Cleanup_WhenRecordIdNotSet_NoOp(t *testing.T) {
2024-03-03 14:41:55 +00:00
provider, mux := setupTest(t)
mux.HandleFunc("/zones/records/all/example.com", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "method")
assert.Equal(t, "format=json", r.URL.RawQuery, "query")
assert.Equal(t, "Basic VE9LRU46U0VDUkVU", r.Header.Get(authorizationHeader), authorizationHeader)
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintf(w, `{
"msg": "string",
"status": 200,
"tm": 0,
"data": [{
"id": "60898922",
"domain": "example.com",
"host": "hosta",
"ttl": "300",
"prio": "0",
"geozone_id": "0",
"type": "A",
"rdata": "1.2.3.4",
"last_mod": "2019-08-28 19:09:50"
}],
"count": 0,
"total": 0,
"start": 0,
"max": 0
}
`)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
2019-04-29 00:03:04 +00:00
err := provider.CleanUp("example.com", "token", "keyAuth")
require.NoError(t, err)
}
func TestDNSProvider_Cleanup_WhenRecordIdSet_DeletesTxtRecord(t *testing.T) {
provider, mux := setupTest(t)
2019-04-29 00:03:04 +00:00
2024-03-03 14:41:55 +00:00
mux.HandleFunc("/zones/records/all/example.com", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "method")
assert.Equal(t, "format=json", r.URL.RawQuery, "query")
assert.Equal(t, "Basic VE9LRU46U0VDUkVU", r.Header.Get(authorizationHeader), authorizationHeader)
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintf(w, `{
"msg": "string",
"status": 200,
"tm": 0,
"data": [{
"id": "60898922",
"domain": "example.com",
"host": "hosta",
"ttl": "300",
"prio": "0",
"geozone_id": "0",
"type": "A",
"rdata": "1.2.3.4",
"last_mod": "2019-08-28 19:09:50"
}],
"count": 0,
"total": 0,
"start": 0,
"max": 0
}
`)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
2019-04-29 00:03:04 +00:00
mux.HandleFunc("/zones/records/example.com/123456", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodDelete, r.Method, "method")
assert.Equal(t, "format=json", r.URL.RawQuery, "query")
2023-05-05 07:49:38 +00:00
assert.Equal(t, "Basic VE9LRU46U0VDUkVU", r.Header.Get(authorizationHeader), authorizationHeader)
2019-04-29 00:03:04 +00:00
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintf(w, `{
"msg": "OK",
"data": {
"domain": "example.com",
"id": "123456"
},
"status": 200
}`)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
2021-11-03 19:39:12 +00:00
return
2019-04-29 00:03:04 +00:00
}
})
provider.recordIDs["_acme-challenge.example.com.|pW9ZKG0xz_PCriK-nCMOjADy9eJcgGWIzkkj2fN4uZM"] = "123456"
err := provider.CleanUp("example.com", "token", "keyAuth")
require.NoError(t, err)
}
func TestDNSProvider_Cleanup_WhenHttpError_ReturnsError(t *testing.T) {
provider, mux := setupTest(t)
2019-04-29 00:03:04 +00:00
2024-03-03 14:41:55 +00:00
mux.HandleFunc("/zones/records/all/example.com", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "method")
assert.Equal(t, "format=json", r.URL.RawQuery, "query")
assert.Equal(t, "Basic VE9LRU46U0VDUkVU", r.Header.Get(authorizationHeader), authorizationHeader)
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintf(w, `{
"msg": "string",
"status": 200,
"tm": 0,
"data": [{
"id": "60898922",
"domain": "example.com",
"host": "hosta",
"ttl": "300",
"prio": "0",
"geozone_id": "0",
"type": "A",
"rdata": "1.2.3.4",
"last_mod": "2019-08-28 19:09:50"
}],
"count": 0,
"total": 0,
"start": 0,
"max": 0
}
`)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
2019-04-29 00:03:04 +00:00
errorMessage := `{
"error": {
"code": 406,
"message": "Provided id is invalid or you do not have permission to access it."
}
}`
mux.HandleFunc("/zones/records/example.com/123456", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodDelete, r.Method, "method")
assert.Equal(t, "format=json", r.URL.RawQuery, "query")
2023-05-05 07:49:38 +00:00
assert.Equal(t, "Basic VE9LRU46U0VDUkVU", r.Header.Get(authorizationHeader), authorizationHeader)
2019-04-29 00:03:04 +00:00
w.WriteHeader(http.StatusNotAcceptable)
2019-10-24 23:27:40 +00:00
_, err := fmt.Fprint(w, errorMessage)
2019-04-29 00:03:04 +00:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
2021-11-03 19:39:12 +00:00
return
2019-04-29 00:03:04 +00:00
}
})
provider.recordIDs["_acme-challenge.example.com.|pW9ZKG0xz_PCriK-nCMOjADy9eJcgGWIzkkj2fN4uZM"] = "123456"
err := provider.CleanUp("example.com", "token", "keyAuth")
2023-05-05 07:49:38 +00:00
expectedError := fmt.Sprintf("easydns: unexpected status code: [status code: 406] body: %v", errorMessage)
2019-04-29 00:03:04 +00:00
require.EqualError(t, err, expectedError)
}
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)
}