Files
Docwell/Server/internal/config/config_test.go
T
tajniak81andClaude Opus 5.5 3c7403ddf9 Create the first superadmin from DOCWELL_ADMIN_EMAIL on start
When DOCWELL_ADMIN_EMAIL and DOCWELL_ADMIN_PASSWORD are set (and
optionally DOCWELL_ADMIN_NAME), docwell-server creates that account as
superadmin on start while the instance has no accounts. Once any account
exists it changes nothing, so restarts never reset the password or a role
changed in the admin panel. Without them, the first sign-up still wins.

Pass the variables through every compose file, document them in the
.env examples and the configuration, access and quick-start guides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 22:32:29 +01:00

96 lines
2.8 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package config
import (
"testing"
"time"
)
func lookup(env map[string]string) func(string) (string, bool) {
return func(k string) (string, bool) {
v, ok := env[k]
return v, ok
}
}
func TestDefaults(t *testing.T) {
cfg, err := fromLookup(lookup(map[string]string{
"DOCWELL_PB_EMAIL": "admin@docwell.test",
"DOCWELL_PB_PASSWORD": "secret",
}))
if err != nil {
t.Fatal(err)
}
if cfg.Addr != ":8080" {
t.Errorf("Addr = %q", cfg.Addr)
}
if cfg.ShutdownTimeout != 15*time.Second {
t.Errorf("ShutdownTimeout = %v", cfg.ShutdownTimeout)
}
if len(cfg.CORSOrigins) != 2 {
t.Errorf("CORSOrigins = %v", cfg.CORSOrigins)
}
if !cfg.SignupEnabled || len(cfg.SignupDomains) != 0 {
t.Errorf("signup = %v %v", cfg.SignupEnabled, cfg.SignupDomains)
}
}
func TestOverridesAndTrimming(t *testing.T) {
cfg, err := fromLookup(lookup(map[string]string{
"DOCWELL_PB_EMAIL": "a@b.c",
"DOCWELL_PB_PASSWORD": "x",
"DOCWELL_PB_URL": "http://db:8070/",
"DOCWELL_CORS_ORIGINS": " https://a.test , ,https://b.test",
"DOCWELL_SHUTDOWN_TIMEOUT": "3s",
"DOCWELL_SIGNUP_ENABLED": "false",
"DOCWELL_SIGNUP_DOMAINS": "acme.test",
}))
if err != nil {
t.Fatal(err)
}
if cfg.PocketBaseURL != "http://db:8070" {
t.Errorf("PocketBaseURL = %q", cfg.PocketBaseURL)
}
if len(cfg.CORSOrigins) != 2 || cfg.CORSOrigins[1] != "https://b.test" {
t.Errorf("CORSOrigins = %v", cfg.CORSOrigins)
}
if cfg.ShutdownTimeout != 3*time.Second {
t.Errorf("ShutdownTimeout = %v", cfg.ShutdownTimeout)
}
if cfg.SignupEnabled || len(cfg.SignupDomains) != 1 {
t.Errorf("signup = %v %v", cfg.SignupEnabled, cfg.SignupDomains)
}
}
func TestAdminAccount(t *testing.T) {
base := func(extra map[string]string) map[string]string {
env := map[string]string{"DOCWELL_PB_EMAIL": "a@b.c", "DOCWELL_PB_PASSWORD": "x"}
for k, v := range extra {
env[k] = v
}
return env
}
cfg, err := fromLookup(lookup(base(map[string]string{
"DOCWELL_ADMIN_EMAIL": " root@docwell.test ", "DOCWELL_ADMIN_PASSWORD": "password123", "DOCWELL_ADMIN_NAME": "Root",
})))
if err != nil || cfg.AdminEmail != "root@docwell.test" || cfg.AdminPassword != "password123" || cfg.AdminName != "Root" {
t.Errorf("admin = %+v, %v", cfg, err)
}
for name, extra := range map[string]map[string]string{
"email only": {"DOCWELL_ADMIN_EMAIL": "root@docwell.test"},
"password only": {"DOCWELL_ADMIN_PASSWORD": "password123"},
"short password": {"DOCWELL_ADMIN_EMAIL": "root@docwell.test", "DOCWELL_ADMIN_PASSWORD": "short"},
} {
if _, err := fromLookup(lookup(base(extra))); err == nil {
t.Errorf("%s: expected an error", name)
}
}
}
func TestMissingCredentials(t *testing.T) {
if _, err := fromLookup(lookup(nil)); err == nil {
t.Fatal("expected error without PocketBase credentials")
}
}