2021-04-13 21:32:15 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-08-25 11:44:11 +02:00
|
|
|
"os"
|
2021-04-13 21:32:15 +02:00
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupNew(t *testing.T, expectedForm string, filename string) *Client {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
|
|
|
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
err := req.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := regexp.MustCompile(`"auth":"\w+",`)
|
|
|
|
|
|
|
|
form := req.PostForm.Get("request")
|
|
|
|
form = exp.ReplaceAllString(form, `"auth":"xxx",`)
|
|
|
|
|
|
|
|
if form != expectedForm {
|
|
|
|
t.Logf("invalid form data: %s", req.PostForm.Get("request"))
|
|
|
|
http.Error(rw, fmt.Sprintf("invalid form data: %s", req.PostForm.Get("request")), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:44:11 +02:00
|
|
|
data, err := os.ReadFile(fmt.Sprintf("./fixtures/%s.json", filename))
|
2021-04-13 21:32:15 +02:00
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.Header().Set("Content-Type", "application/json")
|
|
|
|
_, _ = rw.Write(data)
|
|
|
|
})
|
|
|
|
|
|
|
|
client := NewClient("user", "secret")
|
|
|
|
client.baseURL = server.URL
|
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_GetRecords(t *testing.T) {
|
|
|
|
expectedForm := `{"request":{"user":"user","auth":"xxx","command":"dns-rows-list","data":{"domain":"example.com"}}}`
|
|
|
|
client := setupNew(t, expectedForm, commandDNSRowsList)
|
|
|
|
|
|
|
|
records, err := client.GetRecords(context.Background(), "example.com.")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, records, 4)
|
|
|
|
|
|
|
|
expected := []DNSRow{
|
|
|
|
{
|
|
|
|
ID: "911",
|
|
|
|
TTL: "1800",
|
|
|
|
Type: "A",
|
|
|
|
Data: "1.2.3.4",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "913",
|
|
|
|
TTL: "1800",
|
|
|
|
Type: "MX",
|
|
|
|
Data: "1 mail1.wedos.net",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "914",
|
|
|
|
TTL: "1800",
|
|
|
|
Type: "MX",
|
|
|
|
Data: "10 mailbackup.wedos.net",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "912",
|
|
|
|
Name: "*",
|
|
|
|
TTL: "1800",
|
|
|
|
Type: "A",
|
|
|
|
Data: "1.2.3.4",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, records)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_AddRecord(t *testing.T) {
|
2021-04-14 10:13:48 +02:00
|
|
|
expectedForm := `{"request":{"user":"user","auth":"xxx","command":"dns-row-add","data":{"domain":"example.com","name":"foo","ttl":1800,"type":"TXT","rdata":"foobar"}}}`
|
2021-04-13 21:32:15 +02:00
|
|
|
|
|
|
|
client := setupNew(t, expectedForm, commandDNSRowAdd)
|
|
|
|
|
|
|
|
record := DNSRow{
|
2021-04-14 10:13:48 +02:00
|
|
|
ID: "",
|
|
|
|
Name: "foo",
|
|
|
|
TTL: "1800",
|
|
|
|
Type: "TXT",
|
|
|
|
Data: "foobar",
|
2021-04-13 21:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := client.AddRecord(context.Background(), "example.com.", record)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_AddRecord_update(t *testing.T) {
|
2021-04-14 10:13:48 +02:00
|
|
|
expectedForm := `{"request":{"user":"user","auth":"xxx","command":"dns-row-update","data":{"row_id":"1","domain":"example.com","ttl":1800,"type":"TXT","rdata":"foobar"}}}`
|
2021-04-13 21:32:15 +02:00
|
|
|
|
|
|
|
client := setupNew(t, expectedForm, commandDNSRowUpdate)
|
|
|
|
|
|
|
|
record := DNSRow{
|
2021-04-14 10:13:48 +02:00
|
|
|
ID: "1",
|
|
|
|
Name: "foo",
|
|
|
|
TTL: "1800",
|
|
|
|
Type: "TXT",
|
|
|
|
Data: "foobar",
|
2021-04-13 21:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := client.AddRecord(context.Background(), "example.com.", record)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_DeleteRecord(t *testing.T) {
|
2021-04-14 10:13:48 +02:00
|
|
|
expectedForm := `{"request":{"user":"user","auth":"xxx","command":"dns-row-delete","data":{"row_id":"1","domain":"example.com","rdata":""}}}`
|
2021-04-13 21:32:15 +02:00
|
|
|
|
|
|
|
client := setupNew(t, expectedForm, commandDNSRowDelete)
|
|
|
|
|
|
|
|
err := client.DeleteRecord(context.Background(), "example.com.", "1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_Commit(t *testing.T) {
|
|
|
|
expectedForm := `{"request":{"user":"user","auth":"xxx","command":"dns-domain-commit","data":{"name":"example.com"}}}`
|
|
|
|
|
|
|
|
client := setupNew(t, expectedForm, commandDNSDomainCommit)
|
|
|
|
|
|
|
|
err := client.Commit(context.Background(), "example.com.")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|