CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Test (windows-latest) (push) Canceled after 0s
CI / Storage tests on PostgreSQL (push) Canceled after 0s
CI / Complement (federation off) (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
CI / Smoke test the binary (push) Canceled after 0s
Implements PLAN.md Phase 9. internal/appservice reads registration files (appservice.config_files), matches namespaces the way Synapse does (anchored at the start), and delivers events to each service as transactions read from the roomserver's event stream at a cursor kept in appservice_state. A transaction in flight is recorded, so after a restart it is resent under the same ID; failures back off from 2s to 5m, and a successful ping cuts the backoff short. In the Client-Server API an as_token acts as the service's sender or, with ?user_id=, as any existing user in its namespace. Services register and log in their users with m.login.application_service; exclusive user and alias namespaces are refused to everybody else with M_EXCLUSIVE. Unknown aliases and users in a namespace are asked of the service first. The ping endpoint reports M_URL_NOT_SET, M_BAD_STATUS, M_CONNECTION_FAILED and M_CONNECTION_TIMEOUT. Services' own requests, and their users' when rate_limited is false, skip the rate limiter. Users can change their password (POST /account/password) and deactivate their account (POST /account/deactivate), both behind the password. Deactivation revokes tokens, deletes devices, optionally erases the profile and leaves every room through a new userapi.AccountObserver. The admin API is complete: user detail, the admin flag, deactivation and reactivation, password resets, devices, media quotas (media.max_user_storage_bytes plus per-user overrides), registration tokens with use limits and expiry (registration.require_token), server notices (internal/servernotices, server_notices.*), an editor for the federation allow/deny lists persisted in server_settings, a delivery view of the application services, and an audit log of every change. Migration 0011 adds the registration token, server notice room, media quota, audit log and appservice cursor tables. Policy lists are now returned sorted. The Complement entrypoint loads the registrations Complement mounts in /complement/appservice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
248 lines
6.6 KiB
Go
248 lines
6.6 KiB
Go
package settings_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/dariuszjaniak/warp/server/internal/settings"
|
|
)
|
|
|
|
// memStore is an in-memory storage.SettingsStore.
|
|
type memStore struct {
|
|
mu sync.Mutex
|
|
values map[string]string
|
|
failSet error
|
|
}
|
|
|
|
func newMemStore(initial map[string]string) *memStore {
|
|
values := map[string]string{}
|
|
for k, v := range initial {
|
|
values[k] = v
|
|
}
|
|
return &memStore{values: values}
|
|
}
|
|
|
|
func (m *memStore) Get(_ context.Context, key string) (string, bool, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
v, ok := m.values[key]
|
|
return v, ok, nil
|
|
}
|
|
|
|
func (m *memStore) GetAll(context.Context) (map[string]string, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
out := map[string]string{}
|
|
for k, v := range m.values {
|
|
out[k] = v
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (m *memStore) Set(_ context.Context, key, value string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if m.failSet != nil {
|
|
return m.failSet
|
|
}
|
|
m.values[key] = value
|
|
return nil
|
|
}
|
|
|
|
func (m *memStore) Delete(_ context.Context, key string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
delete(m.values, key)
|
|
return nil
|
|
}
|
|
|
|
func togglableDefaults() settings.Defaults {
|
|
return settings.Defaults{
|
|
FederationEnabled: false,
|
|
FederationAllowRuntimeToggle: true,
|
|
RegistrationEnabled: false,
|
|
RegistrationAllowRuntimeToggle: true,
|
|
}
|
|
}
|
|
|
|
func TestStoredValueOverridesConfig(t *testing.T) {
|
|
store := newMemStore(map[string]string{settings.KeyFederationEnabled: "true"})
|
|
|
|
svc, err := settings.New(t.Context(), store, togglableDefaults(), nil)
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
if !svc.FederationEnabled() {
|
|
t.Error("the persisted value should win over the config default")
|
|
}
|
|
}
|
|
|
|
func TestPinnedSettingIgnoresStoredValue(t *testing.T) {
|
|
store := newMemStore(map[string]string{settings.KeyFederationEnabled: "true"})
|
|
defaults := togglableDefaults()
|
|
defaults.FederationAllowRuntimeToggle = false
|
|
|
|
svc, err := settings.New(t.Context(), store, defaults, nil)
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
if svc.FederationEnabled() {
|
|
t.Error("with allow_runtime_toggle false the config file is authoritative")
|
|
}
|
|
if err := svc.SetFederationEnabled(t.Context(), true); !errors.Is(err, settings.ErrNotToggleable) {
|
|
t.Errorf("SetFederationEnabled = %v, want ErrNotToggleable", err)
|
|
}
|
|
if _, found, _ := store.Get(t.Context(), settings.KeyFederationEnabled); !found {
|
|
t.Error("the stored value should be kept for when the toggle is re-enabled")
|
|
}
|
|
}
|
|
|
|
func TestUnparseableStoredValueFallsBackToConfig(t *testing.T) {
|
|
store := newMemStore(map[string]string{settings.KeyRegistrationEnabled: "maybe"})
|
|
defaults := togglableDefaults()
|
|
defaults.RegistrationEnabled = true
|
|
|
|
svc, err := settings.New(t.Context(), store, defaults, nil)
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
if !svc.RegistrationEnabled() {
|
|
t.Error("a corrupt stored value must fall back to the config value, not to false")
|
|
}
|
|
}
|
|
|
|
func TestSetPersistsAndNotifies(t *testing.T) {
|
|
store := newMemStore(nil)
|
|
svc, err := settings.New(t.Context(), store, togglableDefaults(), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var got []settings.Snapshot
|
|
svc.Subscribe(func(s settings.Snapshot) { got = append(got, s) })
|
|
|
|
if err := svc.SetFederationEnabled(t.Context(), true); err != nil {
|
|
t.Fatalf("SetFederationEnabled: %v", err)
|
|
}
|
|
if !svc.FederationEnabled() {
|
|
t.Error("the cached value was not updated")
|
|
}
|
|
if v, _, _ := store.Get(t.Context(), settings.KeyFederationEnabled); v != "true" {
|
|
t.Errorf("stored value = %q, want \"true\"", v)
|
|
}
|
|
if len(got) != 1 || !got[0].FederationEnabled {
|
|
t.Fatalf("subscriber saw %v, want one snapshot with federation on", got)
|
|
}
|
|
|
|
// Setting the same value again changes nothing and must not notify.
|
|
if err := svc.SetFederationEnabled(t.Context(), true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 1 {
|
|
t.Errorf("a no-op change notified subscribers: %v", got)
|
|
}
|
|
}
|
|
|
|
func TestSetKeepsCacheOnStoreFailure(t *testing.T) {
|
|
store := newMemStore(nil)
|
|
store.failSet = errors.New("disk on fire")
|
|
|
|
svc, err := settings.New(t.Context(), store, togglableDefaults(), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := svc.SetFederationEnabled(t.Context(), true); err == nil {
|
|
t.Fatal("expected the store failure to surface")
|
|
}
|
|
if svc.FederationEnabled() {
|
|
t.Error("the cached value must not change when persistence fails")
|
|
}
|
|
}
|
|
|
|
func TestSnapshotIsConsistent(t *testing.T) {
|
|
store := newMemStore(map[string]string{
|
|
settings.KeyFederationEnabled: "true",
|
|
settings.KeyRegistrationEnabled: "true",
|
|
})
|
|
svc, err := settings.New(t.Context(), store, togglableDefaults(), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
snap := svc.Snapshot()
|
|
if !snap.FederationEnabled || !snap.RegistrationEnabled {
|
|
t.Errorf("Snapshot() = %+v, want both enabled", snap)
|
|
}
|
|
}
|
|
|
|
func TestConcurrentReadsAndWrites(t *testing.T) {
|
|
store := newMemStore(nil)
|
|
svc, err := settings.New(t.Context(), store, togglableDefaults(), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
for i := range 8 {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
if i%2 == 0 {
|
|
_ = svc.SetFederationEnabled(t.Context(), i%4 == 0)
|
|
return
|
|
}
|
|
_ = svc.FederationEnabled()
|
|
_ = svc.Snapshot()
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestFederationListsPersist(t *testing.T) {
|
|
ctx := t.Context()
|
|
store := newMemStore(nil)
|
|
defaults := settings.Defaults{
|
|
FederationAllowlist: []string{"a.example"},
|
|
FederationDenylist: []string{"bad.example"},
|
|
}
|
|
|
|
s, err := settings.New(ctx, store, defaults, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := s.FederationLists(); got.Edited || len(got.Allowlist) != 1 || got.Denylist[0] != "bad.example" {
|
|
t.Fatalf("defaults = %+v", got)
|
|
}
|
|
if err := s.SetFederationLists(ctx, []string{"b.example"}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// A restart keeps the edited lists over the file's.
|
|
again, err := settings.New(ctx, store, defaults, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := again.FederationLists()
|
|
if !got.Edited || len(got.Allowlist) != 1 || got.Allowlist[0] != "b.example" || got.Denylist == nil || len(got.Denylist) != 0 {
|
|
t.Fatalf("after restart = %+v", got)
|
|
}
|
|
|
|
reset, err := again.ResetFederationLists(ctx)
|
|
if err != nil || reset.Edited || reset.Allowlist[0] != "a.example" {
|
|
t.Fatalf("reset = %+v, %v", reset, err)
|
|
}
|
|
if _, ok, _ := store.Get(ctx, settings.KeyFederationLists); ok {
|
|
t.Error("the stored lists survived a reset")
|
|
}
|
|
|
|
// A failed write changes nothing.
|
|
store.failSet = errors.New("disk full")
|
|
if err := again.SetFederationLists(ctx, []string{"c.example"}, nil); err == nil {
|
|
t.Fatal("a failed write succeeded")
|
|
}
|
|
if got := again.FederationLists(); got.Edited || got.Allowlist[0] != "a.example" {
|
|
t.Errorf("after a failed write = %+v", got)
|
|
}
|
|
}
|