@jskit-ai/workspaces-core 0.2.58 → 0.2.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/locales/en.json +11 -1
- package/migrations/workspaces_core_memberships_accepted_invite.cjs +22 -0
- package/migrations/workspaces_core_settings_invites_require_same_email.cjs +20 -0
- package/package.json +9 -9
- package/patterns/workspace-server/PATTERN.md +26 -6
- package/src/server/WorkspacesFeature.js +1 -0
- package/src/server/WorkspacesIntegrationsProvider.js +2 -2
- package/src/server/common/repositories/workspaceInvitesRepository.js +21 -9
- package/src/server/common/repositories/workspaceMemberPagesRepository.js +12 -5
- package/src/server/common/repositories/workspaceMembershipsRepository.js +38 -8
- package/src/server/common/repositories/workspacesRepository.js +18 -0
- package/src/server/common/services/workspaceDirectoryService.js +33 -3
- package/src/server/support/workspaceInvitationsPolicy.js +5 -1
- package/src/server/types.js +24 -2
- package/src/server/workspaceMembers/appEnvironmentInviteMailer.js +26 -2
- package/src/server/workspaceMembers/defaultWorkspaceInviteEmail.js +1 -1
- package/src/server/workspaceMembers/workspaceMembersService.js +1 -0
- package/src/server/workspacePendingInvitations/inviterNotice.js +75 -0
- package/src/server/workspacePendingInvitations/workspacePendingInvitationsService.js +130 -26
- package/src/server/workspaceSettings/workspaceSettingsRepository.js +16 -4
- package/src/server/workspaceSettings/workspaceSettingsService.js +6 -1
- package/src/server/workspacesRuntime.js +16 -2
- package/src/shared/resources/workspaceMembersResource.js +13 -1
- package/src/shared/resources/workspaceMembershipsResource.js +11 -1
- package/src/shared/resources/workspacePendingInvitationsResource.js +2 -0
- package/src/shared/resources/workspaceSettingsResource.js +24 -2
- package/src/shared/tenancyProfile.js +15 -2
- package/test/appEnvironmentInviteMailer.test.js +33 -0
- package/test/featureRuntime.test.js +9 -1
- package/test/inviterNotice.test.js +66 -0
- package/test/repositories.integration.test.js +10 -12
- package/test/repositoryContracts.test.js +1 -0
- package/test/roleLabels.test.js +7 -2
- package/test/support/indexesByUserCases.js +2 -8
- package/test/support/invitationTransactions.js +14 -4
- package/test/support/mysqlWorkspaces.js +8 -9
- package/test/support/transferOwnershipCases.js +3 -9
- package/test/support/workspacesMigrations.js +23 -0
- package/test/tenancyProfile.test.js +16 -0
- package/test/workspaceDirectoryService.mysql.test.js +74 -0
- package/test/workspaceInvitationsPolicy.test.js +11 -0
- package/test/workspaceMemberListRoutes.test.js +9 -2
- package/test/workspaceMembersService.mysql.test.js +3 -3
- package/test/workspacePendingInvitationsService.mysql.test.js +94 -8
- package/test/workspacePendingInvitationsService.test.js +231 -5
- package/test/workspaceRecords.test.js +1 -0
- package/test/workspaceRecordsFeature.test.js +2 -7
- package/test/workspaceSettingsService.test.js +30 -5
|
@@ -69,3 +69,14 @@ test("workspace invitations policy disables invitations when app config disables
|
|
|
69
69
|
|
|
70
70
|
assert.equal(policy.enabled, false);
|
|
71
71
|
});
|
|
72
|
+
|
|
73
|
+
test("workspace invitations policy lets any account accept only when the app opts in and invitations are on", () => {
|
|
74
|
+
const workspaces = { mode: "workspaces", workspace: { enabled: true } };
|
|
75
|
+
assert.equal(resolveWorkspaceInvitationsPolicy({ appConfig: {}, tenancyProfile: workspaces }).anyAccount, false);
|
|
76
|
+
assert.equal(resolveWorkspaceInvitationsPolicy({ appConfig: { workspaceInvitations: { anyAccount: "yes" } }, tenancyProfile: workspaces }).anyAccount, false);
|
|
77
|
+
assert.equal(resolveWorkspaceInvitationsPolicy({ appConfig: { workspaceInvitations: { anyAccount: true } }, tenancyProfile: workspaces }).anyAccount, true);
|
|
78
|
+
assert.equal(resolveWorkspaceInvitationsPolicy({
|
|
79
|
+
appConfig: { workspaceInvitations: { anyAccount: true, enabled: false } },
|
|
80
|
+
tenancyProfile: workspaces
|
|
81
|
+
}).anyAccount, false);
|
|
82
|
+
});
|
|
@@ -20,7 +20,7 @@ const config = Object.freeze({
|
|
|
20
20
|
appPublicUrl: "http://localhost:5173",
|
|
21
21
|
tenancyMode: "workspaces",
|
|
22
22
|
tenancyPolicy: { workspace: { allowSelfCreate: true } },
|
|
23
|
-
workspaceInvitations: { enabled: true },
|
|
23
|
+
workspaceInvitations: { enabled: true, anyAccount: true },
|
|
24
24
|
workspaceMembers: { defaults: { inviteExpiresInMs: 86400000 } },
|
|
25
25
|
workspaceSettings: { defaults: { invitesEnabled: true } },
|
|
26
26
|
surfaceDefaultId: "app",
|
|
@@ -87,7 +87,13 @@ async function membersApp(t) {
|
|
|
87
87
|
const workspace = await workspaces.services.directory.ensurePersonalWorkspaceForUser(owner);
|
|
88
88
|
await workspaces.services.directory.ensurePersonalWorkspaceForUser(outsider);
|
|
89
89
|
await memberships.addMember({ workspaceId: workspace.id, userId: member.id });
|
|
90
|
-
|
|
90
|
+
// The guest joined through an invitation sent to another of their addresses.
|
|
91
|
+
const guestInvite = await workspaces.services.members.createInvite(workspace, owner, { email: "guest.work@example.com", roleSid: "guest" });
|
|
92
|
+
await workspaces.services.pendingInvitations.acceptInviteByToken({
|
|
93
|
+
user: guest,
|
|
94
|
+
token: guestInvite.value.inviteTokenPreview,
|
|
95
|
+
acceptAs: guest.email
|
|
96
|
+
});
|
|
91
97
|
for (const email of ["ivy@example.com", "jon@example.com", "kim@example.com"]) {
|
|
92
98
|
await workspaces.services.members.createInvite(workspace, owner, { email, roleSid: "member" });
|
|
93
99
|
}
|
|
@@ -130,6 +136,7 @@ test("the members list answers its first page and each next page through its rou
|
|
|
130
136
|
for (const request of [page(20), page()]) {
|
|
131
137
|
const all = listAttributes(await get(user, "members", request), "workspace-members", workspace);
|
|
132
138
|
assert.deepEqual(displayNames(all), ["guest", "member", "owner"]);
|
|
139
|
+
assert.deepEqual(all.members.map(({ invitedEmail }) => invitedEmail), ["guest.work@example.com", "", ""]);
|
|
133
140
|
assert.equal(all.nextCursor, null);
|
|
134
141
|
assert.equal(all.workspace.slug, workspace.slug);
|
|
135
142
|
assert.ok(all.roleCatalog.roles.length > 0);
|
|
@@ -166,9 +166,9 @@ test("updateMemberRole and removeMember change an active member of their workspa
|
|
|
166
166
|
assert.deepEqual(Object.keys(updated.value), ["workspace", "members", "nextCursor", "roleCatalog"]);
|
|
167
167
|
assert.deepEqual(updated.value.workspace, membersWorkspace(workspace));
|
|
168
168
|
assert.deepEqual(updated.value.members, [
|
|
169
|
-
{ userId: members.ann.id, roleSid: "admin", status: "active", displayName: "roles-ann", email: "roles-ann@example.test", isOwner: false },
|
|
170
|
-
{ userId: members.bea.id, roleSid: "member", status: "active", displayName: "roles-bea", email: "roles-bea@example.test", isOwner: false },
|
|
171
|
-
{ userId: owner.id, roleSid: "owner", status: "active", displayName: "roles-owner", email: "roles-owner@example.test", isOwner: true }
|
|
169
|
+
{ userId: members.ann.id, roleSid: "admin", status: "active", displayName: "roles-ann", email: "roles-ann@example.test", invitedEmail: "", isOwner: false },
|
|
170
|
+
{ userId: members.bea.id, roleSid: "member", status: "active", displayName: "roles-bea", email: "roles-bea@example.test", invitedEmail: "", isOwner: false },
|
|
171
|
+
{ userId: owner.id, roleSid: "owner", status: "active", displayName: "roles-owner", email: "roles-owner@example.test", invitedEmail: "", isOwner: true }
|
|
172
172
|
], "active members, by name, with the owner marked");
|
|
173
173
|
assert.equal((await db.memberships.findByWorkspaceIdAndUserId(workspace.id, members.ann.id)).roleSid, "admin");
|
|
174
174
|
assert.equal((await db.memberships.findByWorkspaceIdAndUserId(other.id, members.ann.id)).roleSid, "member",
|
|
@@ -18,7 +18,7 @@ import { workspacesDatabase } from "./support/mysqlWorkspaces.js";
|
|
|
18
18
|
const database = workspacesDatabase("da_ws_pending");
|
|
19
19
|
const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/u;
|
|
20
20
|
|
|
21
|
-
function servicesOf(db, acceptanceParticipant = noInviteAcceptanceParticipant) {
|
|
21
|
+
function servicesOf(db, acceptanceParticipant = noInviteAcceptanceParticipant, { anyAccount = false, notices = [] } = {}) {
|
|
22
22
|
return {
|
|
23
23
|
members: createMembersService({
|
|
24
24
|
workspaceMembershipsRepository: db.memberships,
|
|
@@ -36,8 +36,14 @@ function servicesOf(db, acceptanceParticipant = noInviteAcceptanceParticipant) {
|
|
|
36
36
|
pending: createPendingService({
|
|
37
37
|
workspaceInvitesRepository: db.invites,
|
|
38
38
|
workspaceMembershipsRepository: db.memberships,
|
|
39
|
+
workspacesRepository: db.workspaces,
|
|
40
|
+
workspaceSettingsRepository: db.settings,
|
|
39
41
|
roleCatalog: db.roleCatalog,
|
|
40
|
-
acceptanceParticipant
|
|
42
|
+
acceptanceParticipant,
|
|
43
|
+
invitationsAnyAccount: anyAccount,
|
|
44
|
+
async notifyInviter(acceptance) {
|
|
45
|
+
notices.push(acceptance);
|
|
46
|
+
}
|
|
41
47
|
})
|
|
42
48
|
};
|
|
43
49
|
}
|
|
@@ -129,6 +135,7 @@ test("the public token view answers an invite in each state without changing it"
|
|
|
129
135
|
roleSid: "admin",
|
|
130
136
|
roleLabel: "Admin",
|
|
131
137
|
expiresAt: saved.expiresAt,
|
|
138
|
+
requiresSameEmail: true,
|
|
132
139
|
workspace: { id: team.workspace.id, slug: "view", name: "Team view", avatarUrl: "" }
|
|
133
140
|
});
|
|
134
141
|
assert.deepEqual((await services.pending.resolveInviteByToken(encodeInviteTokenHash(saved.tokenHash))).value.id, pending.id,
|
|
@@ -159,6 +166,7 @@ test("the public token view answers an invite in each state without changing it"
|
|
|
159
166
|
maskedEmail: "",
|
|
160
167
|
roleSid: "",
|
|
161
168
|
expiresAt: null,
|
|
169
|
+
requiresSameEmail: true,
|
|
162
170
|
workspace: { id: "", slug: "", name: "", avatarUrl: "" }
|
|
163
171
|
});
|
|
164
172
|
assert.equal((await services.pending.resolveInviteByToken("not-a-real-token")).value.status, "not_found",
|
|
@@ -242,19 +250,26 @@ test("accepting activates the membership with the invite's role and stamps the i
|
|
|
242
250
|
await assert.rejects(services.pending.acceptInviteByToken({ user: other, token: "" }),
|
|
243
251
|
errorWith(400, "workspaces.invite.tokenRequired"));
|
|
244
252
|
|
|
245
|
-
// An expired invite is
|
|
253
|
+
// An expired invite is marked expired, and that commits before the refusal; it reads expired from then on, and every
|
|
254
|
+
// later redeem says so.
|
|
246
255
|
const late = await db.createProfile("accept-quin");
|
|
247
256
|
const expiring = await inviteTo(services, team, late.email, "member");
|
|
248
257
|
await db.knex("workspace_invites").where({ id: expiring.id }).update({ expires_at: "2000-01-01 00:00:00" });
|
|
249
|
-
|
|
258
|
+
const expiredRefusal = (error) => {
|
|
250
259
|
assert.equal(error.status, 409);
|
|
251
260
|
assert.equal(error.code, "workspaces.invite.expired");
|
|
252
|
-
assert.equal(error.transactionOutcome, "committed");
|
|
253
261
|
return true;
|
|
262
|
+
};
|
|
263
|
+
await assert.rejects(services.pending.acceptInviteByToken({ user: late, token: expiring.token }), (error) => {
|
|
264
|
+
assert.equal(error.transactionOutcome, "committed");
|
|
265
|
+
return expiredRefusal(error);
|
|
254
266
|
});
|
|
255
|
-
const
|
|
256
|
-
assert.equal(
|
|
257
|
-
assert.
|
|
267
|
+
const expired = await db.invites.findById(expiring.id);
|
|
268
|
+
assert.equal(expired.status, "expired");
|
|
269
|
+
assert.equal(expired.revokedAt, null);
|
|
270
|
+
assert.equal((await services.pending.resolveInviteByToken(expiring.token)).value.status, "expired");
|
|
271
|
+
await assert.rejects(services.pending.acceptInviteByToken({ user: late, token: expiring.token }), expiredRefusal);
|
|
272
|
+
await assert.rejects(services.pending.refuseInviteByToken({ user: late, token: expiring.token }), expiredRefusal);
|
|
258
273
|
assert.equal(await db.memberships.findByWorkspaceIdAndUserId(team.workspace.id, late.id), null);
|
|
259
274
|
});
|
|
260
275
|
|
|
@@ -290,3 +305,74 @@ test("refusing revokes the invite and adds no membership; redeem accepts only on
|
|
|
290
305
|
"the listed opaque token redeems the invite");
|
|
291
306
|
assert.equal((await db.memberships.findByWorkspaceIdAndUserId(team.workspace.id, user.id)).roleSid, "admin");
|
|
292
307
|
});
|
|
308
|
+
|
|
309
|
+
test("with anyAccount another account joins once it confirms, and the members list shows the address it was invited at", async () => {
|
|
310
|
+
requireTestDatabase(process.env);
|
|
311
|
+
const db = await database.open();
|
|
312
|
+
const notices = [];
|
|
313
|
+
const services = servicesOf(db, noInviteAcceptanceParticipant, { anyAccount: true, notices });
|
|
314
|
+
const team = await teamOf(db, "any");
|
|
315
|
+
const member = await db.createProfile("any-una");
|
|
316
|
+
const sent = await inviteTo(services, team, "una.work@example.test", "member");
|
|
317
|
+
|
|
318
|
+
await assert.rejects(services.pending.acceptInviteByToken({ user: member, token: sent.token }),
|
|
319
|
+
errorWith(409, "workspaces.invite.differentEmailUnconfirmed"));
|
|
320
|
+
assert.equal(await db.memberships.findByWorkspaceIdAndUserId(team.workspace.id, member.id), null);
|
|
321
|
+
assert.equal((await db.invites.findById(sent.id)).status, "pending");
|
|
322
|
+
|
|
323
|
+
await services.pending.acceptInviteByToken({ user: member, token: sent.token, acceptAs: member.email });
|
|
324
|
+
assert.equal((await db.invites.findById(sent.id)).status, "accepted");
|
|
325
|
+
const summary = await db.memberships.findActiveByWorkspaceIdAndUserId(team.workspace.id, member.id);
|
|
326
|
+
assert.equal(summary.email, member.email);
|
|
327
|
+
assert.equal(summary.invitedEmail, "una.work@example.test");
|
|
328
|
+
const page = await db.memberPages.listActivePage(team.workspace.id, { size: 10 });
|
|
329
|
+
const listed = Object.fromEntries(page.members.map((entry) => [entry.userId, entry.invitedEmail]));
|
|
330
|
+
assert.deepEqual(listed, { [team.owner.id]: "", [member.id]: "una.work@example.test" });
|
|
331
|
+
assert.deepEqual(notices.map(({ invite, user }) => [invite.email, user.email]), [["una.work@example.test", member.email]]);
|
|
332
|
+
|
|
333
|
+
// A later invitation to the member's own address, accepted, replaces the one they joined through.
|
|
334
|
+
const own = await inviteTo(services, team, member.email, "admin");
|
|
335
|
+
await services.pending.acceptInviteByToken({ user: member, token: own.token });
|
|
336
|
+
assert.equal((await db.memberships.findActiveByWorkspaceIdAndUserId(team.workspace.id, member.id)).invitedEmail, "");
|
|
337
|
+
assert.equal((await db.memberPages.listActivePage(team.workspace.id, { size: 10 })).members
|
|
338
|
+
.find((entry) => entry.userId === member.id).invitedEmail, "");
|
|
339
|
+
assert.equal(notices.length, 1, "the invited address accepting tells nobody");
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
test("a workspace that requires the invited address refuses another account, and its setting is saved per workspace", async () => {
|
|
343
|
+
requireTestDatabase(process.env);
|
|
344
|
+
const db = await database.open();
|
|
345
|
+
const services = servicesOf(db, noInviteAcceptanceParticipant, { anyAccount: true });
|
|
346
|
+
const team = await teamOf(db, "strict");
|
|
347
|
+
const other = await db.createProfile("strict-vic");
|
|
348
|
+
const sent = await inviteTo(services, team, "strict-wes@example.test", "member");
|
|
349
|
+
|
|
350
|
+
assert.equal((await db.settings.ensureForWorkspaceId(team.workspace.id)).invitesRequireSameEmail, false, "off by default");
|
|
351
|
+
assert.equal((await services.pending.resolveInviteByToken(sent.token)).value.requiresSameEmail, false);
|
|
352
|
+
await db.settings.updateSettingsByWorkspaceId(team.workspace.id, { invitesRequireSameEmail: true });
|
|
353
|
+
assert.equal(await db.settings.findInvitesRequireSameEmail(team.workspace.id), true);
|
|
354
|
+
assert.equal((await services.pending.resolveInviteByToken(sent.token)).value.requiresSameEmail, true);
|
|
355
|
+
|
|
356
|
+
await assert.rejects(services.pending.acceptInviteByToken({ user: other, token: sent.token, acceptAs: other.email }),
|
|
357
|
+
errorWith(403, "workspaces.invite.emailMismatchUser"));
|
|
358
|
+
assert.equal(await db.memberships.findByWorkspaceIdAndUserId(team.workspace.id, other.id), null);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
test("an acceptance participant that fails rolls back the membership and the invitation's acceptance, and nobody is told", async () => {
|
|
362
|
+
requireTestDatabase(process.env);
|
|
363
|
+
const db = await database.open();
|
|
364
|
+
const notices = [];
|
|
365
|
+
const services = servicesOf(db, async (acceptance) => {
|
|
366
|
+
assert.deepEqual(acceptance.workspace, { id: team.workspace.id, slug: "rollback", name: "Team rollback", ownerUserId: team.owner.id });
|
|
367
|
+
throw new Error("The participant refused.");
|
|
368
|
+
}, { anyAccount: true, notices });
|
|
369
|
+
const team = await teamOf(db, "rollback");
|
|
370
|
+
const member = await db.createProfile("rollback-xan");
|
|
371
|
+
const sent = await inviteTo(services, team, "rollback-elsewhere@example.test", "member");
|
|
372
|
+
|
|
373
|
+
await assert.rejects(services.pending.acceptInviteByToken({ user: member, token: sent.token, acceptAs: member.email }),
|
|
374
|
+
/The participant refused\./);
|
|
375
|
+
assert.equal(await db.memberships.findByWorkspaceIdAndUserId(team.workspace.id, member.id), null);
|
|
376
|
+
assert.equal((await db.invites.findById(sent.id)).status, "pending");
|
|
377
|
+
assert.deepEqual(notices, []);
|
|
378
|
+
});
|
|
@@ -9,18 +9,34 @@ import { createWorkspaceRoleCatalog } from "../src/shared/roles.js";
|
|
|
9
9
|
function createFixture({
|
|
10
10
|
pendingInvitesByEmail = [],
|
|
11
11
|
inviteByTokenHash = null,
|
|
12
|
-
resolvedInviteByTokenHash = null
|
|
12
|
+
resolvedInviteByTokenHash = null,
|
|
13
|
+
invitesByTokenHash = {},
|
|
14
|
+
anyAccount = false,
|
|
15
|
+
requireSameEmail = false,
|
|
16
|
+
acceptanceParticipant = noInviteAcceptanceParticipant
|
|
13
17
|
} = {}) {
|
|
14
18
|
const tokenHashCalls = [];
|
|
15
19
|
const resolveTokenHashCalls = [];
|
|
16
20
|
const upsertCalls = [];
|
|
17
21
|
const revokeCalls = [];
|
|
18
22
|
const acceptCalls = [];
|
|
23
|
+
const expireCalls = [];
|
|
24
|
+
const notices = [];
|
|
25
|
+
const events = [];
|
|
19
26
|
|
|
20
27
|
const service = createService({
|
|
21
28
|
workspaceInvitesRepository: {
|
|
22
29
|
async withTransaction(options, work) {
|
|
23
|
-
|
|
30
|
+
events.push("begin");
|
|
31
|
+
const result = await work({ ...options, trx: {} });
|
|
32
|
+
events.push("commit");
|
|
33
|
+
return result;
|
|
34
|
+
},
|
|
35
|
+
async findByTokenHash(tokenHash) {
|
|
36
|
+
return invitesByTokenHash[String(tokenHash || "")] || null;
|
|
37
|
+
},
|
|
38
|
+
async markExpiredById(inviteId) {
|
|
39
|
+
expireCalls.push(Number(inviteId));
|
|
24
40
|
},
|
|
25
41
|
async listPendingByEmail() {
|
|
26
42
|
return Array.isArray(pendingInvitesByEmail) ? [...pendingInvitesByEmail] : [];
|
|
@@ -57,8 +73,23 @@ function createFixture({
|
|
|
57
73
|
});
|
|
58
74
|
}
|
|
59
75
|
},
|
|
76
|
+
workspacesRepository: {
|
|
77
|
+
async findById(workspaceId) {
|
|
78
|
+
return { id: String(workspaceId), slug: "acme", name: "Acme", ownerUserId: "2" };
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
workspaceSettingsRepository: {
|
|
82
|
+
async findInvitesRequireSameEmail() {
|
|
83
|
+
return requireSameEmail;
|
|
84
|
+
}
|
|
85
|
+
},
|
|
60
86
|
roleCatalog: createWorkspaceRoleCatalog(),
|
|
61
|
-
acceptanceParticipant
|
|
87
|
+
acceptanceParticipant,
|
|
88
|
+
invitationsAnyAccount: anyAccount,
|
|
89
|
+
async notifyInviter(acceptance) {
|
|
90
|
+
events.push("notify");
|
|
91
|
+
notices.push(acceptance);
|
|
92
|
+
}
|
|
62
93
|
});
|
|
63
94
|
|
|
64
95
|
return {
|
|
@@ -68,7 +99,10 @@ function createFixture({
|
|
|
68
99
|
resolveTokenHashCalls,
|
|
69
100
|
upsertCalls,
|
|
70
101
|
revokeCalls,
|
|
71
|
-
acceptCalls
|
|
102
|
+
acceptCalls,
|
|
103
|
+
expireCalls,
|
|
104
|
+
notices,
|
|
105
|
+
events
|
|
72
106
|
}
|
|
73
107
|
};
|
|
74
108
|
}
|
|
@@ -348,10 +382,202 @@ for (const transactionOutcome of ["committed", "unknown", "rolledBack"]) {
|
|
|
348
382
|
}
|
|
349
383
|
},
|
|
350
384
|
workspaceMembershipsRepository: {},
|
|
385
|
+
workspacesRepository: {},
|
|
386
|
+
workspaceSettingsRepository: {},
|
|
351
387
|
roleCatalog: createWorkspaceRoleCatalog(),
|
|
352
|
-
acceptanceParticipant: noInviteAcceptanceParticipant
|
|
388
|
+
acceptanceParticipant: noInviteAcceptanceParticipant,
|
|
389
|
+
invitationsAnyAccount: false,
|
|
390
|
+
async notifyInviter() {}
|
|
353
391
|
});
|
|
354
392
|
await assert.rejects(service.acceptInviteByToken({ user: { id: "1", email: "a@example.test" }, token: "valid-token" }),
|
|
355
393
|
(error) => error === failure && error.cause === cause);
|
|
356
394
|
});
|
|
357
395
|
}
|
|
396
|
+
|
|
397
|
+
// An invitation to Acme sent to invited@example.com by user 2, pending until 2030.
|
|
398
|
+
function pendingInvite(tokenHash, overrides = {}) {
|
|
399
|
+
return {
|
|
400
|
+
id: "60",
|
|
401
|
+
workspaceId: "1",
|
|
402
|
+
workspaceSlug: "acme",
|
|
403
|
+
workspaceName: "Acme",
|
|
404
|
+
workspaceAvatarUrl: "",
|
|
405
|
+
email: "invited@example.com",
|
|
406
|
+
roleSid: "member",
|
|
407
|
+
status: "pending",
|
|
408
|
+
tokenHash,
|
|
409
|
+
invitedByUserId: "2",
|
|
410
|
+
expiresAt: "2030-01-01T00:00:00.000Z",
|
|
411
|
+
...overrides
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const OTHER_ACCOUNT = Object.freeze({ id: "7", email: "Other@Example.com", displayName: "Olive" });
|
|
416
|
+
const refusedWith = (status, code) => (error) => error.status === status && error.code === code;
|
|
417
|
+
|
|
418
|
+
test("with anyAccount another account accepts only once it confirms joining as itself, and the inviter is told after commit", async () => {
|
|
419
|
+
const tokenHash = "d".repeat(64);
|
|
420
|
+
const token = encodeInviteTokenHash(tokenHash);
|
|
421
|
+
const participations = [];
|
|
422
|
+
const { service, calls } = createFixture({
|
|
423
|
+
anyAccount: true,
|
|
424
|
+
inviteByTokenHash: { [tokenHash]: pendingInvite(tokenHash) },
|
|
425
|
+
async acceptanceParticipant(acceptance, options) {
|
|
426
|
+
calls.events.push("participant");
|
|
427
|
+
participations.push({ acceptance, trx: options.trx });
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
await assert.rejects(service.acceptInviteByToken({ user: OTHER_ACCOUNT, token }), (error) => {
|
|
432
|
+
assert.equal(error.status, 409);
|
|
433
|
+
assert.equal(error.code, "workspaces.invite.differentEmailUnconfirmed");
|
|
434
|
+
assert.deepEqual(error.params, { email: "other@example.com", maskedEmail: "in*****@example.com" });
|
|
435
|
+
assert.equal(error.message, "This invitation was sent to in*****@example.com. Confirm that you want to join as other@example.com.");
|
|
436
|
+
return true;
|
|
437
|
+
});
|
|
438
|
+
await assert.rejects(
|
|
439
|
+
service.acceptInviteByToken({ user: OTHER_ACCOUNT, token, acceptAs: "someone@example.com" }),
|
|
440
|
+
refusedWith(409, "workspaces.invite.differentEmailUnconfirmed")
|
|
441
|
+
);
|
|
442
|
+
assert.deepEqual(calls.upsertCalls, [], "an unconfirmed acceptance changes nothing");
|
|
443
|
+
assert.deepEqual(calls.acceptCalls, []);
|
|
444
|
+
assert.deepEqual(calls.notices, []);
|
|
445
|
+
|
|
446
|
+
calls.events.length = 0;
|
|
447
|
+
const response = await service.acceptInviteByToken({ user: OTHER_ACCOUNT, token, acceptAs: " OTHER@example.com " });
|
|
448
|
+
assert.deepEqual(response, { decision: "accepted", workspaceId: "1" });
|
|
449
|
+
assert.deepEqual(calls.upsertCalls, [
|
|
450
|
+
{ workspaceId: 1, userId: 7, payload: { roleSid: "member", status: "active", acceptedInviteId: "60" } }
|
|
451
|
+
]);
|
|
452
|
+
assert.deepEqual(calls.acceptCalls, [60]);
|
|
453
|
+
assert.deepEqual(calls.events, ["begin", "participant", "commit", "notify"], "the inviter is told only after commit");
|
|
454
|
+
const [{ acceptance, trx }] = participations;
|
|
455
|
+
assert.ok(trx, "the participant runs in the acceptance's transaction");
|
|
456
|
+
assert.deepEqual(
|
|
457
|
+
{ invite: pick(acceptance.invite, ["id", "email", "workspaceId", "invitedByUserId", "roleSid", "status"]), user: acceptance.user, workspace: acceptance.workspace },
|
|
458
|
+
{
|
|
459
|
+
invite: { id: "60", email: "invited@example.com", workspaceId: "1", invitedByUserId: "2", roleSid: "member", status: "pending" },
|
|
460
|
+
user: { id: "7", email: "other@example.com", displayName: "Olive" },
|
|
461
|
+
workspace: { id: "1", slug: "acme", name: "Acme", ownerUserId: "2" }
|
|
462
|
+
}
|
|
463
|
+
);
|
|
464
|
+
assert.equal(calls.notices.length, 1);
|
|
465
|
+
assert.equal(calls.notices[0], acceptance, "the inviter's notice tells what the participant was told");
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
function pick(source, keys) {
|
|
469
|
+
return Object.fromEntries(keys.map((key) => [key, source[key]]));
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
test("the invited address accepts as before, whatever its case, with no confirmation and no notice", async () => {
|
|
473
|
+
const tokenHash = "f".repeat(64);
|
|
474
|
+
const { service, calls } = createFixture({ anyAccount: true, inviteByTokenHash: { [tokenHash]: pendingInvite(tokenHash) } });
|
|
475
|
+
|
|
476
|
+
const response = await service.acceptInviteByToken({
|
|
477
|
+
user: { id: "8", email: "INVITED@example.com", displayName: "Ivy" },
|
|
478
|
+
token: encodeInviteTokenHash(tokenHash)
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
assert.equal(response.decision, "accepted");
|
|
482
|
+
assert.deepEqual(calls.notices, []);
|
|
483
|
+
assert.deepEqual(calls.events, ["begin", "commit"]);
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
for (const { name, anyAccount, requireSameEmail } of [
|
|
487
|
+
{ name: "without anyAccount", anyAccount: false, requireSameEmail: false },
|
|
488
|
+
{ name: "in a workspace that requires the invited address", anyAccount: true, requireSameEmail: true }
|
|
489
|
+
]) {
|
|
490
|
+
test(`${name} another account cannot accept, confirmed or not`, async () => {
|
|
491
|
+
const tokenHash = "9".repeat(64);
|
|
492
|
+
const token = encodeInviteTokenHash(tokenHash);
|
|
493
|
+
const { service, calls } = createFixture({ anyAccount, requireSameEmail, inviteByTokenHash: { [tokenHash]: pendingInvite(tokenHash) } });
|
|
494
|
+
|
|
495
|
+
for (const acceptAs of [undefined, "other@example.com"]) {
|
|
496
|
+
await assert.rejects(
|
|
497
|
+
service.acceptInviteByToken({ user: OTHER_ACCOUNT, token, acceptAs }),
|
|
498
|
+
refusedWith(403, "workspaces.invite.emailMismatchUser")
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
assert.deepEqual(calls.upsertCalls, []);
|
|
502
|
+
assert.deepEqual(calls.notices, []);
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
test("only the invited address refuses an invitation, with anyAccount too", async () => {
|
|
507
|
+
const tokenHash = "8".repeat(64);
|
|
508
|
+
const { service, calls } = createFixture({ anyAccount: true, inviteByTokenHash: { [tokenHash]: pendingInvite(tokenHash) } });
|
|
509
|
+
|
|
510
|
+
await assert.rejects(
|
|
511
|
+
service.redeemInviteByToken(OTHER_ACCOUNT, { token: encodeInviteTokenHash(tokenHash), decision: "refuse", acceptAs: "other@example.com" }),
|
|
512
|
+
refusedWith(403, "workspaces.invite.emailMismatchUser")
|
|
513
|
+
);
|
|
514
|
+
assert.deepEqual(calls.revokeCalls, []);
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
test("redeem hands the confirmation on to the acceptance", async () => {
|
|
518
|
+
const tokenHash = "7".repeat(64);
|
|
519
|
+
const { service, calls } = createFixture({ anyAccount: true, inviteByTokenHash: { [tokenHash]: pendingInvite(tokenHash) } });
|
|
520
|
+
|
|
521
|
+
const response = await service.redeemInviteByToken(OTHER_ACCOUNT, {
|
|
522
|
+
token: encodeInviteTokenHash(tokenHash),
|
|
523
|
+
decision: "accept",
|
|
524
|
+
acceptAs: "other@example.com"
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
assert.deepEqual(response.value, { decision: "accepted", workspaceId: "1" });
|
|
528
|
+
assert.equal(calls.notices.length, 1);
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
test("the token view says whether only the invited address may accept", async () => {
|
|
532
|
+
const tokenHash = "6".repeat(64);
|
|
533
|
+
const resolvedInviteByTokenHash = { [tokenHash]: pendingInvite(tokenHash) };
|
|
534
|
+
const token = encodeInviteTokenHash(tokenHash);
|
|
535
|
+
for (const [anyAccount, requireSameEmail, expected] of [[false, false, true], [true, false, false], [true, true, true]]) {
|
|
536
|
+
const { service } = createFixture({ anyAccount, requireSameEmail, resolvedInviteByTokenHash });
|
|
537
|
+
assert.equal((await service.resolveInviteByToken(token)).value.requiresSameEmail, expected, `anyAccount ${anyAccount}, strict ${requireSameEmail}`);
|
|
538
|
+
}
|
|
539
|
+
const { service } = createFixture({ anyAccount: true });
|
|
540
|
+
assert.equal((await service.resolveInviteByToken(token)).value.requiresSameEmail, true, "an invitation that is not there");
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
test("sign-up with another address keeps the invitation's own email where any account may accept it", async () => {
|
|
544
|
+
const tokenHash = "5".repeat(64);
|
|
545
|
+
const resolvedInviteByTokenHash = { [tokenHash]: pendingInvite(tokenHash) };
|
|
546
|
+
const token = encodeInviteTokenHash(tokenHash);
|
|
547
|
+
|
|
548
|
+
const lenient = createFixture({ anyAccount: true, resolvedInviteByTokenHash }).service;
|
|
549
|
+
const context = await lenient.resolveInviteContextForAuth({ token, email: "other@example.com" });
|
|
550
|
+
assert.equal(context.email, "invited@example.com", "so the account still confirms its own address");
|
|
551
|
+
assert.equal(context.workspaceId, "1");
|
|
552
|
+
|
|
553
|
+
const strict = createFixture({ anyAccount: true, requireSameEmail: true, resolvedInviteByTokenHash }).service;
|
|
554
|
+
await assert.rejects(strict.resolveInviteContextForAuth({ token, email: "other@example.com" }),
|
|
555
|
+
refusedWith(403, "workspaces.invite.emailMismatchAccount"));
|
|
556
|
+
const off = createFixture({ resolvedInviteByTokenHash }).service;
|
|
557
|
+
await assert.rejects(off.resolveInviteContextForAuth({ token, email: "other@example.com" }),
|
|
558
|
+
refusedWith(403, "workspaces.invite.emailMismatchAccount"));
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
test("an invitation past its expiry is marked expired, not revoked, and every later redeem says expired", async () => {
|
|
562
|
+
const tokenHash = "4".repeat(64);
|
|
563
|
+
const token = encodeInviteTokenHash(tokenHash);
|
|
564
|
+
const late = pendingInvite(tokenHash, { email: "late@example.com", expiresAt: "2000-01-01T00:00:00.000Z" });
|
|
565
|
+
const { service, calls } = createFixture({ inviteByTokenHash: { [tokenHash]: late } });
|
|
566
|
+
const user = { id: "9", email: "late@example.com" };
|
|
567
|
+
|
|
568
|
+
await assert.rejects(service.acceptInviteByToken({ user, token }), (error) => {
|
|
569
|
+
assert.equal(error.code, "workspaces.invite.expired");
|
|
570
|
+
assert.equal(error.transactionOutcome, "committed");
|
|
571
|
+
return true;
|
|
572
|
+
});
|
|
573
|
+
assert.deepEqual(calls.expireCalls, [60]);
|
|
574
|
+
assert.deepEqual(calls.revokeCalls, []);
|
|
575
|
+
|
|
576
|
+
const after = createFixture({ invitesByTokenHash: { [tokenHash]: { ...late, status: "expired" } } }).service;
|
|
577
|
+
for (const decision of ["accept", "refuse"]) {
|
|
578
|
+
await assert.rejects(after.redeemInviteByToken(user, { token, decision }), refusedWith(409, "workspaces.invite.expired"));
|
|
579
|
+
}
|
|
580
|
+
const handled = createFixture({ invitesByTokenHash: { [tokenHash]: { ...late, status: "accepted" } } }).service;
|
|
581
|
+
await assert.rejects(handled.redeemInviteByToken(user, { token, decision: "accept" }),
|
|
582
|
+
refusedWith(404, "workspaces.invite.invitationNotFoundOrHandled"));
|
|
583
|
+
});
|
|
@@ -42,6 +42,7 @@ test("ensureOwnerMembership makes an existing membership an active owner by a pa
|
|
|
42
42
|
userId: "9",
|
|
43
43
|
roleSid: "owner",
|
|
44
44
|
status: "active",
|
|
45
|
+
acceptedInviteId: null,
|
|
45
46
|
createdAt: toIsoString("2026-03-09 00:26:35.710"),
|
|
46
47
|
updatedAt: toIsoString(patches[0].changes.updatedAt)
|
|
47
48
|
});
|
|
@@ -5,18 +5,13 @@ import { defineFeature } from "@jskit-ai/kernel/server/features";
|
|
|
5
5
|
import { startTestFeatures } from "@jskit-ai/resource-runtime/test";
|
|
6
6
|
import { UsersExtensionsProvider } from "@jskit-ai/users-core/server/UsersExtensionsProvider";
|
|
7
7
|
import { UsersIdentityProvider } from "@jskit-ai/users-core/server/UsersIdentityProvider";
|
|
8
|
-
import
|
|
9
|
-
import usersUsername from "../../users-core/migrations/users_core_profile_username.cjs";
|
|
10
|
-
import usersUpdatedAt from "../../users-core/migrations/users_core_profile_updated_at.cjs";
|
|
11
|
-
import workspacesInitial from "../migrations/workspaces_core_initial.cjs";
|
|
12
|
-
import workspaceSettingsMigration from "../migrations/workspaces_core_workspace_settings_single_name_source.cjs";
|
|
13
|
-
import workspaceColorMigration from "../migrations/workspaces_core_workspaces_drop_color.cjs";
|
|
8
|
+
import { WORKSPACES_TEST_MIGRATIONS } from "./support/workspacesMigrations.js";
|
|
14
9
|
import { WorkspaceRecordsFeature } from "../src/test/WorkspaceRecordsFeature.js";
|
|
15
10
|
|
|
16
11
|
test("an app feature's test starts it beside the users and workspaces owners and reaches their records", async (t) => {
|
|
17
12
|
const knex = knexLib({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true });
|
|
18
13
|
t.after(() => knex.destroy());
|
|
19
|
-
for (const migration of
|
|
14
|
+
for (const migration of WORKSPACES_TEST_MIGRATIONS) {
|
|
20
15
|
await migration.up(knex);
|
|
21
16
|
}
|
|
22
17
|
// An app feature that reads a workspace's members through workspaces.core's owner API.
|
|
@@ -15,7 +15,7 @@ function authorizedOptions(permissions = []) {
|
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
function createFixture({ workspaceInvitationsEnabled = true } = {}) {
|
|
18
|
+
function createFixture({ workspaceInvitationsEnabled = true, workspaceInvitationsAnyAccount = false, invitesRequireSameEmail = false } = {}) {
|
|
19
19
|
const defaultTheme = resolveWorkspaceThemePalettes({
|
|
20
20
|
lightPrimaryColor: "#0F6B54"
|
|
21
21
|
});
|
|
@@ -36,12 +36,14 @@ function createFixture({ workspaceInvitationsEnabled = true } = {}) {
|
|
|
36
36
|
darkSecondaryColor: defaultTheme.dark.secondaryColor,
|
|
37
37
|
darkSurfaceColor: defaultTheme.dark.surfaceColor,
|
|
38
38
|
darkSurfaceVariantColor: defaultTheme.dark.surfaceVariantColor,
|
|
39
|
-
invitesEnabled: true
|
|
39
|
+
invitesEnabled: true,
|
|
40
|
+
invitesRequireSameEmail
|
|
40
41
|
}
|
|
41
42
|
};
|
|
42
43
|
|
|
43
44
|
const service = createService({
|
|
44
45
|
workspaceInvitationsEnabled,
|
|
46
|
+
workspaceInvitationsAnyAccount,
|
|
45
47
|
workspaceSettingsRepository: {
|
|
46
48
|
async ensureForWorkspaceId(workspaceId) {
|
|
47
49
|
assert.equal(Number(workspaceId), 7);
|
|
@@ -82,7 +84,9 @@ test("workspaceSettingsService.getWorkspaceSettings returns the stored invitesEn
|
|
|
82
84
|
darkSurfaceVariantColor: "#C8C8C8",
|
|
83
85
|
invitesEnabled: true,
|
|
84
86
|
invitesAvailable: true,
|
|
85
|
-
invitesEffective: true
|
|
87
|
+
invitesEffective: true,
|
|
88
|
+
invitesRequireSameEmail: true,
|
|
89
|
+
invitesAnyAccountAvailable: false
|
|
86
90
|
});
|
|
87
91
|
});
|
|
88
92
|
|
|
@@ -111,7 +115,9 @@ test("workspaceSettingsService.updateWorkspaceSettings writes editable fields th
|
|
|
111
115
|
darkSurfaceVariantColor: "#C8C8C8",
|
|
112
116
|
invitesEnabled: false,
|
|
113
117
|
invitesAvailable: true,
|
|
114
|
-
invitesEffective: false
|
|
118
|
+
invitesEffective: false,
|
|
119
|
+
invitesRequireSameEmail: true,
|
|
120
|
+
invitesAnyAccountAvailable: false
|
|
115
121
|
});
|
|
116
122
|
});
|
|
117
123
|
|
|
@@ -136,6 +142,25 @@ test("workspaceSettingsService disables invite settings in output when app polic
|
|
|
136
142
|
darkSurfaceVariantColor: "#C8C8C8",
|
|
137
143
|
invitesEnabled: false,
|
|
138
144
|
invitesAvailable: false,
|
|
139
|
-
invitesEffective: false
|
|
145
|
+
invitesEffective: false,
|
|
146
|
+
invitesRequireSameEmail: true,
|
|
147
|
+
invitesAnyAccountAvailable: false
|
|
140
148
|
});
|
|
141
149
|
});
|
|
150
|
+
|
|
151
|
+
test("workspaceSettingsService answers and saves invitesRequireSameEmail where the app lets any account accept", async () => {
|
|
152
|
+
const lenient = createFixture({ workspaceInvitationsAnyAccount: true });
|
|
153
|
+
const read = (await lenient.service.getWorkspaceSettings(lenient.state.workspace, authorizedOptions())).value.settings;
|
|
154
|
+
assert.deepEqual(
|
|
155
|
+
{ invitesRequireSameEmail: read.invitesRequireSameEmail, invitesAnyAccountAvailable: read.invitesAnyAccountAvailable },
|
|
156
|
+
{ invitesRequireSameEmail: false, invitesAnyAccountAvailable: true }
|
|
157
|
+
);
|
|
158
|
+
const saved = (await lenient.service.updateWorkspaceSettings(lenient.state.workspace, { invitesRequireSameEmail: true }, authorizedOptions()))
|
|
159
|
+
.value.settings;
|
|
160
|
+
assert.deepEqual(lenient.state.settingsPatch, { invitesRequireSameEmail: true });
|
|
161
|
+
assert.equal(saved.invitesRequireSameEmail, true);
|
|
162
|
+
|
|
163
|
+
// Without anyAccount only the invited address accepts, whatever the workspace saved.
|
|
164
|
+
const off = createFixture({ invitesRequireSameEmail: false });
|
|
165
|
+
assert.equal((await off.service.getWorkspaceSettings(off.state.workspace, authorizedOptions())).value.settings.invitesRequireSameEmail, true);
|
|
166
|
+
});
|