lego/providers/dns/otc/internal/mock.go

164 lines
4.5 KiB
Go
Raw Normal View History

2023-05-05 07:49:38 +00:00
package internal
import (
"fmt"
2021-08-25 09:44:11 +00:00
"io"
"net/http"
"net/http/httptest"
2023-05-05 07:49:38 +00:00
"os"
"path/filepath"
"testing"
2018-05-30 17:53:04 +00:00
"github.com/stretchr/testify/assert"
)
const fakeOTCToken = "62244bc21da68d03ebac94e6636ff01f"
2023-05-05 07:49:38 +00:00
func writeFixture(rw http.ResponseWriter, filename string) {
file, err := os.Open(filepath.Join("internal", "fixtures", filename))
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
defer func() { _ = file.Close() }()
_, _ = io.Copy(rw, file)
}
2020-05-08 17:35:25 +00:00
// DNSServerMock mock.
type DNSServerMock struct {
t *testing.T
server *httptest.Server
2023-05-05 07:49:38 +00:00
mux *http.ServeMux
}
2020-05-08 17:35:25 +00:00
// NewDNSServerMock create a new DNSServerMock.
func NewDNSServerMock(t *testing.T) *DNSServerMock {
2020-12-28 22:39:00 +00:00
t.Helper()
mux := http.NewServeMux()
return &DNSServerMock{
t: t,
server: httptest.NewServer(mux),
2023-05-05 07:49:38 +00:00
mux: mux,
}
}
func (m *DNSServerMock) GetServerURL() string {
return m.server.URL
}
2020-05-08 17:35:25 +00:00
// ShutdownServer creates the mock server.
func (m *DNSServerMock) ShutdownServer() {
m.server.Close()
}
2020-05-08 17:35:25 +00:00
// HandleAuthSuccessfully Handle auth successfully.
func (m *DNSServerMock) HandleAuthSuccessfully() {
2023-05-05 07:49:38 +00:00
m.mux.HandleFunc("/v3/auth/token", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("X-Subject-Token", fakeOTCToken)
2023-05-05 07:49:38 +00:00
_, _ = fmt.Fprintf(w, `{
"token": {
"catalog": [
{
"type": "dns",
"id": "56cd81db1f8445d98652479afe07c5ba",
"name": "",
"endpoints": [
{
"url": "%s",
"region": "eu-de",
"region_id": "eu-de",
"interface": "public",
"id": "0047a06690484d86afe04877074efddf"
}
]
}
]
}}`, m.server.URL)
})
}
2020-05-08 17:35:25 +00:00
// HandleListZonesSuccessfully Handle list zones successfully.
func (m *DNSServerMock) HandleListZonesSuccessfully() {
2023-05-05 07:49:38 +00:00
m.mux.HandleFunc("/v2/zones", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(m.t, http.MethodGet, r.Method)
assert.Equal(m.t, "/v2/zones", r.URL.Path)
assert.Equal(m.t, "name=example.com.", r.URL.RawQuery)
assert.Equal(m.t, "application/json", r.Header.Get("Accept"))
writeFixture(w, "zones_GET.json")
})
}
2020-05-08 17:35:25 +00:00
// HandleListZonesEmpty Handle list zones empty.
func (m *DNSServerMock) HandleListZonesEmpty() {
2023-05-05 07:49:38 +00:00
m.mux.HandleFunc("/v2/zones", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(m.t, http.MethodGet, r.Method)
assert.Equal(m.t, "/v2/zones", r.URL.Path)
assert.Equal(m.t, "name=example.com.", r.URL.RawQuery)
assert.Equal(m.t, "application/json", r.Header.Get("Accept"))
writeFixture(w, "zones_GET_empty.json")
})
}
2020-05-08 17:35:25 +00:00
// HandleDeleteRecordsetsSuccessfully Handle delete recordsets successfully.
func (m *DNSServerMock) HandleDeleteRecordsetsSuccessfully() {
2023-05-05 07:49:38 +00:00
m.mux.HandleFunc("/v2/zones/123123/recordsets/321321", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(m.t, http.MethodDelete, r.Method)
assert.Equal(m.t, "/v2/zones/123123/recordsets/321321", r.URL.Path)
assert.Equal(m.t, "application/json", r.Header.Get("Accept"))
writeFixture(w, "zones-recordsets_DELETE.json")
})
}
2020-05-08 17:35:25 +00:00
// HandleListRecordsetsEmpty Handle list recordsets empty.
func (m *DNSServerMock) HandleListRecordsetsEmpty() {
2023-05-05 07:49:38 +00:00
m.mux.HandleFunc("/v2/zones/123123/recordsets", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(m.t, "/v2/zones/123123/recordsets", r.URL.Path)
assert.Equal(m.t, "name=_acme-challenge.example.com.&type=TXT", r.URL.RawQuery)
writeFixture(w, "zones-recordsets_GET_empty.json")
})
}
2018-05-30 17:53:04 +00:00
2020-05-08 17:35:25 +00:00
// HandleListRecordsetsSuccessfully Handle list recordsets successfully.
func (m *DNSServerMock) HandleListRecordsetsSuccessfully() {
2023-05-05 07:49:38 +00:00
m.mux.HandleFunc("/v2/zones/123123/recordsets", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(m.t, "application/json", r.Header.Get("Accept"))
if r.Method == http.MethodGet {
2023-05-05 07:49:38 +00:00
assert.Equal(m.t, "/v2/zones/123123/recordsets", r.URL.Path)
assert.Equal(m.t, "name=_acme-challenge.example.com.&type=TXT", r.URL.RawQuery)
writeFixture(w, "zones-recordsets_GET.json")
return
}
if r.Method == http.MethodPost {
2023-05-05 07:49:38 +00:00
assert.Equal(m.t, "application/json", r.Header.Get("Content-Type"))
2023-05-05 07:49:38 +00:00
raw, err := io.ReadAll(r.Body)
assert.Nil(m.t, err)
2021-03-27 12:49:40 +00:00
exceptedString := `{
"name": "_acme-challenge.example.com.",
"description": "Added TXT record for ACME dns-01 challenge using lego client",
"type": "TXT",
"ttl": 300,
"records": ["\"w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI\""]
}`
2023-05-05 07:49:38 +00:00
assert.JSONEq(m.t, exceptedString, string(raw))
writeFixture(w, "zones-recordsets_POST.json")
return
}
http.Error(w, fmt.Sprintf("Expected method to be 'GET' or 'POST' but got '%s'", r.Method), http.StatusBadRequest)
})
}