2020-03-15 16:17:17 +00:00
|
|
|
package selectel
|
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2020-03-15 16:17:17 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2023-05-05 07:49:38 +00:00
|
|
|
"net/url"
|
2020-03-15 16:17:17 +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) (*Client, *http.ServeMux) {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-03-15 16:17:17 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
2021-11-01 23:52:38 +00:00
|
|
|
t.Cleanup(server.Close)
|
2020-03-15 16:17:17 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
client := NewClient("token")
|
|
|
|
client.BaseURL, _ = url.Parse(server.URL)
|
|
|
|
client.HTTPClient = server.Client()
|
|
|
|
|
|
|
|
return client, mux
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_ListRecords(t *testing.T) {
|
|
|
|
client, mux := setupTest(t)
|
|
|
|
|
2020-03-15 16:17:17 +00:00
|
|
|
mux.HandleFunc("/123/records/", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fixture := "./fixtures/list_records.json"
|
|
|
|
|
|
|
|
err := writeResponse(rw, fixture)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
records, err := client.ListRecords(context.Background(), 123)
|
2020-03-15 16:17:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := []Record{
|
|
|
|
{ID: 123, Name: "example.com", Type: "TXT", TTL: 60, Email: "email@example.com", Content: "txttxttxtA"},
|
|
|
|
{ID: 1234, Name: "example.org", Type: "TXT", TTL: 60, Email: "email@example.org", Content: "txttxttxtB"},
|
|
|
|
{ID: 12345, Name: "example.net", Type: "TXT", TTL: 60, Email: "email@example.net", Content: "txttxttxtC"},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, records)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_ListRecords_error(t *testing.T) {
|
2023-05-05 07:49:38 +00:00
|
|
|
client, mux := setupTest(t)
|
2020-03-15 16:17:17 +00:00
|
|
|
|
|
|
|
mux.HandleFunc("/123/records/", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.WriteHeader(http.StatusUnauthorized)
|
|
|
|
err := writeResponse(rw, "./fixtures/error.json")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
records, err := client.ListRecords(context.Background(), 123)
|
2020-03-15 16:17:17 +00:00
|
|
|
|
|
|
|
assert.EqualError(t, err, "request failed with status code 401: API error: 400 - error description - field that the error occurred in")
|
|
|
|
assert.Nil(t, records)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_GetDomainByName(t *testing.T) {
|
2023-05-05 07:49:38 +00:00
|
|
|
client, mux := setupTest(t)
|
2020-03-15 16:17:17 +00:00
|
|
|
|
|
|
|
mux.HandleFunc("/sub.sub.example.org", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.WriteHeader(http.StatusNotFound)
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/sub.example.org", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.WriteHeader(http.StatusNotFound)
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/example.org", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fixture := "./fixtures/domains.json"
|
|
|
|
|
|
|
|
err := writeResponse(rw, fixture)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
domain, err := client.GetDomainByName(context.Background(), "sub.sub.example.org")
|
2020-03-15 16:17:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := &Domain{
|
|
|
|
ID: 123,
|
|
|
|
Name: "example.org",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_AddRecord(t *testing.T) {
|
2023-05-05 07:49:38 +00:00
|
|
|
client, mux := setupTest(t)
|
2020-03-15 16:17:17 +00:00
|
|
|
|
|
|
|
mux.HandleFunc("/123/records/", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodPost {
|
|
|
|
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := Record{}
|
|
|
|
|
|
|
|
err := json.NewDecoder(req.Body).Decode(&rec)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rec.ID = 456
|
|
|
|
|
|
|
|
err = json.NewEncoder(rw).Encode(rec)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
record, err := client.AddRecord(context.Background(), 123, Record{
|
2020-03-15 16:17:17 +00:00
|
|
|
Name: "example.org",
|
|
|
|
Type: "TXT",
|
|
|
|
TTL: 60,
|
|
|
|
Email: "email@example.org",
|
|
|
|
Content: "txttxttxttxt",
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := &Record{
|
|
|
|
ID: 456,
|
|
|
|
Name: "example.org",
|
|
|
|
Type: "TXT",
|
|
|
|
TTL: 60,
|
|
|
|
Email: "email@example.org",
|
|
|
|
Content: "txttxttxttxt",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, record)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_DeleteRecord(t *testing.T) {
|
2023-05-05 07:49:38 +00:00
|
|
|
client, mux := setupTest(t)
|
2020-03-15 16:17:17 +00:00
|
|
|
|
|
|
|
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodDelete {
|
|
|
|
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err := client.DeleteRecord(context.Background(), 123, 456)
|
2020-03-15 16:17:17 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeResponse(rw io.Writer, filename string) error {
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() { _ = file.Close() }()
|
|
|
|
|
|
|
|
_, err = io.Copy(rw, file)
|
|
|
|
return err
|
|
|
|
}
|