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

329 lines
7.2 KiB
Go
Raw Permalink Normal View History

2020-04-27 17:58:18 +00:00
package internal
import (
2023-05-05 07:49:38 +00:00
"context"
2020-04-27 17:58:18 +00:00
"encoding/json"
"net/http"
"net/http/httptest"
2023-05-05 07:49:38 +00:00
"net/url"
2020-04-27 17:58:18 +00:00
"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) {
t.Helper()
2020-04-27 17:58:18 +00:00
mux := http.NewServeMux()
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
2020-04-27 17:58:18 +00:00
client, err := NewClient("lego")
require.NoError(t, err)
2020-04-27 17:58:18 +00:00
client.HTTPClient = server.Client()
2023-05-05 07:49:38 +00:00
client.baseURL, _ = url.Parse(server.URL)
2023-05-05 07:49:38 +00:00
return client, mux
2020-04-27 17:58:18 +00: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 19:39:12 +00:00
return
2020-04-27 17:58:18 +00: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 07:49:38 +00:00
BaseResponse: BaseResponse{
Success: "ok",
},
2020-04-27 17:58:18 +00:00
}
err = json.NewEncoder(w).Encode(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
2021-11-03 19:39:12 +00:00
return
2020-04-27 17:58:18 +00: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 19:39:12 +00:00
return
2020-04-27 17:58:18 +00:00
}
assert.Equal(t, `content=txtTXTtxtTXTtxtTXT&domain=example.com&subdomain=foo&ttl=300&type=TXT`, r.PostForm.Encode())
response := AddResponse{
2023-05-05 07:49:38 +00:00
Domain: "example.com",
BaseResponse: BaseResponse{
Success: "error",
Error: "bad things",
},
2020-04-27 17:58:18 +00:00
}
err = json.NewEncoder(w).Encode(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
2021-11-03 19:39:12 +00:00
return
2020-04-27 17:58:18 +00: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 07:49:38 +00:00
client, mux := setupTest(t)
2020-04-27 17:58:18 +00:00
mux.HandleFunc("/add", test.handler)
2023-05-05 07:49:38 +00:00
record, err := client.AddRecord(context.Background(), test.data)
2020-04-27 17:58:18 +00: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 19:39:12 +00:00
return
2020-04-27 17:58:18 +00:00
}
assert.Equal(t, `domain=example.com&record_id=6`, r.PostForm.Encode())
response := RemoveResponse{
Domain: "example.com",
RecordID: 6,
2023-05-05 07:49:38 +00:00
BaseResponse: BaseResponse{
Success: "ok",
},
2020-04-27 17:58:18 +00:00
}
err = json.NewEncoder(w).Encode(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
2021-11-03 19:39:12 +00:00
return
2020-04-27 17:58:18 +00: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 19:39:12 +00:00
return
2020-04-27 17:58:18 +00:00
}
assert.Equal(t, `domain=example.com&record_id=6`, r.PostForm.Encode())
response := RemoveResponse{
Domain: "example.com",
RecordID: 6,
2023-05-05 07:49:38 +00:00
BaseResponse: BaseResponse{
Success: "error",
Error: "bad things",
},
2020-04-27 17:58:18 +00:00
}
err = json.NewEncoder(w).Encode(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
2021-11-03 19:39:12 +00:00
return
2020-04-27 17:58:18 +00: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 07:49:38 +00:00
client, mux := setupTest(t)
2020-04-27 17:58:18 +00:00
mux.HandleFunc("/del", test.handler)
2023-05-05 07:49:38 +00:00
id, err := client.RemoveRecord(context.Background(), test.data)
2020-04-27 17:58:18 +00: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 07:49:38 +00:00
BaseResponse: BaseResponse{
Success: "ok",
},
2020-04-27 17:58:18 +00:00
}
err := json.NewEncoder(w).Encode(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
2021-11-03 19:39:12 +00:00
return
2020-04-27 17:58:18 +00: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 07:49:38 +00:00
Domain: "example.com",
BaseResponse: BaseResponse{
Success: "error",
Error: "bad things",
},
2020-04-27 17:58:18 +00:00
}
err := json.NewEncoder(w).Encode(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
2021-11-03 19:39:12 +00:00
return
2020-04-27 17:58:18 +00:00
}
},
domain: "example.com",
expectError: true,
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
2023-05-05 07:49:38 +00:00
client, mux := setupTest(t)
2020-04-27 17:58:18 +00:00
mux.HandleFunc("/list", test.handler)
2023-05-05 07:49:38 +00:00
records, err := client.GetRecords(context.Background(), test.domain)
2020-04-27 17:58:18 +00:00
if test.expectError {
require.Error(t, err)
require.Empty(t, records)
} else {
require.NoError(t, err)
require.Len(t, records, 2)
}
})
}
}