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

185 lines
4.2 KiB
Go
Raw Normal View History

2020-05-10 04:03:54 +00:00
package internal
import (
2023-05-05 07:49:38 +00:00
"context"
2020-05-10 04:03:54 +00:00
"fmt"
"io"
"net/http"
"net/http/httptest"
2023-05-05 07:49:38 +00:00
"net/url"
2020-05-10 04:03:54 +00:00
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2023-05-05 07:49:38 +00:00
func setupTest(t *testing.T, apiToken string) (*Client, *http.ServeMux) {
t.Helper()
2020-05-10 04:03:54 +00:00
mux := http.NewServeMux()
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
2023-05-05 07:49:38 +00:00
client := NewClient("me", apiToken)
client.baseURL, _ = url.Parse(server.URL)
client.HTTPClient = server.Client()
return client, mux
}
func TestClient_ListZones(t *testing.T) {
client, mux := setupTest(t, "secretA")
2020-05-10 04:03:54 +00:00
mux.HandleFunc("/v1/zones", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(rw, fmt.Sprintf("invalid method: %s", req.Method), http.StatusMethodNotAllowed)
return
}
auth := req.Header.Get("Authorization")
if auth != "Basic bWU6c2VjcmV0QQ==" {
http.Error(rw, fmt.Sprintf("invalid authentication: %s", auth), http.StatusUnauthorized)
2021-11-03 19:39:12 +00:00
return
2020-05-10 04:03:54 +00:00
}
file, err := os.Open("./fixtures/list_zones.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
zones, err := client.ListZones(context.Background())
2020-05-10 04:03:54 +00:00
require.NoError(t, err)
expected := []DNSZone{
{
ID: 1,
Name: "example.com",
Synced: false,
QueriesCount: 0,
RecordsCount: 3,
AliasesCount: 0,
RedirectsCount: 0,
ForwardsCount: 0,
TemplateID: 0,
},
{
ID: 2,
Name: "example.net",
Synced: false,
QueriesCount: 0,
RecordsCount: 3,
AliasesCount: 0,
RedirectsCount: 0,
ForwardsCount: 0,
TemplateID: 0,
},
}
assert.Equal(t, expected, zones)
}
func TestClient_CreateRecord(t *testing.T) {
2023-05-05 07:49:38 +00:00
client, mux := setupTest(t, "secretB")
2020-05-10 04:03:54 +00:00
mux.HandleFunc("/v1/zones/1/records", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(rw, fmt.Sprintf("invalid method: %s", req.Method), http.StatusMethodNotAllowed)
return
}
auth := req.Header.Get("Authorization")
if auth != "Basic bWU6c2VjcmV0Qg==" {
http.Error(rw, fmt.Sprintf("invalid authentication: %s", auth), http.StatusUnauthorized)
2021-11-03 19:39:12 +00:00
return
2020-05-10 04:03:54 +00:00
}
file, err := os.Open("./fixtures/create_record.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
}
})
zone := DNSZone{ID: 1}
record := DNSRecord{
Name: "example.com.",
Type: "MX",
Content: "10 mail.example.com.",
TTL: 300,
}
2023-05-05 07:49:38 +00:00
newRecord, err := client.CreateRecord(context.Background(), zone, record)
2020-05-10 04:03:54 +00:00
require.NoError(t, err)
expected := &DNSRecord{
ID: 100,
Name: "example.com.",
Type: "MX",
Content: "10 mail.example.com.",
TTL: 300,
ZoneID: 1,
}
assert.Equal(t, expected, newRecord)
}
func TestClient_DeleteRecord(t *testing.T) {
2023-05-05 07:49:38 +00:00
client, mux := setupTest(t, "secretC")
2020-05-10 04:03:54 +00:00
mux.HandleFunc("/v1/zones/1/records/2", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodDelete {
http.Error(rw, fmt.Sprintf("invalid method: %s", req.Method), http.StatusMethodNotAllowed)
return
}
auth := req.Header.Get("Authorization")
if auth != "Basic bWU6c2VjcmV0Qw==" {
http.Error(rw, fmt.Sprintf("invalid authentication: %s", auth), http.StatusUnauthorized)
2021-11-03 19:39:12 +00:00
return
2020-05-10 04:03:54 +00:00
}
file, err := os.Open("./fixtures/delete_record.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
}
})
record := &DNSRecord{
ID: 2,
Name: "example.com.",
Type: "MX",
Content: "10 mail.example.com.",
TTL: 300,
ZoneID: 1,
}
2023-05-05 07:49:38 +00:00
err := client.DeleteRecord(context.Background(), record)
2020-05-10 04:03:54 +00:00
require.NoError(t, err)
}