@jskit-ai/workspaces-core 0.2.57 → 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 +8 -0
- package/locales/en.json +11 -2
- 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 +47 -10
- package/src/server/common/services/workspaceMemberContext.js +3 -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 +16 -19
- 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 +78 -3
- package/test/workspaceInvitationsPolicy.test.js +11 -0
- package/test/workspaceMemberListRoutes.test.js +10 -3
- 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/workspaceRequestContextQueries.test.js +51 -10
- package/test/workspaceService.test.js +1 -1
- package/test/workspaceSettingsService.test.js +30 -5
|
@@ -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.
|
|
@@ -11,7 +11,7 @@ import { jsonApiResultValue } from "@jskit-ai/http-runtime/shared/validators/jso
|
|
|
11
11
|
import { resolveActorId } from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
12
12
|
import { defineFeature } from "@jskit-ai/kernel/server/features";
|
|
13
13
|
import { HttpProvider } from "@jskit-ai/kernel/server/http";
|
|
14
|
-
import {
|
|
14
|
+
import { BootstrapProvider } from "@jskit-ai/kernel/server/runtime";
|
|
15
15
|
import { defineTableResource } from "@jskit-ai/resource-core/shared/tableResource";
|
|
16
16
|
import { defineCrudContract } from "@jskit-ai/resource-crud-core/shared/crudContract";
|
|
17
17
|
import { createPackageTestKnex, startTestFeatures } from "@jskit-ai/resource-runtime/test";
|
|
@@ -184,6 +184,7 @@ async function issuesApp(t) {
|
|
|
184
184
|
createTables: true,
|
|
185
185
|
features: [
|
|
186
186
|
HttpProvider,
|
|
187
|
+
BootstrapProvider,
|
|
187
188
|
AuthExtensionsProvider,
|
|
188
189
|
AuthPolicyProvider,
|
|
189
190
|
UsersExtensionsProvider,
|
|
@@ -195,18 +196,32 @@ async function issuesApp(t) {
|
|
|
195
196
|
NoticeFeature,
|
|
196
197
|
ContextFeature
|
|
197
198
|
],
|
|
198
|
-
requires: {
|
|
199
|
+
requires: {
|
|
200
|
+
profiles: "users.profiles",
|
|
201
|
+
workspaces: "workspaces.core",
|
|
202
|
+
memberships: "workspaces.memberships",
|
|
203
|
+
actions: "runtime.actions",
|
|
204
|
+
bootstrap: "runtime.bootstrap"
|
|
205
|
+
},
|
|
199
206
|
inputs: {
|
|
200
207
|
"auth.service": headerAuthService(signedIn),
|
|
201
208
|
"runtime.app-root": process.cwd(),
|
|
202
|
-
"runtime.bootstrap": createBootstrapRuntime(),
|
|
203
209
|
"runtime.config": config,
|
|
204
210
|
"runtime.env": { APP_ENV: "test" },
|
|
205
211
|
"runtime.fastify": app
|
|
206
212
|
}
|
|
207
213
|
});
|
|
208
214
|
t.after(started.stop);
|
|
209
|
-
const { profiles, workspaces, memberships, actions } = started.capabilities;
|
|
215
|
+
const { profiles, workspaces, memberships, actions, bootstrap } = started.capabilities;
|
|
216
|
+
// The session part of the bootstrap, as users-core's contributor gives it: who the request is signed in as.
|
|
217
|
+
bootstrap.register({
|
|
218
|
+
id: "test.session",
|
|
219
|
+
order: 100,
|
|
220
|
+
contribute: ({ request }) => {
|
|
221
|
+
const profile = signedIn.get(String(request.headers["x-test-user"] || ""));
|
|
222
|
+
return { session: profile ? { authenticated: true, userId: profile.id } : { authenticated: false } };
|
|
223
|
+
}
|
|
224
|
+
});
|
|
210
225
|
const person = async (name) => {
|
|
211
226
|
const profile = await profiles.upsert({
|
|
212
227
|
authProvider: "local",
|
|
@@ -242,6 +257,12 @@ async function issuesApp(t) {
|
|
|
242
257
|
const list = (user, slug = workspace.slug) => get(user, slug, "issues");
|
|
243
258
|
const notice = (user, slug = workspace.slug) => get(user, slug, "notice");
|
|
244
259
|
const context = (user, slug = workspace.slug) => get(user, slug, "context");
|
|
260
|
+
// The browser's bootstrap for a page of a workspace.
|
|
261
|
+
const bootstrapFor = (user, slug) => app.inject({
|
|
262
|
+
method: "GET",
|
|
263
|
+
url: `/api/bootstrap?workspaceSlug=${encodeURIComponent(slug)}`,
|
|
264
|
+
headers: user ? { "x-test-user": user.id } : {}
|
|
265
|
+
});
|
|
245
266
|
// A background job of a feature with a system scope, running one action as a member of the workspace.
|
|
246
267
|
const systemContext = started.resources.declareSystemScope("test.member-jobs", "act for members of every workspace");
|
|
247
268
|
async function runAsMember(user, actionId) {
|
|
@@ -259,7 +280,7 @@ async function issuesApp(t) {
|
|
|
259
280
|
);
|
|
260
281
|
return countQueries(knex, () => actions.execute({ actionId, input: { workspaceSlug: workspace.slug }, context: { ...acting, surface: "app" } }));
|
|
261
282
|
}
|
|
262
|
-
return { list, notice, context, runAsMember, runInRole, owner, member, guest, outsider, workspace };
|
|
283
|
+
return { list, notice, context, bootstrapFor, runAsMember, runInRole, owner, member, guest, outsider, workspace };
|
|
263
284
|
}
|
|
264
285
|
|
|
265
286
|
// The statements of one read of the workspace context, whatever the database quotes its names with: the workspace by
|
|
@@ -297,7 +318,7 @@ test("a workspace CRUD list request refuses as strictly as before", async (t) =>
|
|
|
297
318
|
|
|
298
319
|
const refusals = [
|
|
299
320
|
["signed out", await list(null), 401, "auth.session.required"],
|
|
300
|
-
["not a member", await list(outsider),
|
|
321
|
+
["not a member", await list(outsider), 404, "workspaces.workspace.notFound"],
|
|
301
322
|
["a member without crud.issues.list", await list(guest), 403, "ACTION_PERMISSION_DENIED"],
|
|
302
323
|
["an unknown workspace", await list(owner, "no-such-workspace"), 404, "workspaces.workspace.notFound"]
|
|
303
324
|
];
|
|
@@ -309,6 +330,25 @@ test("a workspace CRUD list request refuses as strictly as before", async (t) =>
|
|
|
309
330
|
}
|
|
310
331
|
});
|
|
311
332
|
|
|
333
|
+
// Someone outside a workspace learns nothing of it: every workspace route and the bootstrap answer its slug exactly as
|
|
334
|
+
// they answer a slug no workspace has, so they can't tell an existing workspace from a typo.
|
|
335
|
+
test("a workspace the person isn't in is answered exactly as one that doesn't exist", async (t) => {
|
|
336
|
+
const { list, notice, context, bootstrapFor, outsider, workspace } = await issuesApp(t);
|
|
337
|
+
const unknown = "no-such-workspace";
|
|
338
|
+
|
|
339
|
+
for (const [label, read] of [["a CRUD list", list], ["an optional-context route", notice], ["a member's route", context]]) {
|
|
340
|
+
const [theirs, missing] = [(await read(outsider, workspace.slug)).response, (await read(outsider, unknown)).response];
|
|
341
|
+
assert.equal(theirs.statusCode, 404, `${label}: ${theirs.body}`);
|
|
342
|
+
assert.deepEqual([theirs.statusCode, theirs.json()], [missing.statusCode, missing.json()], label);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const [theirs, missing] = [await bootstrapFor(outsider, workspace.slug), await bootstrapFor(outsider, unknown)];
|
|
346
|
+
assert.equal(theirs.statusCode, 200, theirs.body);
|
|
347
|
+
assert.deepEqual(theirs.json().requestedWorkspace, { slug: workspace.slug, status: "not_found" });
|
|
348
|
+
const withoutSlug = (payload) => ({ ...payload, requestedWorkspace: { ...payload.requestedWorkspace, slug: "" } });
|
|
349
|
+
assert.deepEqual(withoutSlug(theirs.json()), withoutSlug(missing.json()));
|
|
350
|
+
});
|
|
351
|
+
|
|
312
352
|
test("a route whose auth policy and action context differ in requiring membership reads the context once", async (t) => {
|
|
313
353
|
const { notice, owner, member, outsider, workspace } = await issuesApp(t);
|
|
314
354
|
|
|
@@ -326,8 +366,8 @@ test("a route whose auth policy and action context differ in requiring membershi
|
|
|
326
366
|
|
|
327
367
|
// The shared read still refuses a non-member where membership is required.
|
|
328
368
|
const { response, queries } = await notice(outsider);
|
|
329
|
-
assert.equal(response.statusCode,
|
|
330
|
-
assert.equal(response.body.includes("workspaces.workspace.
|
|
369
|
+
assert.equal(response.statusCode, 404, response.body);
|
|
370
|
+
assert.equal(response.body.includes("workspaces.workspace.notFound"), true, response.body);
|
|
331
371
|
assert.equal(reading(queries, "workspace").length, 1, queries.join("\n"));
|
|
332
372
|
});
|
|
333
373
|
|
|
@@ -383,8 +423,9 @@ test("a role context runs an action as a member in that role would, for a user w
|
|
|
383
423
|
}
|
|
384
424
|
|
|
385
425
|
// The permission check passes as the role: the member role lists the workspace's issues, which the outsider's and
|
|
386
|
-
// the guest's own requests may not, and the guest role is refused as a
|
|
387
|
-
|
|
426
|
+
// the guest's own requests may not (the outsider is told it doesn't exist), and the guest role is refused as a
|
|
427
|
+
// guest's request is.
|
|
428
|
+
assert.equal((await list(outsider)).response.statusCode, 404);
|
|
388
429
|
assert.equal((await list(guest)).response.statusCode, 403);
|
|
389
430
|
const memberTitles = (await list(member)).response.json().data.map((record) => record.attributes.title).sort();
|
|
390
431
|
for (const user of [outsider, guest]) {
|
|
@@ -726,7 +726,7 @@ test("workspaceService.resolveWorkspaceContextForUserBySlug can resolve an exist
|
|
|
726
726
|
{ id: "7", email: "customer@example.com" },
|
|
727
727
|
"dog-and-groom"
|
|
728
728
|
),
|
|
729
|
-
/
|
|
729
|
+
/Workspace not found/
|
|
730
730
|
);
|
|
731
731
|
|
|
732
732
|
const context = await service.resolveWorkspaceContextForUserBySlug(
|
|
@@ -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
|
+
});
|