2020-10-27 02:03:26 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2023-05-05 09:49:38 +02:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-10-27 02:03:26 +02:00
|
|
|
"fmt"
|
2021-08-25 11:44:11 +02:00
|
|
|
"io"
|
2020-10-27 02:03:26 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupTest(t *testing.T) (*Client, *http.ServeMux) {
|
2020-12-29 00:39:00 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2020-10-27 02:03:26 +02:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
client, err := New(OAuthStaticAccessToken(server.Client(), "token"), server.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return client, mux
|
2020-10-27 02:03:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_CreateDNSRecord(t *testing.T) {
|
|
|
|
client, mux := setupTest(t)
|
|
|
|
|
|
|
|
mux.HandleFunc("/1/domain/666/dns/record", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodPost {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Header.Get("Authorization") != "Bearer token" {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:44:11 +02:00
|
|
|
raw, err := io.ReadAll(req.Body)
|
2020-10-27 02:03:26 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() { _ = req.Body.Close() }()
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
if string(bytes.TrimSpace(raw)) != `{"source":"foo","type":"TXT","ttl":60,"target":"txtxtxttxt"}` {
|
2020-10-27 02:03:26 +02:00
|
|
|
http.Error(rw, fmt.Sprintf("invalid request body: %s", string(raw)), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response := `{"result":"success","data": "123"}`
|
|
|
|
|
|
|
|
_, err = rw.Write([]byte(response))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2020-10-27 02:03:26 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
domain := &DNSDomain{
|
|
|
|
ID: 666,
|
|
|
|
CustomerName: "test",
|
|
|
|
}
|
|
|
|
|
|
|
|
record := Record{
|
|
|
|
Source: "foo",
|
|
|
|
Target: "txtxtxttxt",
|
|
|
|
Type: "TXT",
|
|
|
|
TTL: 60,
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
recordID, err := client.CreateDNSRecord(context.Background(), domain, record)
|
2020-10-27 02:03:26 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "123", recordID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_GetDomainByName(t *testing.T) {
|
|
|
|
client, mux := setupTest(t)
|
|
|
|
|
|
|
|
mux.HandleFunc("/1/product", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Header.Get("Authorization") != "Bearer token" {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceName := req.URL.Query().Get("service_name")
|
|
|
|
if serviceName != "domain" {
|
|
|
|
http.Error(rw, fmt.Sprintf("invalid service_name: %s", serviceName), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
customerName := req.URL.Query().Get("customer_name")
|
|
|
|
if customerName == "" {
|
|
|
|
http.Error(rw, fmt.Sprintf("invalid customer_name: %s", customerName), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response := `
|
2023-02-09 18:19:58 +02:00
|
|
|
{
|
|
|
|
"result": "success",
|
|
|
|
"data": [
|
|
|
|
{
|
|
|
|
"id": 123,
|
|
|
|
"customer_name": "two.three.example.com"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 456,
|
|
|
|
"customer_name": "three.example.com"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
2020-10-27 02:03:26 +02:00
|
|
|
|
|
|
|
_, err := rw.Write([]byte(response))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2020-10-27 02:03:26 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
domain, err := client.GetDomainByName(context.Background(), "one.two.three.example.com.")
|
2020-10-27 02:03:26 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := &DNSDomain{ID: 123, CustomerName: "two.three.example.com"}
|
|
|
|
assert.Equal(t, expected, domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_DeleteDNSRecord(t *testing.T) {
|
|
|
|
client, mux := setupTest(t)
|
|
|
|
|
|
|
|
mux.HandleFunc("/1/domain/123/dns/record/456", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodDelete {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Header.Get("Authorization") != "Bearer token" {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := rw.Write([]byte((`{"result":"success"}`)))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
2021-11-03 21:39:12 +02:00
|
|
|
return
|
2020-10-27 02:03:26 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
err := client.DeleteDNSRecord(context.Background(), 123, "456")
|
2020-10-27 02:03:26 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|