lego/providers/dns/servercow/internal/client_test.go

223 lines
5.4 KiB
Go
Raw Normal View History

package internal
import (
2023-05-05 07:49:38 +00:00
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
2023-05-05 07:49:38 +00:00
"net/url"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setupTest(t *testing.T) (*Client, *http.ServeMux) {
t.Helper()
mux := http.NewServeMux()
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
client := NewClient("", "")
2023-05-05 07:49:38 +00:00
client.HTTPClient = server.Client()
client.baseURL, _ = url.Parse(server.URL)
return client, mux
}
func TestClient_GetRecords(t *testing.T) {
client, handler := setupTest(t)
handler.HandleFunc("/lego.wtf", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
return
}
file, err := os.Open("./fixtures/records-01.json")
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
defer func() { _ = file.Close() }()
_, err = io.Copy(rw, file)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
2023-05-05 07:49:38 +00:00
records, err := client.GetRecords(context.Background(), "lego.wtf")
require.NoError(t, err)
recordsJSON, err := json.Marshal(records)
require.NoError(t, err)
2021-08-25 09:44:11 +00:00
expectedContent, err := os.ReadFile("./fixtures/records-01.json")
require.NoError(t, err)
assert.JSONEq(t, string(expectedContent), string(recordsJSON))
}
func TestClient_GetRecords_error(t *testing.T) {
client, handler := setupTest(t)
handler.HandleFunc("/lego.wtf", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
return
}
err := json.NewEncoder(rw).Encode(Message{ErrorMsg: "authentication failed"})
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
2023-05-05 07:49:38 +00:00
records, err := client.GetRecords(context.Background(), "lego.wtf")
require.Error(t, err)
assert.Nil(t, records)
}
func TestClient_CreateUpdateRecord(t *testing.T) {
client, handler := setupTest(t)
handler.HandleFunc("/lego.wtf", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
return
}
2021-08-25 09:44:11 +00:00
content, err := io.ReadAll(req.Body)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
expectedRequest := `{"name":"_acme-challenge.www","type":"TXT","ttl":30,"content":["aaa","bbb"]}`
if !assert.JSONEq(t, expectedRequest, string(content)) {
http.Error(rw, "invalid content", http.StatusBadRequest)
return
}
err = json.NewEncoder(rw).Encode(Message{Message: "ok"})
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
record := Record{
Name: "_acme-challenge.www",
Type: "TXT",
TTL: 30,
Content: Value{"aaa", "bbb"},
}
2023-05-05 07:49:38 +00:00
msg, err := client.CreateUpdateRecord(context.Background(), "lego.wtf", record)
require.NoError(t, err)
expected := &Message{Message: "ok"}
assert.Equal(t, expected, msg)
}
func TestClient_CreateUpdateRecord_error(t *testing.T) {
client, handler := setupTest(t)
handler.HandleFunc("/lego.wtf", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
return
}
err := json.NewEncoder(rw).Encode(Message{ErrorMsg: "parameter type must be cname, txt, tlsa, caa, a or aaaa"})
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
record := Record{
Name: "_acme-challenge.www",
}
2023-05-05 07:49:38 +00:00
msg, err := client.CreateUpdateRecord(context.Background(), "lego.wtf", record)
require.Error(t, err)
assert.Nil(t, msg)
}
func TestClient_DeleteRecord(t *testing.T) {
client, handler := setupTest(t)
handler.HandleFunc("/lego.wtf", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodDelete {
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
return
}
2021-08-25 09:44:11 +00:00
content, err := io.ReadAll(req.Body)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
expectedRequest := `{"name":"_acme-challenge.www","type":"TXT"}`
if !assert.JSONEq(t, expectedRequest, string(content)) {
http.Error(rw, "invalid content", http.StatusBadRequest)
return
}
err = json.NewEncoder(rw).Encode(Message{Message: "ok"})
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
record := Record{
Name: "_acme-challenge.www",
Type: "TXT",
}
2023-05-05 07:49:38 +00:00
msg, err := client.DeleteRecord(context.Background(), "lego.wtf", record)
require.NoError(t, err)
expected := &Message{Message: "ok"}
assert.Equal(t, expected, msg)
}
func TestClient_DeleteRecord_error(t *testing.T) {
client, handler := setupTest(t)
handler.HandleFunc("/lego.wtf", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodDelete {
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
return
}
err := json.NewEncoder(rw).Encode(Message{ErrorMsg: "parameter type must be cname, txt, tlsa, caa, a or aaaa"})
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
record := Record{
Name: "_acme-challenge.www",
}
2023-05-05 07:49:38 +00:00
msg, err := client.DeleteRecord(context.Background(), "lego.wtf", record)
require.Error(t, err)
assert.Nil(t, msg)
}