summaryrefslogtreecommitdiff
path: root/api/dns/dns_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'api/dns/dns_test.go')
-rw-r--r--api/dns/dns_test.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/api/dns/dns_test.go b/api/dns/dns_test.go
index 30baedf..c4c581b 100644
--- a/api/dns/dns_test.go
+++ b/api/dns/dns_test.go
@@ -57,6 +57,7 @@ func setup() (*sql.DB, *types.RequestContext, func()) {
type SignallingExternalDnsAdapter struct {
AddChannel chan *database.DNSRecord
RmChannel chan string
+ UpdateChan chan *database.DNSRecord
}
func (adapter *SignallingExternalDnsAdapter) CreateDNSRecord(record *database.DNSRecord) (string, error) {
@@ -72,6 +73,12 @@ func (adapter *SignallingExternalDnsAdapter) DeleteDNSRecord(id string) error {
return nil
}
+func (adapter *SignallingExternalDnsAdapter) UpdateDNSRecord(record *database.DNSRecord) error {
+ go func() { adapter.UpdateChan <- record }()
+
+ return nil
+}
+
func TestThatOwnerCanPutRecordInDomain(t *testing.T) {
db, context, cleanup := setup()
defer cleanup()
@@ -172,6 +179,73 @@ func TestThatUserCanAddToPublicEndpoints(t *testing.T) {
}
}
+func TestThatUserCanUpdateExistingRecord(t *testing.T) {
+ db, context, cleanup := setup()
+ defer cleanup()
+
+ updateChannel := make(chan *database.DNSRecord)
+ signallingDnsAdapter := &SignallingExternalDnsAdapter{
+ UpdateChan: updateChannel,
+ }
+
+ testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ dns.CreateDNSRecordContinuation(signallingDnsAdapter, MAX_USER_RECORDS, USER_OWNED_INTERNAL_FMT_DOMAINS)(context, r, w)(IdContinuation, IdContinuation)
+ }))
+ defer testServer.Close()
+
+ responseRecorder := httptest.NewRecorder()
+ nonexistantRecord := httptest.NewRequest("POST", testServer.URL, nil)
+
+ id := "1"
+ name := "test." + context.User.Username
+ nonexistantRecord.Form = map[string][]string{
+ "id": {id},
+ "internal": {"off"},
+ "name": {name},
+ "type": {"CNAME"},
+ "ttl": {"43000"},
+ "content": {"new.domain."},
+ }
+
+ testServer.Config.Handler.ServeHTTP(responseRecorder, nonexistantRecord)
+ if responseRecorder.Code != http.StatusInternalServerError {
+ t.Errorf("expected internal server error return, got %d", responseRecorder.Code)
+ }
+
+ record := &database.DNSRecord{
+ ID: id,
+ Internal: false,
+ Name: name,
+ Type: "CNAME",
+ Content: "test.domain.",
+ TTL: 43000,
+ UserID: context.User.ID,
+ }
+ _, err := database.SaveDNSRecord(db, record)
+ if err != nil {
+ t.Error(err)
+ }
+
+ existantRecord := nonexistantRecord
+ existantRecordRecorder := httptest.NewRecorder()
+ testServer.Config.Handler.ServeHTTP(existantRecordRecorder, existantRecord)
+ if existantRecordRecorder.Code != http.StatusOK {
+ t.Errorf("expected valid return, got %d", existantRecordRecorder.Code)
+ }
+ select {
+ case req := <-updateChannel:
+ newRecord, err := database.GetDNSRecord(db, req.ID)
+ if err != nil {
+ t.Error(err)
+ }
+ if newRecord.Content != "new.domain." {
+ t.Errorf("expected updated record, got %s", newRecord.Content)
+ }
+ case <-time.After(100 * time.Millisecond):
+ t.Errorf("expected updated record channel")
+ }
+}
+
func TestThatExternalDnsSaves(t *testing.T) {
db, context, cleanup := setup()
defer cleanup()