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"
|
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
const envDomain = envNamespace + "TESTDOMAIN"
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
var envTest = tester.NewEnvTest(
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAPIAccessKey,
|
|
|
|
EnvAPISecretKey,
|
|
|
|
EnvDoServiceCode).
|
|
|
|
WithDomain(envDomain)
|
2018-10-12 19:29:18 +02:00
|
|
|
|
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAPIAccessKey: "A",
|
|
|
|
EnvAPISecretKey: "B",
|
|
|
|
EnvDoServiceCode: "C",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAPIAccessKey: "",
|
|
|
|
EnvAPISecretKey: "",
|
|
|
|
EnvDoServiceCode: "",
|
2018-10-12 19:29:18 +02: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-12 00:51:10 +02:00
|
|
|
EnvAPIAccessKey: "",
|
|
|
|
EnvAPISecretKey: "B",
|
|
|
|
EnvDoServiceCode: "C",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
expected: "iij: some credentials information are missing: IIJ_API_ACCESS_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing secret key",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAPIAccessKey: "A",
|
|
|
|
EnvAPISecretKey: "",
|
|
|
|
EnvDoServiceCode: "C",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
expected: "iij: some credentials information are missing: IIJ_API_SECRET_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing do service code",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAPIAccessKey: "A",
|
|
|
|
EnvAPISecretKey: "B",
|
|
|
|
EnvDoServiceCode: "",
|
2018-10-12 19:29:18 +02: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 17:52:57 +02:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.envVars)
|
2018-10-12 19:29:18 +02:00
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
2021-03-04 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2018-10-12 19:29:18 +02: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 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2018-10-12 19:29:18 +02: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 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",
|
|
|
|
},
|
|
|
|
{
|
2022-11-27 21:04:28 +02:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
{
|
2022-11-27 21:04:28 +02:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
{
|
2022-11-27 21:04:28 +02:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestLivePresent(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-09-08 12:52:36 +02:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
envTest.RestoreEnv()
|
2018-09-08 12:52:36 +02:00
|
|
|
provider, err := NewDNSProvider()
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err)
|
2018-09-08 12:52:36 +02:00
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err)
|
2018-09-08 12:52:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-09-08 12:52:36 +02:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
envTest.RestoreEnv()
|
2018-09-08 12:52:36 +02:00
|
|
|
provider, err := NewDNSProvider()
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err)
|
2018-09-08 12:52:36 +02:00
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err)
|
2018-09-08 12:52:36 +02:00
|
|
|
}
|