fix(mail): fall back to os.Hostname() before hardcoded domain

When the public URL is not configured, GetMailDomain() now tries
os.Hostname() before falling back to the hardcoded "vikunja" string,
and logs a warning in both fallback cases.
This commit is contained in:
kolaente
2026-04-03 20:10:39 +02:00
committed by kolaente
parent 5249366aa3
commit 07aa3c1b04
2 changed files with 22 additions and 4 deletions

View File

@@ -18,8 +18,10 @@ package mail
import (
"net/url"
"os"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log"
)
// GetMailDomain returns the hostname from the configured public URL,
@@ -32,5 +34,10 @@ func GetMailDomain() string {
return parsedURL.Hostname()
}
}
if hostname, err := os.Hostname(); err == nil && hostname != "" {
log.Warningf("Could not determine mail domain from public URL, falling back to hostname %q", hostname)
return hostname
}
log.Warningf("Could not determine mail domain from public URL or hostname, falling back to %q", "vikunja")
return "vikunja"
}

View File

@@ -17,6 +17,7 @@
package mail
import (
"os"
"testing"
"code.vikunja.io/api/pkg/config"
@@ -24,9 +25,14 @@ import (
)
func TestGetMailDomain(t *testing.T) {
t.Run("returns fallback when public URL is empty", func(t *testing.T) {
t.Run("falls back to os.Hostname when public URL is empty", func(t *testing.T) {
config.ServicePublicURL.Set("")
assert.Equal(t, "vikunja", GetMailDomain())
expectedHostname, err := os.Hostname()
if err != nil || expectedHostname == "" {
assert.Equal(t, "vikunja", GetMailDomain())
} else {
assert.Equal(t, expectedHostname, GetMailDomain())
}
})
t.Run("extracts hostname from public URL", func(t *testing.T) {
@@ -39,8 +45,13 @@ func TestGetMailDomain(t *testing.T) {
assert.Equal(t, "tasks.example.com", GetMailDomain())
})
t.Run("returns fallback for invalid URL", func(t *testing.T) {
t.Run("falls back to os.Hostname for invalid URL", func(t *testing.T) {
config.ServicePublicURL.Set("://bad")
assert.Equal(t, "vikunja", GetMailDomain())
expectedHostname, err := os.Hostname()
if err != nil || expectedHostname == "" {
assert.Equal(t, "vikunja", GetMailDomain())
} else {
assert.Equal(t, expectedHostname, GetMailDomain())
}
})
}