2022-04-29 20:50:39 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2022-04-29 20:50:39 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func setupTest(t *testing.T) (*Client, *http.ServeMux) {
|
2022-04-29 20:50:39 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
client := NewClient(OAuthStaticAccessToken(server.Client(), "secret"), "123")
|
2022-04-29 20:50:39 +00:00
|
|
|
client.baseURL, _ = url.Parse(server.URL)
|
|
|
|
|
|
|
|
return client, mux
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_CreateRecord(t *testing.T) {
|
2023-05-05 07:49:38 +00:00
|
|
|
client, mux := setupTest(t)
|
2022-04-29 20:50:39 +00:00
|
|
|
|
|
|
|
mux.HandleFunc("/v2/domains/example.com/records", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodPost {
|
|
|
|
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := req.Header.Get("Authorization")
|
|
|
|
if auth != "Bearer secret" {
|
|
|
|
http.Error(rw, fmt.Sprintf("invalid API token: %s", auth), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
teamID := req.URL.Query().Get("teamId")
|
|
|
|
if teamID != "123" {
|
|
|
|
http.Error(rw, fmt.Sprintf("invalid team ID: %s", teamID), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reqBody, err := io.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedReqBody := `{"name":"_acme-challenge.example.com.","type":"TXT","value":"w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI","ttl":60}`
|
2023-05-05 07:49:38 +00:00
|
|
|
assert.Equal(t, expectedReqBody, string(bytes.TrimSpace(reqBody)))
|
2022-04-29 20:50:39 +00:00
|
|
|
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
_, err = fmt.Fprintf(rw, `{
|
|
|
|
"uid": "9e2eab60-0ba5-4dff-b481-2999c9764b84",
|
|
|
|
"updated": 1
|
|
|
|
}`)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
record := Record{
|
|
|
|
Name: "_acme-challenge.example.com.",
|
|
|
|
Type: "TXT",
|
|
|
|
Value: "w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI",
|
|
|
|
TTL: 60,
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
resp, err := client.CreateRecord(context.Background(), "example.com.", record)
|
2022-04-29 20:50:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := &CreateRecordResponse{
|
|
|
|
UID: "9e2eab60-0ba5-4dff-b481-2999c9764b84",
|
|
|
|
Updated: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_DeleteRecord(t *testing.T) {
|
2023-05-05 07:49:38 +00:00
|
|
|
client, mux := setupTest(t)
|
2022-04-29 20:50:39 +00:00
|
|
|
|
|
|
|
mux.HandleFunc("/v2/domains/example.com/records/1234567", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodDelete {
|
|
|
|
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
auth := req.Header.Get("Authorization")
|
|
|
|
if auth != "Bearer secret" {
|
|
|
|
http.Error(rw, fmt.Sprintf("invalid API token: %s", auth), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
teamID := req.URL.Query().Get("teamId")
|
|
|
|
if teamID != "123" {
|
|
|
|
http.Error(rw, fmt.Sprintf("invalid team ID: %s", teamID), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
})
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err := client.DeleteRecord(context.Background(), "example.com.", "1234567")
|
2022-04-29 20:50:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|