2016-02-29 02:48:41 +00:00
|
|
|
package cloudflare
|
2015-12-03 20:01:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-02 22:02:01 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-12-03 20:01:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-10-12 17:29:18 +00:00
|
|
|
liveTest bool
|
|
|
|
envTestEmail string
|
|
|
|
envTestAPIKey string
|
|
|
|
envTestDomain string
|
2015-12-03 20:01:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-10-12 17:29:18 +00:00
|
|
|
envTestEmail = os.Getenv("CLOUDFLARE_EMAIL")
|
|
|
|
envTestAPIKey = os.Getenv("CLOUDFLARE_API_KEY")
|
|
|
|
envTestDomain = os.Getenv("CLOUDFLARE_DOMAIN")
|
|
|
|
if len(envTestEmail) > 0 && len(envTestAPIKey) > 0 && len(envTestDomain) > 0 {
|
|
|
|
liveTest = true
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
func restoreEnv() {
|
2018-10-12 17:29:18 +00:00
|
|
|
os.Setenv("CLOUDFLARE_EMAIL", envTestEmail)
|
|
|
|
os.Setenv("CLOUDFLARE_API_KEY", envTestAPIKey)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 22:02:01 +00:00
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"CLOUDFLARE_EMAIL": "test@example.com",
|
|
|
|
"CLOUDFLARE_API_KEY": "123",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"CLOUDFLARE_EMAIL": "",
|
|
|
|
"CLOUDFLARE_API_KEY": "",
|
|
|
|
},
|
|
|
|
expected: "cloudflare: some credentials information are missing: CLOUDFLARE_EMAIL,CLOUDFLARE_API_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing email",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"CLOUDFLARE_EMAIL": "",
|
|
|
|
"CLOUDFLARE_API_KEY": "key",
|
|
|
|
},
|
|
|
|
expected: "cloudflare: some credentials information are missing: CLOUDFLARE_EMAIL",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api key",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"CLOUDFLARE_EMAIL": "awesome@possum.com",
|
|
|
|
"CLOUDFLARE_API_KEY": "",
|
|
|
|
},
|
|
|
|
expected: "cloudflare: some credentials information are missing: CLOUDFLARE_API_KEY",
|
|
|
|
},
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-10-02 22:02:01 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
defer restoreEnv()
|
|
|
|
for key, value := range test.envVars {
|
|
|
|
if len(value) == 0 {
|
|
|
|
os.Unsetenv(key)
|
|
|
|
} else {
|
|
|
|
os.Setenv(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
|
|
|
if len(test.expected) == 0 {
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
assert.NotNil(t, p.config)
|
|
|
|
assert.NotNil(t, p.client)
|
2018-10-02 22:02:01 +00:00
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 22:02:01 +00:00
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
authEmail string
|
|
|
|
authKey string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
authEmail: "test@example.com",
|
|
|
|
authKey: "123",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "invalid credentials: key & email must not be empty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing email",
|
|
|
|
authKey: "123",
|
|
|
|
expected: "invalid credentials: key & email must not be empty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api key",
|
|
|
|
authEmail: "test@example.com",
|
|
|
|
expected: "invalid credentials: key & email must not be empty",
|
|
|
|
},
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-10-02 22:02:01 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
defer restoreEnv()
|
|
|
|
os.Unsetenv("CLOUDFLARE_EMAIL")
|
|
|
|
os.Unsetenv("CLOUDFLARE_API_KEY")
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.AuthEmail = test.authEmail
|
|
|
|
config.AuthKey = test.authKey
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
|
|
|
if len(test.expected) == 0 {
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
assert.NotNil(t, p.config)
|
|
|
|
assert.NotNil(t, p.client)
|
2018-10-02 22:02:01 +00:00
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-04-25 15:12:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestPresent(t *testing.T) {
|
|
|
|
if !liveTest {
|
2015-12-03 20:01:46 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2018-10-12 17:29:18 +00:00
|
|
|
config.AuthEmail = envTestEmail
|
|
|
|
config.AuthKey = envTestAPIKey
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-10-02 22:02:01 +00:00
|
|
|
require.NoError(t, err)
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.Present(envTestDomain, "", "123d==")
|
2018-10-02 22:02:01 +00:00
|
|
|
require.NoError(t, err)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestCleanUp(t *testing.T) {
|
|
|
|
if !liveTest {
|
2015-12-03 20:01:46 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2016-02-16 14:50:24 +00:00
|
|
|
time.Sleep(time.Second * 2)
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2018-10-12 17:29:18 +00:00
|
|
|
config.AuthEmail = envTestEmail
|
|
|
|
config.AuthKey = envTestAPIKey
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2018-10-02 22:02:01 +00:00
|
|
|
require.NoError(t, err)
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err = provider.CleanUp(envTestDomain, "", "123d==")
|
2018-10-02 22:02:01 +00:00
|
|
|
require.NoError(t, err)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|