diff options
| author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-06-24 00:18:28 -0700 |
|---|---|---|
| committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-06-24 00:36:13 -0700 |
| commit | dbd548d428f222babb4e1d6a182b90f19b192e1f (patch) | |
| tree | 8a1fed7502b703b6a1ff2b0ccd1260f39bbd98e0 /api/dns/dns_test.go | |
| parent | fca8f5d8addeebba7d55d4b69159745c84f27a91 (diff) | |
| download | hatecomputers.club-dbd548d428f222babb4e1d6a182b90f19b192e1f.tar.gz hatecomputers.club-dbd548d428f222babb4e1d6a182b90f19b192e1f.zip | |
POST record with id to update to fix cloudflare 500
Diffstat (limited to 'api/dns/dns_test.go')
| -rw-r--r-- | api/dns/dns_test.go | 74 |
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() |
