mirror of
https://github.com/go-vikunja/vikunja.git
synced 2026-05-08 12:57:52 -05:00
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:
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user