lego/providers/dns/versio/versio_test.go

319 lines
7.1 KiB
Go
Raw Normal View History

2019-06-27 17:30:10 +00:00
package versio
import (
"fmt"
"io"
2019-06-27 17:30:10 +00:00
"net/http"
"net/http/httptest"
"testing"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/log"
"github.com/go-acme/lego/v4/platform/tester"
2019-06-27 17:30:10 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testDomain = "example.com"
const envDomain = envNamespace + "DOMAIN"
var envTest = tester.NewEnvTest(EnvUsername, EnvPassword, EnvEndpoint).WithDomain(envDomain)
2019-06-27 17:30:10 +00:00
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
desc string
envVars map[string]string
expected string
}{
{
desc: "success",
envVars: map[string]string{
EnvUsername: "me@example.com",
EnvPassword: "SECRET",
2019-06-27 17:30:10 +00:00
},
},
{
desc: "missing token",
envVars: map[string]string{
EnvPassword: "me@example.com",
2019-06-27 17:30:10 +00:00
},
expected: "versio: some credentials information are missing: VERSIO_USERNAME",
},
{
desc: "missing key",
envVars: map[string]string{
EnvUsername: "TOKEN",
2019-06-27 17:30:10 +00:00
},
expected: "versio: some credentials information are missing: VERSIO_PASSWORD",
},
{
desc: "missing credentials",
envVars: map[string]string{},
expected: "versio: some credentials information are missing: VERSIO_USERNAME,VERSIO_PASSWORD",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
2021-03-04 19:16:59 +00:00
if test.expected == "" {
2019-06-27 17:30:10 +00:00
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
} else {
require.EqualError(t, err, test.expected)
}
})
}
}
func TestNewDNSProviderConfig(t *testing.T) {
testCases := []struct {
desc string
config *Config
expected string
}{
{
desc: "success",
config: &Config{
Username: "me@example.com",
Password: "PW",
},
},
{
desc: "nil config",
config: nil,
expected: "versio: the configuration of the DNS provider is nil",
},
{
desc: "missing username",
config: &Config{
Password: "PW",
},
expected: "versio: the versio username is missing",
},
{
desc: "missing password",
config: &Config{
Username: "UN",
},
expected: "versio: the versio password is missing",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
p, err := NewDNSProviderConfig(test.config)
2021-03-04 19:16:59 +00:00
if test.expected == "" {
2019-06-27 17:30:10 +00:00
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
} else {
require.EqualError(t, err, test.expected)
}
})
}
}
func TestDNSProvider_Present(t *testing.T) {
testCases := []struct {
desc string
handler http.Handler
expectedError string
}{
{
desc: "Success",
handler: muxSuccess(),
},
{
desc: "FailToFindZone",
handler: muxFailToFindZone(),
2023-05-05 07:49:38 +00:00
expectedError: `versio: [status code: 401] 401: ObjectDoesNotExist|Domain not found`,
2019-06-27 17:30:10 +00:00
},
{
desc: "FailToCreateTXT",
handler: muxFailToCreateTXT(),
2023-05-05 07:49:38 +00:00
expectedError: `versio: [status code: 400] 400: ProcessError|DNS record invalid type _acme-challenge.example.eu. TST`,
2019-06-27 17:30:10 +00:00
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer envTest.RestoreEnv()
envTest.ClearEnv()
baseURL := setupTest(t, test.handler)
2019-06-27 17:30:10 +00:00
envTest.Apply(map[string]string{
EnvUsername: "me@example.com",
EnvPassword: "secret",
EnvEndpoint: baseURL,
2019-06-27 17:30:10 +00:00
})
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(testDomain, "token", "keyAuth")
2021-03-04 19:16:59 +00:00
if test.expectedError == "" {
2019-06-27 17:30:10 +00:00
require.NoError(t, err)
} else {
assert.EqualError(t, err, test.expectedError)
}
})
}
}
func TestDNSProvider_CleanUp(t *testing.T) {
testCases := []struct {
desc string
handler http.Handler
expectedError string
}{
{
desc: "Success",
handler: muxSuccess(),
},
{
desc: "FailToFindZone",
handler: muxFailToFindZone(),
2023-05-05 07:49:38 +00:00
expectedError: `versio: [status code: 401] 401: ObjectDoesNotExist|Domain not found`,
2019-06-27 17:30:10 +00:00
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer envTest.RestoreEnv()
envTest.ClearEnv()
baseURL := setupTest(t, test.handler)
2019-06-27 17:30:10 +00:00
envTest.Apply(map[string]string{
EnvUsername: "me@example.com",
EnvPassword: "secret",
EnvEndpoint: baseURL,
2019-06-27 17:30:10 +00:00
})
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.CleanUp(testDomain, "token", "keyAuth")
2021-03-04 19:16:59 +00:00
if test.expectedError == "" {
2019-06-27 17:30:10 +00:00
require.NoError(t, err)
} else {
require.EqualError(t, err, test.expectedError)
}
})
}
}
func muxSuccess() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/domains/example.com", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && r.URL.Query().Get("show_dns_records") == "true" {
fmt.Fprint(w, tokenResponseMock)
return
}
w.WriteHeader(http.StatusBadRequest)
})
mux.HandleFunc("/domains/example.com/update", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
fmt.Fprint(w, tokenResponseMock)
return
}
w.WriteHeader(http.StatusBadRequest)
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Printf("unexpected request: %+v\n\n", r)
data, _ := io.ReadAll(r.Body)
defer func() { _ = r.Body.Close() }()
log.Println(string(data))
2019-06-27 17:30:10 +00:00
http.NotFound(w, r)
})
return mux
}
func muxFailToFindZone() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/domains/example.com", func(w http.ResponseWriter, _ *http.Request) {
http.Error(w, tokenFailToFindZoneMock, http.StatusUnauthorized)
})
return mux
}
func muxFailToCreateTXT() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/domains/example.com", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && r.URL.Query().Get("show_dns_records") == "true" {
fmt.Fprint(w, tokenResponseMock)
return
}
w.WriteHeader(http.StatusBadRequest)
})
mux.HandleFunc("/domains/example.com/update", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
http.Error(w, tokenFailToCreateTXTMock, http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusBadRequest)
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Printf("unexpected request: %+v\n\n", r)
data, _ := io.ReadAll(r.Body)
defer func() { _ = r.Body.Close() }()
log.Println(string(data))
http.NotFound(w, r)
})
2019-06-27 17:30:10 +00:00
return mux
}
func setupTest(t *testing.T, handler http.Handler) string {
t.Helper()
server := httptest.NewServer(handler)
t.Cleanup(server.Close)
return server.URL
2019-06-27 17:30:10 +00:00
}
func TestLivePresent(t *testing.T) {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
func TestLiveCleanUp(t *testing.T) {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}
envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}