Skip to content

Commit 21ef581

Browse files
committed
fix: use mock DNS resolver in tests instead of real DNS queries
1 parent b36f6af commit 21ef581

File tree

1 file changed

+86
-2
lines changed

1 file changed

+86
-2
lines changed

internal/verification/dns_test.go

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@ func TestVerifyDNSRecordSuccess(t *testing.T) {
1919
}
2020

2121
domain := "example.com"
22-
result, err := verification.VerifyDNSRecord(domain, token)
22+
23+
// Create mock resolver with the verification token
24+
mockResolver := verification.NewMockDNSResolver()
25+
mockResolver.SetVerificationToken(domain, token)
26+
27+
// Use custom config with mock resolver
28+
config := verification.DefaultDNSConfig()
29+
config.Resolver = mockResolver
30+
31+
result, err := verification.VerifyDNSRecordWithConfig(domain, token, config)
2332
if err != nil {
2433
t.Errorf("VerifyDNSRecord returned unexpected error: %v", err)
2534
}
@@ -28,6 +37,10 @@ func TestVerifyDNSRecordSuccess(t *testing.T) {
2837
t.Fatal("VerifyDNSRecord returned nil result")
2938
}
3039

40+
if !result.Success {
41+
t.Errorf("Expected successful verification, got: %s", result.Message)
42+
}
43+
3144
if result.Domain != domain {
3245
t.Errorf("Result domain = %s, want %s", result.Domain, domain)
3346
}
@@ -36,6 +49,64 @@ func TestVerifyDNSRecordSuccess(t *testing.T) {
3649
t.Errorf("Result token = %s, want %s", result.Token, token)
3750
}
3851

52+
// Verify the mock was called
53+
if mockResolver.CallCount != 1 {
54+
t.Errorf("Expected 1 DNS call, got %d", mockResolver.CallCount)
55+
}
56+
57+
if mockResolver.LastDomain != domain {
58+
t.Errorf("Expected DNS query for %s, got %s", domain, mockResolver.LastDomain)
59+
}
60+
61+
t.Logf("DNS verification result: %+v", result)
62+
}
63+
64+
func TestVerifyDNSRecordTokenNotFound(t *testing.T) {
65+
token, err := verification.GenerateVerificationToken()
66+
if err != nil {
67+
t.Fatalf("Failed to generate test token: %v", err)
68+
}
69+
70+
domain := "example.com"
71+
72+
// Create mock resolver with different TXT records (no verification token)
73+
mockResolver := verification.NewMockDNSResolver()
74+
mockResolver.SetTXTRecord(domain, "v=spf1 -all", "some-other-record")
75+
76+
// Use custom config with mock resolver
77+
config := verification.DefaultDNSConfig()
78+
config.Resolver = mockResolver
79+
80+
result, err := verification.VerifyDNSRecordWithConfig(domain, token, config)
81+
if err != nil {
82+
t.Errorf("VerifyDNSRecord returned unexpected error: %v", err)
83+
}
84+
85+
if result == nil {
86+
t.Fatal("VerifyDNSRecord returned nil result")
87+
}
88+
89+
if result.Success {
90+
t.Error("Expected verification to fail when token is not found")
91+
}
92+
93+
if !strings.Contains(result.Message, "verification token not found") {
94+
t.Errorf("Expected 'token not found' message, got: %s", result.Message)
95+
}
96+
97+
if result.Domain != domain {
98+
t.Errorf("Result domain = %s, want %s", result.Domain, domain)
99+
}
100+
101+
if result.Token != token {
102+
t.Errorf("Result token = %s, want %s", result.Token, token)
103+
}
104+
105+
// Verify TXT records are included in result
106+
if len(result.TXTRecords) != 2 {
107+
t.Errorf("Expected 2 TXT records in result, got %d", len(result.TXTRecords))
108+
}
109+
39110
t.Logf("DNS verification result: %+v", result)
40111
}
41112

@@ -98,7 +169,16 @@ func TestVerifyDNSRecordTokenFormatValidation(t *testing.T) {
98169
}
99170

100171
domain := "example.com"
101-
result, err := verification.VerifyDNSRecord(domain, token)
172+
173+
// Create mock resolver with the verification token
174+
mockResolver := verification.NewMockDNSResolver()
175+
mockResolver.SetVerificationToken(domain, token)
176+
177+
// Use custom config with mock resolver
178+
config := verification.DefaultDNSConfig()
179+
config.Resolver = mockResolver
180+
181+
result, err := verification.VerifyDNSRecordWithConfig(domain, token, config)
102182

103183
if err != nil {
104184
var dnsErr *verification.DNSVerificationError
@@ -113,6 +193,10 @@ func TestVerifyDNSRecordTokenFormatValidation(t *testing.T) {
113193
t.Fatal("Expected result but got nil")
114194
}
115195

196+
if !result.Success {
197+
t.Errorf("Expected successful verification, got: %s", result.Message)
198+
}
199+
116200
if result.Domain != domain {
117201
t.Errorf("Result domain = %s, want %s", result.Domain, domain)
118202
}

0 commit comments

Comments
 (0)