2016-10-12 00:42:20 +00:00
|
|
|
package auroradns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2018-06-11 15:32:50 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-10-12 00:42:20 +00:00
|
|
|
)
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
var fakeAuroraDNSUserID = "asdf1234"
|
2016-10-12 00:42:20 +00:00
|
|
|
var fakeAuroraDNSKey = "key"
|
|
|
|
|
|
|
|
func TestAuroraDNSPresent(t *testing.T) {
|
|
|
|
var requestReceived bool
|
|
|
|
|
|
|
|
mock := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2018-06-21 17:06:16 +00:00
|
|
|
if r.Method == http.MethodGet && r.URL.Path == "/zones" {
|
2016-10-12 00:42:20 +00:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `[{
|
|
|
|
"id": "c56a4180-65aa-42ec-a945-5fd21dec0538",
|
|
|
|
"name": "example.com"
|
|
|
|
}]`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requestReceived = true
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
assert.Equal(t, http.MethodPost, r.Method, "method")
|
|
|
|
assert.Equal(t, "/zones/c56a4180-65aa-42ec-a945-5fd21dec0538/records", r.URL.Path, "Path")
|
|
|
|
assert.Equal(t, "application/json", r.Header.Get("Content-Type"), "Content-Type")
|
2016-10-12 00:42:20 +00:00
|
|
|
|
|
|
|
reqBody, err := ioutil.ReadAll(r.Body)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "reading request body")
|
|
|
|
assert.Equal(t, `{"type":"TXT","name":"_acme-challenge","content":"w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI","ttl":300}`, string(reqBody))
|
2016-10-12 00:42:20 +00:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `{
|
|
|
|
"id": "c56a4180-65aa-42ec-a945-5fd21dec0538",
|
|
|
|
"type": "TXT",
|
|
|
|
"name": "_acme-challenge",
|
|
|
|
"ttl": 300
|
|
|
|
}`)
|
|
|
|
}))
|
|
|
|
|
|
|
|
defer mock.Close()
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.UserID = fakeAuroraDNSUserID
|
|
|
|
config.Key = fakeAuroraDNSKey
|
|
|
|
config.BaseURL = mock.URL
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-15 17:07:24 +00:00
|
|
|
require.NotNil(t, provider)
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
err = provider.Present("example.com", "", "foobar")
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "fail to create TXT record")
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
assert.True(t, requestReceived, "Expected request to be received by mock backend, but it wasn't")
|
2016-10-12 00:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuroraDNSCleanUp(t *testing.T) {
|
|
|
|
var requestReceived bool
|
|
|
|
|
|
|
|
mock := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2018-06-21 17:06:16 +00:00
|
|
|
if r.Method == http.MethodGet && r.URL.Path == "/zones" {
|
2016-10-12 00:42:20 +00:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `[{
|
|
|
|
"id": "c56a4180-65aa-42ec-a945-5fd21dec0538",
|
|
|
|
"name": "example.com"
|
|
|
|
}]`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
if r.Method == http.MethodPost && r.URL.Path == "/zones/c56a4180-65aa-42ec-a945-5fd21dec0538/records" {
|
2016-10-12 00:42:20 +00:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `{
|
|
|
|
"id": "ec56a4180-65aa-42ec-a945-5fd21dec0538",
|
|
|
|
"type": "TXT",
|
|
|
|
"name": "_acme-challenge",
|
|
|
|
"ttl": 300
|
|
|
|
}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requestReceived = true
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
assert.Equal(t, http.MethodDelete, r.Method, "method")
|
|
|
|
assert.Equal(t, "/zones/c56a4180-65aa-42ec-a945-5fd21dec0538/records/ec56a4180-65aa-42ec-a945-5fd21dec0538", r.URL.Path, "Path")
|
|
|
|
assert.Equal(t, "application/json", r.Header.Get("Content-Type"), "Content-Type")
|
2016-10-12 00:42:20 +00:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `{}`)
|
|
|
|
}))
|
|
|
|
defer mock.Close()
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.UserID = fakeAuroraDNSUserID
|
|
|
|
config.Key = fakeAuroraDNSKey
|
|
|
|
config.BaseURL = mock.URL
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-15 17:07:24 +00:00
|
|
|
require.NotNil(t, provider)
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
err = provider.Present("example.com", "", "foobar")
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "fail to create TXT record")
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
err = provider.CleanUp("example.com", "", "foobar")
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "fail to remove TXT record")
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
assert.True(t, requestReceived, "Expected request to be received by mock backend, but it wasn't")
|
2016-10-12 00:42:20 +00:00
|
|
|
}
|