lego/providers/dns/constellix/internal/txtrecords_test.go
2020-02-25 21:56:40 +01:00

182 lines
4.6 KiB
Go

package internal
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTxtRecordService_Create(t *testing.T) {
client, handler, tearDown := setupAPIMock()
defer tearDown()
handler.HandleFunc("/v1/domains/12345/records/txt", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
return
}
file, err := os.Open("./fixtures/records-01.json")
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
defer func() { _ = file.Close() }()
_, err = io.Copy(rw, file)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
records, err := client.TxtRecords.Create(12345, RecordRequest{})
require.NoError(t, err)
recordsJSON, err := json.Marshal(records)
require.NoError(t, err)
expectedContent, err := ioutil.ReadFile("./fixtures/records-01.json")
require.NoError(t, err)
assert.JSONEq(t, string(expectedContent), string(recordsJSON))
}
func TestTxtRecordService_GetAll(t *testing.T) {
client, handler, tearDown := setupAPIMock()
defer tearDown()
handler.HandleFunc("/v1/domains/12345/records/txt", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
return
}
file, err := os.Open("./fixtures/records-01.json")
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
defer func() { _ = file.Close() }()
_, err = io.Copy(rw, file)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
records, err := client.TxtRecords.GetAll(12345)
require.NoError(t, err)
recordsJSON, err := json.Marshal(records)
require.NoError(t, err)
expectedContent, err := ioutil.ReadFile("./fixtures/records-01.json")
require.NoError(t, err)
assert.JSONEq(t, string(expectedContent), string(recordsJSON))
}
func TestTxtRecordService_Get(t *testing.T) {
client, handler, tearDown := setupAPIMock()
defer tearDown()
handler.HandleFunc("/v1/domains/12345/records/txt/6789", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
return
}
file, err := os.Open("./fixtures/records-02.json")
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
defer func() { _ = file.Close() }()
_, err = io.Copy(rw, file)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
record, err := client.TxtRecords.Get(12345, 6789)
require.NoError(t, err)
expected := &Record{
ID: 3557066,
Type: "TXT",
RecordType: "txt",
Name: "test",
TTL: 300,
RecordOption: "roundRobin",
GtdRegion: 1,
ParentID: 273302,
Parent: "domain",
Source: "Domain",
ModifiedTs: 1580908547863,
Value: []RecordValue{{
Value: `"test"`,
}},
RoundRobin: []RecordValue{{
Value: `"test"`,
}},
}
assert.Equal(t, expected, record)
}
func TestTxtRecordService_Update(t *testing.T) {
client, handler, tearDown := setupAPIMock()
defer tearDown()
handler.HandleFunc("/v1/domains/12345/records/txt/6789", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPut {
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
return
}
_, err := rw.Write([]byte(`{"success":"Record updated successfully"}`))
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
msg, err := client.TxtRecords.Update(12345, 6789, RecordRequest{})
require.NoError(t, err)
expected := &SuccessMessage{Success: "Record updated successfully"}
assert.Equal(t, expected, msg)
}
func TestTxtRecordService_Delete(t *testing.T) {
client, handler, tearDown := setupAPIMock()
defer tearDown()
handler.HandleFunc("/v1/domains/12345/records/txt/6789", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodDelete {
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
return
}
_, err := rw.Write([]byte(`{"success":"Record deleted successfully"}`))
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
msg, err := client.TxtRecords.Delete(12345, 6789)
require.NoError(t, err)
expected := &SuccessMessage{Success: "Record deleted successfully"}
assert.Equal(t, expected, msg)
}