2020-04-27 19:58:18 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2023-05-05 09:49:38 +02:00
|
|
|
"context"
|
2020-04-27 19:58:18 +02:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2023-05-05 09:49:38 +02:00
|
|
|
"net/url"
|
2020-04-27 19:58:18 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
func setupTest(t *testing.T) (*Client, *http.ServeMux) {
|
2021-11-02 01:52:38 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2020-04-27 19:58:18 +02:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
2021-11-02 01:52:38 +02:00
|
|
|
t.Cleanup(server.Close)
|
2020-04-27 19:58:18 +02:00
|
|
|
|
|
|
|
client, err := NewClient("lego")
|
2021-11-02 01:52:38 +02:00
|
|
|
require.NoError(t, err)
|
2020-04-27 19:58:18 +02:00
|
|
|
|
2021-11-02 01:52:38 +02:00
|
|
|
client.HTTPClient = server.Client()
|
2023-05-05 09:49:38 +02:00
|
|
|
client.baseURL, _ = url.Parse(server.URL)
|
2021-11-02 01:52:38 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
return client, mux
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddRecord(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
handler http.HandlerFunc
|
|
|
|
data Record
|
|
|
|
expectError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
require.Equal(t, http.MethodPost, r.Method)
|
|
|
|
assert.Equal(t, "lego", r.Header.Get(pddTokenHeader))
|
|
|
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, `content=txtTXTtxtTXTtxtTXT&domain=example.com&subdomain=foo&ttl=300&type=TXT`, r.PostForm.Encode())
|
|
|
|
|
|
|
|
response := AddResponse{
|
|
|
|
Domain: "example.com",
|
|
|
|
Record: &Record{
|
|
|
|
ID: 1,
|
|
|
|
Type: "TXT",
|
|
|
|
Domain: "example.com",
|
|
|
|
SubDomain: "foo",
|
|
|
|
FQDN: "foo.example.com.",
|
|
|
|
Content: "txtTXTtxtTXTtxtTXT",
|
|
|
|
TTL: 300,
|
|
|
|
},
|
2023-05-05 09:49:38 +02:00
|
|
|
BaseResponse: BaseResponse{
|
|
|
|
Success: "ok",
|
|
|
|
},
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data: Record{
|
|
|
|
Domain: "example.com",
|
|
|
|
Type: "TXT",
|
|
|
|
Content: "txtTXTtxtTXTtxtTXT",
|
|
|
|
SubDomain: "foo",
|
|
|
|
TTL: 300,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "error",
|
|
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
require.Equal(t, http.MethodPost, r.Method)
|
|
|
|
assert.Equal(t, "lego", r.Header.Get(pddTokenHeader))
|
|
|
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, `content=txtTXTtxtTXTtxtTXT&domain=example.com&subdomain=foo&ttl=300&type=TXT`, r.PostForm.Encode())
|
|
|
|
|
|
|
|
response := AddResponse{
|
2023-05-05 09:49:38 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
BaseResponse: BaseResponse{
|
|
|
|
Success: "error",
|
|
|
|
Error: "bad things",
|
|
|
|
},
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data: Record{
|
|
|
|
Domain: "example.com",
|
|
|
|
Type: "TXT",
|
|
|
|
Content: "txtTXTtxtTXTtxtTXT",
|
|
|
|
SubDomain: "foo",
|
|
|
|
TTL: 300,
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2023-05-05 09:49:38 +02:00
|
|
|
client, mux := setupTest(t)
|
2020-04-27 19:58:18 +02:00
|
|
|
|
|
|
|
mux.HandleFunc("/add", test.handler)
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
record, err := client.AddRecord(context.Background(), test.data)
|
2020-04-27 19:58:18 +02:00
|
|
|
if test.expectError {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, record)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, record)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveRecord(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
handler http.HandlerFunc
|
|
|
|
data Record
|
|
|
|
expectError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
require.Equal(t, http.MethodPost, r.Method)
|
|
|
|
assert.Equal(t, "lego", r.Header.Get(pddTokenHeader))
|
|
|
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, `domain=example.com&record_id=6`, r.PostForm.Encode())
|
|
|
|
|
|
|
|
response := RemoveResponse{
|
|
|
|
Domain: "example.com",
|
|
|
|
RecordID: 6,
|
2023-05-05 09:49:38 +02:00
|
|
|
BaseResponse: BaseResponse{
|
|
|
|
Success: "ok",
|
|
|
|
},
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data: Record{
|
|
|
|
ID: 6,
|
|
|
|
Domain: "example.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "error",
|
|
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
require.Equal(t, http.MethodPost, r.Method)
|
|
|
|
assert.Equal(t, "lego", r.Header.Get(pddTokenHeader))
|
|
|
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, `domain=example.com&record_id=6`, r.PostForm.Encode())
|
|
|
|
|
|
|
|
response := RemoveResponse{
|
|
|
|
Domain: "example.com",
|
|
|
|
RecordID: 6,
|
2023-05-05 09:49:38 +02:00
|
|
|
BaseResponse: BaseResponse{
|
|
|
|
Success: "error",
|
|
|
|
Error: "bad things",
|
|
|
|
},
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data: Record{
|
|
|
|
ID: 6,
|
|
|
|
Domain: "example.com",
|
|
|
|
},
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2023-05-05 09:49:38 +02:00
|
|
|
client, mux := setupTest(t)
|
2020-04-27 19:58:18 +02:00
|
|
|
|
|
|
|
mux.HandleFunc("/del", test.handler)
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
id, err := client.RemoveRecord(context.Background(), test.data)
|
2020-04-27 19:58:18 +02:00
|
|
|
if test.expectError {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, 0, id)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 6, id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRecords(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
handler http.HandlerFunc
|
|
|
|
domain string
|
|
|
|
expectError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
require.Equal(t, http.MethodGet, r.Method)
|
|
|
|
assert.Equal(t, "lego", r.Header.Get(pddTokenHeader))
|
|
|
|
|
|
|
|
assert.Equal(t, "domain=example.com", r.URL.RawQuery)
|
|
|
|
|
|
|
|
response := ListResponse{
|
|
|
|
Domain: "example.com",
|
|
|
|
Records: []Record{
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
Type: "TXT",
|
|
|
|
Domain: "example.com",
|
|
|
|
SubDomain: "foo",
|
|
|
|
FQDN: "foo.example.com.",
|
|
|
|
Content: "txtTXTtxtTXTtxtTXT",
|
|
|
|
TTL: 300,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
Type: "NS",
|
|
|
|
Domain: "example.com",
|
|
|
|
SubDomain: "foo",
|
|
|
|
FQDN: "foo.example.com.",
|
|
|
|
Content: "bar",
|
|
|
|
TTL: 300,
|
|
|
|
},
|
|
|
|
},
|
2023-05-05 09:49:38 +02:00
|
|
|
BaseResponse: BaseResponse{
|
|
|
|
Success: "ok",
|
|
|
|
},
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
domain: "example.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "error",
|
|
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
require.Equal(t, http.MethodGet, r.Method)
|
|
|
|
assert.Equal(t, "lego", r.Header.Get(pddTokenHeader))
|
|
|
|
|
|
|
|
assert.Equal(t, "domain=example.com", r.URL.RawQuery)
|
|
|
|
|
|
|
|
response := ListResponse{
|
2023-05-05 09:49:38 +02:00
|
|
|
Domain: "example.com",
|
|
|
|
BaseResponse: BaseResponse{
|
|
|
|
Success: "error",
|
|
|
|
Error: "bad things",
|
|
|
|
},
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2020-04-27 19:58:18 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
domain: "example.com",
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-05-05 09:49:38 +02:00
|
|
|
client, mux := setupTest(t)
|
2020-04-27 19:58:18 +02:00
|
|
|
|
|
|
|
mux.HandleFunc("/list", test.handler)
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
records, err := client.GetRecords(context.Background(), test.domain)
|
2020-04-27 19:58:18 +02:00
|
|
|
if test.expectError {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Empty(t, records)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, records, 2)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|