All checks were successful
check / check (push) Successful in 1m9s
Closes #45. ## Problem 1. The README didn't clearly explain what `WEBHOOKER_ENVIRONMENT=dev` vs `prod` actually changes. 2. The dev-mode default for `DATA_DIR` was `./data` — a relative path whose meaning depends on the working directory. There's no reason to use a relative path even in development. ## Changes ### Code (`internal/config/config.go`) - Replace the dev default `DATA_DIR` from `./data` to `$XDG_DATA_HOME/webhooker` (falling back to `$HOME/.local/share/webhooker`). This follows the XDG Base Directory Specification and ensures the data directory is always an absolute path regardless of the working directory. - Add `devDataDir()` helper that resolves the XDG path, with a `/tmp/webhooker` last-resort fallback if `$HOME` can't be determined. ### Tests (`internal/config/config_test.go`) - `TestDevDataDir`: verifies XDG_DATA_HOME is respected, HOME fallback works, and the result is always absolute. - `TestDevDefaultDataDirIsAbsolute`: integration test that creates a full Config via fx and asserts the dev default DataDir is absolute. ### README - Add a table documenting exactly what `dev` vs `prod` changes: DATA_DIR default, CORS policy, and session cookie Secure flag. - Clarify that log format and security headers are independent of the environment setting. - Update the DATA_DIR default in the configuration variable table. Co-authored-by: clawbot <clawbot@eeqj.de> Co-authored-by: user <user@Mac.lan guest wan> Reviewed-on: #46 Co-authored-by: clawbot <clawbot@noreply.example.org> Co-committed-by: clawbot <clawbot@noreply.example.org>
143 lines
3.0 KiB
Go
143 lines
3.0 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/fx"
|
|
"go.uber.org/fx/fxtest"
|
|
"sneak.berlin/go/webhooker/internal/globals"
|
|
"sneak.berlin/go/webhooker/internal/logger"
|
|
)
|
|
|
|
func TestEnvironmentConfig(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
envValue string
|
|
envVars map[string]string
|
|
expectError bool
|
|
isDev bool
|
|
isProd bool
|
|
}{
|
|
{
|
|
name: "default is dev",
|
|
envValue: "",
|
|
envVars: map[string]string{},
|
|
expectError: false,
|
|
isDev: true,
|
|
isProd: false,
|
|
},
|
|
{
|
|
name: "explicit dev",
|
|
envValue: "dev",
|
|
envVars: map[string]string{},
|
|
expectError: false,
|
|
isDev: true,
|
|
isProd: false,
|
|
},
|
|
{
|
|
name: "explicit prod",
|
|
envValue: "prod",
|
|
envVars: map[string]string{},
|
|
expectError: false,
|
|
isDev: false,
|
|
isProd: true,
|
|
},
|
|
{
|
|
name: "invalid environment",
|
|
envValue: "staging",
|
|
envVars: map[string]string{},
|
|
expectError: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Set environment variable if specified
|
|
if tt.envValue != "" {
|
|
os.Setenv("WEBHOOKER_ENVIRONMENT", tt.envValue)
|
|
defer os.Unsetenv("WEBHOOKER_ENVIRONMENT")
|
|
} else {
|
|
os.Unsetenv("WEBHOOKER_ENVIRONMENT")
|
|
}
|
|
|
|
// Set additional environment variables
|
|
for k, v := range tt.envVars {
|
|
os.Setenv(k, v)
|
|
defer os.Unsetenv(k)
|
|
}
|
|
|
|
if tt.expectError {
|
|
// Use regular fx.New for error cases since fxtest doesn't expose errors the same way
|
|
var cfg *Config
|
|
app := fx.New(
|
|
fx.NopLogger, // Suppress fx logs in tests
|
|
fx.Provide(
|
|
globals.New,
|
|
logger.New,
|
|
New,
|
|
),
|
|
fx.Populate(&cfg),
|
|
)
|
|
assert.Error(t, app.Err())
|
|
} else {
|
|
// Use fxtest for success cases
|
|
var cfg *Config
|
|
app := fxtest.New(
|
|
t,
|
|
fx.Provide(
|
|
globals.New,
|
|
logger.New,
|
|
New,
|
|
),
|
|
fx.Populate(&cfg),
|
|
)
|
|
require.NoError(t, app.Err())
|
|
app.RequireStart()
|
|
defer app.RequireStop()
|
|
|
|
assert.Equal(t, tt.isDev, cfg.IsDev())
|
|
assert.Equal(t, tt.isProd, cfg.IsProd())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDefaultDataDir(t *testing.T) {
|
|
// Verify that when DATA_DIR is unset, the default is /var/lib/webhooker
|
|
// regardless of the environment setting.
|
|
for _, env := range []string{"", "dev", "prod"} {
|
|
name := env
|
|
if name == "" {
|
|
name = "unset"
|
|
}
|
|
t.Run("env="+name, func(t *testing.T) {
|
|
if env != "" {
|
|
os.Setenv("WEBHOOKER_ENVIRONMENT", env)
|
|
defer os.Unsetenv("WEBHOOKER_ENVIRONMENT")
|
|
} else {
|
|
os.Unsetenv("WEBHOOKER_ENVIRONMENT")
|
|
}
|
|
os.Unsetenv("DATA_DIR")
|
|
|
|
var cfg *Config
|
|
app := fxtest.New(
|
|
t,
|
|
fx.Provide(
|
|
globals.New,
|
|
logger.New,
|
|
New,
|
|
),
|
|
fx.Populate(&cfg),
|
|
)
|
|
require.NoError(t, app.Err())
|
|
app.RequireStart()
|
|
defer app.RequireStop()
|
|
|
|
assert.Equal(t, "/var/lib/webhooker", cfg.DataDir)
|
|
})
|
|
}
|
|
}
|