1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-23 17:21:50 +02:00
lego/providers/dns/zoneee/zoneee_test.go

421 lines
10 KiB
Go
Raw Normal View History

package zoneee
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
2023-05-05 09:49:38 +02:00
"path"
"testing"
"time"
2020-09-02 03:20:01 +02:00
"github.com/go-acme/lego/v4/platform/tester"
2023-05-05 09:49:38 +02:00
"github.com/go-acme/lego/v4/providers/dns/zoneee/internal"
"github.com/stretchr/testify/require"
)
const envDomain = envNamespace + "DOMAIN"
var envTest = tester.NewEnvTest(EnvEndpoint, EnvAPIUser, EnvAPIKey).
WithLiveTestRequirements(EnvAPIUser, EnvAPIKey).
WithDomain(envDomain)
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
desc string
envVars map[string]string
expected string
}{
{
desc: "success",
envVars: map[string]string{
EnvAPIUser: "123",
EnvAPIKey: "456",
},
},
{
desc: "missing credentials",
envVars: map[string]string{
EnvAPIUser: "",
EnvAPIKey: "",
},
expected: "zoneee: some credentials information are missing: ZONEEE_API_USER,ZONEEE_API_KEY",
},
{
desc: "missing username",
envVars: map[string]string{
EnvAPIUser: "",
EnvAPIKey: "456",
},
expected: "zoneee: some credentials information are missing: ZONEEE_API_USER",
},
{
desc: "missing API key",
envVars: map[string]string{
EnvAPIUser: "123",
EnvAPIKey: "",
},
expected: "zoneee: some credentials information are missing: ZONEEE_API_KEY",
},
{
desc: "invalid URL",
envVars: map[string]string{
EnvAPIUser: "123",
EnvAPIKey: "456",
EnvEndpoint: ":",
},
2020-02-26 01:02:29 +02:00
expected: `zoneee: parse ":": missing protocol scheme`,
},
}
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 == "" {
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
apiUser string
apiKey string
endpoint string
expected string
}{
{
desc: "success",
apiKey: "123",
apiUser: "456",
},
{
desc: "missing credentials",
expected: "zoneee: credentials missing: username",
},
{
desc: "missing api key",
apiUser: "456",
expected: "zoneee: credentials missing: API key",
},
{
desc: "missing username",
apiKey: "123",
expected: "zoneee: credentials missing: username",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
config := NewDefaultConfig()
config.APIKey = test.apiKey
config.Username = test.apiUser
2023-05-05 09:49:38 +02:00
if test.endpoint != "" {
config.Endpoint = mustParse(test.endpoint)
}
p, err := NewDNSProviderConfig(config)
2021-03-04 21:16:59 +02:00
if test.expected == "" {
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) {
2020-02-08 12:38:57 +02:00
hostedZone := "example.com"
domain := "prefix." + hostedZone
testCases := []struct {
desc string
username string
apiKey string
handlers map[string]http.HandlerFunc
expectedError string
}{
{
desc: "success",
username: "bar",
apiKey: "foo",
handlers: map[string]http.HandlerFunc{
2023-05-05 09:49:38 +02:00
path.Join("/", "dns", hostedZone, "txt"): mockHandlerCreateRecord,
},
},
{
desc: "invalid auth",
username: "nope",
apiKey: "foo",
handlers: map[string]http.HandlerFunc{
2023-05-05 09:49:38 +02:00
path.Join("/", "dns", hostedZone, "txt"): mockHandlerCreateRecord,
},
2023-05-05 09:49:38 +02:00
expectedError: "zoneee: unexpected status code: [status code: 401] body: Unauthorized",
},
{
desc: "error",
username: "bar",
apiKey: "foo",
2023-05-05 09:49:38 +02:00
expectedError: "zoneee: unexpected status code: [status code: 404] body: 404 page not found",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
2023-05-05 09:49:38 +02:00
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
for uri, handler := range test.handlers {
mux.HandleFunc(uri, handler)
}
config := NewDefaultConfig()
config.Endpoint = mustParse(server.URL)
config.Username = test.username
config.APIKey = test.apiKey
p, err := NewDNSProviderConfig(config)
require.NoError(t, err)
err = p.Present(domain, "token", "key")
if test.expectedError == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, test.expectedError)
}
})
}
}
func TestDNSProvider_Cleanup(t *testing.T) {
2020-02-08 12:38:57 +02:00
hostedZone := "example.com"
domain := "prefix." + hostedZone
testCases := []struct {
desc string
username string
apiKey string
handlers map[string]http.HandlerFunc
expectedError string
}{
{
desc: "success",
username: "bar",
apiKey: "foo",
handlers: map[string]http.HandlerFunc{
2023-05-05 09:49:38 +02:00
path.Join("/", "dns", hostedZone, "txt"): mockHandlerGetRecords([]internal.TXTRecord{{
ID: "1234",
Name: domain,
Destination: "LHDhK3oGRvkiefQnx7OOczTY5Tic_xZ6HcMOc_gmtoM",
Delete: true,
Modify: true,
}}),
2023-05-05 09:49:38 +02:00
path.Join("/", "dns", hostedZone, "txt", "1234"): mockHandlerDeleteRecord,
},
},
{
desc: "no txt records",
username: "bar",
apiKey: "foo",
handlers: map[string]http.HandlerFunc{
2023-05-05 09:49:38 +02:00
path.Join("/", "dns", hostedZone, "txt"): mockHandlerGetRecords([]internal.TXTRecord{}),
path.Join("/", "dns", hostedZone, "txt", "1234"): mockHandlerDeleteRecord,
},
expectedError: "zoneee: txt record does not exist for LHDhK3oGRvkiefQnx7OOczTY5Tic_xZ6HcMOc_gmtoM",
},
{
desc: "invalid auth",
username: "nope",
apiKey: "foo",
handlers: map[string]http.HandlerFunc{
2023-05-05 09:49:38 +02:00
path.Join("/", "dns", hostedZone, "txt"): mockHandlerGetRecords([]internal.TXTRecord{{
ID: "1234",
Name: domain,
Destination: "LHDhK3oGRvkiefQnx7OOczTY5Tic_xZ6HcMOc_gmtoM",
Delete: true,
Modify: true,
}}),
2023-05-05 09:49:38 +02:00
path.Join("/", "dns", hostedZone, "txt", "1234"): mockHandlerDeleteRecord,
},
2023-05-05 09:49:38 +02:00
expectedError: "zoneee: unexpected status code: [status code: 401] body: Unauthorized",
},
{
desc: "error",
username: "bar",
apiKey: "foo",
2023-05-05 09:49:38 +02:00
expectedError: "zoneee: unexpected status code: [status code: 404] body: 404 page not found",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
2023-05-05 09:49:38 +02:00
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
for uri, handler := range test.handlers {
mux.HandleFunc(uri, handler)
}
config := NewDefaultConfig()
config.Endpoint = mustParse(server.URL)
config.Username = test.username
config.APIKey = test.apiKey
p, err := NewDNSProviderConfig(config)
require.NoError(t, err)
err = p.CleanUp(domain, "token", "key")
if test.expectedError == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, test.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)
}
func mustParse(rawURL string) *url.URL {
uri, err := url.Parse(rawURL)
if err != nil {
panic(err)
}
return uri
}
func mockHandlerCreateRecord(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(rw, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
2021-11-03 21:39:12 +02:00
return
}
username, apiKey, ok := req.BasicAuth()
if username != "bar" || apiKey != "foo" || !ok {
2021-11-03 21:39:12 +02:00
rw.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm=%q`, "Please enter your username and API key."))
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
2023-05-05 09:49:38 +02:00
record := internal.TXTRecord{}
err := json.NewDecoder(req.Body).Decode(&record)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
record.ID = "1234"
record.Delete = true
record.Modify = true
record.ResourceURL = req.URL.String() + "/1234"
2023-05-05 09:49:38 +02:00
bytes, err := json.Marshal([]internal.TXTRecord{record})
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
if _, err = rw.Write(bytes); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
}
2023-05-05 09:49:38 +02:00
func mockHandlerGetRecords(records []internal.TXTRecord) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(rw, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
2021-11-03 21:39:12 +02:00
return
}
username, apiKey, ok := req.BasicAuth()
if username != "bar" || apiKey != "foo" || !ok {
2021-11-03 21:39:12 +02:00
rw.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm=%q`, "Please enter your username and API key."))
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
for _, value := range records {
2021-03-04 21:16:59 +02:00
if value.ResourceURL == "" {
value.ResourceURL = req.URL.String() + "/" + value.ID
}
}
bytes, err := json.Marshal(records)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
if _, err = rw.Write(bytes); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
}
}
func mockHandlerDeleteRecord(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodDelete {
http.Error(rw, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
username, apiKey, ok := req.BasicAuth()
if username != "bar" || apiKey != "foo" || !ok {
2021-11-03 21:39:12 +02:00
rw.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm=%q`, "Please enter your username and API key."))
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
rw.WriteHeader(http.StatusNoContent)
}