2023-04-14 07:44:20 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2023-04-14 07:44:20 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
2023-05-05 07:49:38 +00:00
|
|
|
"path/filepath"
|
2023-04-14 07:44:20 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func setupTest(t *testing.T, filename string) *Client {
|
2023-04-14 07:44:20 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2023-05-05 07:49:38 +00:00
|
|
|
file, err := os.Open(filepath.Join("fixtures", filename))
|
2023-04-14 07:44:20 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
defer func() { _ = file.Close() }()
|
2023-04-14 07:44:20 +00:00
|
|
|
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
2023-05-05 07:49:38 +00:00
|
|
|
_, err = io.Copy(rw, file)
|
2023-04-14 07:44:20 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
|
|
|
client, err := NewClient("test_user", "apiKey")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
client.HTTPClient = server.Client()
|
2023-05-05 07:49:38 +00:00
|
|
|
client.baseURL = server.URL
|
2023-04-14 07:44:20 +00:00
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_StatusDomain(t *testing.T) {
|
2023-05-05 07:49:38 +00:00
|
|
|
client := setupTest(t, "status-domain.json")
|
2023-04-14 07:44:20 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
domain, err := client.StatusDomain(context.Background(), "example.com")
|
2023-04-14 07:44:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
expected := &StatusResponse{
|
|
|
|
RenewalMode: []string{"DEFAULT"},
|
|
|
|
Status: []string{"clientTransferProhibited"},
|
|
|
|
TransferLock: []int{1},
|
|
|
|
Registrar: []string{"brandit"},
|
|
|
|
PaidUntilDate: []string{"2021-12-15 05:00:00.0"},
|
|
|
|
Nameserver: []string{"NS1.RRPPROXY.NET", "NS2.RRPPROXY.NET"},
|
|
|
|
RegistrationExpirationDate: []string{"2021-12-15 05:00:00.0"},
|
|
|
|
Domain: []string{"example.com"},
|
|
|
|
RenewalDate: []string{"2024-01-19 05:00:00.0"},
|
|
|
|
UpdatedDate: []string{"2022-12-16 08:01:27.0"},
|
|
|
|
BillingContact: []string{"example"},
|
|
|
|
XDomainRoID: []string{"example"},
|
|
|
|
AdminContact: []string{"example"},
|
|
|
|
TechContact: []string{"example"},
|
|
|
|
DomainIDN: []string{"example.com"},
|
|
|
|
CreatedDate: []string{"2016-12-16 05:00:00.0"},
|
|
|
|
RegistrarTransferDate: []string{"2021-12-09 05:17:42.0"},
|
|
|
|
Zone: []string{"com"},
|
|
|
|
Auth: []string{"example"},
|
|
|
|
UpdatedBy: []string{"example"},
|
|
|
|
RoID: []string{"example"},
|
|
|
|
OwnerContact: []string{"example"},
|
|
|
|
CreatedBy: []string{"example"},
|
|
|
|
TransferMode: []string{"auto"},
|
2023-04-14 07:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, domain)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func TestClient_StatusDomain_error(t *testing.T) {
|
|
|
|
client := setupTest(t, "error.json")
|
|
|
|
|
|
|
|
_, err := client.StatusDomain(context.Background(), "example.com")
|
|
|
|
require.ErrorIs(t, err, APIError{Code: 402, Status: "error", Message: "Invalid user."})
|
|
|
|
}
|
|
|
|
|
2023-04-14 07:44:20 +00:00
|
|
|
func TestClient_ListRecords(t *testing.T) {
|
2023-05-05 07:49:38 +00:00
|
|
|
client := setupTest(t, "list-records.json")
|
2023-04-14 07:44:20 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
resp, err := client.ListRecords(context.Background(), "example", "example.com")
|
2023-04-14 07:44:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
expected := &ListRecordsResponse{
|
|
|
|
Limit: []int{100},
|
|
|
|
Column: []string{"rr"},
|
|
|
|
Count: []int{1},
|
|
|
|
First: []int{0},
|
|
|
|
Total: []int{1},
|
|
|
|
RR: []string{"example.com. 600 IN TXT txttxttxt"},
|
|
|
|
Last: []int{0},
|
2023-04-14 07:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, resp)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func TestClient_ListRecords_error(t *testing.T) {
|
|
|
|
client := setupTest(t, "error.json")
|
|
|
|
|
|
|
|
_, err := client.ListRecords(context.Background(), "example", "example.com")
|
|
|
|
require.ErrorIs(t, err, APIError{Code: 402, Status: "error", Message: "Invalid user."})
|
|
|
|
}
|
|
|
|
|
2023-04-14 07:44:20 +00:00
|
|
|
func TestClient_AddRecord(t *testing.T) {
|
2023-05-05 07:49:38 +00:00
|
|
|
client := setupTest(t, "add-record.json")
|
2023-04-14 07:44:20 +00:00
|
|
|
|
|
|
|
testRecord := Record{
|
|
|
|
ID: 2565,
|
|
|
|
Type: "TXT",
|
|
|
|
Name: "example.com",
|
|
|
|
Content: "txttxttxt",
|
|
|
|
TTL: 600,
|
|
|
|
}
|
2023-05-05 07:49:38 +00:00
|
|
|
resp, err := client.AddRecord(context.Background(), "example.com", "test", "2565", testRecord)
|
2023-04-14 07:44:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := &AddRecord{
|
|
|
|
Response: AddRecordResponse{
|
|
|
|
ZoneType: []string{"com"},
|
|
|
|
Signed: []int{1},
|
|
|
|
},
|
|
|
|
Record: "example.com 600 IN TXT txttxttxt",
|
|
|
|
Code: 200,
|
|
|
|
Status: "success",
|
|
|
|
Error: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, resp)
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func TestClient_AddRecord_error(t *testing.T) {
|
|
|
|
client := setupTest(t, "error.json")
|
|
|
|
|
|
|
|
testRecord := Record{
|
|
|
|
ID: 2565,
|
|
|
|
Type: "TXT",
|
|
|
|
Name: "example.com",
|
|
|
|
Content: "txttxttxt",
|
|
|
|
TTL: 600,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := client.AddRecord(context.Background(), "example.com", "test", "2565", testRecord)
|
|
|
|
require.ErrorIs(t, err, APIError{Code: 402, Status: "error", Message: "Invalid user."})
|
|
|
|
}
|
|
|
|
|
2023-04-14 07:44:20 +00:00
|
|
|
func TestClient_DeleteRecord(t *testing.T) {
|
2023-05-05 07:49:38 +00:00
|
|
|
client := setupTest(t, "delete-record.json")
|
2023-04-14 07:44:20 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err := client.DeleteRecord(context.Background(), "example.com", "test", "example.com 600 IN TXT txttxttxt", "2374")
|
2023-04-14 07:44:20 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-05 07:49:38 +00:00
|
|
|
}
|
2023-04-14 07:44:20 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func TestClient_DeleteRecord_error(t *testing.T) {
|
|
|
|
client := setupTest(t, "error.json")
|
2023-04-14 07:44:20 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err := client.DeleteRecord(context.Background(), "example.com", "test", "example.com 600 IN TXT txttxttxt", "2374")
|
|
|
|
require.ErrorIs(t, err, APIError{Code: 402, Status: "error", Message: "Invalid user."})
|
2023-04-14 07:44:20 +00:00
|
|
|
}
|