2016-02-29 02:48:41 +00:00
|
|
|
package digitalocean
|
2016-01-27 00:57:55 +00:00
|
|
|
|
|
|
|
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-01-27 00:57:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var fakeDigitalOceanAuth = "asdf1234"
|
|
|
|
|
|
|
|
func TestDigitalOceanPresent(t *testing.T) {
|
|
|
|
var requestReceived bool
|
|
|
|
|
|
|
|
mock := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
requestReceived = true
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
assert.Equal(t, http.MethodPost, r.Method, "method")
|
|
|
|
assert.Equal(t, "/v2/domains/example.com/records", r.URL.Path, "Path")
|
|
|
|
assert.Equal(t, "application/json", r.Header.Get("Content-Type"), "Content-Type")
|
|
|
|
assert.Equal(t, "Bearer asdf1234", r.Header.Get("Authorization"), "Authorization")
|
2016-01-27 00:57:55 +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.example.com.","data":"w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI","ttl":30}`, string(reqBody))
|
2016-01-27 00:57:55 +00:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `{
|
|
|
|
"domain_record": {
|
|
|
|
"id": 1234567,
|
|
|
|
"type": "TXT",
|
|
|
|
"name": "_acme-challenge",
|
|
|
|
"data": "w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI",
|
|
|
|
"priority": null,
|
|
|
|
"port": null,
|
|
|
|
"weight": null
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
}))
|
|
|
|
defer mock.Close()
|
|
|
|
digitalOceanBaseURL = mock.URL
|
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
doprov, err := NewDNSProviderCredentials(fakeDigitalOceanAuth)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, doprov)
|
2016-01-27 00:57:55 +00:00
|
|
|
|
|
|
|
err = doprov.Present("example.com", "", "foobar")
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "fail to create TXT record")
|
|
|
|
|
|
|
|
assert.True(t, requestReceived, "Expected request to be received by mock backend, but it wasn't")
|
2016-01-27 00:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDigitalOceanCleanUp(t *testing.T) {
|
|
|
|
var requestReceived bool
|
|
|
|
|
|
|
|
mock := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
requestReceived = true
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
assert.Equal(t, http.MethodDelete, r.Method, "method")
|
|
|
|
assert.Equal(t, "/v2/domains/example.com/records/1234567", r.URL.Path, "Path")
|
2016-01-27 00:57:55 +00:00
|
|
|
// NOTE: Even though the body is empty, DigitalOcean API docs still show setting this Content-Type...
|
2018-06-11 15:32:50 +00:00
|
|
|
assert.Equal(t, "application/json", r.Header.Get("Content-Type"), "Content-Type")
|
|
|
|
assert.Equal(t, "Bearer asdf1234", r.Header.Get("Authorization"), "Authorization")
|
2016-01-27 00:57:55 +00:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}))
|
|
|
|
defer mock.Close()
|
|
|
|
digitalOceanBaseURL = mock.URL
|
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
doprov, err := NewDNSProviderCredentials(fakeDigitalOceanAuth)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, doprov)
|
2016-01-27 00:57:55 +00:00
|
|
|
|
|
|
|
doprov.recordIDsMu.Lock()
|
|
|
|
doprov.recordIDs["_acme-challenge.example.com."] = 1234567
|
|
|
|
doprov.recordIDsMu.Unlock()
|
|
|
|
|
|
|
|
err = doprov.CleanUp("example.com", "", "")
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "fail to remove TXT record")
|
|
|
|
|
|
|
|
assert.True(t, requestReceived, "Expected request to be received by mock backend, but it wasn't")
|
2016-01-27 00:57:55 +00:00
|
|
|
}
|