lego/providers/dns/gandiv5/gandiv5_test.go

188 lines
4.6 KiB
Go
Raw Normal View History

package gandiv5
import (
2018-10-08 14:51:29 +00:00
"fmt"
2021-08-25 09:44:11 +00:00
"io"
"net/http"
"net/http/httptest"
"regexp"
"testing"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/log"
"github.com/go-acme/lego/v4/platform/tester"
"github.com/stretchr/testify/require"
)
2018-10-08 14:51:29 +00:00
var envTest = tester.NewEnvTest(EnvAPIKey, EnvPersonalAccessToken)
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
desc string
envVars map[string]string
expected string
}{
{
desc: "success",
envVars: map[string]string{
EnvAPIKey: "123",
},
},
{
desc: "missing api key",
envVars: map[string]string{
EnvAPIKey: "",
},
expected: "gandiv5: credentials information are missing",
},
}
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 == "" {
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
require.NotNil(t, p.inProgressFQDNs)
} 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: "gandiv5: credentials information are 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 19:16:59 +00:00
if test.expected == "" {
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
require.NotNil(t, p.inProgressFQDNs)
} else {
require.EqualError(t, err, test.expected)
}
})
}
}
// TestDNSProvider runs Present and CleanUp against a fake Gandi RPC
// Server, whose responses are predetermined for particular requests.
func TestDNSProvider(t *testing.T) {
// serverResponses is the JSON Request->Response map used by the
// fake JSON server.
2020-07-09 23:48:18 +00:00
serverResponses := map[string]map[string]string{
http.MethodGet: {
``: `{"rrset_ttl":300,"rrset_values":[],"rrset_name":"_acme-challenge.abc.def","rrset_type":"TXT"}`,
},
http.MethodPut: {
`{"rrset_ttl":300,"rrset_values":["TOKEN"]}`: `{"message": "Zone Record Created"}`,
},
http.MethodDelete: {
``: ``,
},
}
fakeKeyAuth := "XXXX"
regexpToken := regexp.MustCompile(`"rrset_values":\[".+"\]`)
// start fake RPC server
mux := http.NewServeMux()
2023-05-05 07:49:38 +00:00
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
mux.HandleFunc("/domains/example.com/records/_acme-challenge.abc.def/TXT", func(rw http.ResponseWriter, req *http.Request) {
2018-10-08 14:51:29 +00:00
log.Infof("request: %s %s", req.Method, req.URL)
if req.Header.Get("Authorization") == "" {
http.Error(rw, `{"message": "missing Authorization"}`, http.StatusUnauthorized)
2018-10-08 14:51:29 +00:00
return
}
if req.Method == http.MethodPost && req.Header.Get("Content-Type") != "application/json" {
http.Error(rw, `{"message": "invalid content type"}`, http.StatusBadRequest)
return
}
2021-08-25 09:44:11 +00:00
body, errS := io.ReadAll(req.Body)
2018-10-08 14:51:29 +00:00
if errS != nil {
http.Error(rw, fmt.Sprintf(`{"message": "read body error: %v"}`, errS), http.StatusInternalServerError)
return
}
body = regexpToken.ReplaceAllLiteral(body, []byte(`"rrset_values":["TOKEN"]`))
responses, ok := serverResponses[req.Method]
if !ok {
http.Error(rw, fmt.Sprintf(`{"message": "Server response for request not found: %#q"}`, string(body)), http.StatusInternalServerError)
return
}
resp := responses[string(body)]
_, errS = rw.Write([]byte(resp))
if errS != nil {
http.Error(rw, fmt.Sprintf(`{"message": "failed to write response: %v"}`, errS), http.StatusInternalServerError)
return
}
})
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
2018-10-08 14:51:29 +00:00
log.Infof("request: %s %s", req.Method, req.URL)
http.Error(rw, fmt.Sprintf(`{"message": "URL doesn't match: %s"}`, req.URL), http.StatusNotFound)
})
// define function to override findZoneByFqdn with
fakeFindZoneByFqdn := func(fqdn string) (string, error) {
return "example.com.", nil
}
config := NewDefaultConfig()
config.PersonalAccessToken = "123412341234123412341234"
2018-10-08 14:51:29 +00:00
config.BaseURL = server.URL
provider, err := NewDNSProviderConfig(config)
require.NoError(t, err)
// override findZoneByFqdn function
savedFindZoneByFqdn := provider.findZoneByFqdn
defer func() {
provider.findZoneByFqdn = savedFindZoneByFqdn
}()
provider.findZoneByFqdn = fakeFindZoneByFqdn
// run Present
err = provider.Present("abc.def.example.com", "", fakeKeyAuth)
require.NoError(t, err)
// run CleanUp
err = provider.CleanUp("abc.def.example.com", "", fakeKeyAuth)
require.NoError(t, err)
}