2016-10-12 00:42:20 +00:00
|
|
|
package auroradns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-10-12 17:29:18 +00:00
|
|
|
"os"
|
2016-10-12 00:42:20 +00:00
|
|
|
"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-10-12 17:29:18 +00:00
|
|
|
var (
|
|
|
|
envTestUserID string
|
|
|
|
envTestKey string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
envTestUserID = os.Getenv("AURORA_USER_ID")
|
|
|
|
envTestKey = os.Getenv("AURORA_KEY")
|
|
|
|
}
|
|
|
|
|
|
|
|
func restoreEnv() {
|
|
|
|
os.Setenv("AURORA_USER_ID", envTestUserID)
|
|
|
|
os.Setenv("AURORA_KEY", envTestKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupTest() (*DNSProvider, *http.ServeMux, func()) {
|
|
|
|
handler := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(handler)
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.UserID = "asdf1234"
|
|
|
|
config.Key = "key"
|
|
|
|
config.BaseURL = server.URL
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
return provider, handler, server.Close
|
|
|
|
}
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"AURORA_USER_ID": "123",
|
|
|
|
"AURORA_KEY": "456",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"AURORA_USER_ID": "",
|
|
|
|
"AURORA_KEY": "",
|
|
|
|
},
|
|
|
|
expected: "aurora: some credentials information are missing: AURORA_USER_ID,AURORA_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing user id",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"AURORA_USER_ID": "",
|
|
|
|
"AURORA_KEY": "456",
|
|
|
|
},
|
|
|
|
expected: "aurora: some credentials information are missing: AURORA_USER_ID",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing key",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"AURORA_USER_ID": "123",
|
|
|
|
"AURORA_KEY": "",
|
|
|
|
},
|
|
|
|
expected: "aurora: some credentials information are missing: AURORA_KEY",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
defer restoreEnv()
|
|
|
|
for key, value := range test.envVars {
|
|
|
|
if len(value) == 0 {
|
|
|
|
os.Unsetenv(key)
|
|
|
|
} else {
|
|
|
|
os.Setenv(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
|
|
|
if len(test.expected) == 0 {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.client)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
userID string
|
|
|
|
key string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
userID: "123",
|
|
|
|
key: "456",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
userID: "",
|
|
|
|
key: "",
|
|
|
|
expected: "aurora: some credentials information are missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing user id",
|
|
|
|
userID: "",
|
|
|
|
key: "456",
|
|
|
|
expected: "aurora: some credentials information are missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing key",
|
|
|
|
userID: "123",
|
|
|
|
key: "",
|
|
|
|
expected: "aurora: some credentials information are missing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
defer restoreEnv()
|
|
|
|
os.Unsetenv("AURORA_USER_ID")
|
|
|
|
os.Unsetenv("AURORA_KEY")
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.UserID = test.userID
|
|
|
|
config.Key = test.key
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
|
|
|
if len(test.expected) == 0 {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.client)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
|
|
|
provider, mux, tearDown := setupTest()
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
mux.HandleFunc("/zones", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodGet, r.Method, "method")
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `[{
|
2016-10-12 00:42:20 +00:00
|
|
|
"id": "c56a4180-65aa-42ec-a945-5fd21dec0538",
|
|
|
|
"name": "example.com"
|
|
|
|
}]`)
|
2018-10-12 17:29:18 +00:00
|
|
|
})
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
mux.HandleFunc("/zones/c56a4180-65aa-42ec-a945-5fd21dec0538/records", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodPost, r.Method)
|
2018-06-11 15:32:50 +00:00
|
|
|
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-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2018-06-11 15:32:50 +00:00
|
|
|
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
|
|
|
|
}`)
|
2018-10-12 17:29:18 +00:00
|
|
|
})
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +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-10-12 17:29:18 +00:00
|
|
|
func TestDNSProvider_CleanUp(t *testing.T) {
|
|
|
|
provider, mux, tearDown := setupTest()
|
|
|
|
defer tearDown()
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
mux.HandleFunc("/zones", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodGet, r.Method)
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `[{
|
2016-10-12 00:42:20 +00:00
|
|
|
"id": "c56a4180-65aa-42ec-a945-5fd21dec0538",
|
|
|
|
"name": "example.com"
|
|
|
|
}]`)
|
2018-10-12 17:29:18 +00:00
|
|
|
})
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
mux.HandleFunc("/zones/c56a4180-65aa-42ec-a945-5fd21dec0538/records", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodPost, r.Method)
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `{
|
2016-10-12 00:42:20 +00:00
|
|
|
"id": "ec56a4180-65aa-42ec-a945-5fd21dec0538",
|
|
|
|
"type": "TXT",
|
|
|
|
"name": "_acme-challenge",
|
|
|
|
"ttl": 300
|
|
|
|
}`)
|
2018-10-12 17:29:18 +00:00
|
|
|
})
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
mux.HandleFunc("/zones/c56a4180-65aa-42ec-a945-5fd21dec0538/records/ec56a4180-65aa-42ec-a945-5fd21dec0538", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodDelete, r.Method)
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
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, `{}`)
|
2018-10-12 17:29:18 +00:00
|
|
|
})
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +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
|
|
|
}
|