2017-07-19 22:54:35 +00:00
package otc
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
2018-05-30 17:53:04 +00:00
"github.com/stretchr/testify/assert"
2017-07-19 22:54:35 +00:00
)
2018-12-06 21:50:17 +00:00
const fakeOTCToken = "62244bc21da68d03ebac94e6636ff01f"
2017-07-19 22:54:35 +00:00
2018-10-16 15:52:57 +00:00
// DNSServerMock mock
type DNSServerMock struct {
2017-07-19 22:54:35 +00:00
t * testing . T
2018-10-16 15:52:57 +00:00
server * httptest . Server
2017-07-19 22:54:35 +00:00
Mux * http . ServeMux
}
2018-10-16 15:52:57 +00:00
// NewDNSServerMock create a new DNSServerMock
func NewDNSServerMock ( t * testing . T ) * DNSServerMock {
mux := http . NewServeMux ( )
return & DNSServerMock {
t : t ,
server : httptest . NewServer ( mux ) ,
Mux : mux ,
2017-07-19 22:54:35 +00:00
}
}
2018-10-16 15:52:57 +00:00
func ( m * DNSServerMock ) GetServerURL ( ) string {
return m . server . URL
2017-07-19 22:54:35 +00:00
}
// ShutdownServer creates the mock server
2018-10-16 15:52:57 +00:00
func ( m * DNSServerMock ) ShutdownServer ( ) {
m . server . Close ( )
2017-07-19 22:54:35 +00:00
}
2018-05-30 17:53:04 +00:00
// HandleAuthSuccessfully Handle auth successfully
2018-10-16 15:52:57 +00:00
func ( m * DNSServerMock ) HandleAuthSuccessfully ( ) {
2019-02-06 17:15:53 +00:00
m . Mux . HandleFunc ( "/v3/auth/token" , func ( w http . ResponseWriter , _ * http . Request ) {
2017-07-19 22:54:35 +00:00
w . Header ( ) . Set ( "X-Subject-Token" , fakeOTCToken )
fmt . Fprintf ( w , ` {
"token" : {
"catalog" : [
{
"type" : "dns" ,
"id" : "56cd81db1f8445d98652479afe07c5ba" ,
"name" : "" ,
"endpoints" : [
{
"url" : "%s" ,
"region" : "eu-de" ,
"region_id" : "eu-de" ,
"interface" : "public" ,
"id" : "0047a06690484d86afe04877074efddf"
}
]
}
]
2018-10-16 15:52:57 +00:00
} } ` , m . server . URL )
2017-07-19 22:54:35 +00:00
} )
}
2018-05-30 17:53:04 +00:00
// HandleListZonesSuccessfully Handle list zones successfully
2018-10-16 15:52:57 +00:00
func ( m * DNSServerMock ) HandleListZonesSuccessfully ( ) {
2017-07-19 22:54:35 +00:00
m . Mux . HandleFunc ( "/v2/zones" , func ( w http . ResponseWriter , r * http . Request ) {
2018-10-16 15:52:57 +00:00
assert . Equal ( m . t , r . Method , http . MethodGet )
assert . Equal ( m . t , r . URL . Path , "/v2/zones" )
assert . Equal ( m . t , r . URL . RawQuery , "name=example.com." )
assert . Equal ( m . t , r . Header . Get ( "Content-Type" ) , "application/json" )
2017-07-19 22:54:35 +00:00
fmt . Fprintf ( w , ` {
"zones" : [ {
"id" : "123123"
} ] }
` )
} )
}
2018-05-30 17:53:04 +00:00
// HandleListZonesEmpty Handle list zones empty
2018-10-16 15:52:57 +00:00
func ( m * DNSServerMock ) HandleListZonesEmpty ( ) {
2017-07-19 22:54:35 +00:00
m . Mux . HandleFunc ( "/v2/zones" , func ( w http . ResponseWriter , r * http . Request ) {
2018-06-21 17:06:16 +00:00
assert . Equal ( m . t , r . Method , http . MethodGet )
2017-07-19 22:54:35 +00:00
assert . Equal ( m . t , r . URL . Path , "/v2/zones" )
assert . Equal ( m . t , r . URL . RawQuery , "name=example.com." )
assert . Equal ( m . t , r . Header . Get ( "Content-Type" ) , "application/json" )
2018-10-16 15:52:57 +00:00
fmt . Fprintf ( w , ` {
"zones" : [
] }
` )
2017-07-19 22:54:35 +00:00
} )
}
2018-05-30 17:53:04 +00:00
// HandleDeleteRecordsetsSuccessfully Handle delete recordsets successfully
2018-10-16 15:52:57 +00:00
func ( m * DNSServerMock ) HandleDeleteRecordsetsSuccessfully ( ) {
2017-07-19 22:54:35 +00:00
m . Mux . HandleFunc ( "/v2/zones/123123/recordsets/321321" , func ( w http . ResponseWriter , r * http . Request ) {
2018-10-16 15:52:57 +00:00
assert . Equal ( m . t , r . Method , http . MethodDelete )
assert . Equal ( m . t , r . URL . Path , "/v2/zones/123123/recordsets/321321" )
assert . Equal ( m . t , r . Header . Get ( "Content-Type" ) , "application/json" )
2017-07-19 22:54:35 +00:00
fmt . Fprintf ( w , ` {
"zones" : [ {
"id" : "123123"
} ] }
` )
} )
}
2018-05-30 17:53:04 +00:00
// HandleListRecordsetsEmpty Handle list recordsets empty
2018-10-16 15:52:57 +00:00
func ( m * DNSServerMock ) HandleListRecordsetsEmpty ( ) {
2017-07-19 22:54:35 +00:00
m . Mux . HandleFunc ( "/v2/zones/123123/recordsets" , func ( w http . ResponseWriter , r * http . Request ) {
2018-10-16 15:52:57 +00:00
assert . Equal ( m . t , r . URL . Path , "/v2/zones/123123/recordsets" )
assert . Equal ( m . t , r . URL . RawQuery , "type=TXT&name=_acme-challenge.example.com." )
2017-07-19 22:54:35 +00:00
fmt . Fprintf ( w , ` {
"recordsets" : [
] }
` )
} )
}
2018-05-30 17:53:04 +00:00
// HandleListRecordsetsSuccessfully Handle list recordsets successfully
2018-10-16 15:52:57 +00:00
func ( m * DNSServerMock ) HandleListRecordsetsSuccessfully ( ) {
2017-07-19 22:54:35 +00:00
m . Mux . HandleFunc ( "/v2/zones/123123/recordsets" , func ( w http . ResponseWriter , r * http . Request ) {
2018-06-21 17:06:16 +00:00
if r . Method == http . MethodGet {
2018-10-16 15:52:57 +00:00
assert . Equal ( m . t , r . URL . Path , "/v2/zones/123123/recordsets" )
assert . Equal ( m . t , r . URL . RawQuery , "type=TXT&name=_acme-challenge.example.com." )
assert . Equal ( m . t , r . Header . Get ( "Content-Type" ) , "application/json" )
2017-07-19 22:54:35 +00:00
fmt . Fprintf ( w , ` {
"recordsets" : [ {
"id" : "321321"
} ] }
` )
2018-10-16 15:52:57 +00:00
return
}
2017-07-19 22:54:35 +00:00
2018-10-16 15:52:57 +00:00
if r . Method == http . MethodPost {
assert . Equal ( m . t , r . Header . Get ( "Content-Type" ) , "application/json" )
2017-07-19 22:54:35 +00:00
body , err := ioutil . ReadAll ( r . Body )
assert . Nil ( m . t , err )
exceptedString := "{\"name\":\"_acme-challenge.example.com.\",\"description\":\"Added TXT record for ACME dns-01 challenge using lego client\",\"type\":\"TXT\",\"ttl\":300,\"records\":[\"\\\"w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI\\\"\"]}"
assert . Equal ( m . t , string ( body ) , exceptedString )
fmt . Fprintf ( w , ` {
"recordsets" : [ {
"id" : "321321"
} ] }
` )
2018-10-16 15:52:57 +00:00
return
2017-07-19 22:54:35 +00:00
}
2018-10-16 15:52:57 +00:00
http . Error ( w , fmt . Sprintf ( "Expected method to be 'GET' or 'POST' but got '%s'" , r . Method ) , http . StatusBadRequest )
2017-07-19 22:54:35 +00:00
} )
}