1
0
mirror of https://github.com/go-acme/lego.git synced 2025-12-05 02:04:20 +02:00
Files

198 lines
4.5 KiB
Go
Raw Permalink Normal View History

2019-03-11 17:56:48 +01:00
package internal
2018-09-08 14:08:07 +02:00
import (
"net/http"
"net/http/httptest"
2018-09-08 14:08:07 +02:00
"testing"
2025-07-12 13:57:15 +02:00
"github.com/go-acme/lego/v4/platform/tester/servermock"
2018-09-08 14:08:07 +02:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2018-09-08 14:08:07 +02:00
)
2025-07-12 13:57:15 +02:00
func mockBuilder() *servermock.Builder[*Client] {
return servermock.NewBuilder[*Client](
func(server *httptest.Server) (*Client, error) {
client, err := NewClient("a", "b", "c")
if err != nil {
return nil, err
}
2025-07-12 13:57:15 +02:00
client.baseURL = server.URL
client.HTTPClient = server.Client()
2025-07-12 13:57:15 +02:00
return client, nil
},
servermock.CheckHeader().WithJSONHeaders(),
)
}
func TestGetDNSRecordIdx(t *testing.T) {
records := []DNSRecord{
{
ID: 12345,
Hostname: "asdf",
RecordType: "TXT",
Priority: "0",
Destination: "randomtext",
DeleteRecord: false,
State: "yes",
},
{
ID: 23456,
Hostname: "@",
RecordType: "A",
Priority: "0",
Destination: "127.0.0.1",
DeleteRecord: false,
State: "yes",
},
{
ID: 34567,
Hostname: "dfgh",
RecordType: "CNAME",
Priority: "0",
Destination: "example.com",
DeleteRecord: false,
State: "yes",
},
{
ID: 45678,
Hostname: "fghj",
RecordType: "MX",
Priority: "10",
Destination: "mail.example.com",
DeleteRecord: false,
State: "yes",
},
}
testCases := []struct {
desc string
record DNSRecord
expectError bool
}{
{
desc: "simple",
record: DNSRecord{
ID: 12345,
Hostname: "asdf",
RecordType: "TXT",
Priority: "0",
Destination: "randomtext",
DeleteRecord: false,
State: "yes",
},
},
{
desc: "wrong Destination",
record: DNSRecord{
ID: 12345,
Hostname: "asdf",
RecordType: "TXT",
Priority: "0",
Destination: "wrong",
DeleteRecord: false,
State: "yes",
},
expectError: true,
},
{
desc: "record type CNAME",
record: DNSRecord{
ID: 12345,
Hostname: "asdf",
RecordType: "CNAME",
Priority: "0",
Destination: "randomtext",
DeleteRecord: false,
State: "yes",
},
expectError: true,
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
idx, err := GetDNSRecordIdx(records, test.record)
if test.expectError {
assert.Error(t, err)
assert.Equal(t, -1, idx)
} else {
assert.NoError(t, err)
assert.Equal(t, records[idx], test.record)
}
})
}
}
func TestClient_GetDNSRecords(t *testing.T) {
2025-07-12 13:57:15 +02:00
client := mockBuilder().
Route("POST /", servermock.ResponseFromFixture("get_dns_records.json"),
2025-07-15 21:09:01 +02:00
servermock.CheckRequestJSONBodyFromFixture("get_dns_records-request.json")).
2025-07-12 13:57:15 +02:00
Build(t)
expected := []DNSRecord{{
ID: 1,
Hostname: "example.com",
RecordType: "TXT",
Priority: "1",
Destination: "bGVnbzE=",
DeleteRecord: false,
State: "yes",
}, {
ID: 2,
Hostname: "example2.com",
RecordType: "TXT",
Priority: "1",
Destination: "bGVnbw==",
DeleteRecord: false,
State: "yes",
}}
2025-07-08 17:23:52 +02:00
records, err := client.GetDNSRecords(t.Context(), "example.com")
require.NoError(t, err)
assert.Equal(t, expected, records)
}
func TestClient_GetDNSRecords_errors(t *testing.T) {
testCases := []struct {
2025-07-12 13:57:15 +02:00
desc string
handler http.Handler
expected string
}{
{
2025-07-12 13:57:15 +02:00
desc: "HTTP error",
handler: servermock.Noop().WithStatusCode(http.StatusInternalServerError),
expected: `error when sending the request: unexpected status code: [status code: 500] body: `,
},
{
2025-07-12 13:57:15 +02:00
desc: "API error",
handler: servermock.ResponseFromFixture("get_dns_records_error.json"),
expected: `error when sending the request: an error occurred during the action infoDnsRecords: [Status=error, StatusCode=4013, ShortMessage=Validation Error., LongMessage=Message is empty.]`,
},
{
2025-07-12 13:57:15 +02:00
desc: "responsedata marshaling error",
handler: servermock.ResponseFromFixture("get_dns_records_error_unmarshal.json"),
expected: `error when sending the request: unable to unmarshal response: [status code: 200] body: "" error: json: cannot unmarshal string into Go value of type internal.InfoDNSRecordsResponse`,
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
2025-07-12 13:57:15 +02:00
client := mockBuilder().
Route("POST /", test.handler).
Build(t)
2025-07-08 17:23:52 +02:00
records, err := client.GetDNSRecords(t.Context(), "example.com")
2025-07-12 13:57:15 +02:00
require.EqualError(t, err, test.expected)
assert.Empty(t, records)
})
}
}