2018-02-02 19:22:33 +00:00
|
|
|
package gandiv5
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2018-06-11 15:32:50 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-02 19:22:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestDNSProvider runs Present and CleanUp against a fake Gandi RPC
|
|
|
|
// Server, whose responses are predetermined for particular requests.
|
|
|
|
func TestDNSProvider(t *testing.T) {
|
|
|
|
fakeAPIKey := "123412341234123412341234"
|
|
|
|
fakeKeyAuth := "XXXX"
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
provider, err := NewDNSProviderCredentials(fakeAPIKey)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
regexpToken, err := regexp.Compile(`"rrset_values":\[".+"\]`)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// start fake RPC server
|
|
|
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2018-06-11 15:32:50 +00:00
|
|
|
require.Equal(t, "application/json", r.Header.Get("Content-Type"), "invalid content type")
|
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
req, err := ioutil.ReadAll(r.Body)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req = regexpToken.ReplaceAllLiteral(req, []byte(`"rrset_values":["TOKEN"]`))
|
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
resp, ok := serverResponses[string(req)]
|
2018-06-11 15:32:50 +00:00
|
|
|
require.True(t, ok, "Server response for request not found")
|
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
_, err = io.Copy(w, strings.NewReader(resp))
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
2018-02-02 19:22:33 +00:00
|
|
|
}))
|
|
|
|
defer fakeServer.Close()
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// define function to override findZoneByFqdn with
|
|
|
|
fakeFindZoneByFqdn := func(fqdn string, nameserver []string) (string, error) {
|
|
|
|
return "example.com.", nil
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// override gandi endpoint and findZoneByFqdn function
|
|
|
|
savedEndpoint, savedFindZoneByFqdn := endpoint, findZoneByFqdn
|
|
|
|
defer func() {
|
|
|
|
endpoint, findZoneByFqdn = savedEndpoint, savedFindZoneByFqdn
|
|
|
|
}()
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
endpoint, findZoneByFqdn = fakeServer.URL, fakeFindZoneByFqdn
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// run Present
|
|
|
|
err = provider.Present("abc.def.example.com", "", fakeKeyAuth)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// run CleanUp
|
|
|
|
err = provider.CleanUp("abc.def.example.com", "", fakeKeyAuth)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
2018-02-02 19:22:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// serverResponses is the JSON Request->Response map used by the
|
|
|
|
// fake JSON server.
|
|
|
|
var serverResponses = map[string]string{
|
|
|
|
// Present Request->Response (addTXTRecord)
|
|
|
|
`{"rrset_ttl":300,"rrset_values":["TOKEN"]}`: `{"message": "Zone Record Created"}`,
|
|
|
|
// CleanUp Request->Response (deleteTXTRecord)
|
|
|
|
`{"delete":true}`: ``,
|
|
|
|
}
|