2019-03-11 16:56:48 +00:00
package cloudflare
2015-12-03 20:01:46 +00:00
import (
"testing"
"time"
2019-07-30 19:19:32 +00:00
"github.com/go-acme/lego/v3/platform/tester"
2015-12-03 20:01:46 +00:00
"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
)
2018-10-16 15:52:57 +00:00
var envTest = tester . NewEnvTest (
"CLOUDFLARE_EMAIL" ,
2019-09-01 12:32:20 +00:00
"CLOUDFLARE_API_KEY" ,
"CLOUDFLARE_API_TOKEN" ) .
2018-10-16 15:52:57 +00:00
WithDomain ( "CLOUDFLARE_DOMAIN" )
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
} {
{
2019-09-01 12:32:20 +00:00
desc : "success email, API key" ,
2018-10-02 22:02:01 +00:00
envVars : map [ string ] string {
"CLOUDFLARE_EMAIL" : "test@example.com" ,
"CLOUDFLARE_API_KEY" : "123" ,
} ,
} ,
2019-09-01 12:32:20 +00:00
{
desc : "success API token" ,
envVars : map [ string ] string {
"CLOUDFLARE_API_TOKEN" : "012345abcdef" ,
} ,
} ,
2018-10-02 22:02:01 +00:00
{
desc : "missing credentials" ,
envVars : map [ string ] string {
2019-09-01 12:32:20 +00:00
"CLOUDFLARE_EMAIL" : "" ,
"CLOUDFLARE_API_KEY" : "" ,
"CLOUDFLARE_API_TOKEN" : "" ,
2018-10-02 22:02:01 +00:00
} ,
2019-09-01 12:32:20 +00:00
expected : "cloudflare: some credentials information are missing: CLOUDFLARE_EMAIL,CLOUDFLARE_API_KEY or some credentials information are missing: CLOUDFLARE_API_TOKEN" ,
2018-10-02 22:02:01 +00:00
} ,
{
desc : "missing email" ,
envVars : map [ string ] string {
"CLOUDFLARE_EMAIL" : "" ,
"CLOUDFLARE_API_KEY" : "key" ,
} ,
2019-09-01 12:32:20 +00:00
expected : "cloudflare: some credentials information are missing: CLOUDFLARE_EMAIL or some credentials information are missing: CLOUDFLARE_API_TOKEN" ,
2018-10-02 22:02:01 +00:00
} ,
{
desc : "missing api key" ,
envVars : map [ string ] string {
"CLOUDFLARE_EMAIL" : "awesome@possum.com" ,
"CLOUDFLARE_API_KEY" : "" ,
} ,
2019-09-01 12:32:20 +00:00
expected : "cloudflare: some credentials information are missing: CLOUDFLARE_API_KEY or some credentials information are missing: CLOUDFLARE_API_TOKEN" ,
2018-10-02 22:02:01 +00:00
} ,
}
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 ) {
2018-10-16 15:52:57 +00:00
defer envTest . RestoreEnv ( )
envTest . ClearEnv ( )
envTest . Apply ( test . envVars )
2018-10-02 22:02:01 +00:00
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
2019-09-01 12:32:20 +00:00
authToken string
2018-10-02 22:02:01 +00:00
expected string
} {
{
2019-09-01 12:32:20 +00:00
desc : "success with email and api key" ,
authEmail : "test@example.com" ,
authKey : "123" ,
} ,
{
desc : "success with api token" ,
authToken : "012345abcdef" ,
} ,
{
desc : "prefer api token" ,
authToken : "012345abcdef" ,
2018-10-02 22:02:01 +00:00
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" ,
} ,
2019-09-01 12:32:20 +00:00
{
desc : "missing api token, fallback to api key/email" ,
authToken : "" ,
expected : "invalid credentials: key & email must not be empty" ,
} ,
2018-10-02 22:02:01 +00:00
}
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 ) {
config := NewDefaultConfig ( )
config . AuthEmail = test . authEmail
config . AuthKey = test . authKey
2019-09-01 12:32:20 +00:00
config . AuthToken = test . authToken
2018-10-02 22:02:01 +00:00
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-16 15:52:57 +00:00
func TestLivePresent ( t * testing . T ) {
if ! envTest . IsLiveTest ( ) {
2015-12-03 20:01:46 +00:00
t . Skip ( "skipping live test" )
}
2018-10-16 15:52:57 +00:00
envTest . RestoreEnv ( )
provider , err := NewDNSProvider ( )
2018-10-02 22:02:01 +00:00
require . NoError ( t , err )
2015-12-03 20:01:46 +00:00
2018-10-16 15:52:57 +00:00
err = provider . Present ( envTest . GetDomain ( ) , "" , "123d==" )
2018-10-02 22:02:01 +00:00
require . NoError ( t , err )
2015-12-03 20:01:46 +00:00
}
2018-10-16 15:52:57 +00:00
func TestLiveCleanUp ( t * testing . T ) {
if ! envTest . IsLiveTest ( ) {
2015-12-03 20:01:46 +00:00
t . Skip ( "skipping live test" )
}
2018-10-16 15:52:57 +00:00
envTest . RestoreEnv ( )
provider , err := NewDNSProvider ( )
2018-10-02 22:02:01 +00:00
require . NoError ( t , err )
2015-12-03 20:01:46 +00:00
2018-10-16 15:52:57 +00:00
time . Sleep ( 2 * time . Second )
err = provider . CleanUp ( envTest . GetDomain ( ) , "" , "123d==" )
2018-10-02 22:02:01 +00:00
require . NoError ( t , err )
2015-12-03 20:01:46 +00:00
}