@jskit-ai/workspaces-core 0.2.58 → 0.2.60
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
|
@@ -85,10 +85,14 @@ const WORKSPACE_SETTINGS_FIELD_MESSAGES = deepFreeze({
|
|
|
85
85
|
invitesEnabledInvalid: {
|
|
86
86
|
code: "workspaces.settings.invitesEnabled.invalid",
|
|
87
87
|
message: "invitesEnabled must be a boolean."
|
|
88
|
+
},
|
|
89
|
+
invitesRequireSameEmailInvalid: {
|
|
90
|
+
code: "workspaces.settings.invitesRequireSameEmail.invalid",
|
|
91
|
+
message: "invitesRequireSameEmail must be a boolean."
|
|
88
92
|
}
|
|
89
93
|
});
|
|
90
94
|
|
|
91
|
-
const WORKSPACE_SETTINGS_FIELD_KEYS = deepFreeze([...WORKSPACE_SETTINGS_COLOR_FIELDS, "invitesEnabled"]);
|
|
95
|
+
const WORKSPACE_SETTINGS_FIELD_KEYS = deepFreeze([...WORKSPACE_SETTINGS_COLOR_FIELDS, "invitesEnabled", "invitesRequireSameEmail"]);
|
|
92
96
|
|
|
93
97
|
const workspaceSettingsBodySchema = createSchema({
|
|
94
98
|
lightPrimaryColor: {
|
|
@@ -195,6 +199,15 @@ const workspaceSettingsBodySchema = createSchema({
|
|
|
195
199
|
required: WORKSPACE_SETTINGS_FIELD_MESSAGES.invitesEnabledRequired,
|
|
196
200
|
default: WORKSPACE_SETTINGS_FIELD_MESSAGES.invitesEnabledInvalid
|
|
197
201
|
}
|
|
202
|
+
},
|
|
203
|
+
// Optional in every write, so a full write made before it existed stays valid; a new workspace takes the app's default.
|
|
204
|
+
invitesRequireSameEmail: {
|
|
205
|
+
type: "boolean",
|
|
206
|
+
required: false,
|
|
207
|
+
strictBoolean: true,
|
|
208
|
+
messages: {
|
|
209
|
+
default: WORKSPACE_SETTINGS_FIELD_MESSAGES.invitesRequireSameEmailInvalid
|
|
210
|
+
}
|
|
198
211
|
}
|
|
199
212
|
});
|
|
200
213
|
|
|
@@ -215,7 +228,11 @@ const workspaceSettingsViewSchema = createSchema({
|
|
|
215
228
|
darkSurfaceVariantColor: { type: "string", required: true, minLength: 7, maxLength: 7, pattern: "^#[0-9A-Fa-f]{6}$" },
|
|
216
229
|
invitesEnabled: { type: "boolean", required: true },
|
|
217
230
|
invitesAvailable: { type: "boolean", required: true },
|
|
218
|
-
invitesEffective: { type: "boolean", required: true }
|
|
231
|
+
invitesEffective: { type: "boolean", required: true },
|
|
232
|
+
// Whether only the invited address may accept the workspace's invitations: true whenever the app keeps that rule
|
|
233
|
+
// for every workspace (invitesAnyAccountAvailable false).
|
|
234
|
+
invitesRequireSameEmail: { type: "boolean", required: true },
|
|
235
|
+
invitesAnyAccountAvailable: { type: "boolean", required: true }
|
|
219
236
|
});
|
|
220
237
|
|
|
221
238
|
const workspaceSettingsOutputSchema = createSchema({
|
|
@@ -323,6 +340,11 @@ const workspaceSettingsResource = defineTableResource({
|
|
|
323
340
|
defaultTo: true,
|
|
324
341
|
storage: { column: "invites_enabled" }
|
|
325
342
|
},
|
|
343
|
+
invitesRequireSameEmail: {
|
|
344
|
+
type: "boolean",
|
|
345
|
+
defaultTo: false,
|
|
346
|
+
storage: { column: "invites_require_same_email" }
|
|
347
|
+
},
|
|
326
348
|
createdAt: {
|
|
327
349
|
type: "dateTime",
|
|
328
350
|
storage: {
|
|
@@ -9,6 +9,18 @@ import { isRecord } from "@jskit-ai/kernel/shared/support/normalize";
|
|
|
9
9
|
const WORKSPACE_SLUG_POLICY_NONE = "none";
|
|
10
10
|
const WORKSPACE_SLUG_POLICY_IMMUTABLE_USERNAME = "immutable_username";
|
|
11
11
|
const WORKSPACE_SLUG_POLICY_USER_SELECTED = "user_selected";
|
|
12
|
+
// autoProvision's third value: a personal workspace only for a person who is no active member of any workspace and
|
|
13
|
+
// whose sign-in carries no invitation they can use.
|
|
14
|
+
const WORKSPACE_AUTO_PROVISION_UNLESS_MEMBER = "unless-member";
|
|
15
|
+
|
|
16
|
+
function resolveAutoProvisionOverride(value) {
|
|
17
|
+
if (value === undefined || typeof value === "boolean" || value === WORKSPACE_AUTO_PROVISION_UNLESS_MEMBER) {
|
|
18
|
+
return value ?? null;
|
|
19
|
+
}
|
|
20
|
+
throw new TypeError(
|
|
21
|
+
`tenancyPolicy.workspace.autoProvision must be true, false or "${WORKSPACE_AUTO_PROVISION_UNLESS_MEMBER}". Received: ${String(value)}`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
12
24
|
|
|
13
25
|
function resolveWorkspacePolicyOverrides(appConfig = {}) {
|
|
14
26
|
const tenancyPolicy = isRecord(appConfig.tenancyPolicy) ? appConfig.tenancyPolicy : {};
|
|
@@ -16,7 +28,7 @@ function resolveWorkspacePolicyOverrides(appConfig = {}) {
|
|
|
16
28
|
|
|
17
29
|
return Object.freeze({
|
|
18
30
|
allowSelfCreate: typeof workspacePolicy.allowSelfCreate === "boolean" ? workspacePolicy.allowSelfCreate : null,
|
|
19
|
-
autoProvision:
|
|
31
|
+
autoProvision: resolveAutoProvisionOverride(workspacePolicy.autoProvision)
|
|
20
32
|
});
|
|
21
33
|
}
|
|
22
34
|
|
|
@@ -41,7 +53,7 @@ function resolveWorkspacePolicy(mode, overrides = {}) {
|
|
|
41
53
|
|
|
42
54
|
return Object.freeze({
|
|
43
55
|
enabled: true,
|
|
44
|
-
autoProvision: overrides.autoProvision
|
|
56
|
+
autoProvision: overrides.autoProvision ?? false,
|
|
45
57
|
allowSelfCreate: overrides.allowSelfCreate === true,
|
|
46
58
|
slugPolicy: WORKSPACE_SLUG_POLICY_USER_SELECTED
|
|
47
59
|
});
|
|
@@ -69,6 +81,7 @@ export {
|
|
|
69
81
|
WORKSPACE_SLUG_POLICY_NONE,
|
|
70
82
|
WORKSPACE_SLUG_POLICY_IMMUTABLE_USERNAME,
|
|
71
83
|
WORKSPACE_SLUG_POLICY_USER_SELECTED,
|
|
84
|
+
WORKSPACE_AUTO_PROVISION_UNLESS_MEMBER,
|
|
72
85
|
resolveTenancyProfile,
|
|
73
86
|
isWorkspacesTenancyMode
|
|
74
87
|
};
|
|
@@ -75,3 +75,36 @@ test("staging without STAGING_EMAIL_TO logs each invitation, and production send
|
|
|
75
75
|
assert.equal((await production.mailer.sendWorkspaceInvite(INVITE, {})).status, "sent");
|
|
76
76
|
assert.deepEqual(production.sent[0].invite, INVITE);
|
|
77
77
|
});
|
|
78
|
+
|
|
79
|
+
const NOTICE = Object.freeze({
|
|
80
|
+
inviteId: "7",
|
|
81
|
+
invite: { id: "7", email: "guest@example.com", roleSid: "member" },
|
|
82
|
+
inviter: { id: "2", email: "owner@example.com", displayName: "Olga" },
|
|
83
|
+
member: { id: "9", email: "other@example.com", displayName: "Otto" },
|
|
84
|
+
workspace: { id: "3", slug: "books", name: "Books" },
|
|
85
|
+
locale: "en",
|
|
86
|
+
message: Object.freeze({ subject: "Otto joined Books", text: "Otto accepted.", html: "<p>Otto accepted.</p>" })
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("the notice that another address accepted is sent as invitations are, only by a mailer that sends it", async () => {
|
|
90
|
+
const noticesSent = [];
|
|
91
|
+
const withNotice = createAppEnvironmentInviteMailer({
|
|
92
|
+
env: { ...SERVER_NODE_ENV, APP_ENV: "staging", STAGING_EMAIL_TO: "qa@example.test" },
|
|
93
|
+
logger: { info() {}, warn() {}, error() {}, debug() {} },
|
|
94
|
+
mailer: {
|
|
95
|
+
async sendWorkspaceInvite() {},
|
|
96
|
+
async sendInviteAcceptedByOtherAddress(notice, options) {
|
|
97
|
+
noticesSent.push({ notice, options });
|
|
98
|
+
return { status: "sent" };
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
assert.deepEqual(await withNotice.sendInviteAcceptedByOtherAddress(NOTICE, { context: "c" }), { status: "sent" });
|
|
103
|
+
assert.equal(noticesSent.length, 1);
|
|
104
|
+
assert.equal(noticesSent[0].notice.inviter.email, "qa@example.test", "staging sends it to STAGING_EMAIL_TO");
|
|
105
|
+
assert.match(noticesSent[0].notice.message.text, /^Staging: this email was for owner@example\.com\./u);
|
|
106
|
+
assert.deepEqual(noticesSent[0].options, { context: "c" });
|
|
107
|
+
|
|
108
|
+
const { mailer: withoutNotice } = mailerIn({ APP_ENV: "development" });
|
|
109
|
+
assert.equal(Object.hasOwn(withoutNotice, "sendInviteAcceptedByOtherAddress"), false);
|
|
110
|
+
});
|
|
@@ -15,7 +15,7 @@ function runtimeConfig() {
|
|
|
15
15
|
return {
|
|
16
16
|
appPublicUrl: "http://localhost:5173",
|
|
17
17
|
tenancyMode: "workspaces",
|
|
18
|
-
tenancyPolicy: { workspace: { allowSelfCreate: true } },
|
|
18
|
+
tenancyPolicy: { workspace: { allowSelfCreate: true, autoProvision: "unless-member" } },
|
|
19
19
|
workspaceInvitations: { enabled: true },
|
|
20
20
|
workspaceMembers: { defaults: { inviteExpiresInMs: 86400000 } },
|
|
21
21
|
workspaceSettings: { defaults: { invitesEnabled: true } },
|
|
@@ -122,6 +122,14 @@ test("WorkspacesFeature assembles workspace behavior and explicit integrations",
|
|
|
122
122
|
assert.deepEqual(authExtensions.diagnostics().invitationContextResolverIds, ["workspaces.invitation"]);
|
|
123
123
|
assert.deepEqual(authExtensions.diagnostics().policyContextResolverIds, ["workspaces.policy"]);
|
|
124
124
|
assert.deepEqual(audienceContributors.map((entry) => entry.id), ["workspaces.memberships"]);
|
|
125
|
+
|
|
126
|
+
// Sign-in provisioning is told the invitation the sign-in carries: with "unless-member" one naming a workspace means
|
|
127
|
+
// no workspace of one's own, decided before any read (this database has no tables, so a read would fail).
|
|
128
|
+
const [provisioning] = usersExtensions.profileSyncLifecycleContributors();
|
|
129
|
+
const profile = { id: "5", email: "ivy@example.com", displayName: "Ivy" };
|
|
130
|
+
await provisioning.afterIdentityProfileSynced({ profile, created: true, invitation: { workspaceId: "3" }, options: {} });
|
|
131
|
+
await assert.rejects(provisioning.afterIdentityProfileSynced({ profile, created: true, invitation: null, options: {} }),
|
|
132
|
+
/workspace_memberships/u, "without an invitation it reads the person's memberships");
|
|
125
133
|
});
|
|
126
134
|
|
|
127
135
|
test("workspace realtime audiences are the actor's active memberships", async () => {
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { createWorkspacesServerI18n } from "../src/server/i18n.js";
|
|
4
|
+
import { createInviterNotice } from "../src/server/workspacePendingInvitations/inviterNotice.js";
|
|
5
|
+
|
|
6
|
+
const ACCEPTANCE = Object.freeze({
|
|
7
|
+
invite: { id: "60", email: "invited@example.com", workspaceId: "1", invitedByUserId: "2", roleSid: "member", tokenHash: "secret" },
|
|
8
|
+
user: { id: "7", email: "other@example.com", displayName: "Olive" },
|
|
9
|
+
workspace: { id: "1", slug: "acme", name: "Acme <Labs>", ownerUserId: "2" }
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
function noticeWith({ mailer, profiles = { "2": { id: "2", email: "owner@example.com", displayName: "Oscar" } } }) {
|
|
13
|
+
const warnings = [];
|
|
14
|
+
const reads = [];
|
|
15
|
+
const notify = createInviterNotice({
|
|
16
|
+
inviteMailer: mailer,
|
|
17
|
+
userProfiles: { async findById(id) { reads.push(id); return profiles[id] ?? null; } },
|
|
18
|
+
userSettings: { async findByUserId() { return { locale: "en" }; } },
|
|
19
|
+
i18n: createWorkspacesServerI18n(),
|
|
20
|
+
logger: { warn: (details, message) => warnings.push({ details, message }) }
|
|
21
|
+
});
|
|
22
|
+
return { notify, warnings, reads };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
test("the inviter is sent what happened, with the facts and the email, and nothing secret", async () => {
|
|
26
|
+
const sent = [];
|
|
27
|
+
const { notify } = noticeWith({ mailer: { async sendWorkspaceInvite() {}, async sendInviteAcceptedByOtherAddress(notice, options) { sent.push({ notice, options }); } } });
|
|
28
|
+
|
|
29
|
+
await notify(ACCEPTANCE, { context: "request" });
|
|
30
|
+
|
|
31
|
+
assert.equal(sent.length, 1);
|
|
32
|
+
const { message, ...facts } = sent[0].notice;
|
|
33
|
+
assert.deepEqual(facts, {
|
|
34
|
+
inviteId: "60",
|
|
35
|
+
invite: { id: "60", email: "invited@example.com", roleSid: "member" },
|
|
36
|
+
inviter: { id: "2", email: "owner@example.com", displayName: "Oscar" },
|
|
37
|
+
member: { id: "7", email: "other@example.com", displayName: "Olive" },
|
|
38
|
+
workspace: { id: "1", slug: "acme", name: "Acme <Labs>" },
|
|
39
|
+
locale: "en"
|
|
40
|
+
});
|
|
41
|
+
assert.equal(message.subject, "Olive joined Acme <Labs>");
|
|
42
|
+
assert.equal(message.text,
|
|
43
|
+
"Olive accepted your invitation to Acme <Labs>, which you sent to invited@example.com. They joined with their account other@example.com." +
|
|
44
|
+
"\n\nIf you did not expect this, remove them from the workspace's members.");
|
|
45
|
+
assert.match(message.html, /<p>Olive accepted your invitation to Acme <Labs>,/u);
|
|
46
|
+
assert.deepEqual(sent[0].options, { context: "request" });
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("a failed notice is logged and the acceptance stands; a mailer without it, or an inviter who is gone, sends nothing", async () => {
|
|
50
|
+
const failing = noticeWith({ mailer: { async sendInviteAcceptedByOtherAddress() { throw new Error("SMTP down"); } } });
|
|
51
|
+
await failing.notify(ACCEPTANCE);
|
|
52
|
+
assert.equal(failing.warnings.length, 1);
|
|
53
|
+
assert.equal(failing.warnings[0].message, "The inviter was not told that another address accepted their invitation.");
|
|
54
|
+
assert.deepEqual(failing.warnings[0].details.inviteId, "60");
|
|
55
|
+
|
|
56
|
+
const silent = noticeWith({ mailer: { async sendWorkspaceInvite() {} } });
|
|
57
|
+
await silent.notify(ACCEPTANCE);
|
|
58
|
+
assert.deepEqual(silent.reads, [], "no mailer method: nothing is read or sent");
|
|
59
|
+
|
|
60
|
+
const sent = [];
|
|
61
|
+
const gone = noticeWith({ mailer: { async sendInviteAcceptedByOtherAddress(notice) { sent.push(notice); } }, profiles: {} });
|
|
62
|
+
await gone.notify(ACCEPTANCE);
|
|
63
|
+
await gone.notify({ ...ACCEPTANCE, invite: { ...ACCEPTANCE.invite, invitedByUserId: null } });
|
|
64
|
+
assert.deepEqual(sent, []);
|
|
65
|
+
assert.deepEqual(gone.warnings, []);
|
|
66
|
+
});
|
|
@@ -12,12 +12,7 @@ import { userProfileTableResource } from "@jskit-ai/users-core/shared/resources/
|
|
|
12
12
|
import { userSettingsTableResource } from "@jskit-ai/users-core/shared/resources/userSettingsResource";
|
|
13
13
|
import { UsersExtensionsProvider } from "@jskit-ai/users-core/server/UsersExtensionsProvider";
|
|
14
14
|
import { UsersIdentityProvider } from "@jskit-ai/users-core/server/UsersIdentityProvider";
|
|
15
|
-
import
|
|
16
|
-
import usersUsername from "../../users-core/migrations/users_core_profile_username.cjs";
|
|
17
|
-
import usersUpdatedAt from "../../users-core/migrations/users_core_profile_updated_at.cjs";
|
|
18
|
-
import workspacesInitial from "../migrations/workspaces_core_initial.cjs";
|
|
19
|
-
import workspaceSettingsMigration from "../migrations/workspaces_core_workspace_settings_single_name_source.cjs";
|
|
20
|
-
import workspaceColorMigration from "../migrations/workspaces_core_workspaces_drop_color.cjs";
|
|
15
|
+
import { WORKSPACES_TEST_MIGRATIONS } from "./support/workspacesMigrations.js";
|
|
21
16
|
import { createWorkspaceRecords, createWorkspacesRuntime } from "../src/server/WorkspacesFeature.js";
|
|
22
17
|
import { ROLE_CONTEXT_TABLES, createStandaloneRoleContext } from "../src/server/standaloneRoleContext.js";
|
|
23
18
|
import { noInviteAcceptanceParticipant } from "../src/server/workspacePendingInvitations/noInviteAcceptanceParticipant.js";
|
|
@@ -38,7 +33,7 @@ import { createWorkspaceRoleCatalog } from "../src/shared/roles.js";
|
|
|
38
33
|
import { verifyInvitationTransactions } from "./support/invitationTransactions.js";
|
|
39
34
|
|
|
40
35
|
const TABLES = [
|
|
41
|
-
"
|
|
36
|
+
"workspace_memberships", "workspace_invites", "workspace_settings", "workspaces", "user_settings", "users"
|
|
42
37
|
];
|
|
43
38
|
const databaseUrl = process.env.JSKIT_REPOSITORY_TEST_DATABASE_URL;
|
|
44
39
|
|
|
@@ -122,9 +117,7 @@ describe(`users and workspaces repositories on migrated ${databaseLabel}`, () =>
|
|
|
122
117
|
await knex.raw("PRAGMA foreign_keys = ON");
|
|
123
118
|
}
|
|
124
119
|
ownsTables = true;
|
|
125
|
-
for (const migration of
|
|
126
|
-
usersInitial, usersUsername, usersUpdatedAt, workspacesInitial, workspaceSettingsMigration, workspaceColorMigration
|
|
127
|
-
]) {
|
|
120
|
+
for (const migration of WORKSPACES_TEST_MIGRATIONS) {
|
|
128
121
|
await migration.up(knex);
|
|
129
122
|
}
|
|
130
123
|
// The tables owned as UsersIdentityProvider and WorkspacesFeature own them.
|
|
@@ -142,9 +135,13 @@ describe(`users and workspaces repositories on migrated ${databaseLabel}`, () =>
|
|
|
142
135
|
workspaceTable = resources.workspaces;
|
|
143
136
|
({ workspaces, memberships } = createWorkspaceRecords(resources));
|
|
144
137
|
invites = createInvitesRepository({ workspaceInvites: resources.workspaceInvites, workspaces });
|
|
145
|
-
memberPages = createMemberPagesRepository({
|
|
138
|
+
memberPages = createMemberPagesRepository({
|
|
139
|
+
workspaceMemberships: resources.workspaceMemberships,
|
|
140
|
+
workspaceInvites: resources.workspaceInvites,
|
|
141
|
+
userProfiles: profiles
|
|
142
|
+
});
|
|
146
143
|
settings = createSettingsRepository({
|
|
147
|
-
workspaceSettings: resources.workspaceSettings, defaultInvitesEnabled: false, defaultColorSettings: {}
|
|
144
|
+
workspaceSettings: resources.workspaceSettings, defaultInvitesEnabled: false, defaultInvitesRequireSameEmail: false, defaultColorSettings: {}
|
|
148
145
|
});
|
|
149
146
|
});
|
|
150
147
|
|
|
@@ -780,6 +777,7 @@ describe(`users and workspaces repositories on migrated ${databaseLabel}`, () =>
|
|
|
780
777
|
inviteMailer: unconfiguredWorkspaceInviteMailer,
|
|
781
778
|
inviteEmailTemplate: renderDefaultWorkspaceInviteEmail,
|
|
782
779
|
inviteAcceptanceParticipant: noInviteAcceptanceParticipant,
|
|
780
|
+
logger: silentLogger,
|
|
783
781
|
personalWorkspaceName: buildWorkspaceName,
|
|
784
782
|
resources: workspaceResources,
|
|
785
783
|
userProfiles: profiles,
|
package/test/roleLabels.test.js
CHANGED
|
@@ -109,6 +109,7 @@ test("Members and Invitations response validation preserves labels and saved rol
|
|
|
109
109
|
test("Workspace Settings response validation preserves configured labels", async () => {
|
|
110
110
|
const service = createSettingsService({
|
|
111
111
|
workspaceInvitationsEnabled: true,
|
|
112
|
+
workspaceInvitationsAnyAccount: false,
|
|
112
113
|
roleCatalog: catalog,
|
|
113
114
|
workspaceSettingsRepository: {
|
|
114
115
|
async ensureForWorkspaceId() {
|
|
@@ -137,7 +138,11 @@ test("pending and resolved invitation responses derive labels while acceptance s
|
|
|
137
138
|
async withTransaction(options, work) { return work({ ...options, trx: {} }); },
|
|
138
139
|
async markAcceptedById() {}
|
|
139
140
|
},
|
|
140
|
-
workspaceMembershipsRepository: { async upsertMembership(_workspaceId, _userId, patch) { membership = patch; } }
|
|
141
|
+
workspaceMembershipsRepository: { async upsertMembership(_workspaceId, _userId, patch) { membership = patch; } },
|
|
142
|
+
workspacesRepository: { async findById(workspaceId) { return { id: workspaceId, slug: "acme", name: "Acme", ownerUserId: "1" }; } },
|
|
143
|
+
workspaceSettingsRepository: { async findInvitesRequireSameEmail() { return true; } },
|
|
144
|
+
invitationsAnyAccount: false,
|
|
145
|
+
async notifyInviter() {}
|
|
141
146
|
});
|
|
142
147
|
const user = { id: "11", email: invite.email };
|
|
143
148
|
const pending = output(workspacePendingInvitationsContract, "list", { pendingInvites: await service.listPendingInvitesForUser(user) });
|
|
@@ -146,7 +151,7 @@ test("pending and resolved invitation responses derive labels while acceptance s
|
|
|
146
151
|
assert.equal(resolved.roleLabel, "Operator");
|
|
147
152
|
assert.equal(resolved.roleSid, "safety_manager");
|
|
148
153
|
await service.acceptInviteByToken({ user, token });
|
|
149
|
-
assert.deepEqual(membership, { roleSid: "safety_manager", status: "active" });
|
|
154
|
+
assert.deepEqual(membership, { roleSid: "safety_manager", status: "active", acceptedInviteId: "10" });
|
|
150
155
|
assert.equal(Object.hasOwn(invite, "roleLabel"), false);
|
|
151
156
|
});
|
|
152
157
|
|
|
@@ -9,13 +9,8 @@ import { requireTestDatabase } from "@jskit-ai/database-runtime/server/testDatab
|
|
|
9
9
|
import { startTestFeatures } from "@jskit-ai/resource-runtime/test";
|
|
10
10
|
import { UsersExtensionsProvider } from "@jskit-ai/users-core/server/UsersExtensionsProvider";
|
|
11
11
|
import { UsersIdentityProvider } from "@jskit-ai/users-core/server/UsersIdentityProvider";
|
|
12
|
-
import
|
|
13
|
-
import usersUsername from "../../../users-core/migrations/users_core_profile_username.cjs";
|
|
14
|
-
import usersUpdatedAt from "../../../users-core/migrations/users_core_profile_updated_at.cjs";
|
|
12
|
+
import { WORKSPACES_TEST_MIGRATIONS } from "./workspacesMigrations.js";
|
|
15
13
|
import indexesByUser from "../../migrations/workspaces_core_indexes_by_user.cjs";
|
|
16
|
-
import workspacesInitial from "../../migrations/workspaces_core_initial.cjs";
|
|
17
|
-
import workspaceSettingsMigration from "../../migrations/workspaces_core_workspace_settings_single_name_source.cjs";
|
|
18
|
-
import workspaceColorMigration from "../../migrations/workspaces_core_workspaces_drop_color.cjs";
|
|
19
14
|
import { WorkspaceRecordsFeature } from "../../src/test/WorkspaceRecordsFeature.js";
|
|
20
15
|
|
|
21
16
|
// A person's workspaces are read through an index on every dialect: the statements workspaces.listForUserId (their
|
|
@@ -23,7 +18,6 @@ import { WorkspaceRecordsFeature } from "../../src/test/WorkspaceRecordsFeature.
|
|
|
23
18
|
// by the database's own planner. On SQLite and PostgreSQL the indexes are workspaces_core_indexes_by_user's; on
|
|
24
19
|
// MariaDB/MySQL they are the ones InnoDB made for the foreign keys, and the migration changes nothing.
|
|
25
20
|
|
|
26
|
-
const EARLIER_MIGRATIONS = [usersInitial, usersUsername, usersUpdatedAt, workspacesInitial, workspaceSettingsMigration, workspaceColorMigration];
|
|
27
21
|
const PEOPLE = 60;
|
|
28
22
|
// The index each read uses, by dialect: the migration's own, or InnoDB's for the foreign key.
|
|
29
23
|
const READS = Object.freeze({
|
|
@@ -152,7 +146,7 @@ function indexesByUserCases(open, { dialect, database = true, skip = false }) {
|
|
|
152
146
|
test(`a person's memberships and owned workspaces are read through an index (${dialect})`, { skip }, async (t) => {
|
|
153
147
|
if (database) requireTestDatabase(process.env);
|
|
154
148
|
const knex = await open(t);
|
|
155
|
-
for (const migration of
|
|
149
|
+
for (const migration of WORKSPACES_TEST_MIGRATIONS) await migration.up(knex);
|
|
156
150
|
await indexesByUser.up(knex);
|
|
157
151
|
const person = await seed(knex);
|
|
158
152
|
if (dialect === "postgres") await knex.raw("analyze");
|
|
@@ -67,6 +67,13 @@ export async function verifyInvitationTransactions(t, db) {
|
|
|
67
67
|
}
|
|
68
68
|
})]);
|
|
69
69
|
const { workspaces, memberships } = createWorkspaceRecords(resources);
|
|
70
|
+
// The pending invitations service's other collaborators, where only the invited address accepts (no anyAccount).
|
|
71
|
+
const INVITED_ADDRESS_ONLY = Object.freeze({
|
|
72
|
+
workspacesRepository: workspaces,
|
|
73
|
+
workspaceSettingsRepository: { async findInvitesRequireSameEmail() { return true; } },
|
|
74
|
+
invitationsAnyAccount: false,
|
|
75
|
+
async notifyInviter() {}
|
|
76
|
+
});
|
|
70
77
|
const invites = invitesRepository({ workspaceInvites: resources.workspaceInvites, workspaces });
|
|
71
78
|
const roleCatalog = createWorkspaceRoleCatalog({ roleCatalog: {
|
|
72
79
|
workspace: { defaultInviteRole: "member" },
|
|
@@ -109,7 +116,8 @@ export async function verifyInvitationTransactions(t, db) {
|
|
|
109
116
|
let participant = noInviteAcceptanceParticipant;
|
|
110
117
|
const pending = pendingService({
|
|
111
118
|
workspaceInvitesRepository: inviteRepository, workspaceMembershipsRepository: memberships, roleCatalog: createWorkspaceRoleCatalog(),
|
|
112
|
-
acceptanceParticipant: (...args) => participant(...args)
|
|
119
|
+
acceptanceParticipant: (...args) => participant(...args),
|
|
120
|
+
...INVITED_ADDRESS_ONLY
|
|
113
121
|
});
|
|
114
122
|
const options = { context: { actor: user, channel: "api", permissions: ["workspace.members.invite"] } };
|
|
115
123
|
const payload = { email: user.email, roleSid: "member" };
|
|
@@ -321,7 +329,8 @@ export async function verifyInvitationTransactions(t, db) {
|
|
|
321
329
|
let called = false;
|
|
322
330
|
const pending = pendingService({
|
|
323
331
|
workspaceInvitesRepository: invites, workspaceMembershipsRepository: memberships, roleCatalog: createWorkspaceRoleCatalog(),
|
|
324
|
-
acceptanceParticipant: async () => { called = true; }
|
|
332
|
+
acceptanceParticipant: async () => { called = true; },
|
|
333
|
+
...INVITED_ADDRESS_ONLY
|
|
325
334
|
});
|
|
326
335
|
const membershipBefore = await memberships.findByWorkspaceIdAndUserId(f.workspace.id, f.user.id);
|
|
327
336
|
await assert.rejects(pending.acceptInviteByToken({
|
|
@@ -330,7 +339,7 @@ export async function verifyInvitationTransactions(t, db) {
|
|
|
330
339
|
}), (error) => [401, 403, 404, 409].includes(error.status));
|
|
331
340
|
assert.equal(called, false);
|
|
332
341
|
assert.deepEqual(await memberships.findByWorkspaceIdAndUserId(f.workspace.id, f.user.id), membershipBefore);
|
|
333
|
-
if (state === "expired") assert.equal((await f.invitation(prepared)).status, "
|
|
342
|
+
if (state === "expired") assert.equal((await f.invitation(prepared)).status, "expired");
|
|
334
343
|
});
|
|
335
344
|
}
|
|
336
345
|
|
|
@@ -472,7 +481,8 @@ export async function verifyInvitationTransactions(t, db) {
|
|
|
472
481
|
if (acceptanceWins && competitor === "replace") assert.equal(loser.status, "fulfilled", loser.reason?.stack);
|
|
473
482
|
else {
|
|
474
483
|
assert.equal(loser.status, "rejected");
|
|
475
|
-
|
|
484
|
+
// A replaced invitation is stored expired, and redeeming it says so.
|
|
485
|
+
assert.equal(loser.reason.status, competitor === "replace" ? 409 : 404, loser.reason.stack);
|
|
476
486
|
}
|
|
477
487
|
assert.notEqual(connections[0], connections[1], "Competing units own independent database connections");
|
|
478
488
|
assert.equal(participation, acceptanceWins ? 1 : 0);
|
|
@@ -5,12 +5,7 @@ import { createTestResourceRuntime } from "@jskit-ai/resource-runtime/test";
|
|
|
5
5
|
import { silentLogger } from "@jskit-ai/kernel/server/runtime/logger";
|
|
6
6
|
import { UsersIdentityProvider } from "@jskit-ai/users-core/server/UsersIdentityProvider";
|
|
7
7
|
import { createUsersExtensions } from "@jskit-ai/users-core/server/usersExtensions";
|
|
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 "./workspacesMigrations.js";
|
|
14
9
|
import { createWorkspaceRecords } from "../../src/server/WorkspacesFeature.js";
|
|
15
10
|
import { createRepository as createInvitesRepository } from "../../src/server/common/repositories/workspaceInvitesRepository.js";
|
|
16
11
|
import { createRepository as createMemberPagesRepository } from "../../src/server/common/repositories/workspaceMemberPagesRepository.js";
|
|
@@ -24,7 +19,6 @@ import { createWorkspaceRoleCatalog } from "../../src/shared/roles.js";
|
|
|
24
19
|
// The migrated users and workspaces tables in a MariaDB/MySQL database of the test file's own, named after
|
|
25
20
|
// TEST_DB_NAME with the file's suffix, so the characterization files run in parallel. The database is made on first
|
|
26
21
|
// use and dropped when the file ends. Each test keeps to its own users and workspaces (names carry the test's tag).
|
|
27
|
-
const MIGRATIONS = [usersInitial, usersUsername, usersUpdatedAt, workspacesInitial, workspaceSettingsMigration, workspaceColorMigration];
|
|
28
22
|
|
|
29
23
|
// The roles the characterization uses: an owner, and assignable admin and member roles.
|
|
30
24
|
const ROLE_CATALOG = createWorkspaceRoleCatalog({
|
|
@@ -64,7 +58,7 @@ async function openDatabase(suffix, cleanups) {
|
|
|
64
58
|
});
|
|
65
59
|
await admin.raw("DROP DATABASE IF EXISTS ??", [name]);
|
|
66
60
|
await admin.raw("CREATE DATABASE ??", [name]);
|
|
67
|
-
for (const migration of
|
|
61
|
+
for (const migration of WORKSPACES_TEST_MIGRATIONS) await migration.up(knex);
|
|
68
62
|
// The users and workspaces tables, owned as UsersIdentityProvider and WorkspacesFeature own them.
|
|
69
63
|
const runtime = await createTestResourceRuntime({ knex, logger: silentLogger });
|
|
70
64
|
const usersExtensions = createUsersExtensions();
|
|
@@ -91,11 +85,16 @@ async function openDatabase(suffix, cleanups) {
|
|
|
91
85
|
roleCatalog: ROLE_CATALOG,
|
|
92
86
|
workspaces: records.workspaces,
|
|
93
87
|
memberships: records.memberships,
|
|
94
|
-
memberPages: createMemberPagesRepository({
|
|
88
|
+
memberPages: createMemberPagesRepository({
|
|
89
|
+
workspaceMemberships: resources.workspaceMemberships,
|
|
90
|
+
workspaceInvites: resources.workspaceInvites,
|
|
91
|
+
userProfiles: profiles
|
|
92
|
+
}),
|
|
95
93
|
invites: createInvitesRepository({ workspaceInvites: resources.workspaceInvites, workspaces: records.workspaces }),
|
|
96
94
|
settings: createSettingsRepository({
|
|
97
95
|
workspaceSettings: resources.workspaceSettings,
|
|
98
96
|
defaultInvitesEnabled: true,
|
|
97
|
+
defaultInvitesRequireSameEmail: false,
|
|
99
98
|
defaultColorSettings: {}
|
|
100
99
|
}),
|
|
101
100
|
// A user whose email and identity carry `name`.
|
|
@@ -6,12 +6,7 @@ import { silentLogger } from "@jskit-ai/kernel/server/runtime/logger";
|
|
|
6
6
|
import { expectFlatQueries, startTestFeatures } from "@jskit-ai/resource-runtime/test";
|
|
7
7
|
import { UsersExtensionsProvider } from "@jskit-ai/users-core/server/UsersExtensionsProvider";
|
|
8
8
|
import { UsersIdentityProvider } from "@jskit-ai/users-core/server/UsersIdentityProvider";
|
|
9
|
-
import
|
|
10
|
-
import usersUsername from "../../../users-core/migrations/users_core_profile_username.cjs";
|
|
11
|
-
import usersUpdatedAt from "../../../users-core/migrations/users_core_profile_updated_at.cjs";
|
|
12
|
-
import workspacesInitial from "../../migrations/workspaces_core_initial.cjs";
|
|
13
|
-
import workspaceSettingsMigration from "../../migrations/workspaces_core_workspace_settings_single_name_source.cjs";
|
|
14
|
-
import workspaceColorMigration from "../../migrations/workspaces_core_workspaces_drop_color.cjs";
|
|
9
|
+
import { WORKSPACES_TEST_MIGRATIONS } from "./workspacesMigrations.js";
|
|
15
10
|
import { WORKSPACES_TABLES, createWorkspaceRecords } from "../../src/server/workspacesRuntime.js";
|
|
16
11
|
import { createRepository as createInvitesRepository } from "../../src/server/common/repositories/workspaceInvitesRepository.js";
|
|
17
12
|
import { buildWorkspaceName, createService as createDirectoryService } from "../../src/server/common/services/workspaceDirectoryService.js";
|
|
@@ -23,7 +18,6 @@ import { resolveTenancyProfile } from "../../src/shared/tenancyProfile.js";
|
|
|
23
18
|
// workspaces.transferOwnership on the migrated users and workspaces tables: the moves it makes, what it refuses with
|
|
24
19
|
// nothing changed, the caller's transaction, two transfers at once and its statements among few and many members.
|
|
25
20
|
|
|
26
|
-
const MIGRATIONS = [usersInitial, usersUsername, usersUpdatedAt, workspacesInitial, workspaceSettingsMigration, workspaceColorMigration];
|
|
27
21
|
const NOT_ACTIVE = "workspaces.member.newOwnerNotActive";
|
|
28
22
|
const NOT_FOUND = "workspaces.workspace.notFound";
|
|
29
23
|
// expectFlatQueries() runs only against a test database, which TEST_DB_NAME names: SQLite's in-memory one is one.
|
|
@@ -88,7 +82,7 @@ function transferOwnershipCases(open, { dialect, database = true, skip = false }
|
|
|
88
82
|
if (database) requireTestDatabase(process.env);
|
|
89
83
|
const knex = await open(t);
|
|
90
84
|
if (!native) await knex.raw("PRAGMA foreign_keys = ON");
|
|
91
|
-
for (const migration of
|
|
85
|
+
for (const migration of WORKSPACES_TEST_MIGRATIONS) await migration.up(knex);
|
|
92
86
|
const started = await startTestFeatures({
|
|
93
87
|
knex,
|
|
94
88
|
logger: silentLogger,
|
|
@@ -101,7 +95,7 @@ function transferOwnershipCases(open, { dialect, database = true, skip = false }
|
|
|
101
95
|
const { workspaces, memberships } = createWorkspaceRecords(resources);
|
|
102
96
|
const invites = createInvitesRepository({ workspaceInvites: resources.workspaceInvites, workspaces });
|
|
103
97
|
const settings = createSettingsRepository({
|
|
104
|
-
workspaceSettings: resources.workspaceSettings, defaultInvitesEnabled: true, defaultColorSettings: {}
|
|
98
|
+
workspaceSettings: resources.workspaceSettings, defaultInvitesEnabled: true, defaultInvitesRequireSameEmail: false, defaultColorSettings: {}
|
|
105
99
|
});
|
|
106
100
|
const transfer = (input, options) => workspaces.transferOwnership(input, options);
|
|
107
101
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import usersInitial from "../../../users-core/migrations/users_core_generic_initial.cjs";
|
|
2
|
+
import usersUsername from "../../../users-core/migrations/users_core_profile_username.cjs";
|
|
3
|
+
import usersUpdatedAt from "../../../users-core/migrations/users_core_profile_updated_at.cjs";
|
|
4
|
+
import workspacesInitial from "../../migrations/workspaces_core_initial.cjs";
|
|
5
|
+
import membershipsAcceptedInvite from "../../migrations/workspaces_core_memberships_accepted_invite.cjs";
|
|
6
|
+
import settingsInvitesRequireSameEmail from "../../migrations/workspaces_core_settings_invites_require_same_email.cjs";
|
|
7
|
+
import workspaceSettingsSingleNameSource from "../../migrations/workspaces_core_workspace_settings_single_name_source.cjs";
|
|
8
|
+
import workspacesDropColor from "../../migrations/workspaces_core_workspaces_drop_color.cjs";
|
|
9
|
+
|
|
10
|
+
// The users and workspaces tables' migrations the tests run, in the order an application's db:migrate runs them,
|
|
11
|
+
// without the index and collation ones the tests that need them run themselves.
|
|
12
|
+
const WORKSPACES_TEST_MIGRATIONS = Object.freeze([
|
|
13
|
+
usersInitial,
|
|
14
|
+
usersUsername,
|
|
15
|
+
usersUpdatedAt,
|
|
16
|
+
workspacesInitial,
|
|
17
|
+
membershipsAcceptedInvite,
|
|
18
|
+
settingsInvitesRequireSameEmail,
|
|
19
|
+
workspaceSettingsSingleNameSource,
|
|
20
|
+
workspacesDropColor
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
export { WORKSPACES_TEST_MIGRATIONS };
|
|
@@ -81,3 +81,19 @@ test("resolveTenancyProfile lets workspace mode opt into a personal workspace fo
|
|
|
81
81
|
true
|
|
82
82
|
);
|
|
83
83
|
});
|
|
84
|
+
|
|
85
|
+
test("resolveTenancyProfile keeps workspace mode's \"unless-member\" provisioning and refuses any other value", () => {
|
|
86
|
+
const unlessMember = { workspace: { autoProvision: "unless-member" } };
|
|
87
|
+
assert.equal(
|
|
88
|
+
resolveTenancyProfile({ tenancyMode: TENANCY_MODE_WORKSPACES, tenancyPolicy: unlessMember }).workspace.autoProvision,
|
|
89
|
+
"unless-member"
|
|
90
|
+
);
|
|
91
|
+
assert.equal(
|
|
92
|
+
resolveTenancyProfile({ tenancyMode: TENANCY_MODE_PERSONAL, tenancyPolicy: unlessMember }).workspace.autoProvision,
|
|
93
|
+
true
|
|
94
|
+
);
|
|
95
|
+
assert.throws(
|
|
96
|
+
() => resolveTenancyProfile({ tenancyMode: TENANCY_MODE_WORKSPACES, tenancyPolicy: { workspace: { autoProvision: "yes" } } }),
|
|
97
|
+
/tenancyPolicy\.workspace\.autoProvision must be true, false or "unless-member"\. Received: yes/
|
|
98
|
+
);
|
|
99
|
+
});
|
|
@@ -257,3 +257,77 @@ test("get and update read the context first and save only the patchable fields",
|
|
|
257
257
|
errorWith(404, "workspaces.workspace.notFound"));
|
|
258
258
|
assert.equal((await db.workspaces.findById(workspace.id)).name, "Renamed");
|
|
259
259
|
});
|
|
260
|
+
|
|
261
|
+
test('"unless-member" provisions a personal workspace only for someone in no workspace who signs in without an invitation', async () => {
|
|
262
|
+
requireTestDatabase(process.env);
|
|
263
|
+
const db = await database.open();
|
|
264
|
+
const directory = directoryOf(db, { tenancyMode: "workspaces", tenancyPolicy: { workspace: { autoProvision: "unless-member" } } });
|
|
265
|
+
const ownedBy = (user) => countWhere(db, "workspaces", { owner_user_id: user.id });
|
|
266
|
+
|
|
267
|
+
const alone = await db.createProfile("unless-ada");
|
|
268
|
+
const made = await directory.ensureProvisionedWorkspaceForAuthenticatedUser(alone);
|
|
269
|
+
assert.equal(made.ownerUserId, alone.id, "no membership and no invitation: a workspace of their own");
|
|
270
|
+
assert.equal(made.isPersonal, true);
|
|
271
|
+
|
|
272
|
+
const host = await db.createProfile("unless-host");
|
|
273
|
+
const team = await db.workspaces.insert({ slug: "unless-team", name: "Team", ownerUserId: host.id });
|
|
274
|
+
const member = await db.createProfile("unless-bea");
|
|
275
|
+
await db.memberships.addMember({ workspaceId: team.id, userId: member.id });
|
|
276
|
+
assert.equal(await directory.ensureProvisionedWorkspaceForAuthenticatedUser(member), null);
|
|
277
|
+
assert.equal(await ownedBy(member), 0, "an active member of another workspace gets none");
|
|
278
|
+
|
|
279
|
+
const invited = await db.createProfile("unless-cal");
|
|
280
|
+
const invitation = { token: "opaque", workspaceId: team.id, email: "invited@example.test" };
|
|
281
|
+
assert.equal(await directory.ensureProvisionedWorkspaceForAuthenticatedUser(invited, {}, { invitation }), null);
|
|
282
|
+
assert.equal(await ownedBy(invited), 0, "an account from an invitation gets none");
|
|
283
|
+
|
|
284
|
+
// An invitation the resolver refused reaches provisioning as none; one passed through unresolved names no workspace.
|
|
285
|
+
for (const unusable of [null, { token: "opaque", source: "workspace-invite" }]) {
|
|
286
|
+
const someone = await db.createProfile(`unless-${unusable ? "dot" : "eve"}`);
|
|
287
|
+
assert.equal((await directory.ensureProvisionedWorkspaceForAuthenticatedUser(someone, {}, { invitation: unusable })).ownerUserId,
|
|
288
|
+
someone.id);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const removed = await db.createProfile("unless-fay");
|
|
292
|
+
await db.memberships.upsertMembership(team.id, removed.id, { roleSid: "member", status: "revoked" });
|
|
293
|
+
assert.equal((await directory.ensureProvisionedWorkspaceForAuthenticatedUser(removed)).ownerUserId, removed.id,
|
|
294
|
+
"a membership that is no longer active does not count");
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
test("deleteWorkspace deletes the workspace in the caller's transaction, which a rollback undoes", async () => {
|
|
298
|
+
requireTestDatabase(process.env);
|
|
299
|
+
const db = await database.open();
|
|
300
|
+
const directory = directoryOf(db, TEAMS);
|
|
301
|
+
const owner = await db.createProfile("delete-gus");
|
|
302
|
+
const workspace = (await directory.createWorkspaceForAuthenticatedUser(owner, { name: "Doomed", slug: "delete-doomed" })).value;
|
|
303
|
+
|
|
304
|
+
await assert.rejects(db.workspaces.withTransaction({}, async (txOptions) => {
|
|
305
|
+
assert.equal(await directory.deleteWorkspace(workspace.id, txOptions), true);
|
|
306
|
+
assert.equal(await db.workspaces.findById(workspace.id, txOptions), null, "gone inside the transaction");
|
|
307
|
+
throw new Error("Roll the deletion back");
|
|
308
|
+
}), /Roll the deletion back/u);
|
|
309
|
+
assert.equal((await db.workspaces.findById(workspace.id)).id, workspace.id, "the rollback keeps it");
|
|
310
|
+
assert.equal(await countWhere(db, "workspace_memberships", { workspace_id: workspace.id }), 1);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
test("deleteWorkspace leaves no memberships, settings or invites of the workspace, and answers false for a missing one", async () => {
|
|
314
|
+
requireTestDatabase(process.env);
|
|
315
|
+
const db = await database.open();
|
|
316
|
+
const directory = directoryOf(db, TEAMS);
|
|
317
|
+
const owner = await db.createProfile("delete-hal");
|
|
318
|
+
const member = await db.createProfile("delete-ina");
|
|
319
|
+
const workspace = (await directory.createWorkspaceForAuthenticatedUser(owner, { name: "Gone", slug: "delete-gone" })).value;
|
|
320
|
+
const kept = (await directory.createWorkspaceForAuthenticatedUser(owner, { name: "Kept", slug: "delete-kept" })).value;
|
|
321
|
+
await db.memberships.addMember({ workspaceId: workspace.id, userId: member.id });
|
|
322
|
+
const invite = await db.invites.insert({ workspaceId: workspace.id, email: "later@example.test", tokenHash: "h".repeat(64), invitedByUserId: owner.id });
|
|
323
|
+
await db.memberships.upsertMembership(workspace.id, member.id, { acceptedInviteId: invite.id });
|
|
324
|
+
|
|
325
|
+
assert.equal(await db.workspaces.withTransaction({}, (txOptions) => directory.deleteWorkspace(workspace.id, txOptions)), true);
|
|
326
|
+
|
|
327
|
+
assert.equal(await db.workspaces.findById(workspace.id), null);
|
|
328
|
+
for (const table of ["workspace_memberships", "workspace_settings", "workspace_invites"]) {
|
|
329
|
+
assert.equal(await countWhere(db, table, { workspace_id: workspace.id }), 0, `no ${table} rows left`);
|
|
330
|
+
}
|
|
331
|
+
assert.equal(await countWhere(db, "workspace_memberships", { workspace_id: kept.id }), 1, "another workspace keeps its own");
|
|
332
|
+
assert.equal(await directory.deleteWorkspace(workspace.id), false, "a missing workspace answers false");
|
|
333
|
+
});
|