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

133 lines
3.2 KiB
Go
Raw Normal View History

2020-04-24 01:13:25 +00:00
package internal
import (
2023-05-05 07:49:38 +00:00
"context"
2020-04-24 01:13:25 +00:00
"encoding/json"
"net/http"
"net/http/httptest"
2023-05-05 07:49:38 +00:00
"net/url"
2020-04-24 01:13:25 +00:00
"testing"
"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-24 01:13:25 +00:00
mux := http.NewServeMux()
2023-05-05 07:49:38 +00:00
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
client := NewClient("clientID", "email@example.com", "secret", 300)
client.HTTPClient = server.Client()
client.apiBaseURL, _ = url.Parse(server.URL + "/api")
client.loginURL, _ = url.Parse(server.URL + "/login")
return client, mux
}
func TestClient_AddRecord(t *testing.T) {
client, mux := setupTest(t)
2020-04-24 01:13:25 +00:00
mux.HandleFunc("/api/domain/search", func(rw http.ResponseWriter, req *http.Request) {
response := SearchResponse{
Items: []Domain{
{
ID: "A",
DomainName: "example.com",
},
},
}
err := json.NewEncoder(rw).Encode(response)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
mux.HandleFunc("/api/record-txt", func(rw http.ResponseWriter, req *http.Request) {})
mux.HandleFunc("/api/domain/A/publish", func(rw http.ResponseWriter, req *http.Request) {})
mux.HandleFunc("/login", func(rw http.ResponseWriter, req *http.Request) {
response := AuthResponse{
Auth: Auth{
AccessToken: "at",
RefreshToken: "",
},
}
err := json.NewEncoder(rw).Encode(response)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
2023-05-05 07:49:38 +00:00
err := client.AddRecord(context.Background(), "example.com", "_acme-challenge.example.com", "txt")
2020-04-24 01:13:25 +00:00
require.NoError(t, err)
}
func TestClient_DeleteRecord(t *testing.T) {
2023-05-05 07:49:38 +00:00
client, mux := setupTest(t)
2020-04-24 01:13:25 +00:00
mux.HandleFunc("/api/domain/search", func(rw http.ResponseWriter, req *http.Request) {
response := SearchResponse{
Items: []Domain{
{
ID: "A",
DomainName: "example.com",
},
},
}
err := json.NewEncoder(rw).Encode(response)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
mux.HandleFunc("/api/domain/A", func(rw http.ResponseWriter, req *http.Request) {
response := DomainInfo{
ID: "Z",
DomainName: "example.com",
LastDomainRecordList: []Record{
{
ID: "R01",
DomainID: "A",
Name: "_acme-challenge.example.com",
Value: "txt",
Type: "TXT",
},
},
SoaTTL: 300,
}
err := json.NewEncoder(rw).Encode(response)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
mux.HandleFunc("/api/record/R01", func(rw http.ResponseWriter, req *http.Request) {})
mux.HandleFunc("/api/domain/A/publish", func(rw http.ResponseWriter, req *http.Request) {})
mux.HandleFunc("/login", func(rw http.ResponseWriter, req *http.Request) {
response := AuthResponse{
Auth: Auth{
AccessToken: "at",
RefreshToken: "",
},
}
err := json.NewEncoder(rw).Encode(response)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
2023-05-05 07:49:38 +00:00
ctx, err := client.CreateAuthenticatedContext(context.Background())
require.NoError(t, err)
2020-04-24 01:13:25 +00:00
2023-05-05 07:49:38 +00:00
err = client.DeleteRecord(ctx, "example.com", "_acme-challenge.example.com")
2020-04-24 01:13:25 +00:00
require.NoError(t, err)
}