Files
realDynDNS/pkg/dnsProvider/ionos/ionos_test.go

226 lines
5.5 KiB
Go

package ionosDnsProvider_test
import (
"errors"
"io"
"net"
"net/http"
common "realdnydns/model/common"
ionosDnsProvider "realdnydns/pkg/dnsProvider/ionos"
"testing"
)
func TestNew(t *testing.T) {
//t.Run("Config cannot be decoded", testNewConfigCannotBeDecoded())
t.Run("API key is required", testNewAPIKeyIsRequired())
t.Run("Base URL is required", testNewBaseURLIsRequired())
t.Run("Valid config", testNewValidConfig())
}
func testNewAPIKeyIsRequired() func(*testing.T) {
return func(t *testing.T) {
config := ionosDnsProvider.IONOSConfig{
BaseURL: "https://api.ionos.com",
}
_, err := ionosDnsProvider.NewIonos(&config)
if err == nil {
t.Error("Expected error, got nil")
}
}
}
func testNewBaseURLIsRequired() func(*testing.T) {
return func(t *testing.T) {
config := ionosDnsProvider.IONOSConfig{
APIKey: "1234",
}
_, err := ionosDnsProvider.NewIonos(&config)
if err == nil {
t.Error("Expected error, got nil")
}
}
}
func testNewValidConfig() func(*testing.T) {
return func(t *testing.T) {
config := ionosDnsProvider.IONOSConfig{
APIKey: "1234",
BaseURL: "https://api.ionos.com",
}
_, err := ionosDnsProvider.NewIonos(&config)
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
}
}
type MockIonosAPI struct {
SetARecordFunc func(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error)
GetARecordFunc func(tld string, subdomain string) (*common.ARecord, error)
GetRecordIdFunc func(zoneId string, tld string, subdomain string, recordType string) (string, error)
GetZoneIdFunc func(tld string) (string, error)
HttpCallFunc func(method string, url string, body io.Reader) (*http.Response, error)
}
func (m *MockIonosAPI) SetARecord(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error) {
return m.SetARecordFunc(tld, subdomain, ip, ttl, prio, disabled)
}
func (m *MockIonosAPI) GetARecord(tld string, subdomain string) (*common.ARecord, error) {
return m.GetARecordFunc(tld, subdomain)
}
func (m *MockIonosAPI) GetRecordId(zoneId string, tld string, subdomain string, recordType string) (string, error) {
return m.GetRecordIdFunc(zoneId, tld, subdomain, recordType)
}
func (m *MockIonosAPI) GetZoneId(tld string) (string, error) {
return m.GetZoneIdFunc(tld)
}
func (m *MockIonosAPI) HttpCall(method string, url string, body io.Reader) (*http.Response, error) {
return m.HttpCallFunc(method, url, body)
}
func TestUpdateRecord(t *testing.T) {
t.Run("API error", testUpdateRecordAPIError())
t.Run("API success", testUpdateRecordAPISuccess())
}
func testUpdateRecordAPIError() func(*testing.T) {
return func(t *testing.T) {
api := &MockIonosAPI{
SetARecordFunc: func(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error) {
return nil, errors.New("API error")
},
}
provider := ionosDnsProvider.IONOS{
API: api,
}
_, err := provider.UpdateRecord("example.com", "sub", net.ParseIP("127.0.0.1"), 60, 0, false)
if err == nil {
t.Error("Expected error, got nil")
}
}
}
func testUpdateRecordAPISuccess() func(*testing.T) {
return func(t *testing.T) {
api := &MockIonosAPI{
SetARecordFunc: func(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error) {
return &common.ARecord{
Domain: "sub",
IP: "127.0.0.1",
TTL: 60,
Prio: 0,
Disabled: false,
}, nil
},
}
provider := ionosDnsProvider.IONOS{
API: api,
}
record, err := provider.UpdateRecord("example.com", "sub", net.ParseIP("127.0.0.1"), 60, 0, false)
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
if record.Domain != "sub" {
t.Errorf("Expected sub, got %s", record.Domain)
}
if record.IP != "127.0.0.1" {
t.Errorf("Expected 127.0.0.1, got %s", record.IP)
}
if record.TTL != 60 {
t.Errorf("Expected 60, got %d", record.TTL)
}
if record.Prio != 0 {
t.Errorf("Expected 0, got %d", record.Prio)
}
if record.Disabled {
t.Error("Expected false, got true")
}
}
}
func TestGetRecord(t *testing.T) {
t.Run("API error", testGetRecordAPIError())
t.Run("API success", testGetRecordAPISuccess())
}
func testGetRecordAPIError() func(*testing.T) {
return func(t *testing.T) {
api := &MockIonosAPI{
GetARecordFunc: func(tld string, subdomain string) (*common.ARecord, error) {
return nil, errors.New("API error")
},
}
provider := ionosDnsProvider.IONOS{
API: api,
}
_, err := provider.GetRecord("example.com", "sub")
if err == nil {
t.Error("Expected error, got nil")
}
}
}
func testGetRecordAPISuccess() func(*testing.T) {
return func(t *testing.T) {
api := &MockIonosAPI{
GetARecordFunc: func(tld string, subdomain string) (*common.ARecord, error) {
return &common.ARecord{
Domain: "sub",
IP: "127.0.0.1",
TTL: 60,
Prio: 0,
Disabled: false,
}, nil
},
}
provider := ionosDnsProvider.IONOS{
API: api,
}
record, err := provider.GetRecord("example.com", "sub")
if err != nil {
t.Errorf("expected nil, got %v", err)
}
if record.Domain != "sub" {
t.Errorf("expected sub, got %s", record.Domain)
}
if record.IP != "127.0.0.1" {
t.Errorf("expected 127.0.0.1, got %s", record.IP)
}
if record.TTL != 60 {
t.Errorf("expected 60, got %d", record.TTL)
}
if record.Prio != 0 {
t.Errorf("expected 0, got %d", record.Prio)
}
if record.Disabled {
t.Error("expected false, got true")
}
}
}