2019-03-11 18:56:48 +02:00
package vegadns
2018-07-01 01:45:24 +02:00
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
2020-09-02 03:20:01 +02:00
"github.com/go-acme/lego/v4/platform/tester"
2018-07-01 01:45:24 +02:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2018-09-24 21:07:20 +02:00
const testDomain = "example.com"
2020-03-12 00:51:10 +02:00
var envTest = tester . NewEnvTest ( EnvKey , EnvSecret , EnvURL )
2018-10-16 17:52:57 +02:00
2018-10-12 19:29:18 +02:00
func TestNewDNSProvider_Fail ( t * testing . T ) {
2018-10-16 17:52:57 +02:00
defer envTest . RestoreEnv ( )
envTest . ClearEnv ( )
2018-07-01 01:45:24 +02:00
_ , err := NewDNSProvider ( )
assert . Error ( t , err , "VEGADNS_URL env missing" )
}
2018-10-12 19:29:18 +02:00
func TestDNSProvider_TimeoutSuccess ( t * testing . T ) {
2018-10-16 17:52:57 +02:00
defer envTest . RestoreEnv ( )
envTest . ClearEnv ( )
2021-11-02 01:52:38 +02:00
setupTest ( t , muxSuccess ( ) )
2018-07-01 01:45:24 +02:00
provider , err := NewDNSProvider ( )
require . NoError ( t , err )
timeout , interval := provider . Timeout ( )
2019-06-24 18:34:04 +02:00
assert . Equal ( t , timeout , 12 * time . Minute )
assert . Equal ( t , interval , 1 * time . Minute )
2018-07-01 01:45:24 +02:00
}
2018-09-24 21:07:20 +02:00
func TestDNSProvider_Present ( t * testing . T ) {
testCases := [ ] struct {
desc string
2019-06-24 18:34:04 +02:00
handler http . Handler
2018-09-24 21:07:20 +02:00
expectedError string
} {
{
2019-06-24 18:34:04 +02:00
desc : "Success" ,
handler : muxSuccess ( ) ,
2018-09-24 21:07:20 +02:00
} ,
{
desc : "FailToFindZone" ,
2019-06-24 18:34:04 +02:00
handler : muxFailToFindZone ( ) ,
2018-09-24 21:07:20 +02:00
expectedError : "vegadns: can't find Authoritative Zone for _acme-challenge.example.com. in Present: Unable to find auth zone for fqdn _acme-challenge.example.com" ,
} ,
{
desc : "FailToCreateTXT" ,
2019-06-24 18:34:04 +02:00
handler : muxFailToCreateTXT ( ) ,
2018-09-24 21:07:20 +02:00
expectedError : "vegadns: Got bad answer from VegaDNS on CreateTXT. Code: 400. Message: " ,
} ,
}
2018-07-01 01:45:24 +02:00
2018-09-24 21:07:20 +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 ( )
2021-11-02 01:52:38 +02:00
setupTest ( t , test . handler )
2018-07-01 01:45:24 +02:00
2018-09-24 21:07:20 +02:00
provider , err := NewDNSProvider ( )
require . NoError ( t , err )
2018-07-01 01:45:24 +02:00
2018-09-24 21:07:20 +02:00
err = provider . Present ( testDomain , "token" , "keyAuth" )
2021-03-04 21:16:59 +02:00
if test . expectedError == "" {
2018-09-24 21:07:20 +02:00
assert . NoError ( t , err )
} else {
assert . EqualError ( t , err , test . expectedError )
}
} )
}
2018-07-01 01:45:24 +02:00
}
2018-09-24 21:07:20 +02:00
func TestDNSProvider_CleanUp ( t * testing . T ) {
testCases := [ ] struct {
desc string
2019-06-24 18:34:04 +02:00
handler http . Handler
2018-09-24 21:07:20 +02:00
expectedError string
} {
{
2019-06-24 18:34:04 +02:00
desc : "Success" ,
handler : muxSuccess ( ) ,
2018-09-24 21:07:20 +02:00
} ,
{
desc : "FailToFindZone" ,
2019-06-24 18:34:04 +02:00
handler : muxFailToFindZone ( ) ,
2018-09-24 21:07:20 +02:00
expectedError : "vegadns: can't find Authoritative Zone for _acme-challenge.example.com. in CleanUp: Unable to find auth zone for fqdn _acme-challenge.example.com" ,
} ,
{
desc : "FailToGetRecordID" ,
2019-06-24 18:34:04 +02:00
handler : muxFailToGetRecordID ( ) ,
2018-09-24 21:07:20 +02:00
expectedError : "vegadns: couldn't get Record ID in CleanUp: Got bad answer from VegaDNS on GetRecordID. Code: 404. Message: " ,
} ,
}
2018-07-01 01:45:24 +02:00
2018-09-24 21:07:20 +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 ( )
2021-11-02 01:52:38 +02:00
setupTest ( t , test . handler )
2018-07-01 01:45:24 +02:00
2018-09-24 21:07:20 +02:00
provider , err := NewDNSProvider ( )
require . NoError ( t , err )
2018-07-01 01:45:24 +02:00
2018-09-24 21:07:20 +02:00
err = provider . CleanUp ( testDomain , "token" , "keyAuth" )
2021-03-04 21:16:59 +02:00
if test . expectedError == "" {
2018-09-24 21:07:20 +02:00
assert . NoError ( t , err )
} else {
assert . EqualError ( t , err , test . expectedError )
}
} )
}
2018-07-01 01:45:24 +02:00
}
2018-10-12 19:29:18 +02:00
func muxSuccess ( ) * http . ServeMux {
2018-07-01 01:45:24 +02:00
mux := http . NewServeMux ( )
mux . HandleFunc ( "/1.0/token" , func ( w http . ResponseWriter , r * http . Request ) {
2018-12-06 23:50:17 +02:00
if r . Method == http . MethodPost {
2018-07-01 01:45:24 +02:00
w . WriteHeader ( http . StatusOK )
2019-02-06 19:15:53 +02:00
fmt . Fprint ( w , tokenResponseMock )
2018-07-01 01:45:24 +02:00
return
}
w . WriteHeader ( http . StatusBadRequest )
} )
mux . HandleFunc ( "/1.0/domains" , func ( w http . ResponseWriter , r * http . Request ) {
if r . URL . Query ( ) . Get ( "search" ) == "example.com" {
w . WriteHeader ( http . StatusOK )
2019-02-06 19:15:53 +02:00
fmt . Fprint ( w , domainsResponseMock )
2018-07-01 01:45:24 +02:00
return
}
w . WriteHeader ( http . StatusNotFound )
} )
mux . HandleFunc ( "/1.0/records" , func ( w http . ResponseWriter , r * http . Request ) {
switch r . Method {
case http . MethodGet :
if r . URL . Query ( ) . Get ( "domain_id" ) == "1" {
w . WriteHeader ( http . StatusOK )
2019-02-06 19:15:53 +02:00
fmt . Fprint ( w , recordsResponseMock )
2018-07-01 01:45:24 +02:00
return
}
w . WriteHeader ( http . StatusNotFound )
return
case http . MethodPost :
w . WriteHeader ( http . StatusCreated )
2019-02-06 19:15:53 +02:00
fmt . Fprint ( w , recordCreatedResponseMock )
2018-07-01 01:45:24 +02:00
return
}
w . WriteHeader ( http . StatusBadRequest )
} )
mux . HandleFunc ( "/1.0/records/3" , func ( w http . ResponseWriter , r * http . Request ) {
2018-12-06 23:50:17 +02:00
if r . Method == http . MethodDelete {
2018-07-01 01:45:24 +02:00
w . WriteHeader ( http . StatusOK )
2019-02-06 19:15:53 +02:00
fmt . Fprint ( w , recordDeletedResponseMock )
2018-07-01 01:45:24 +02:00
return
}
w . WriteHeader ( http . StatusBadRequest )
} )
mux . HandleFunc ( "/" , func ( w http . ResponseWriter , r * http . Request ) {
w . WriteHeader ( http . StatusNotFound )
fmt . Printf ( "Not Found for Request: (%+v)\n\n" , r )
} )
return mux
}
2018-10-12 19:29:18 +02:00
func muxFailToFindZone ( ) * http . ServeMux {
2018-07-01 01:45:24 +02:00
mux := http . NewServeMux ( )
mux . HandleFunc ( "/1.0/token" , func ( w http . ResponseWriter , r * http . Request ) {
2018-12-06 23:50:17 +02:00
if r . Method == http . MethodPost {
2018-07-01 01:45:24 +02:00
w . WriteHeader ( http . StatusOK )
2019-02-06 19:15:53 +02:00
fmt . Fprint ( w , tokenResponseMock )
2018-07-01 01:45:24 +02:00
return
}
w . WriteHeader ( http . StatusBadRequest )
} )
2019-02-06 19:15:53 +02:00
mux . HandleFunc ( "/1.0/domains" , func ( w http . ResponseWriter , _ * http . Request ) {
2018-07-01 01:45:24 +02:00
w . WriteHeader ( http . StatusNotFound )
} )
return mux
}
2018-10-12 19:29:18 +02:00
func muxFailToCreateTXT ( ) * http . ServeMux {
2018-07-01 01:45:24 +02:00
mux := http . NewServeMux ( )
mux . HandleFunc ( "/1.0/token" , func ( w http . ResponseWriter , r * http . Request ) {
2018-12-06 23:50:17 +02:00
if r . Method == http . MethodPost {
2018-07-01 01:45:24 +02:00
w . WriteHeader ( http . StatusOK )
2019-02-06 19:15:53 +02:00
fmt . Fprint ( w , tokenResponseMock )
2018-07-01 01:45:24 +02:00
return
}
w . WriteHeader ( http . StatusBadRequest )
} )
mux . HandleFunc ( "/1.0/domains" , func ( w http . ResponseWriter , r * http . Request ) {
2018-09-24 21:07:20 +02:00
if r . URL . Query ( ) . Get ( "search" ) == testDomain {
2018-07-01 01:45:24 +02:00
w . WriteHeader ( http . StatusOK )
2019-02-06 19:15:53 +02:00
fmt . Fprint ( w , domainsResponseMock )
2018-07-01 01:45:24 +02:00
return
}
w . WriteHeader ( http . StatusNotFound )
} )
mux . HandleFunc ( "/1.0/records" , func ( w http . ResponseWriter , r * http . Request ) {
switch r . Method {
case http . MethodGet :
if r . URL . Query ( ) . Get ( "domain_id" ) == "1" {
w . WriteHeader ( http . StatusOK )
2019-02-06 19:15:53 +02:00
fmt . Fprint ( w , recordsResponseMock )
2018-07-01 01:45:24 +02:00
return
}
w . WriteHeader ( http . StatusNotFound )
return
case http . MethodPost :
w . WriteHeader ( http . StatusBadRequest )
return
}
w . WriteHeader ( http . StatusBadRequest )
} )
return mux
}
2018-10-12 19:29:18 +02:00
func muxFailToGetRecordID ( ) * http . ServeMux {
2018-07-01 01:45:24 +02:00
mux := http . NewServeMux ( )
mux . HandleFunc ( "/1.0/token" , func ( w http . ResponseWriter , r * http . Request ) {
2018-12-06 23:50:17 +02:00
if r . Method == http . MethodPost {
2018-07-01 01:45:24 +02:00
w . WriteHeader ( http . StatusOK )
2019-02-06 19:15:53 +02:00
fmt . Fprint ( w , tokenResponseMock )
2018-07-01 01:45:24 +02:00
return
}
w . WriteHeader ( http . StatusBadRequest )
} )
mux . HandleFunc ( "/1.0/domains" , func ( w http . ResponseWriter , r * http . Request ) {
2018-09-24 21:07:20 +02:00
if r . URL . Query ( ) . Get ( "search" ) == testDomain {
2018-07-01 01:45:24 +02:00
w . WriteHeader ( http . StatusOK )
2019-02-06 19:15:53 +02:00
fmt . Fprint ( w , domainsResponseMock )
2018-07-01 01:45:24 +02:00
return
}
w . WriteHeader ( http . StatusNotFound )
} )
mux . HandleFunc ( "/1.0/records" , func ( w http . ResponseWriter , r * http . Request ) {
2018-12-06 23:50:17 +02:00
if r . Method == http . MethodGet {
2018-07-01 01:45:24 +02:00
w . WriteHeader ( http . StatusNotFound )
return
}
w . WriteHeader ( http . StatusBadRequest )
} )
return mux
}
2021-11-02 01:52:38 +02:00
func setupTest ( t * testing . T , mux http . Handler ) {
t . Helper ( )
server := httptest . NewServer ( mux )
t . Cleanup ( server . Close )
2018-07-01 01:45:24 +02:00
2019-06-24 18:34:04 +02:00
envTest . Apply ( map [ string ] string {
2020-03-12 00:51:10 +02:00
EnvKey : "key" ,
EnvSecret : "secret" ,
2021-11-02 01:52:38 +02:00
EnvURL : server . URL ,
2019-06-24 18:34:04 +02:00
} )
2018-07-01 01:45:24 +02:00
}