lego/providers/dns/stackpath/stackpath_test.go

161 lines
3.4 KiB
Go
Raw Permalink Normal View History

2019-03-11 16:56:48 +00:00
package stackpath
2018-10-09 19:58:32 +00:00
import (
"testing"
"time"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/platform/tester"
2018-10-09 19:58:32 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const envDomain = envNamespace + "DOMAIN"
var envTest = tester.NewEnvTest(
EnvClientID,
EnvClientSecret,
EnvStackID).
WithDomain(envDomain)
2018-10-09 19:58:32 +00:00
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
desc string
envVars map[string]string
expected string
}{
{
desc: "success",
envVars: map[string]string{
EnvClientID: "test@example.com",
EnvClientSecret: "123",
EnvStackID: "ID",
2018-10-09 19:58:32 +00:00
},
},
{
desc: "missing credentials",
envVars: map[string]string{
EnvClientID: "",
EnvClientSecret: "",
EnvStackID: "",
2018-10-09 19:58:32 +00:00
},
expected: "stackpath: some credentials information are missing: STACKPATH_CLIENT_ID,STACKPATH_CLIENT_SECRET,STACKPATH_STACK_ID",
},
{
desc: "missing client id",
envVars: map[string]string{
EnvClientID: "",
EnvClientSecret: "123",
EnvStackID: "ID",
2018-10-09 19:58:32 +00:00
},
expected: "stackpath: some credentials information are missing: STACKPATH_CLIENT_ID",
},
{
desc: "missing client secret",
envVars: map[string]string{
EnvClientID: "test@example.com",
EnvClientSecret: "",
EnvStackID: "ID",
2018-10-09 19:58:32 +00:00
},
expected: "stackpath: some credentials information are missing: STACKPATH_CLIENT_SECRET",
},
{
desc: "missing stack id",
envVars: map[string]string{
EnvClientID: "test@example.com",
EnvClientSecret: "123",
EnvStackID: "",
2018-10-09 19:58:32 +00:00
},
expected: "stackpath: some credentials information are missing: STACKPATH_STACK_ID",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
2018-10-09 19:58:32 +00:00
p, err := NewDNSProvider()
2021-03-04 19:16:59 +00:00
if test.expected == "" {
require.NoError(t, err)
2018-10-09 19:58:32 +00:00
assert.NotNil(t, p)
} else {
require.EqualError(t, err, test.expected)
}
})
}
}
func TestNewDNSProviderConfig(t *testing.T) {
testCases := map[string]struct {
config *Config
expectedErr string
}{
"no_config": {
config: nil,
expectedErr: "stackpath: the configuration of the DNS provider is nil",
},
"no_client_id": {
config: &Config{
ClientSecret: "secret",
StackID: "stackID",
},
expectedErr: "stackpath: credentials missing",
},
"no_client_secret": {
config: &Config{
ClientID: "clientID",
StackID: "stackID",
},
expectedErr: "stackpath: credentials missing",
},
"no_stack_id": {
config: &Config{
ClientID: "clientID",
ClientSecret: "secret",
},
expectedErr: "stackpath: stack id missing",
},
}
for desc, test := range testCases {
t.Run(desc, func(t *testing.T) {
t.Parallel()
p, err := NewDNSProviderConfig(test.config)
require.EqualError(t, err, test.expectedErr)
assert.Nil(t, p)
})
}
}
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)
time.Sleep(1 * time.Second)
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}