lego/providers/dns/regru/internal/client_test.go

133 lines
3.5 KiB
Go
Raw Permalink Normal View History

2020-03-10 10:08:32 +00:00
package internal
import (
2023-05-05 07:49:38 +00:00
"context"
2023-01-03 01:45:25 +00:00
"net/http"
2023-05-05 07:49:38 +00:00
"net/url"
2023-01-03 01:45:25 +00:00
"os"
2020-03-10 10:08:32 +00:00
"testing"
2023-01-03 01:45:25 +00:00
"time"
2020-03-10 10:08:32 +00:00
"github.com/stretchr/testify/require"
)
const (
noopBaseURL = "https://api.reg.ru/api/regru2/nop"
officialTestUser = "test"
officialTestPassword = "test"
)
func TestRemoveRecord(t *testing.T) {
2022-11-10 01:23:04 +00:00
// TODO(ldez): remove skip when the reg.ru API will be fixed.
t.Skip("there is a bug with the reg.ru API: INTERNAL_API_ERROR: Внутренняя ошибка, status code: 503")
2020-03-10 10:08:32 +00:00
client := NewClient(officialTestUser, officialTestPassword)
2023-01-03 01:45:25 +00:00
client.HTTPClient = &http.Client{Timeout: 30 * time.Second}
2020-03-10 10:08:32 +00:00
2023-05-05 07:49:38 +00:00
err := client.RemoveTxtRecord(context.Background(), "test.ru", "_acme-challenge", "txttxttxt")
2020-03-10 10:08:32 +00:00
require.NoError(t, err)
}
func TestRemoveRecord_errors(t *testing.T) {
2023-01-03 01:45:25 +00:00
// TODO(ldez): remove skip when the reg.ru API will be fixed.
if os.Getenv("CI") == "true" {
t.Skip("there is a bug with the reg.ru and GitHub action: dial tcp 194.58.116.30:443: i/o timeout")
}
2020-03-10 10:08:32 +00:00
testCases := []struct {
desc string
domain string
username string
password string
baseURL string
expected string
}{
{
desc: "authentication failed",
domain: "test.ru",
username: "",
password: "",
baseURL: noopBaseURL,
expected: "API error: NO_AUTH: No authorization mechanism selected",
},
{
desc: "domain error",
domain: "",
username: officialTestUser,
password: officialTestPassword,
baseURL: defaultBaseURL,
expected: "API error: NO_DOMAIN: domain_name not given or empty",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
client := NewClient(test.username, test.username)
2023-01-03 01:45:25 +00:00
client.HTTPClient = &http.Client{Timeout: 30 * time.Second}
2023-05-05 07:49:38 +00:00
client.baseURL, _ = url.Parse(test.baseURL)
2020-03-10 10:08:32 +00:00
2023-05-05 07:49:38 +00:00
err := client.RemoveTxtRecord(context.Background(), test.domain, "_acme-challenge", "txttxttxt")
2020-03-10 10:08:32 +00:00
require.EqualError(t, err, test.expected)
})
}
}
func TestAddTXTRecord(t *testing.T) {
2022-11-10 01:23:04 +00:00
// TODO(ldez): remove skip when the reg.ru API will be fixed.
t.Skip("there is a bug with the reg.ru API: INTERNAL_API_ERROR: Внутренняя ошибка, status code: 503")
2020-03-10 10:08:32 +00:00
client := NewClient(officialTestUser, officialTestPassword)
2023-01-03 01:45:25 +00:00
client.HTTPClient = &http.Client{Timeout: 30 * time.Second}
2020-03-10 10:08:32 +00:00
2023-05-05 07:49:38 +00:00
err := client.AddTXTRecord(context.Background(), "test.ru", "_acme-challenge", "txttxttxt")
2020-03-10 10:08:32 +00:00
require.NoError(t, err)
}
func TestAddTXTRecord_errors(t *testing.T) {
2023-01-03 01:45:25 +00:00
// TODO(ldez): remove skip when the reg.ru API will be fixed.
if os.Getenv("CI") == "true" {
t.Skip("there is a bug with the reg.ru and GitHub action: dial tcp 194.58.116.30:443: i/o timeout")
}
2020-03-10 10:08:32 +00:00
testCases := []struct {
desc string
domain string
username string
password string
baseURL string
expected string
}{
{
desc: "authentication failed",
domain: "test.ru",
username: "",
password: "",
baseURL: noopBaseURL,
expected: "API error: NO_AUTH: No authorization mechanism selected",
},
{
desc: "domain error",
domain: "",
username: officialTestUser,
password: officialTestPassword,
baseURL: defaultBaseURL,
expected: "API error: NO_DOMAIN: domain_name not given or empty",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
client := NewClient(test.username, test.username)
2023-01-03 01:45:25 +00:00
client.HTTPClient = &http.Client{Timeout: 30 * time.Second}
2023-05-05 07:49:38 +00:00
client.baseURL, _ = url.Parse(test.baseURL)
2020-03-10 10:08:32 +00:00
2023-05-05 07:49:38 +00:00
err := client.AddTXTRecord(context.Background(), test.domain, "_acme-challenge", "txttxttxt")
2020-03-10 10:08:32 +00:00
require.EqualError(t, err, test.expected)
})
}
}