2023-05-05 07:49:38 +00:00
|
|
|
package internal
|
2019-12-07 22:59:59 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2019-12-07 22:59:59 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-08-25 09:44:11 +00:00
|
|
|
"io"
|
2019-12-07 22:59:59 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
2019-12-07 22:59:59 +00:00
|
|
|
"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) {
|
2020-12-28 22:39:00 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
2020-12-28 22:39:00 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
client := NewClient(OAuthStaticAccessToken(server.Client(), "secret"))
|
|
|
|
client.BaseURL, _ = url.Parse(server.URL)
|
2019-12-07 22:59:59 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
return client, mux
|
|
|
|
}
|
2019-12-07 22:59:59 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func checkAuthorizationHeader(req *http.Request) error {
|
|
|
|
val := req.Header.Get("Authorization")
|
|
|
|
if val != "Bearer secret" {
|
|
|
|
return fmt.Errorf("invalid header value, got: %s want %s", val, "Bearer secret")
|
|
|
|
}
|
|
|
|
return nil
|
2019-12-07 22:59:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func TestClient_GetDomainIDByName(t *testing.T) {
|
|
|
|
client, mux := setupTest(t)
|
2019-12-07 22:59:59 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
mux.HandleFunc("/v1/domains", func(rw http.ResponseWriter, req *http.Request) {
|
2019-12-07 22:59:59 +00:00
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
|
2021-05-02 11:41:02 +00:00
|
|
|
return
|
2019-12-07 22:59:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err := checkAuthorizationHeader(req)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-07 22:59:59 +00:00
|
|
|
domainList := DomainListingResponse{
|
|
|
|
Embedded: EmbeddedDomainList{Domains: []*Domain{
|
|
|
|
{ID: 1, Name: "test.com"},
|
|
|
|
{ID: 2, Name: "test.org"},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = json.NewEncoder(rw).Encode(domainList)
|
2019-12-07 22:59:59 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
id, err := client.GetDomainIDByName(context.Background(), "test.com")
|
2019-12-07 22:59:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, id)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func TestClient_CheckNameservers(t *testing.T) {
|
|
|
|
client, mux := setupTest(t)
|
2019-12-07 22:59:59 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
mux.HandleFunc("/v1/domains/1/nameservers", func(rw http.ResponseWriter, req *http.Request) {
|
2019-12-07 22:59:59 +00:00
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
|
2021-05-02 11:41:02 +00:00
|
|
|
return
|
2019-12-07 22:59:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err := checkAuthorizationHeader(req)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-07 22:59:59 +00:00
|
|
|
nsResp := NameserverResponse{
|
|
|
|
Nameservers: []*Nameserver{
|
|
|
|
{Name: ns1},
|
|
|
|
{Name: ns2},
|
|
|
|
// {Name: "ns.fake.de"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = json.NewEncoder(rw).Encode(nsResp)
|
2019-12-07 22:59:59 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err := client.CheckNameservers(context.Background(), 1)
|
2019-12-07 22:59:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func TestClient_CreateRecord(t *testing.T) {
|
|
|
|
client, mux := setupTest(t)
|
2019-12-07 22:59:59 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
mux.HandleFunc("/v1/domains/1/nameservers/records", func(rw http.ResponseWriter, req *http.Request) {
|
2019-12-07 22:59:59 +00:00
|
|
|
if req.Method != http.MethodPost {
|
|
|
|
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
|
2021-05-02 11:41:02 +00:00
|
|
|
return
|
2019-12-07 22:59:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err := checkAuthorizationHeader(req)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-25 09:44:11 +00:00
|
|
|
content, err := io.ReadAll(req.Body)
|
2019-12-07 22:59:59 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if string(bytes.TrimSpace(content)) != `{"name":"test.com","value":"value","ttl":300,"priority":0,"type":"TXT"}` {
|
2019-12-07 22:59:59 +00:00
|
|
|
http.Error(rw, "invalid request body: "+string(content), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
record := &Record{
|
|
|
|
Name: "test.com",
|
|
|
|
TTL: 300,
|
|
|
|
Type: "TXT",
|
|
|
|
Value: "value",
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err := client.CreateRecord(context.Background(), 1, record)
|
2019-12-07 22:59:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func TestClient_DeleteTXTRecord(t *testing.T) {
|
|
|
|
client, mux := setupTest(t)
|
2019-12-07 22:59:59 +00:00
|
|
|
|
|
|
|
domainName := "lego.test"
|
|
|
|
recordValue := "test"
|
|
|
|
|
|
|
|
records := []*Record{
|
|
|
|
{
|
|
|
|
Name: "_acme-challenge",
|
|
|
|
Value: recordValue,
|
|
|
|
Type: "TXT",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "_acme-challenge",
|
|
|
|
Value: recordValue,
|
|
|
|
Type: "A",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "foobar",
|
|
|
|
Value: recordValue,
|
|
|
|
Type: "TXT",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedRecords := []*Record{
|
|
|
|
{
|
|
|
|
Name: "_acme-challenge",
|
|
|
|
Value: recordValue,
|
|
|
|
Type: "A",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "foobar",
|
|
|
|
Value: recordValue,
|
|
|
|
Type: "TXT",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
mux.HandleFunc("/v1/domains/1", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
err := checkAuthorizationHeader(req)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-07 22:59:59 +00:00
|
|
|
resp := DomainResponse{
|
|
|
|
ID: 1,
|
|
|
|
Name: domainName,
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = json.NewEncoder(rw).Encode(resp)
|
2019-12-07 22:59:59 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
mux.HandleFunc("/v1/domains/1/nameservers", func(rw http.ResponseWriter, req *http.Request) {
|
2019-12-07 22:59:59 +00:00
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
|
2021-05-02 11:41:02 +00:00
|
|
|
return
|
2019-12-07 22:59:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsResp := NameserverResponse{
|
|
|
|
Nameservers: []*Nameserver{{Name: ns1}, {Name: ns2}},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.NewEncoder(rw).Encode(nsResp)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
mux.HandleFunc("/v1/domains/1/nameservers/records", func(rw http.ResponseWriter, req *http.Request) {
|
2019-12-07 22:59:59 +00:00
|
|
|
switch req.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
resp := RecordListingResponse{
|
|
|
|
Embedded: EmbeddedRecordList{
|
|
|
|
Records: records,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.NewEncoder(rw).Encode(resp)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
case http.MethodPut:
|
|
|
|
var records []*Record
|
|
|
|
err := json.NewDecoder(req.Body).Decode(&records)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(records) == 0 {
|
|
|
|
http.Error(rw, "empty request body", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expectedRecords, records) {
|
|
|
|
http.Error(rw, fmt.Sprintf("invalid records: %v", records), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domainName, "abc")
|
2023-05-05 07:49:38 +00:00
|
|
|
err := client.DeleteTXTRecord(context.Background(), 1, info.EffectiveFQDN, recordValue)
|
2019-12-07 22:59:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|