2016-02-29 04:48:41 +02:00
|
|
|
package digitalocean
|
2016-01-27 02:57:55 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2018-06-11 17:32:50 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-01-27 02:57:55 +02: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 17:32:50 +02: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 02:57:55 +02:00
|
|
|
|
|
|
|
reqBody, err := ioutil.ReadAll(r.Body)
|
2018-06-11 17:32:50 +02: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 02:57:55 +02: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()
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.AuthToken = fakeDigitalOceanAuth
|
|
|
|
config.BaseURL = mock.URL
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err)
|
2018-09-15 19:07:24 +02:00
|
|
|
require.NotNil(t, provider)
|
2016-01-27 02:57:55 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
err = provider.Present("example.com", "", "foobar")
|
2018-06-11 17:32:50 +02: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 02:57:55 +02: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 17:32:50 +02: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 02:57:55 +02:00
|
|
|
// NOTE: Even though the body is empty, DigitalOcean API docs still show setting this Content-Type...
|
2018-06-11 17:32:50 +02: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 02:57:55 +02:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}))
|
|
|
|
defer mock.Close()
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.AuthToken = fakeDigitalOceanAuth
|
|
|
|
config.BaseURL = mock.URL
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err)
|
2018-09-15 19:07:24 +02:00
|
|
|
require.NotNil(t, provider)
|
2016-01-27 02:57:55 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
provider.recordIDsMu.Lock()
|
|
|
|
provider.recordIDs["_acme-challenge.example.com."] = 1234567
|
|
|
|
provider.recordIDsMu.Unlock()
|
2016-01-27 02:57:55 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
err = provider.CleanUp("example.com", "", "")
|
2018-06-11 17:32:50 +02: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 02:57:55 +02:00
|
|
|
}
|