2016-02-29 04:48:41 +02:00
|
|
|
package digitalocean
|
2016-01-27 02:57:55 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-08-25 11:44:11 +02:00
|
|
|
"io"
|
2016-01-27 02:57:55 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2018-06-11 17:32:50 +02:00
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
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
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
var envTest = tester.NewEnvTest(EnvAuthToken)
|
2018-10-12 19:29:18 +02:00
|
|
|
|
2021-11-02 01:52:38 +02:00
|
|
|
func setupTest(t *testing.T) (*DNSProvider, *http.ServeMux) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
2018-10-12 19:29:18 +02:00
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.AuthToken = "asdf1234"
|
|
|
|
config.BaseURL = server.URL
|
2021-11-02 01:52:38 +02:00
|
|
|
config.HTTPClient = server.Client()
|
2018-10-12 19:29:18 +02:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2021-11-02 01:52:38 +02:00
|
|
|
require.NoError(t, err)
|
2018-10-12 19:29:18 +02:00
|
|
|
|
2021-11-02 01:52:38 +02:00
|
|
|
return provider, mux
|
2018-10-12 19:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAuthToken: "123",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAuthToken: "",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
expected: "digitalocean: some credentials information are missing: DO_AUTH_TOKEN",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.envVars)
|
2018-10-12 19:29:18 +02:00
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
2021-03-04 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.recordIDs)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-01-27 02:57:55 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
authToken string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
authToken: "123",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "digitalocean: credentials missing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.AuthToken = test.authToken
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
2021-03-04 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.recordIDs)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-01-27 02:57:55 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
2021-11-02 01:52:38 +02:00
|
|
|
provider, mux := setupTest(t)
|
2016-01-27 02:57:55 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
mux.HandleFunc("/v2/domains/example.com/records", func(w http.ResponseWriter, r *http.Request) {
|
2018-06-11 17:32:50 +02:00
|
|
|
assert.Equal(t, http.MethodPost, r.Method, "method")
|
2018-10-12 19:29:18 +02:00
|
|
|
|
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
|
|
|
|
2021-08-25 11:44:11 +02:00
|
|
|
reqBody, err := io.ReadAll(r.Body)
|
2018-10-12 19:29:18 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2018-10-12 19:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedReqBody := `{"type":"TXT","name":"_acme-challenge.example.com.","data":"w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI","ttl":30}`
|
|
|
|
assert.Equal(t, expectedReqBody, string(reqBody))
|
2016-01-27 02:57:55 +02:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
2018-10-12 19:29:18 +02:00
|
|
|
_, err = fmt.Fprintf(w, `{
|
2016-01-27 02:57:55 +02:00
|
|
|
"domain_record": {
|
|
|
|
"id": 1234567,
|
|
|
|
"type": "TXT",
|
|
|
|
"name": "_acme-challenge",
|
|
|
|
"data": "w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI",
|
|
|
|
"priority": null,
|
|
|
|
"port": null,
|
|
|
|
"weight": null
|
|
|
|
}
|
|
|
|
}`)
|
2018-10-12 19:29:18 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2018-10-12 19:29:18 +02:00
|
|
|
}
|
|
|
|
})
|
2016-01-27 02:57:55 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
err := provider.Present("example.com", "", "foobar")
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err)
|
2016-01-27 02:57:55 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestDNSProvider_CleanUp(t *testing.T) {
|
2021-11-02 01:52:38 +02:00
|
|
|
provider, mux := setupTest(t)
|
2016-01-27 02:57:55 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
mux.HandleFunc("/v2/domains/example.com/records/1234567", func(w http.ResponseWriter, r *http.Request) {
|
2018-06-11 17:32:50 +02:00
|
|
|
assert.Equal(t, http.MethodDelete, r.Method, "method")
|
2018-10-12 19:29:18 +02:00
|
|
|
|
2018-06-11 17:32:50 +02:00
|
|
|
assert.Equal(t, "/v2/domains/example.com/records/1234567", r.URL.Path, "Path")
|
2018-10-12 19:29:18 +02:00
|
|
|
|
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)
|
2018-10-12 19:29:18 +02:00
|
|
|
})
|
2016-01-27 02:57:55 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
provider.recordIDsMu.Lock()
|
2019-11-05 13:58:13 +02:00
|
|
|
provider.recordIDs["token"] = 1234567
|
2018-09-15 19:07:24 +02:00
|
|
|
provider.recordIDsMu.Unlock()
|
2016-01-27 02:57:55 +02:00
|
|
|
|
2019-11-05 13:58:13 +02:00
|
|
|
err := provider.CleanUp("example.com", "token", "")
|
2018-06-11 17:32:50 +02:00
|
|
|
require.NoError(t, err, "fail to remove TXT record")
|
2016-01-27 02:57:55 +02:00
|
|
|
}
|