2019-03-11 15:54:35 +00:00
|
|
|
package ns1 // import "github.com/xenolf/lego/providers/dns/ns1"
|
2016-10-18 08:20:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-21 15:47:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-16 15:52:57 +00:00
|
|
|
"github.com/xenolf/lego/platform/tester"
|
2016-10-18 08:20:15 +00:00
|
|
|
)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
var envTest = tester.NewEnvTest("NS1_API_KEY").
|
|
|
|
WithDomain("NS1_DOMAIN")
|
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{
|
|
|
|
"NS1_API_KEY": "123",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api key",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"NS1_API_KEY": "",
|
|
|
|
},
|
|
|
|
expected: "ns1: some credentials information are missing: NS1_API_KEY",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
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
|
|
|
|
apiKey string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
apiKey: "123",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "ns1: credentials missing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = test.apiKey
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 15:47:58 +00:00
|
|
|
func Test_getAuthZone(t *testing.T) {
|
|
|
|
type expected struct {
|
|
|
|
AuthZone string
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
fqdn string
|
|
|
|
expected expected
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "valid fqdn",
|
|
|
|
fqdn: "_acme-challenge.myhost.sub.example.com.",
|
|
|
|
expected: expected{
|
|
|
|
AuthZone: "example.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "invalid fqdn",
|
|
|
|
fqdn: "_acme-challenge.myhost.sub.example.com",
|
|
|
|
expected: expected{
|
2018-12-06 21:50:17 +00:00
|
|
|
Error: "could not find the start of authority for _acme-challenge.myhost.sub.example.com: dns: domain must be fully qualified",
|
2018-09-21 15:47:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "invalid authority",
|
|
|
|
fqdn: "_acme-challenge.myhost.sub.domain.tld.",
|
|
|
|
expected: expected{
|
2018-12-06 21:50:17 +00:00
|
|
|
Error: "could not find the start of authority for _acme-challenge.myhost.sub.domain.tld.: NXDOMAIN",
|
2018-09-21 15:47:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
authZone, err := getAuthZone(test.fqdn)
|
|
|
|
|
|
|
|
if len(test.expected.Error) > 0 {
|
|
|
|
assert.EqualError(t, err, test.expected.Error)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, test.expected.AuthZone, authZone)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-18 08:20:15 +00:00
|
|
|
func TestLivePresent(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2016-10-18 08:20:15 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2018-10-12 17:29:18 +00:00
|
|
|
provider, err := NewDNSProvider()
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-18 08:20:15 +00:00
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2016-10-18 08:20:15 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2018-10-12 17:29:18 +00:00
|
|
|
provider, err := NewDNSProvider()
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-18 08:20:15 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|