From d84ce15dd5090f063c93aaa788305d8259c079c7 Mon Sep 17 00:00:00 2001 From: Elliott Street Date: Sun, 26 Feb 2023 12:37:02 -0700 Subject: [PATCH] update dns --- omglol/dns.go | 11 ++++- omglol/dns_test.go | 109 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 100 insertions(+), 20 deletions(-) diff --git a/omglol/dns.go b/omglol/dns.go index 9697819..07cef96 100644 --- a/omglol/dns.go +++ b/omglol/dns.go @@ -16,8 +16,13 @@ func (c *Client) ListDNSRecords(address string) (*[]DNSRecord, error) { return nil, err } + var d []DNSRecord + body, err := c.doRequest(req) if err != nil { + if strings.Contains(err.Error(), "status: 404") { + return &d, nil + } return nil, err } @@ -35,7 +40,9 @@ func (c *Client) ListDNSRecords(address string) (*[]DNSRecord, error) { return nil, err } - return &r.Response.DNS, nil + d = r.Response.DNS + + return &d, nil } // Find a single DNS record from its attributes. @@ -180,7 +187,7 @@ func convertRecordResponse(r dnsRecordContent) *DNSRecord { } // Returns a string representaion of a DNS record -func (d *DNSRecord) ToString() string { +func (d *DNSRecord) String() string { priority := "" if d.Priority != nil { diff --git a/omglol/dns_test.go b/omglol/dns_test.go index 3abfb99..2755b8c 100644 --- a/omglol/dns_test.go +++ b/omglol/dns_test.go @@ -2,10 +2,52 @@ package omglol import ( "testing" + "time" ) +func validateRecord(t *testing.T, r DNSRecord) { + if r.ID <= 0 { + t.Errorf("Record ID is invalid.") + } + if !isOneOf(r.Type, []string{"A", "AAAA", "CAA", "CNAME", "MX", "NS", "SRV", "TXT"}) { + t.Errorf("Unexpected record type: %s.", r.Type) + } + if len(r.Name) <= 0 { + t.Errorf("Name is empty.") + } + if len(r.Data) <= 0 { + t.Errorf("Data is empty.") + } + if r.Type == "MX" && r.Priority == nil { + t.Errorf("Priority cannot be nil for record type 'MX'.") + } + if r.Type != "MX" && r.Priority != nil { + t.Errorf("Record of type '%s' should not have a priority not equal to 'nil'.", r.Type) + } + if r.TTL <= 0 { + t.Errorf("TTL must be greater than 0, got %d", r.TTL) + } + created, err := time.Parse(time.RFC3339, r.CreatedAt) + if err != nil { + t.Error(err.Error()) + } + if created.Unix() <= 0 { + t.Error("Invalid CreatedAt timestamp.") + } + updated, err := time.Parse(time.RFC3339, r.UpdatedAt) + if err != nil { + t.Error(err.Error()) + } + if updated.Unix() <= 0 { + t.Error("Invalid UpdatedAt timestamp.") + } + if updated.Unix() < created.Unix() { + t.Errorf("Updated: %s, cannot be before Created: %s.", r.UpdatedAt, r.CreatedAt) + } +} + func TestListDNSRecords(t *testing.T) { - t.Parallel() + sleep() c, err := NewClient(testEmail, testKey, testHostURL) if err != nil { @@ -17,48 +59,63 @@ func TestListDNSRecords(t *testing.T) { t.Errorf(err.Error()) } - for _, d := range *l { - t.Logf(d.ToString() + "\n") + if l != nil { + for _, d := range *l { + t.Logf(d.String() + "\n") + validateRecord(t, d) + } + } else { + t.Error("ListDNSRecords returned 'nil'.") } + } func TestFilterDNSRecords(t *testing.T) { - t.Parallel() + sleep() c, err := NewClient(testEmail, testKey, testHostURL) - if err != nil { t.Errorf(err.Error()) } // TXT Record criteria1 := map[string]any{ - "ID": 41923511, + "Name": "testlistdns." + testOwnedDomain, "Type": "TXT", "TTL": 300, "Priority": nil, } d1, err := c.FilterDNSRecord(testOwnedDomain, criteria1) - if err != nil { t.Errorf(err.Error()) } - t.Logf(d1.ToString()) + if d1 != nil { + t.Logf(d1.String()) + validateRecord(t, *d1) + } else { + t.Errorf("FilterDNSRecord returned nil.") + } // MX Record criteria2 := map[string]any{ - "ID": int64(42197707), + "Type": "MX", + "Name": "mail." + testOwnedDomain, "Priority": int64(20), } d2, err := c.FilterDNSRecord(testOwnedDomain, criteria2) - t.Logf(d2.ToString()) + if d2 != nil { + t.Logf(d2.String()) + validateRecord(t, *d2) + } else { + t.Errorf("FilterDNSRecord returned nil.") + } } func TestCreateAndDeleteDNSRecord(t *testing.T) { - t.Parallel() + sleep() c, err := NewClient(testEmail, testKey, testHostURL) if err != nil { @@ -72,7 +129,12 @@ func TestCreateAndDeleteDNSRecord(t *testing.T) { t.Errorf(err.Error()) } - t.Logf(r.ToString()) + if r != nil { + t.Logf(r.String()) + validateRecord(t, *r) + } else { + t.Error("CreateDNSRecord returned 'nil'.") + } err = c.DeleteDNSRecord(testOwnedDomain, r.ID) if err != nil { @@ -81,7 +143,7 @@ func TestCreateAndDeleteDNSRecord(t *testing.T) { } func TestCreateUpdateDeleteDNSRecord(t *testing.T) { - t.Parallel() + sleep() c, err := NewClient(testEmail, testKey, testHostURL) if err != nil { @@ -97,16 +159,27 @@ func TestCreateUpdateDeleteDNSRecord(t *testing.T) { t.Errorf(err.Error()) } - t.Logf(create.ToString()) - - replace, err := c.UpdateDNSRecord(testOwnedDomain, *record2, create.ID) + if create != nil { + t.Logf(create.String()) + validateRecord(t, *create) + } else { + t.Error("CreateDNSRecord returned 'nil'.") + } + sleep() + update, err := c.UpdateDNSRecord(testOwnedDomain, *record2, create.ID) if err != nil { t.Errorf(err.Error()) } - t.Logf(replace.ToString()) + if update != nil { + t.Logf(update.String()) + validateRecord(t, *update) + } else { + t.Error("UpdateDNSRecord returned 'nil'.") + } - err = c.DeleteDNSRecord(testOwnedDomain, replace.ID) + sleep() + err = c.DeleteDNSRecord(testOwnedDomain, update.ID) if err != nil { t.Errorf(err.Error()) }