Skip to content

Commit 2390310

Browse files
authored
Merge pull request #14 from jungaretti/fix-comparisons
Fix bug when comparing "" and "0" TTL/priority
2 parents 33a6851 + f6ff94d commit 2390310

File tree

2 files changed

+72
-11
lines changed

2 files changed

+72
-11
lines changed

pkg/porkbun/record.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ func ConvertToClientRecord(src PorkbunRecord) (out client.Record, err error) {
5656
}
5757

5858
func (src PorkbunRecord) HashFuzzy() string {
59-
return fmt.Sprint(src.Name, src.Type, src.Content, src.TTL, src.Priority)
59+
// Treat "" and "0" as the same value
60+
priority := src.Priority
61+
if priority == "" {
62+
priority = "0"
63+
}
64+
65+
return fmt.Sprint(src.Name, src.Type, src.Content, src.TTL, priority)
6066
}
6167

6268
// Trims anything.domain.tld to anything

pkg/porkbun/test/record_test.go

+65-10
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ package porkbun
33
import (
44
"bacon/pkg/client"
55
"bacon/pkg/porkbun"
6+
"encoding/json"
67
"fmt"
78
"testing"
9+
10+
"gopkg.in/yaml.v3"
811
)
912

10-
func TestConvertToPorkbunRecord(t *testing.T) {
13+
func TestConvertToPorkbun(t *testing.T) {
1114
record := client.Record{
1215
Type: "A",
1316
Host: "www.example.com",
1417
Content: "123.456.789.112",
1518
TTL: 600,
1619
}
17-
1820
porkRecord := porkbun.ConvertToPorkbunRecord(record)
1921

2022
if porkRecord.Type != record.Type {
@@ -29,7 +31,6 @@ func TestConvertToPorkbunRecord(t *testing.T) {
2931
t.Log("expected", record.Content, "found", porkRecord.Content)
3032
t.Fail()
3133
}
32-
3334
if porkRecord.TTL != fmt.Sprint(record.TTL) {
3435
t.Log("expected", fmt.Sprint(record.TTL), "found", porkRecord.TTL)
3536
}
@@ -38,34 +39,32 @@ func TestConvertToPorkbunRecord(t *testing.T) {
3839
}
3940
}
4041

41-
func TestConvertToClientRecord(t *testing.T) {
42+
func TestConvertToClientSimple(t *testing.T) {
4243
porkRecord := porkbun.PorkbunRecord{
4344
Id: "abcxyz",
4445
Name: "www.example.com",
4546
Type: "A",
4647
Content: "123.456.789.112",
4748
TTL: "600",
4849
}
49-
5050
record, err := porkbun.ConvertToClientRecord(porkRecord)
5151
if err != nil {
5252
t.Log(err)
5353
t.FailNow()
5454
}
5555

56-
if record.Host != porkRecord.Name {
57-
t.Log("expected", porkRecord.Name, "found", record.Host)
58-
t.Fail()
59-
}
6056
if record.Type != porkRecord.Type {
6157
t.Log("expected", porkRecord.Type, "found", record.Type)
6258
t.Fail()
6359
}
60+
if record.Host != porkRecord.Name {
61+
t.Log("expected", porkRecord.Name, "found", record.Host)
62+
t.Fail()
63+
}
6464
if record.Content != porkRecord.Content {
6565
t.Log("expected", porkRecord.Content, "found", record.Content)
6666
t.Fail()
6767
}
68-
6968
if record.TTL != 600 {
7069
t.Log("expected", porkRecord.TTL, "found", 600)
7170
t.Fail()
@@ -113,3 +112,59 @@ func TestHashFuzzy(t *testing.T) {
113112
t.Fail()
114113
}
115114
}
115+
116+
func TestComparePorkbunResponse(t *testing.T) {
117+
const config = `type: CNAME
118+
host: www.jungaretti.com
119+
content: green-forest-08be42d1e.azurestaticapps.net
120+
ttl: 600`
121+
const response = `{"id": "188254337","name": "www.jungaretti.com","type": "CNAME","content": "green-forest-08be42d1e.azurestaticapps.net","ttl": "600","prio": "0"}`
122+
123+
record := client.Record{}
124+
err := yaml.Unmarshal([]byte(config), &record)
125+
if err != nil {
126+
t.Log(err)
127+
t.FailNow()
128+
}
129+
porkRecord := porkbun.PorkbunRecord{}
130+
err = json.Unmarshal([]byte(response), &porkRecord)
131+
if err != nil {
132+
t.Log(err)
133+
t.FailNow()
134+
}
135+
136+
convertedRecord := porkbun.ConvertToPorkbunRecord(record)
137+
if porkRecord.HashFuzzy() != convertedRecord.HashFuzzy() {
138+
t.Log(porkRecord.HashFuzzy())
139+
t.Log(convertedRecord.HashFuzzy())
140+
t.Fail()
141+
}
142+
}
143+
144+
func TestComparePorkbunResponseNullPriority(t *testing.T) {
145+
const config = `type: CNAME
146+
host: www.jungaretti.com
147+
content: green-forest-08be42d1e.azurestaticapps.net
148+
ttl: 600`
149+
const response = `{"id": "188254337","name": "www.jungaretti.com","type": "CNAME","content": "green-forest-08be42d1e.azurestaticapps.net","ttl": "600","prio": null}`
150+
151+
record := client.Record{}
152+
err := yaml.Unmarshal([]byte(config), &record)
153+
if err != nil {
154+
t.Log(err)
155+
t.FailNow()
156+
}
157+
porkRecord := porkbun.PorkbunRecord{}
158+
err = json.Unmarshal([]byte(response), &porkRecord)
159+
if err != nil {
160+
t.Log(err)
161+
t.FailNow()
162+
}
163+
164+
convertedRecord := porkbun.ConvertToPorkbunRecord(record)
165+
if porkRecord.HashFuzzy() != convertedRecord.HashFuzzy() {
166+
t.Log("one", porkRecord.HashFuzzy())
167+
t.Log("two", convertedRecord.HashFuzzy())
168+
t.Error()
169+
}
170+
}

0 commit comments

Comments
 (0)