Managers invite people into their organization and workspace admins into their workspace; the email links to a new /invite page that creates the account (or attaches an existing one without an organization) and adds the workspace membership. Invites bypass the signup rules, store only a token hash, expire after seven days, and return the link so it can be passed on when mail is not configured. Inviting someone already in the organization adds them to the workspace directly. Magic links reuse PocketBase's email OTP: the email now carries a link to /magic-link next to the code, and the admin settings gain a switch for it. Managers can also send a user a password-reset email. The dev proxies read DOCWELL_DEV_API so a dev server can target another docwell-server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
75 lines
2.3 KiB
Go
75 lines
2.3 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package access
|
|
|
|
import "testing"
|
|
|
|
func TestInviteToOrg(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
c Caller
|
|
role string
|
|
org string
|
|
wantOrg string
|
|
ok bool
|
|
}{
|
|
{"superadmin picks any org", Caller{ID: "s", Role: RoleSuperadmin}, RoleAdmin, "o2", "o2", true},
|
|
{"admin invites into own org", Caller{ID: "a", Role: RoleAdmin, OrgID: "o1"}, RoleUser, "o2", "o1", true},
|
|
{"admin cannot invite superadmins", Caller{ID: "a", Role: RoleAdmin, OrgID: "o1"}, RoleSuperadmin, "", "", false},
|
|
{"plain users cannot", Caller{ID: "u", Role: RoleUser, OrgID: "o1"}, RoleUser, "o1", "", false},
|
|
}
|
|
for _, tc := range cases {
|
|
org, err := InviteToOrg(tc.c, tc.role, tc.org)
|
|
if (err == nil) != tc.ok || org != tc.wantOrg {
|
|
t.Errorf("%s: org=%q err=%v", tc.name, org, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInviteToWorkspace(t *testing.T) {
|
|
if err := InviteToWorkspace(WorkspaceAdmin, WorkspaceMember); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if err := InviteToWorkspace(WorkspaceAdmin, WorkspaceOwner); err == nil {
|
|
t.Error("admin must not grant owner")
|
|
}
|
|
if err := InviteToWorkspace(WorkspaceMember, WorkspaceGuest); err == nil {
|
|
t.Error("members cannot invite")
|
|
}
|
|
if err := InviteToWorkspace(WorkspaceOwner, "boss"); err == nil {
|
|
t.Error("unknown role")
|
|
}
|
|
}
|
|
|
|
func TestManageInvite(t *testing.T) {
|
|
admin := Caller{ID: "a", Role: RoleAdmin, OrgID: "o1"}
|
|
if ManageInvite(admin, "x", "o1", "") != nil {
|
|
t.Error("org admin manages their org's invites")
|
|
}
|
|
if ManageInvite(admin, "x", "o2", "") == nil {
|
|
t.Error("not another org's")
|
|
}
|
|
user := Caller{ID: "u", Role: RoleUser, OrgID: "o1"}
|
|
if ManageInvite(user, "u", "o1", "") != nil {
|
|
t.Error("the inviter manages their own invite")
|
|
}
|
|
if ManageInvite(user, "x", "o1", WorkspaceAdmin) != nil {
|
|
t.Error("workspace admins manage the workspace's invites")
|
|
}
|
|
if ManageInvite(user, "x", "o1", WorkspaceMember) == nil {
|
|
t.Error("members do not")
|
|
}
|
|
}
|
|
|
|
func TestAcceptInvite(t *testing.T) {
|
|
if AcceptInvite(RoleUser, "", "o1") != nil || AcceptInvite(RoleUser, "o1", "o1") != nil {
|
|
t.Error("no org or the same org may accept")
|
|
}
|
|
if AcceptInvite(RoleUser, "o2", "o1") == nil {
|
|
t.Error("another org's member may not")
|
|
}
|
|
if AcceptInvite(RoleSuperadmin, "", "o1") == nil {
|
|
t.Error("superadmins may not join")
|
|
}
|
|
}
|