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

293 lines
6.2 KiB
Go
Raw Permalink Normal View History

2019-03-11 16:56:48 +00:00
package internal
2018-09-15 17:26:45 +00:00
import (
2023-05-05 07:49:38 +00:00
"bytes"
"context"
2018-09-15 17:26:45 +00:00
"encoding/json"
"fmt"
2021-08-25 09:44:11 +00:00
"io"
2018-09-15 17:26:45 +00:00
"net/http"
"net/http/httptest"
2023-05-05 07:49:38 +00:00
"net/url"
2018-09-15 17:26:45 +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, handler http.HandlerFunc) *Client {
t.Helper()
server := httptest.NewServer(handler)
t.Cleanup(server.Close)
client, _ := NewClient("myKey", "mySecret")
client.baseURL, _ = url.Parse(server.URL + "/")
client.HTTPClient = server.Client()
return client
}
func handlerMock(method string, response *apiResponse, data interface{}) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
2018-09-15 17:26:45 +00:00
if req.Method != method {
content, err := json.Marshal(apiResponse{
Code: 999, // random code only for the test
Message: fmt.Sprintf("invalid method: got %s want %s", req.Method, method),
})
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
http.Error(rw, string(content), http.StatusBadRequest)
return
}
jsonData, err := json.Marshal(data)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
response.Data = jsonData
content, err := json.Marshal(response)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
_, err = rw.Write(content)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
2023-05-05 07:49:38 +00:00
}
2018-09-15 17:26:45 +00:00
}
2023-05-05 07:49:38 +00:00
func TestClient_GetDomainInformation(t *testing.T) {
2018-09-15 17:26:45 +00:00
type result struct {
domain *Data
error bool
}
testCases := []struct {
desc string
fqdn string
response *apiResponse
data []Data
expected result
}{
{
desc: "domain found",
2024-02-24 19:49:40 +00:00
fqdn: "_acme-challenge.example.org.",
2018-09-15 17:26:45 +00:00
response: &apiResponse{
Code: 1,
},
data: []Data{
{
ID: "1",
2024-02-24 19:49:40 +00:00
Domain: "example.com.",
2018-09-15 17:26:45 +00:00
},
{
ID: "2",
2024-02-24 19:49:40 +00:00
Domain: "example.org.",
2018-09-15 17:26:45 +00:00
},
},
expected: result{domain: &Data{
ID: "2",
2024-02-24 19:49:40 +00:00
Domain: "example.org.",
2018-09-15 17:26:45 +00:00
}},
},
{
desc: "domains not found",
fqdn: "_acme-challenge.huu.com.",
response: &apiResponse{
Code: 1,
},
data: []Data{
{
ID: "5",
2024-02-24 19:49:40 +00:00
Domain: "example.com.",
2018-09-15 17:26:45 +00:00
},
{
ID: "6",
2024-02-24 19:49:40 +00:00
Domain: "example.org.",
2018-09-15 17:26:45 +00:00
},
},
expected: result{error: true},
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
2023-05-05 07:49:38 +00:00
client := setupTest(t, handlerMock(http.MethodGet, test.response, test.data))
2018-09-15 17:26:45 +00:00
2023-05-05 07:49:38 +00:00
domain, err := client.GetDomainInformation(context.Background(), test.fqdn)
2018-09-15 17:26:45 +00:00
if test.expected.error {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, test.expected.domain, domain)
}
})
}
}
2023-05-05 07:49:38 +00:00
func TestClient_FindTxtRecord(t *testing.T) {
2018-09-15 17:26:45 +00:00
type result struct {
txtRecord *TXTRecord
error bool
}
testCases := []struct {
desc string
fqdn string
zoneID string
txtRecords []TXTRecord
response *apiResponse
expected result
}{
{
desc: "record found",
2024-02-24 19:49:40 +00:00
fqdn: "_acme-challenge.example.org.",
2018-09-15 17:26:45 +00:00
zoneID: "test-zone",
txtRecords: []TXTRecord{
{
ID: 1,
RecordID: "Record-A",
2024-02-24 19:49:40 +00:00
Host: "_acme-challenge.example.org",
2018-09-15 17:26:45 +00:00
Value: "txtTXTtxtTXTtxtTXTtxtTXT",
Type: "TXT",
LineID: 6,
TTL: 30,
},
{
ID: 2,
RecordID: "Record-B",
2024-02-24 19:49:40 +00:00
Host: "_acme-challenge.example.com",
2018-09-15 17:26:45 +00:00
Value: "TXTtxtTXTtxtTXTtxtTXTtxt",
Type: "TXT",
LineID: 6,
TTL: 30,
},
},
response: &apiResponse{
Code: 1,
},
expected: result{
txtRecord: &TXTRecord{
ID: 1,
RecordID: "Record-A",
2024-02-24 19:49:40 +00:00
Host: "_acme-challenge.example.org",
2018-09-15 17:26:45 +00:00
Value: "txtTXTtxtTXTtxtTXTtxtTXT",
Type: "TXT",
LineID: 6,
TTL: 30,
},
},
},
{
desc: "record not found",
fqdn: "_acme-challenge.huu.com.",
zoneID: "test-zone",
txtRecords: []TXTRecord{
{
ID: 1,
RecordID: "Record-A",
2024-02-24 19:49:40 +00:00
Host: "_acme-challenge.example.org",
2018-09-15 17:26:45 +00:00
Value: "txtTXTtxtTXTtxtTXTtxtTXT",
Type: "TXT",
LineID: 6,
TTL: 30,
},
{
ID: 2,
RecordID: "Record-B",
2024-02-24 19:49:40 +00:00
Host: "_acme-challenge.example.com",
2018-09-15 17:26:45 +00:00
Value: "TXTtxtTXTtxtTXTtxtTXTtxt",
Type: "TXT",
LineID: 6,
TTL: 30,
},
},
response: &apiResponse{
Code: 1,
},
expected: result{error: true},
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
2023-05-05 07:49:38 +00:00
client := setupTest(t, handlerMock(http.MethodGet, test.response, test.txtRecords))
2018-09-15 17:26:45 +00:00
2023-05-05 07:49:38 +00:00
txtRecord, err := client.FindTxtRecord(context.Background(), test.zoneID, test.fqdn)
2018-09-15 17:26:45 +00:00
if test.expected.error {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, test.expected.txtRecord, txtRecord)
}
})
}
}
2023-05-05 07:49:38 +00:00
func TestClient_AddTxtRecord(t *testing.T) {
2018-09-15 17:26:45 +00:00
testCases := []struct {
desc string
domain *Data
fqdn string
value string
ttl int
expected string
}{
{
desc: "sub-domain",
domain: &Data{
ID: "1",
2024-02-24 19:49:40 +00:00
Domain: "example.com.",
2018-09-15 17:26:45 +00:00
},
2024-02-24 19:49:40 +00:00
fqdn: "_acme-challenge.foo.example.com.",
2018-09-15 17:26:45 +00:00
value: "txtTXTtxtTXTtxtTXTtxtTXT",
ttl: 30,
expected: `{"domain_id":1,"host":"_acme-challenge.foo","value":"txtTXTtxtTXTtxtTXTtxtTXT","type":"TXT","line_id":"1","ttl":"30"}`,
},
{
desc: "main domain",
domain: &Data{
ID: "2",
2024-02-24 19:49:40 +00:00
Domain: "example.com.",
2018-09-15 17:26:45 +00:00
},
2024-02-24 19:49:40 +00:00
fqdn: "_acme-challenge.example.com.",
2018-09-15 17:26:45 +00:00
value: "TXTtxtTXTtxtTXTtxtTXTtxt",
ttl: 30,
expected: `{"domain_id":2,"host":"_acme-challenge","value":"TXTtxtTXTtxtTXTtxtTXTtxt","type":"TXT","line_id":"1","ttl":"30"}`,
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
response := &apiResponse{
Code: 1,
}
2023-05-05 07:49:38 +00:00
client := setupTest(t, func(rw http.ResponseWriter, req *http.Request) {
2018-09-15 17:26:45 +00:00
assert.NotNil(t, req.Body)
2021-08-25 09:44:11 +00:00
content, err := io.ReadAll(req.Body)
2018-09-15 17:26:45 +00:00
require.NoError(t, err)
2023-05-05 07:49:38 +00:00
assert.Equal(t, test.expected, string(bytes.TrimSpace(content)))
2018-09-15 17:26:45 +00:00
handlerMock(http.MethodPost, response, nil).ServeHTTP(rw, req)
2023-05-05 07:49:38 +00:00
})
2018-09-15 17:26:45 +00:00
2023-05-05 07:49:38 +00:00
err := client.AddTxtRecord(context.Background(), test.domain, test.fqdn, test.value, test.ttl)
2018-09-15 17:26:45 +00:00
require.NoError(t, err)
})
}
}