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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cflareLiveTest bool
|
|
|
|
cflareEmail string
|
|
|
|
cflareAPIKey string
|
|
|
|
cflareDomain string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cflareEmail = os.Getenv("CLOUDFLARE_EMAIL")
|
|
|
|
cflareAPIKey = os.Getenv("CLOUDFLARE_API_KEY")
|
|
|
|
cflareDomain = os.Getenv("CLOUDFLARE_DOMAIN")
|
|
|
|
if len(cflareEmail) > 0 && len(cflareAPIKey) > 0 && len(cflareDomain) > 0 {
|
|
|
|
cflareLiveTest = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
func restoreEnv() {
|
2015-12-03 20:01:46 +00:00
|
|
|
os.Setenv("CLOUDFLARE_EMAIL", cflareEmail)
|
|
|
|
os.Setenv("CLOUDFLARE_API_KEY", cflareAPIKey)
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
func TestNewDNSProviderValid(t *testing.T) {
|
2015-12-03 20:01:46 +00:00
|
|
|
os.Setenv("CLOUDFLARE_EMAIL", "")
|
|
|
|
os.Setenv("CLOUDFLARE_API_KEY", "")
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
_, err := NewDNSProviderCredentials("123", "123")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
func TestNewDNSProviderValidEnv(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2015-12-03 20:01:46 +00:00
|
|
|
os.Setenv("CLOUDFLARE_EMAIL", "test@example.com")
|
|
|
|
os.Setenv("CLOUDFLARE_API_KEY", "123")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
_, err := NewDNSProvider()
|
2015-12-03 20:01:46 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
func TestNewDNSProviderMissingCredErr(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2015-12-03 20:01:46 +00:00
|
|
|
os.Setenv("CLOUDFLARE_EMAIL", "")
|
|
|
|
os.Setenv("CLOUDFLARE_API_KEY", "")
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
_, err := NewDNSProvider()
|
2018-06-11 15:32:50 +00:00
|
|
|
assert.EqualError(t, err, "CloudFlare: some credentials information are missing: CLOUDFLARE_EMAIL,CLOUDFLARE_API_KEY")
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
func TestNewDNSProviderMissingCredErrSingle(t *testing.T) {
|
|
|
|
defer restoreEnv()
|
|
|
|
os.Setenv("CLOUDFLARE_EMAIL", "awesome@possum.com")
|
|
|
|
|
|
|
|
_, err := NewDNSProvider()
|
|
|
|
assert.EqualError(t, err, "CloudFlare: some credentials information are missing: CLOUDFLARE_API_KEY")
|
2018-04-25 15:12:41 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
func TestCloudFlarePresent(t *testing.T) {
|
2015-12-03 20:01:46 +00:00
|
|
|
if !cflareLiveTest {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
provider, err := NewDNSProviderCredentials(cflareEmail, cflareAPIKey)
|
2015-12-03 20:01:46 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
err = provider.Present(cflareDomain, "", "123d==")
|
2015-12-03 20:01:46 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
func TestCloudFlareCleanUp(t *testing.T) {
|
2015-12-03 20:01:46 +00:00
|
|
|
if !cflareLiveTest {
|
|
|
|
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
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
provider, err := NewDNSProviderCredentials(cflareEmail, cflareAPIKey)
|
2015-12-03 20:01:46 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
err = provider.CleanUp(cflareDomain, "", "123d==")
|
2015-12-03 20:01:46 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|