2019-03-11 17:56:48 +01:00
package cloudflare
2015-12-03 21:01:46 +01:00
import (
"testing"
"time"
2020-09-02 03:20:01 +02:00
"github.com/go-acme/lego/v4/platform/tester"
2015-12-03 21:01:46 +01:00
"github.com/stretchr/testify/assert"
2018-10-03 00:02:01 +02:00
"github.com/stretchr/testify/require"
2015-12-03 21:01:46 +01:00
)
2024-11-15 23:21:21 +01:00
const envDomain = envNamespace + "DOMAIN"
2018-10-16 17:52:57 +02:00
var envTest = tester . NewEnvTest (
2024-11-15 23:21:21 +01:00
EnvEmail ,
EnvAPIKey ,
EnvDNSAPIToken ,
EnvZoneAPIToken ,
altEnvEmail ,
altEnvName ( EnvAPIKey ) ,
altEnvName ( EnvDNSAPIToken ) ,
altEnvName ( EnvZoneAPIToken ) ) .
WithDomain ( envDomain )
2015-12-03 21:01:46 +01:00
2018-10-03 00:02:01 +02:00
func TestNewDNSProvider ( t * testing . T ) {
testCases := [ ] struct {
desc string
envVars map [ string ] string
expected string
} {
{
2019-09-01 05:32:20 -07:00
desc : "success email, API key" ,
2018-10-03 00:02:01 +02:00
envVars : map [ string ] string {
2024-11-15 23:21:21 +01:00
EnvEmail : "test@example.com" ,
EnvAPIKey : "123" ,
2018-10-03 00:02:01 +02:00
} ,
} ,
2019-09-01 05:32:20 -07:00
{
desc : "success API token" ,
envVars : map [ string ] string {
2024-11-15 23:21:21 +01:00
EnvDNSAPIToken : "012345abcdef" ,
2019-10-09 02:20:30 +02:00
} ,
} ,
{
desc : "success separate API tokens" ,
envVars : map [ string ] string {
2024-11-15 23:21:21 +01:00
EnvDNSAPIToken : "012345abcdef" ,
EnvZoneAPIToken : "abcdef012345" ,
2019-09-01 05:32:20 -07:00
} ,
} ,
2018-10-03 00:02:01 +02:00
{
desc : "missing credentials" ,
envVars : map [ string ] string {
2024-11-15 23:21:21 +01:00
EnvEmail : "" ,
EnvAPIKey : "" ,
EnvDNSAPIToken : "" ,
2018-10-03 00:02:01 +02:00
} ,
2019-10-09 02:20:30 +02:00
expected : "cloudflare: some credentials information are missing: CLOUDFLARE_EMAIL,CLOUDFLARE_API_KEY or some credentials information are missing: CLOUDFLARE_DNS_API_TOKEN,CLOUDFLARE_ZONE_API_TOKEN" ,
2018-10-03 00:02:01 +02:00
} ,
{
desc : "missing email" ,
envVars : map [ string ] string {
2024-11-15 23:21:21 +01:00
EnvEmail : "" ,
EnvAPIKey : "key" ,
2018-10-03 00:02:01 +02:00
} ,
2019-10-09 02:20:30 +02:00
expected : "cloudflare: some credentials information are missing: CLOUDFLARE_EMAIL or some credentials information are missing: CLOUDFLARE_DNS_API_TOKEN,CLOUDFLARE_ZONE_API_TOKEN" ,
2018-10-03 00:02:01 +02:00
} ,
{
desc : "missing api key" ,
envVars : map [ string ] string {
2024-11-15 23:21:21 +01:00
EnvEmail : "awesome@possum.com" ,
EnvAPIKey : "" ,
2018-10-03 00:02:01 +02:00
} ,
2019-10-09 02:20:30 +02:00
expected : "cloudflare: some credentials information are missing: CLOUDFLARE_API_KEY or some credentials information are missing: CLOUDFLARE_DNS_API_TOKEN,CLOUDFLARE_ZONE_API_TOKEN" ,
2018-10-03 00:02:01 +02:00
} ,
}
2018-06-11 17:32:50 +02:00
2018-10-03 00:02:01 +02:00
for _ , test := range testCases {
t . Run ( test . desc , func ( t * testing . T ) {
2018-10-16 17:52:57 +02:00
defer envTest . RestoreEnv ( )
envTest . ClearEnv ( )
envTest . Apply ( test . envVars )
2018-10-03 00:02:01 +02:00
p , err := NewDNSProvider ( )
2021-03-04 20:16:59 +01:00
if test . expected == "" {
2018-10-12 19:29:18 +02:00
require . NoError ( t , err )
require . NotNil ( t , p )
assert . NotNil ( t , p . config )
assert . NotNil ( t , p . client )
2018-10-03 00:02:01 +02:00
} else {
require . EqualError ( t , err , test . expected )
}
} )
}
2015-12-03 21:01:46 +01:00
}
2019-10-09 02:20:30 +02:00
func TestNewDNSProviderWithToken ( t * testing . T ) {
type expected struct {
dnsToken string
zoneToken string
sameClient bool
error string
}
testCases := [ ] struct {
desc string
// test input
envVars map [ string ] string
// expectations
expected expected
} {
{
desc : "same client when zone token is missing" ,
envVars : map [ string ] string {
2024-11-15 23:21:21 +01:00
EnvDNSAPIToken : "123" ,
2019-10-09 02:20:30 +02:00
} ,
expected : expected {
dnsToken : "123" ,
zoneToken : "123" ,
sameClient : true ,
} ,
} ,
{
desc : "same client when zone token equals dns token" ,
envVars : map [ string ] string {
2024-11-15 23:21:21 +01:00
EnvDNSAPIToken : "123" ,
EnvZoneAPIToken : "123" ,
2019-10-09 02:20:30 +02:00
} ,
expected : expected {
dnsToken : "123" ,
zoneToken : "123" ,
sameClient : true ,
} ,
} ,
{
desc : "failure when only zone api given" ,
envVars : map [ string ] string {
2024-11-15 23:21:21 +01:00
EnvZoneAPIToken : "123" ,
2019-10-09 02:20:30 +02:00
} ,
expected : expected {
error : "cloudflare: some credentials information are missing: CLOUDFLARE_EMAIL,CLOUDFLARE_API_KEY or some credentials information are missing: CLOUDFLARE_DNS_API_TOKEN" ,
} ,
} ,
{
desc : "different clients when zone and dns token differ" ,
envVars : map [ string ] string {
2024-11-15 23:21:21 +01:00
EnvDNSAPIToken : "123" ,
EnvZoneAPIToken : "abc" ,
2019-10-09 02:20:30 +02:00
} ,
expected : expected {
dnsToken : "123" ,
zoneToken : "abc" ,
sameClient : false ,
} ,
} ,
{
desc : "aliases work as expected" , // CLOUDFLARE_* takes precedence over CF_*
envVars : map [ string ] string {
2024-11-15 23:21:21 +01:00
EnvDNSAPIToken : "123" ,
altEnvName ( EnvDNSAPIToken ) : "456" ,
EnvZoneAPIToken : "abc" ,
altEnvName ( EnvZoneAPIToken ) : "def" ,
2019-10-09 02:20:30 +02:00
} ,
expected : expected {
dnsToken : "123" ,
zoneToken : "abc" ,
sameClient : false ,
} ,
} ,
}
defer envTest . RestoreEnv ( )
localEnvTest := tester . NewEnvTest (
2024-11-15 23:21:21 +01:00
EnvDNSAPIToken , altEnvName ( EnvDNSAPIToken ) ,
EnvZoneAPIToken , altEnvName ( EnvZoneAPIToken ) ,
) . WithDomain ( envDomain )
2019-10-09 02:20:30 +02:00
envTest . ClearEnv ( )
for _ , test := range testCases {
t . Run ( test . desc , func ( t * testing . T ) {
defer localEnvTest . RestoreEnv ( )
localEnvTest . ClearEnv ( )
localEnvTest . Apply ( test . envVars )
p , err := NewDNSProvider ( )
if test . expected . error != "" {
require . EqualError ( t , err , test . expected . error )
return
}
require . NoError ( t , err )
require . NotNil ( t , p )
assert . Equal ( t , test . expected . dnsToken , p . config . AuthToken )
assert . Equal ( t , test . expected . zoneToken , p . config . ZoneToken )
if test . expected . sameClient {
assert . Equal ( t , p . client . clientRead , p . client . clientEdit )
} else {
assert . NotEqual ( t , p . client . clientRead , p . client . clientEdit )
}
} )
}
}
2018-10-03 00:02:01 +02:00
func TestNewDNSProviderConfig ( t * testing . T ) {
testCases := [ ] struct {
desc string
authEmail string
authKey string
2019-09-01 05:32:20 -07:00
authToken string
2018-10-03 00:02:01 +02:00
expected string
} {
{
2019-09-01 05:32:20 -07: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-03 00:02:01 +02:00
authEmail : "test@example.com" ,
authKey : "123" ,
} ,
{
desc : "missing credentials" ,
2019-10-09 02:20:30 +02:00
expected : "cloudflare: invalid credentials: key & email must not be empty" ,
2018-10-03 00:02:01 +02:00
} ,
{
desc : "missing email" ,
authKey : "123" ,
2019-10-09 02:20:30 +02:00
expected : "cloudflare: invalid credentials: key & email must not be empty" ,
2018-10-03 00:02:01 +02:00
} ,
{
desc : "missing api key" ,
authEmail : "test@example.com" ,
2019-10-09 02:20:30 +02:00
expected : "cloudflare: invalid credentials: key & email must not be empty" ,
2018-10-03 00:02:01 +02:00
} ,
2019-09-01 05:32:20 -07:00
{
desc : "missing api token, fallback to api key/email" ,
authToken : "" ,
2019-10-09 02:20:30 +02:00
expected : "cloudflare: invalid credentials: key & email must not be empty" ,
2019-09-01 05:32:20 -07:00
} ,
2018-10-03 00:02:01 +02:00
}
2018-06-11 17:32:50 +02:00
2018-10-03 00:02:01 +02: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 05:32:20 -07:00
config . AuthToken = test . authToken
2018-10-03 00:02:01 +02:00
p , err := NewDNSProviderConfig ( config )
2021-03-04 20:16:59 +01:00
if test . expected == "" {
2018-10-12 19:29:18 +02:00
require . NoError ( t , err )
require . NotNil ( t , p )
assert . NotNil ( t , p . config )
assert . NotNil ( t , p . client )
2018-10-03 00:02:01 +02:00
} else {
require . EqualError ( t , err , test . expected )
}
} )
}
2018-04-26 01:12:41 +10:00
}
2018-10-16 17:52:57 +02:00
func TestLivePresent ( t * testing . T ) {
if ! envTest . IsLiveTest ( ) {
2015-12-03 21:01:46 +01:00
t . Skip ( "skipping live test" )
}
2018-10-16 17:52:57 +02:00
envTest . RestoreEnv ( )
provider , err := NewDNSProvider ( )
2018-10-03 00:02:01 +02:00
require . NoError ( t , err )
2015-12-03 21:01:46 +01:00
2018-10-16 17:52:57 +02:00
err = provider . Present ( envTest . GetDomain ( ) , "" , "123d==" )
2018-10-03 00:02:01 +02:00
require . NoError ( t , err )
2015-12-03 21:01:46 +01:00
}
2018-10-16 17:52:57 +02:00
func TestLiveCleanUp ( t * testing . T ) {
if ! envTest . IsLiveTest ( ) {
2015-12-03 21:01:46 +01:00
t . Skip ( "skipping live test" )
}
2018-10-16 17:52:57 +02:00
envTest . RestoreEnv ( )
provider , err := NewDNSProvider ( )
2018-10-03 00:02:01 +02:00
require . NoError ( t , err )
2015-12-03 21:01:46 +01:00
2018-10-16 17:52:57 +02:00
time . Sleep ( 2 * time . Second )
err = provider . CleanUp ( envTest . GetDomain ( ) , "" , "123d==" )
2018-10-03 00:02:01 +02:00
require . NoError ( t , err )
2015-12-03 21:01:46 +01:00
}