2016-10-18 08:20:15 +00:00
|
|
|
package ns1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-21 15:47:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-10-18 08:20:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
liveTest bool
|
|
|
|
apiKey string
|
|
|
|
domain string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
apiKey = os.Getenv("NS1_API_KEY")
|
|
|
|
domain = os.Getenv("NS1_DOMAIN")
|
|
|
|
if len(apiKey) > 0 && len(domain) > 0 {
|
|
|
|
liveTest = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
func restoreEnv() {
|
2016-10-18 08:20:15 +00:00
|
|
|
os.Setenv("NS1_API_KEY", apiKey)
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
Error: "dns: domain must be fully qualified",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "invalid authority",
|
|
|
|
fqdn: "_acme-challenge.myhost.sub.domain.tld.",
|
|
|
|
expected: expected{
|
|
|
|
Error: "could not find the start of authority",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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 TestNewDNSProviderValid(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2016-10-18 08:20:15 +00:00
|
|
|
os.Setenv("NS1_API_KEY", "")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = "123"
|
|
|
|
|
|
|
|
_, err := NewDNSProviderConfig(config)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderMissingCredErr(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2016-10-18 08:20:15 +00:00
|
|
|
os.Setenv("NS1_API_KEY", "")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-10-18 08:20:15 +00:00
|
|
|
_, err := NewDNSProvider()
|
2018-09-15 17:07:24 +00:00
|
|
|
assert.EqualError(t, err, "ns1: some credentials information are missing: NS1_API_KEY")
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLivePresent(t *testing.T) {
|
|
|
|
if !liveTest {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = apiKey
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-18 08:20:15 +00:00
|
|
|
|
|
|
|
err = provider.Present(domain, "", "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) {
|
|
|
|
if !liveTest {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 1)
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = apiKey
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-18 08:20:15 +00:00
|
|
|
|
|
|
|
err = provider.CleanUp(domain, "", "123d==")
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|