- cabana: BackendAccessRevokedEvent with reasons deleted, deactivated, password_changed, password_set, password_reset and permissions, fired synchronously inside the save's transaction; a listener error rolls back - fired on admin delete, deactivate, a password set by another admin, an own password change, admin:reset-password, a role/permissions/superuser change and a role's permission change (every holder); MCPSubjectChanged unchanged - role delete fires nothing (refused while the role has holders); no sign out everywhere action exists - smoke test TestBackendAccessRevokedSmoke (Postgres)
130 lines
4.9 KiB
Go
130 lines
4.9 KiB
Go
package cabana_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"slices"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/cabana"
|
|
)
|
|
|
|
// revokedRecorder records every *cabana.BackendAccessRevokedEvent and, while
|
|
// fail is set, answers an error so the save rolls back.
|
|
type revokedRecorder struct {
|
|
mu sync.Mutex
|
|
events []cabana.BackendAccessRevokedEvent
|
|
fail atomic.Bool
|
|
}
|
|
|
|
func (r *revokedRecorder) take() []cabana.BackendAccessRevokedEvent {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
out := r.events
|
|
r.events = nil
|
|
return out
|
|
}
|
|
|
|
// TestBackendAccessRevokedSmoke drives the D-13 sites over the admin API on
|
|
// Postgres: deleting and deactivating an administrator, another
|
|
// administrator setting a password, an own password change on PUT /account,
|
|
// a role change and a role's permission change each fire one
|
|
// cabana.BackendAccessRevokedEvent with the matching reason, the affected
|
|
// administrator and the save's transaction; a listener error makes the
|
|
// password change fail and leaves the stored hash unchanged.
|
|
func TestBackendAccessRevokedSmoke(t *testing.T) {
|
|
env := newScreensEnv(t)
|
|
rec := &revokedRecorder{}
|
|
env.app.Events.Listen("test", func(_ context.Context, ev *cabana.BackendAccessRevokedEvent) error {
|
|
rec.mu.Lock()
|
|
rec.events = append(rec.events, *ev)
|
|
rec.mu.Unlock()
|
|
if rec.fail.Load() {
|
|
return errors.New("listener refused")
|
|
}
|
|
return nil
|
|
})
|
|
_, rootToken := env.admin(t, "revoked-root", true, "")
|
|
expectOne := func(t *testing.T, reason string, ids ...uint) {
|
|
t.Helper()
|
|
got := rec.take()
|
|
if len(got) != 1 {
|
|
t.Fatalf("%s: %d events, want 1: %+v", reason, len(got), got)
|
|
}
|
|
if got[0].Reason != reason || !slices.Equal(got[0].AdminIDs, ids) || got[0].Tx == nil {
|
|
t.Fatalf("%s: event = %+v, want ids %v with a transaction", reason, got[0], ids)
|
|
}
|
|
}
|
|
|
|
t.Run("delete", func(t *testing.T) {
|
|
gone, _ := env.admin(t, "revoked-gone", false, "")
|
|
rec.take()
|
|
env.expect(t, http.StatusOK, http.MethodDelete, userURL(gone.ID), rootToken, nil)
|
|
expectOne(t, cabana.BackendAccessDeleted, gone.ID)
|
|
})
|
|
|
|
t.Run("deactivate", func(t *testing.T) {
|
|
target, _ := env.admin(t, "revoked-off", false, "")
|
|
rec.take()
|
|
env.expect(t, http.StatusOK, http.MethodPost, userURL(target.ID, "actions", "deactivate"), rootToken, map[string]any{})
|
|
expectOne(t, cabana.BackendAccessDeactivated, target.ID)
|
|
})
|
|
|
|
t.Run("password set by another administrator", func(t *testing.T) {
|
|
target, _ := env.admin(t, "revoked-pw", false, "")
|
|
rec.take()
|
|
env.expect(t, http.StatusOK, http.MethodPut, userURL(target.ID), rootToken,
|
|
map[string]any{"login": target.Login, "email": target.Email, "password": "pass-5678", "password_confirmation": "pass-5678"})
|
|
expectOne(t, cabana.BackendAccessPasswordSet, target.ID)
|
|
})
|
|
|
|
t.Run("role change", func(t *testing.T) {
|
|
target, _ := env.admin(t, "revoked-role", false, "")
|
|
other := env.seedRole(t, roleSeed{Name: "revoked-other"})
|
|
rec.take()
|
|
env.expect(t, http.StatusOK, http.MethodPut, userURL(target.ID), rootToken,
|
|
map[string]any{"login": target.Login, "email": target.Email, "role": other.ID})
|
|
expectOne(t, cabana.BackendAccessPermissions, target.ID)
|
|
})
|
|
|
|
t.Run("role permission change", func(t *testing.T) {
|
|
role := env.seedRole(t, roleSeed{Name: "revoked-perms", Permissions: map[string]int{"acme.demo.access": 1}})
|
|
holder := env.seedAdmin(t, adminSeed{Name: "revoked-holder", Role: role.ID})
|
|
rec.take()
|
|
env.expect(t, http.StatusOK, http.MethodPut, roleURL(role.ID), rootToken,
|
|
roleBody(role, map[string]any{"permissions": map[string]any{"acme.demo.access": 1, pingPermission: 1}}))
|
|
expectOne(t, cabana.BackendAccessPermissions, holder.ID)
|
|
})
|
|
|
|
t.Run("own password change", func(t *testing.T) {
|
|
self, selfToken := env.admin(t, "revoked-self", false, "")
|
|
rec.take()
|
|
env.expect(t, http.StatusOK, http.MethodPut, "/account", selfToken,
|
|
map[string]any{"email": self.Email, "current_password": adminTestPassword, "password": "brand-new-secret", "password_confirmation": "brand-new-secret"})
|
|
expectOne(t, cabana.BackendAccessPasswordChanged, self.ID)
|
|
})
|
|
|
|
t.Run("a listener error rolls the password change back", func(t *testing.T) {
|
|
self, selfToken := env.admin(t, "revoked-veto", false, "")
|
|
before := env.storedUser(t, self.ID).Password
|
|
rec.take()
|
|
rec.fail.Store(true)
|
|
defer rec.fail.Store(false)
|
|
res := env.call(t, http.MethodPut, "/account", selfToken,
|
|
map[string]any{"email": self.Email, "current_password": adminTestPassword, "password": "brand-new-secret", "password_confirmation": "brand-new-secret"})
|
|
if res.Code >= 200 && res.Code < 300 {
|
|
t.Fatalf("vetoed password change answered %d %s", res.Code, res.Body.String())
|
|
}
|
|
if after := env.storedUser(t, self.ID).Password; after != before {
|
|
t.Fatal("a vetoed password change wrote the new hash")
|
|
}
|
|
if got := rec.take(); len(got) != 1 || got[0].Reason != cabana.BackendAccessPasswordChanged {
|
|
t.Fatalf("vetoed change events = %+v", got)
|
|
}
|
|
})
|
|
|
|
}
|