Files
realDynDNS/pkg/realDynDns/realDynDns_test.go
Timo Behrendt 3ad5b1ec0e
All checks were successful
CD / test (push) Successful in 52s
CD / Build and push (push) Successful in 15m10s
feat: logging (#20)
Reviewed-on: #20
Co-authored-by: Timo Behrendt <t.behrendt@t00n.de>
Co-committed-by: Timo Behrendt <t.behrendt@t00n.de>
2024-12-27 19:52:21 +01:00

121 lines
2.8 KiB
Go

package realDynDns
import (
"log/slog"
"net"
"realdnydns/model/common"
"realdnydns/pkg/config"
"testing"
)
type MockExternalIpProvider struct {
ExternalIp string
}
func (m *MockExternalIpProvider) GetExternalIp() (net.IP, error) {
return net.ParseIP(m.ExternalIp), nil
}
type MockDNSProvider interface {
UpdateRecord(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error)
GetRecord(tld string, subdomain string) (*common.ARecord, error)
}
type MockDNSProviderImpl struct {
GetRecordIpResponse string
UpdateRecordIpResponse string
}
func (m *MockDNSProviderImpl) GetRecord(tld string, subdomain string) (*common.ARecord, error) {
return &common.ARecord{
IP: m.GetRecordIpResponse,
TTL: 10,
Prio: 20,
Disabled: false,
}, nil
}
func (m *MockDNSProviderImpl) UpdateRecord(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error) {
return &common.ARecord{
IP: m.UpdateRecordIpResponse,
TTL: 10,
Prio: 20,
Disabled: false,
}, nil
}
type MockedNotificationProvider struct{}
type MockedNotificationProviderImpl struct{}
func (m *MockedNotificationProviderImpl) SendNotification(title string, message string) error {
return nil
}
func TestDetectAndApplyChanges(t *testing.T) {
t.Run("with changes", testDetectAndApplyChangesWithChanges())
t.Run("without changes", testDetectAndApplyChangesWithoutChanges())
}
func testDetectAndApplyChangesWithChanges() func(t *testing.T) {
return func(t *testing.T) {
changeDetector := New(&MockExternalIpProvider{
ExternalIp: "127.0.0.1",
}, &MockDNSProviderImpl{
GetRecordIpResponse: "127.0.0.2",
UpdateRecordIpResponse: "127.0.0.1",
}, &MockedNotificationProviderImpl{},
[]config.DomainConfig{
{
TLD: "example.com",
Subdomains: []string{
"test",
"@",
},
},
},
slog.Default(),
)
numberUpdated, err := changeDetector.RunOnce()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if numberUpdated != 2 {
t.Errorf("expected numberUpdated to be 2, got %v", numberUpdated)
}
}
}
func testDetectAndApplyChangesWithoutChanges() func(t *testing.T) {
return func(t *testing.T) {
changeDetector := New(&MockExternalIpProvider{
ExternalIp: "127.0.0.1",
}, &MockDNSProviderImpl{
GetRecordIpResponse: "127.0.0.1",
UpdateRecordIpResponse: "127.0.0.1",
}, &MockedNotificationProviderImpl{},
[]config.DomainConfig{
{
TLD: "example.com",
Subdomains: []string{
"test",
"@",
},
},
},
slog.Default(),
)
numberUpdated, err := changeDetector.RunOnce()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if numberUpdated != 0 {
t.Errorf("expected numberUpdated to be 0, got %v", numberUpdated)
}
}
}