1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-23 09:15:11 +02:00
lego/providers/dns/constellix/internal/txtrecords_test.go

213 lines
5.5 KiB
Go
Raw Normal View History

package internal
import (
2023-05-05 09:49:38 +02:00
"context"
"encoding/json"
"io"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTxtRecordService_Create(t *testing.T) {
client, mux := setupTest(t)
mux.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
}
2020-04-18 20:39:10 +02:00
file, err := os.Open("./fixtures/records-Create.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
}
})
2023-05-05 09:49:38 +02:00
records, err := client.TxtRecords.Create(context.Background(), 12345, RecordRequest{})
require.NoError(t, err)
recordsJSON, err := json.Marshal(records)
require.NoError(t, err)
2021-08-25 11:44:11 +02:00
expectedContent, err := os.ReadFile("./fixtures/records-Create.json")
require.NoError(t, err)
assert.JSONEq(t, string(expectedContent), string(recordsJSON))
}
func TestTxtRecordService_GetAll(t *testing.T) {
client, mux := setupTest(t)
mux.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
}
2020-04-18 20:39:10 +02:00
file, err := os.Open("./fixtures/records-GetAll.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
}
})
2023-05-05 09:49:38 +02:00
records, err := client.TxtRecords.GetAll(context.Background(), 12345)
require.NoError(t, err)
recordsJSON, err := json.Marshal(records)
require.NoError(t, err)
2021-08-25 11:44:11 +02:00
expectedContent, err := os.ReadFile("./fixtures/records-GetAll.json")
require.NoError(t, err)
assert.JSONEq(t, string(expectedContent), string(recordsJSON))
}
func TestTxtRecordService_Get(t *testing.T) {
client, mux := setupTest(t)
mux.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
}
2020-04-18 20:39:10 +02:00
file, err := os.Open("./fixtures/records-Get.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
}
})
2023-05-05 09:49:38 +02:00
record, err := client.TxtRecords.Get(context.Background(), 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",
2021-03-27 14:49:40 +02:00
ModifiedTS: 1580908547863,
Value: []RecordValue{{
Value: `"test"`,
}},
RoundRobin: []RecordValue{{
Value: `"test"`,
}},
}
assert.Equal(t, expected, record)
}
func TestTxtRecordService_Update(t *testing.T) {
client, mux := setupTest(t)
mux.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
}
})
2023-05-05 09:49:38 +02:00
msg, err := client.TxtRecords.Update(context.Background(), 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, mux := setupTest(t)
mux.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
}
})
2023-05-05 09:49:38 +02:00
msg, err := client.TxtRecords.Delete(context.Background(), 12345, 6789)
require.NoError(t, err)
expected := &SuccessMessage{Success: "Record deleted successfully"}
assert.Equal(t, expected, msg)
}
2020-04-18 20:39:10 +02:00
func TestTxtRecordService_Search(t *testing.T) {
client, mux := setupTest(t)
2020-04-18 20:39:10 +02:00
mux.HandleFunc("/v1/domains/12345/records/txt/search", func(rw http.ResponseWriter, req *http.Request) {
2020-04-18 20:39:10 +02:00
if req.Method != http.MethodGet {
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
return
}
file, err := os.Open("./fixtures/records-Search.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
}
})
2023-05-05 09:49:38 +02:00
records, err := client.TxtRecords.Search(context.Background(), 12345, Exact, "test")
2020-04-18 20:39:10 +02:00
require.NoError(t, err)
recordsJSON, err := json.Marshal(records)
require.NoError(t, err)
2021-08-25 11:44:11 +02:00
expectedContent, err := os.ReadFile("./fixtures/records-Search.json")
2020-04-18 20:39:10 +02:00
require.NoError(t, err)
assert.JSONEq(t, string(expectedContent), string(recordsJSON))
}