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

231 lines
5.1 KiB
Go
Raw Normal View History

2019-03-11 18:56:48 +02:00
package iij
2018-09-08 12:52:36 +02:00
import (
"testing"
2020-09-02 03:20:01 +02:00
"github.com/go-acme/lego/v4/platform/tester"
2018-09-08 12:52:36 +02:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const envDomain = envNamespace + "TESTDOMAIN"
var envTest = tester.NewEnvTest(
EnvAPIAccessKey,
EnvAPISecretKey,
EnvDoServiceCode).
WithDomain(envDomain)
func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
desc string
envVars map[string]string
expected string
}{
{
desc: "success",
envVars: map[string]string{
EnvAPIAccessKey: "A",
EnvAPISecretKey: "B",
EnvDoServiceCode: "C",
},
},
{
desc: "missing credentials",
envVars: map[string]string{
EnvAPIAccessKey: "",
EnvAPISecretKey: "",
EnvDoServiceCode: "",
},
expected: "iij: some credentials information are missing: IIJ_API_ACCESS_KEY,IIJ_API_SECRET_KEY,IIJ_DO_SERVICE_CODE",
},
{
desc: "missing api access key",
envVars: map[string]string{
EnvAPIAccessKey: "",
EnvAPISecretKey: "B",
EnvDoServiceCode: "C",
},
expected: "iij: some credentials information are missing: IIJ_API_ACCESS_KEY",
},
{
desc: "missing secret key",
envVars: map[string]string{
EnvAPIAccessKey: "A",
EnvAPISecretKey: "",
EnvDoServiceCode: "C",
},
expected: "iij: some credentials information are missing: IIJ_API_SECRET_KEY",
},
{
desc: "missing do service code",
envVars: map[string]string{
EnvAPIAccessKey: "A",
EnvAPISecretKey: "B",
EnvDoServiceCode: "",
},
expected: "iij: some credentials information are missing: IIJ_DO_SERVICE_CODE",
},
}
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 21:16:59 +02:00
if test.expected == "" {
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
require.NotNil(t, p.api)
} else {
require.EqualError(t, err, test.expected)
}
})
}
}
func TestNewDNSProviderConfig(t *testing.T) {
testCases := []struct {
desc string
accessKey string
secretKey string
doServiceCode string
expected string
}{
{
desc: "success",
accessKey: "A",
secretKey: "B",
doServiceCode: "C",
},
{
desc: "missing credentials",
expected: "iij: credentials missing",
},
{
desc: "missing access key",
accessKey: "",
secretKey: "B",
doServiceCode: "C",
expected: "iij: credentials missing",
},
{
desc: "missing secret key",
accessKey: "A",
secretKey: "",
doServiceCode: "C",
expected: "iij: credentials missing",
},
{
desc: "missing do service code",
accessKey: "A",
secretKey: "B",
doServiceCode: "",
expected: "iij: credentials missing",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
config := NewDefaultConfig()
config.AccessKey = test.accessKey
config.SecretKey = test.secretKey
config.DoServiceCode = test.doServiceCode
p, err := NewDNSProviderConfig(config)
2021-03-04 21:16:59 +02:00
if test.expected == "" {
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
require.NotNil(t, p.api)
} else {
require.EqualError(t, err, test.expected)
}
})
}
2018-09-08 12:52:36 +02:00
}
func TestSplitDomain(t *testing.T) {
testCases := []struct {
desc string
domain string
zones []string
expectedOwner string
expectedZone string
}{
{
desc: "domain equals zone",
domain: "domain.com",
zones: []string{"domain.com"},
expectedOwner: "_acme-challenge",
expectedZone: "domain.com",
},
{
desc: "with a subdomain",
2018-09-08 12:52:36 +02:00
domain: "my.domain.com",
zones: []string{"domain.com"},
expectedOwner: "_acme-challenge.my",
expectedZone: "domain.com",
},
{
desc: "with a subdomain in a zone",
2018-09-08 12:52:36 +02:00
domain: "my.sub.domain.com",
zones: []string{"sub.domain.com", "domain.com"},
expectedOwner: "_acme-challenge.my",
expectedZone: "sub.domain.com",
},
{
desc: "with a sub-subdomain",
2018-09-08 12:52:36 +02:00
domain: "my.sub.domain.com",
zones: []string{"domain1.com", "domain.com"},
expectedOwner: "_acme-challenge.my.sub",
expectedZone: "domain.com",
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
owner, zone, err := splitDomain(test.domain, test.zones)
require.NoError(t, err)
assert.Equal(t, test.expectedOwner, owner)
assert.Equal(t, test.expectedZone, zone)
})
}
}
func TestLivePresent(t *testing.T) {
if !envTest.IsLiveTest() {
2018-09-08 12:52:36 +02:00
t.Skip("skipping live test")
}
envTest.RestoreEnv()
2018-09-08 12:52:36 +02:00
provider, err := NewDNSProvider()
require.NoError(t, err)
2018-09-08 12:52:36 +02:00
err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
2018-09-08 12:52:36 +02:00
}
func TestLiveCleanUp(t *testing.T) {
if !envTest.IsLiveTest() {
2018-09-08 12:52:36 +02:00
t.Skip("skipping live test")
}
envTest.RestoreEnv()
2018-09-08 12:52:36 +02:00
provider, err := NewDNSProvider()
require.NoError(t, err)
2018-09-08 12:52:36 +02:00
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
2018-09-08 12:52:36 +02:00
}