159 lines
4.6 KiB
Go
159 lines
4.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/jarcoal/httpmock"
|
|
)
|
|
|
|
var (
|
|
pingBody = `{
|
|
"url": "/api/v1",
|
|
"version": 1
|
|
}`
|
|
getZonesBody = `[{
|
|
"account": "",
|
|
"dnssec": false,
|
|
"id": "example.org.",
|
|
"kind": "Native",
|
|
"last_check": 0,
|
|
"masters": [],
|
|
"name": "example.org.",
|
|
"notified_serial": 0,
|
|
"serial": 1,
|
|
"url": "/api/v1/servers/localhost/zones/example.org."},
|
|
{"account": "",
|
|
"dnssec": false,
|
|
"id": "example2.net.",
|
|
"kind": "Native",
|
|
"last_check": 0,
|
|
"masters": [],
|
|
"name": "example2.net.",
|
|
"notified_serial": 0,
|
|
"serial": 2019091801,
|
|
"url": "/api/v1/servers/localhost/zones/example2.net."
|
|
}]`
|
|
badPatchBody = `{
|
|
"error": "DNS Name 'email.example.org' is not canonical"
|
|
}`
|
|
addMasterZonePayload = `{
|
|
"name":"example.org",
|
|
"kind": "Master",
|
|
"dnssec":false,
|
|
"soa-edit":"INCEPTION-INCREMENT",
|
|
"masters": [],
|
|
"nameservers": ["ns1.example.org"]
|
|
}`
|
|
)
|
|
|
|
func TestPdnsPing(t *testing.T) {
|
|
httpmock.Activate()
|
|
defer httpmock.DeactivateAndReset()
|
|
httpmock.RegisterResponder("GET", testBaseURL+"/api",
|
|
func(req *http.Request) (*http.Response, error) {
|
|
if req.Header.Get("X-Api-Key") == testAPIKey {
|
|
return httpmock.NewJsonResponse(201, pingBody)
|
|
}
|
|
return httpmock.NewStringResponse(401, "Unauthorized"), nil
|
|
},
|
|
)
|
|
var response json.RawMessage
|
|
testClient, _ := initializePowerDNSTestClient()
|
|
_, _, err := testClient.sendQuery(context.Background(), "api", "GET", nil, &response)
|
|
if err != nil {
|
|
t.Errorf("%s", err)
|
|
}
|
|
isEqual, _ := areEqualJSON([]byte(strconv.Quote(pingBody)), response)
|
|
if !isEqual {
|
|
t.Error("Test Pdns Ping - Failed")
|
|
}
|
|
}
|
|
|
|
func TestPdnsGetZones(t *testing.T) {
|
|
httpmock.Activate()
|
|
defer httpmock.DeactivateAndReset()
|
|
httpmock.RegisterResponder("GET", generateTestAPIVhostURL()+"/zones",
|
|
func(req *http.Request) (*http.Response, error) {
|
|
if req.Header.Get("X-Api-Key") == testAPIKey {
|
|
return httpmock.NewJsonResponse(201, getZonesBody)
|
|
}
|
|
return httpmock.NewStringResponse(401, "Unauthorized"), nil
|
|
},
|
|
)
|
|
var response json.RawMessage
|
|
testClient, _ := initializePowerDNSTestClient()
|
|
_, _, err := testClient.sendQuery(context.Background(), generateTestRequestURI()+"/zones", "GET", nil, &response)
|
|
if err != nil {
|
|
t.Errorf("%s", err)
|
|
}
|
|
isEqual, _ := areEqualJSON([]byte(strconv.Quote(getZonesBody)), response)
|
|
if !isEqual {
|
|
t.Error("Test Pdns getZones - Failed")
|
|
}
|
|
}
|
|
|
|
func TestPdnsBadGet(t *testing.T) {
|
|
httpmock.Activate()
|
|
defer httpmock.DeactivateAndReset()
|
|
httpmock.RegisterResponder("GET", generateTestAPIVhostURL()+"/zone",
|
|
func(req *http.Request) (*http.Response, error) {
|
|
if req.Header.Get("X-Api-Key") == testAPIKey {
|
|
return httpmock.NewJsonResponse(404, "Not found")
|
|
}
|
|
return httpmock.NewStringResponse(401, "Unauthorized"), nil
|
|
},
|
|
)
|
|
var response json.RawMessage
|
|
testClient, _ := initializePowerDNSTestClient()
|
|
testClient.sendQuery(context.Background(), generateTestRequestURI()+"/zone", "GET", nil, &response)
|
|
isEqual, _ := areEqualJSON([]byte(strconv.Quote("Not found")), response)
|
|
if !isEqual {
|
|
t.Error("Test Pdns bad get - Failed")
|
|
}
|
|
}
|
|
|
|
func testPdnsBadPatch(t *testing.T) {
|
|
httpmock.Activate()
|
|
defer httpmock.DeactivateAndReset()
|
|
httpmock.RegisterResponder("PATCH", generateTestAPIVhostURL()+"/zones/example.org.",
|
|
func(req *http.Request) (*http.Response, error) {
|
|
if req.Header.Get("X-Api-Key") == testAPIKey {
|
|
return httpmock.NewJsonResponse(422, badPatchBody)
|
|
}
|
|
return httpmock.NewStringResponse(401, "Unauthorized"), nil
|
|
},
|
|
)
|
|
var response json.RawMessage
|
|
testClient, _ := initializePowerDNSTestClient()
|
|
testClient.sendQuery(context.Background(), generateTestRequestURI()+"/zones/example.org.", "PATCH", nil, &response)
|
|
isEqual, _ := areEqualJSON([]byte(strconv.Quote(badPatchBody)), response)
|
|
if !isEqual {
|
|
t.Error("Test Pdns bad patch - Failed")
|
|
}
|
|
}
|
|
|
|
func TestPdnsPatchRecord(t *testing.T) {
|
|
httpmock.Activate()
|
|
defer httpmock.DeactivateAndReset()
|
|
httpmock.RegisterResponder("PATCH", generateTestAPIVhostURL()+"/zones/example.org.",
|
|
func(req *http.Request) (*http.Response, error) {
|
|
if req.Header.Get("X-Api-Key") == testAPIKey {
|
|
return httpmock.NewJsonResponse(204, nil)
|
|
}
|
|
return httpmock.NewStringResponse(401, "Unauthorized"), nil
|
|
},
|
|
)
|
|
var response json.RawMessage
|
|
testClient, _ := initializePowerDNSTestClient()
|
|
testClient.sendQuery(context.Background(), generateTestRequestURI()+"/zones/example.org.", "PATCH", nil, &response)
|
|
t.Logf("Response is: %s", response)
|
|
if !bytes.Equal(response, []byte("null")) {
|
|
t.Error("Test Pdns patch record - Failed")
|
|
}
|
|
}
|