2019-03-11 16:56:48 +00:00
|
|
|
package iij
|
2018-09-08 10:52:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2018-09-08 10:52:36 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
const envDomain = envNamespace + "TESTDOMAIN"
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
var envTest = tester.NewEnvTest(
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIAccessKey,
|
|
|
|
EnvAPISecretKey,
|
|
|
|
EnvDoServiceCode).
|
|
|
|
WithDomain(envDomain)
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIAccessKey: "A",
|
|
|
|
EnvAPISecretKey: "B",
|
|
|
|
EnvDoServiceCode: "C",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIAccessKey: "",
|
|
|
|
EnvAPISecretKey: "",
|
|
|
|
EnvDoServiceCode: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
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{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIAccessKey: "",
|
|
|
|
EnvAPISecretKey: "B",
|
|
|
|
EnvDoServiceCode: "C",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
expected: "iij: some credentials information are missing: IIJ_API_ACCESS_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing secret key",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIAccessKey: "A",
|
|
|
|
EnvAPISecretKey: "",
|
|
|
|
EnvDoServiceCode: "C",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
expected: "iij: some credentials information are missing: IIJ_API_SECRET_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing do service code",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIAccessKey: "A",
|
|
|
|
EnvAPISecretKey: "B",
|
|
|
|
EnvDoServiceCode: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
expected: "iij: some credentials information are missing: IIJ_DO_SERVICE_CODE",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.envVars)
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2018-10-12 17:29:18 +00:00
|
|
|
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 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2018-10-12 17:29:18 +00:00
|
|
|
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 10:52:36 +00: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 sub domain",
|
|
|
|
domain: "my.domain.com",
|
|
|
|
zones: []string{"domain.com"},
|
|
|
|
expectedOwner: "_acme-challenge.my",
|
|
|
|
expectedZone: "domain.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with a sub domain in a zone",
|
|
|
|
domain: "my.sub.domain.com",
|
|
|
|
zones: []string{"sub.domain.com", "domain.com"},
|
|
|
|
expectedOwner: "_acme-challenge.my",
|
|
|
|
expectedZone: "sub.domain.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "with a sub sub domain",
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestLivePresent(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-09-08 10:52:36 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2018-09-08 10:52:36 +00:00
|
|
|
provider, err := NewDNSProvider()
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-08 10:52:36 +00:00
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-08 10:52:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-09-08 10:52:36 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2018-09-08 10:52:36 +00:00
|
|
|
provider, err := NewDNSProvider()
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-08 10:52:36 +00:00
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-08 10:52:36 +00:00
|
|
|
}
|