1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-23 17:21:50 +02:00
lego/providers/dns/transip/transip_test.go

165 lines
4.0 KiB
Go
Raw Normal View History

2018-11-06 12:56:28 +02:00
package transip
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/xenolf/lego/platform/tester"
)
var envTest = tester.NewEnvTest(
"TRANSIP_ACCOUNT_NAME",
"TRANSIP_PRIVATE_KEY_PATH").
WithDomain("TRANSIP_DOMAIN")
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
desc string
envVars map[string]string
expected string
}{
{
desc: "success",
envVars: map[string]string{
"TRANSIP_ACCOUNT_NAME": "johndoe",
"TRANSIP_PRIVATE_KEY_PATH": "./fixtures/private.key",
},
},
{
desc: "missing all credentials",
envVars: map[string]string{
"TRANSIP_ACCOUNT_NAME": "",
"TRANSIP_PRIVATE_KEY_PATH": "",
},
expected: "transip: some credentials information are missing: TRANSIP_ACCOUNT_NAME,TRANSIP_PRIVATE_KEY_PATH",
},
{
desc: "missing account name",
envVars: map[string]string{
"TRANSIP_ACCOUNT_NAME": "",
"TRANSIP_PRIVATE_KEY_PATH": "./fixtures/private.key",
},
expected: "transip: some credentials information are missing: TRANSIP_ACCOUNT_NAME",
},
{
desc: "missing private key path",
envVars: map[string]string{
"TRANSIP_ACCOUNT_NAME": "johndoe",
"TRANSIP_PRIVATE_KEY_PATH": "",
},
expected: "transip: some credentials information are missing: TRANSIP_PRIVATE_KEY_PATH",
},
{
desc: "could not open private key path",
envVars: map[string]string{
"TRANSIP_ACCOUNT_NAME": "johndoe",
"TRANSIP_PRIVATE_KEY_PATH": "./fixtures/non/existent/private.key",
},
expected: "transip: could not open private key: stat ./fixtures/non/existent/private.key: no such file or directory",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer envTest.RestoreEnv()
envTest.ClearEnv()
envTest.Apply(test.envVars)
p, err := NewDNSProvider()
if len(test.expected) == 0 {
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
require.NotNil(t, p.client)
} else {
require.EqualError(t, err, test.expected)
}
})
}
}
func TestNewDNSProviderConfig(t *testing.T) {
testCases := []struct {
desc string
accountName string
privateKeyPath string
expected string
}{
{
desc: "success",
accountName: "johndoe",
privateKeyPath: "./fixtures/private.key",
},
{
desc: "missing all credentials",
expected: "transip: AccountName is required",
},
{
desc: "missing account name",
privateKeyPath: "./fixtures/private.key",
expected: "transip: AccountName is required",
},
{
desc: "missing private key path",
accountName: "johndoe",
expected: "transip: PrivateKeyPath or PrivateKeyBody is required",
},
{
desc: "could not open private key path",
accountName: "johndoe",
privateKeyPath: "./fixtures/non/existent/private.key",
expected: "transip: could not open private key: stat ./fixtures/non/existent/private.key: no such file or directory",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
config := NewDefaultConfig()
config.AccountName = test.accountName
config.PrivateKeyPath = test.privateKeyPath
p, err := NewDNSProviderConfig(config)
if len(test.expected) == 0 {
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
require.NotNil(t, p.client)
} else {
require.EqualError(t, err, test.expected)
}
})
}
}
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)
}