@jskit-ai/users-core 0.2.15 → 0.2.17
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 +26 -0
- package/package.json +24 -17
- package/patterns/user-administration-server/PATTERN.md +63 -10
- package/patterns/user-administration-server/example/packages/users/package.json +5 -4
- package/patterns/user-administration-server/example/packages/users/src/server/UsersFeature.js +7 -5
- package/patterns/user-administration-server/example/packages/users/src/shared/index.js +1 -1
- package/patterns/user-administration-server/example/packages/users/src/shared/userCrud.js +21 -0
- package/patterns/user-administration-server/example/packages/users-workspace/package.json +11 -5
- package/patterns/user-administration-server/example/packages/users-workspace/src/server/UsersWorkspaceFeature.js +14 -5
- package/patterns/user-administration-server/example/packages/users-workspace/src/server/workspaceMembersOnly.js +52 -0
- package/patterns/user-administration-server/example/packages/users-workspace/src/shared/index.js +1 -1
- package/patterns/user-administration-server/example/packages/users-workspace/src/shared/userCrud.js +21 -0
- package/src/server/UsersFeature.js +10 -6
- package/src/server/UsersIdentityProvider.js +13 -29
- package/src/server/accountNotifications/accountNotificationsActions.js +3 -3
- package/src/server/accountNotifications/accountNotificationsRoutes.js +3 -3
- package/src/server/accountPreferences/accountPreferencesActions.js +3 -3
- package/src/server/accountPreferences/accountPreferencesRoutes.js +3 -3
- package/src/server/accountProfile/accountProfileActions.js +9 -9
- package/src/server/accountProfile/accountProfileRoutes.js +5 -5
- package/src/server/accountSecurity/accountSecurityActions.js +10 -10
- package/src/server/accountSecurity/accountSecurityRoutes.js +9 -9
- package/src/server/common/repositories/recordsByIds.js +38 -0
- package/src/server/common/repositories/userProfilesRepository.js +261 -146
- package/src/server/common/repositories/userSettingsRepository.js +37 -49
- package/src/server/common/services/authProfileSyncService.js +3 -5
- package/src/server/types.js +9 -1
- package/src/server/usersIdentity.js +11 -11
- package/src/server/usersRuntime.js +13 -11
- package/src/shared/resources/userProfileResource.js +43 -13
- package/src/shared/resources/userSettingsResource.js +35 -29
- package/test/accountSecurityService.test.js +2 -2
- package/test/authProfileSyncService.test.js +13 -9
- package/test/exportsContract.test.js +2 -0
- package/test/featureRuntime.test.js +63 -20
- package/test/i18nCodes.test.js +6 -10
- package/test/listByIds.mysql.test.js +5 -0
- package/test/listByIds.postgres.test.js +6 -0
- package/test/repositories.mysql.test.js +384 -0
- package/test/resourcesCanonical.test.js +58 -22
- package/test/support/listByIdsCases.js +115 -0
- package/test/support/usersDatabases.js +135 -0
- package/test/userSettingsInternalResource.test.js +3 -3
- package/test/userSettingsResource.test.js +3 -3
- package/test/usersPackageScaffoldContract.test.js +70 -12
- package/test/usersRouteRequestInputValidator.test.js +2 -2
- package/test/usersRouteResources.test.js +73 -29
- package/patterns/user-administration-server/example/packages/users/src/shared/userResource.js +0 -69
- package/patterns/user-administration-server/example/packages/users-workspace/src/shared/userResource.js +0 -56
- package/test/repositoryContracts.test.js +0 -596
|
@@ -1,596 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import test from "node:test";
|
|
3
|
-
import { DEFAULT_USER_SETTINGS } from "../src/shared/settings.js";
|
|
4
|
-
import { userProfileResource } from "../src/shared/resources/userProfileResource.js";
|
|
5
|
-
import { createRepository as createUserProfilesRepository } from "../src/server/common/repositories/userProfilesRepository.js";
|
|
6
|
-
import { createRepository as createUserSettingsRepository } from "../src/server/common/repositories/userSettingsRepository.js";
|
|
7
|
-
|
|
8
|
-
function asCollectionDocument(rows = []) {
|
|
9
|
-
return {
|
|
10
|
-
data: Array.isArray(rows) ? rows : []
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
test("users-core repositories delegate transaction ownership and failures to the API", async () => {
|
|
15
|
-
const api = {
|
|
16
|
-
async transaction(work) {
|
|
17
|
-
return work({ trxId: "managed-1" });
|
|
18
|
-
},
|
|
19
|
-
resources: {
|
|
20
|
-
userProfiles: {
|
|
21
|
-
async query() {
|
|
22
|
-
return asCollectionDocument([]);
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
userSettings: {
|
|
26
|
-
async query() {
|
|
27
|
-
return asCollectionDocument([]);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
const repositories = [
|
|
33
|
-
createUserProfilesRepository({ api }),
|
|
34
|
-
createUserSettingsRepository({ api })
|
|
35
|
-
];
|
|
36
|
-
|
|
37
|
-
for (const repository of repositories) {
|
|
38
|
-
assert.equal(typeof repository.withTransaction, "function");
|
|
39
|
-
const result = await repository.withTransaction(async (trx) => ({ id: trx.trxId }));
|
|
40
|
-
assert.deepEqual(result, { id: "managed-1" });
|
|
41
|
-
const failure = new Error("Participant failed");
|
|
42
|
-
await assert.rejects(repository.withTransaction(async () => { throw failure; }), error => error === failure);
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
function createUserProfilesApiStub(expectedRecord) {
|
|
47
|
-
const calls = [];
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
calls,
|
|
51
|
-
api: {
|
|
52
|
-
resources: {
|
|
53
|
-
userProfiles: {
|
|
54
|
-
async query({ queryParams, format }) {
|
|
55
|
-
assert.equal(format, "plain");
|
|
56
|
-
calls.push(queryParams?.filters || {});
|
|
57
|
-
return asCollectionDocument(
|
|
58
|
-
expectedRecord ? [{
|
|
59
|
-
...expectedRecord,
|
|
60
|
-
id: String(expectedRecord.id)
|
|
61
|
-
}] : []
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
test("userSettingsRepository.ensureForUserId sends transaction outside plain data", async () => {
|
|
71
|
-
const trx = { trxId: "trx-1" };
|
|
72
|
-
let queryCount = 0;
|
|
73
|
-
let postParams = null;
|
|
74
|
-
const repository = createUserSettingsRepository({
|
|
75
|
-
api: {
|
|
76
|
-
resources: {
|
|
77
|
-
userSettings: {
|
|
78
|
-
async query() {
|
|
79
|
-
queryCount += 1;
|
|
80
|
-
return asCollectionDocument(
|
|
81
|
-
queryCount < 2
|
|
82
|
-
? []
|
|
83
|
-
: [{
|
|
84
|
-
id: "7",
|
|
85
|
-
...DEFAULT_USER_SETTINGS
|
|
86
|
-
}]
|
|
87
|
-
);
|
|
88
|
-
},
|
|
89
|
-
async post(params) {
|
|
90
|
-
postParams = params;
|
|
91
|
-
return {
|
|
92
|
-
id: "7",
|
|
93
|
-
...DEFAULT_USER_SETTINGS
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
const record = await repository.ensureForUserId("7", { trx });
|
|
102
|
-
|
|
103
|
-
assert.equal(postParams?.format, "plain");
|
|
104
|
-
assert.equal(postParams?.returning, "full");
|
|
105
|
-
assert.equal(postParams?.transaction, trx);
|
|
106
|
-
assert.deepEqual(postParams?.data, {
|
|
107
|
-
id: "7",
|
|
108
|
-
...DEFAULT_USER_SETTINGS
|
|
109
|
-
});
|
|
110
|
-
assert.equal(record?.id, "7");
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
test("userProfilesRepository.upsert sends plain data with transaction outside the record body", async () => {
|
|
114
|
-
const trx = { trxId: "trx-1" };
|
|
115
|
-
let postParams = null;
|
|
116
|
-
const repository = createUserProfilesRepository({
|
|
117
|
-
api: {
|
|
118
|
-
resources: {
|
|
119
|
-
userProfiles: {
|
|
120
|
-
vars: {},
|
|
121
|
-
async query({ queryParams, format }) {
|
|
122
|
-
assert.equal(format, "plain");
|
|
123
|
-
const filters = queryParams?.filters || {};
|
|
124
|
-
if (Object.hasOwn(filters, "authProvider") || Object.hasOwn(filters, "authProviderUserSid")) {
|
|
125
|
-
return asCollectionDocument([]);
|
|
126
|
-
}
|
|
127
|
-
if (Object.hasOwn(filters, "usernames")) {
|
|
128
|
-
return asCollectionDocument([]);
|
|
129
|
-
}
|
|
130
|
-
return asCollectionDocument([]);
|
|
131
|
-
},
|
|
132
|
-
async post(params) {
|
|
133
|
-
postParams = params;
|
|
134
|
-
const attributes = params.data || {};
|
|
135
|
-
return {
|
|
136
|
-
id: "11",
|
|
137
|
-
authProvider: attributes.authProvider,
|
|
138
|
-
authProviderUserSid: attributes.authProviderUserSid,
|
|
139
|
-
email: attributes.email,
|
|
140
|
-
username: attributes.username,
|
|
141
|
-
displayName: attributes.displayName,
|
|
142
|
-
avatarStorageKey: null,
|
|
143
|
-
avatarVersion: null,
|
|
144
|
-
avatarUpdatedAt: null,
|
|
145
|
-
createdAt: attributes.createdAt
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
const record = await repository.upsert({
|
|
154
|
-
authProvider: "supabase",
|
|
155
|
-
authProviderUserSid: "user-11",
|
|
156
|
-
email: "ada@example.com",
|
|
157
|
-
displayName: "Ada Example"
|
|
158
|
-
}, { trx });
|
|
159
|
-
|
|
160
|
-
assert.equal(postParams?.format, "plain");
|
|
161
|
-
assert.equal(postParams?.returning, "full");
|
|
162
|
-
assert.equal(postParams?.transaction, trx);
|
|
163
|
-
assert.equal(postParams?.data?.transaction, undefined);
|
|
164
|
-
assert.equal(postParams?.document, undefined);
|
|
165
|
-
assert.equal(postParams?.inputRecord, undefined);
|
|
166
|
-
assert.equal(postParams?.data?.authProvider, "supabase");
|
|
167
|
-
assert.equal(record?.id, "11");
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
test("userProfilesRepository.upsert patches existing profiles with resource-backed updatedAt", async () => {
|
|
171
|
-
const trx = { trxId: "trx-1" };
|
|
172
|
-
const existingRecord = {
|
|
173
|
-
id: "7",
|
|
174
|
-
authProvider: "local",
|
|
175
|
-
authProviderUserSid: "local-user-7",
|
|
176
|
-
email: "ada@example.com",
|
|
177
|
-
username: "ada",
|
|
178
|
-
displayName: "Ada Example",
|
|
179
|
-
avatarStorageKey: null,
|
|
180
|
-
avatarVersion: null,
|
|
181
|
-
avatarUpdatedAt: null,
|
|
182
|
-
createdAt: "2026-04-20T00:00:00.000Z",
|
|
183
|
-
updatedAt: "2026-04-20T00:00:00.000Z"
|
|
184
|
-
};
|
|
185
|
-
let patchParams = null;
|
|
186
|
-
const repository = createUserProfilesRepository({
|
|
187
|
-
api: {
|
|
188
|
-
resources: {
|
|
189
|
-
userProfiles: {
|
|
190
|
-
async query({ queryParams, format }) {
|
|
191
|
-
assert.equal(format, "plain");
|
|
192
|
-
const filters = queryParams?.filters || {};
|
|
193
|
-
if (Object.hasOwn(filters, "authProvider") || Object.hasOwn(filters, "authProviderUserSid")) {
|
|
194
|
-
return asCollectionDocument([existingRecord]);
|
|
195
|
-
}
|
|
196
|
-
if (Object.hasOwn(filters, "username")) {
|
|
197
|
-
return asCollectionDocument([]);
|
|
198
|
-
}
|
|
199
|
-
return asCollectionDocument([]);
|
|
200
|
-
},
|
|
201
|
-
async patch(params) {
|
|
202
|
-
patchParams = params;
|
|
203
|
-
const attributes = params.data || {};
|
|
204
|
-
return {
|
|
205
|
-
...existingRecord,
|
|
206
|
-
...attributes
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
const record = await repository.upsert({
|
|
215
|
-
authProvider: "local",
|
|
216
|
-
authProviderUserSid: "local-user-7",
|
|
217
|
-
email: " ADA.RENAMED@EXAMPLE.COM ",
|
|
218
|
-
displayName: "Ada Renamed"
|
|
219
|
-
}, { trx });
|
|
220
|
-
|
|
221
|
-
const attributes = patchParams?.data || {};
|
|
222
|
-
assert.equal(userProfileResource.schema.updatedAt?.type, "dateTime");
|
|
223
|
-
assert.equal(userProfileResource.schema.updatedAt?.storage?.column, "updated_at");
|
|
224
|
-
assert.equal(userProfileResource.schema.updatedAt?.storage?.writeSerializer, "datetime-utc");
|
|
225
|
-
assert.equal(patchParams?.transaction, trx);
|
|
226
|
-
assert.deepEqual(Object.keys(attributes).sort(), ["displayName", "email", "updatedAt", "username"]);
|
|
227
|
-
assert.equal(attributes.email, "ada.renamed@example.com");
|
|
228
|
-
assert.equal(attributes.displayName, "Ada Renamed");
|
|
229
|
-
assert.equal(attributes.username, "ada");
|
|
230
|
-
assert.equal(typeof attributes.updatedAt, "string");
|
|
231
|
-
assert.equal(record?.id, "7");
|
|
232
|
-
assert.equal(record?.updatedAt, attributes.updatedAt);
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
test("userProfilesRepository profile patch helpers stamp updatedAt through the resource contract", async () => {
|
|
236
|
-
const trx = { trxId: "trx-1" };
|
|
237
|
-
const calls = [];
|
|
238
|
-
const repository = createUserProfilesRepository({
|
|
239
|
-
api: {
|
|
240
|
-
resources: {
|
|
241
|
-
userProfiles: {
|
|
242
|
-
async query() {
|
|
243
|
-
return asCollectionDocument([]);
|
|
244
|
-
},
|
|
245
|
-
async patch(params) {
|
|
246
|
-
calls.push(params);
|
|
247
|
-
const attributes = params.data || {};
|
|
248
|
-
return {
|
|
249
|
-
id: params.id || "7",
|
|
250
|
-
authProvider: "local",
|
|
251
|
-
authProviderUserSid: "local-user-7",
|
|
252
|
-
email: "ada@example.com",
|
|
253
|
-
username: "ada",
|
|
254
|
-
displayName: attributes.displayName || "Ada Example",
|
|
255
|
-
avatarStorageKey: attributes.avatarStorageKey ?? null,
|
|
256
|
-
avatarVersion: attributes.avatarVersion ?? null,
|
|
257
|
-
avatarUpdatedAt: attributes.avatarUpdatedAt ?? null,
|
|
258
|
-
createdAt: "2026-04-20T00:00:00.000Z",
|
|
259
|
-
updatedAt: attributes.updatedAt
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
const avatarUpdatedAt = new Date("2026-04-21T12:00:00.000Z");
|
|
267
|
-
|
|
268
|
-
await repository.updateDisplayNameById("7", "Ada Updated", { trx });
|
|
269
|
-
await repository.updateAvatarById("7", {
|
|
270
|
-
avatarStorageKey: "avatars/7.png",
|
|
271
|
-
avatarVersion: "v1",
|
|
272
|
-
avatarUpdatedAt
|
|
273
|
-
}, { trx });
|
|
274
|
-
await repository.clearAvatarById("7", { trx });
|
|
275
|
-
|
|
276
|
-
assert.equal(calls.length, 3);
|
|
277
|
-
for (const call of calls) {
|
|
278
|
-
assert.equal(call.transaction, trx);
|
|
279
|
-
assert.equal(call.id, "7");
|
|
280
|
-
assert.equal(call.format, "plain");
|
|
281
|
-
assert.equal(call.returning, "full");
|
|
282
|
-
assert.equal(call.document, undefined);
|
|
283
|
-
assert.equal(call.inputRecord, undefined);
|
|
284
|
-
assert.equal(typeof call.data?.updatedAt, "string");
|
|
285
|
-
}
|
|
286
|
-
assert.deepEqual(Object.keys(calls[0].data).sort(), ["displayName", "updatedAt"]);
|
|
287
|
-
assert.deepEqual(Object.keys(calls[1].data).sort(), [
|
|
288
|
-
"avatarStorageKey",
|
|
289
|
-
"avatarUpdatedAt",
|
|
290
|
-
"avatarVersion",
|
|
291
|
-
"updatedAt"
|
|
292
|
-
]);
|
|
293
|
-
assert.equal(calls[1].data.avatarStorageKey, "avatars/7.png");
|
|
294
|
-
assert.equal(calls[1].data.avatarVersion, "v1");
|
|
295
|
-
assert.equal(calls[1].data.avatarUpdatedAt, avatarUpdatedAt.toISOString());
|
|
296
|
-
assert.deepEqual(calls[2].data.avatarStorageKey, null);
|
|
297
|
-
assert.deepEqual(calls[2].data.avatarVersion, null);
|
|
298
|
-
assert.deepEqual(calls[2].data.avatarUpdatedAt, null);
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
test("userProfilesRepository.findByEmail normalizes email lookup", async () => {
|
|
302
|
-
const { api, calls } = createUserProfilesApiStub({
|
|
303
|
-
id: 7,
|
|
304
|
-
authProvider: "supabase",
|
|
305
|
-
authProviderUserSid: "supabase-user-7",
|
|
306
|
-
email: "ada@example.com",
|
|
307
|
-
username: "ada",
|
|
308
|
-
displayName: "Ada Example",
|
|
309
|
-
avatarStorageKey: null,
|
|
310
|
-
avatarVersion: null,
|
|
311
|
-
avatarUpdatedAt: null,
|
|
312
|
-
createdAt: "2026-04-20T00:00:00.000Z"
|
|
313
|
-
});
|
|
314
|
-
const repository = createUserProfilesRepository({
|
|
315
|
-
api
|
|
316
|
-
});
|
|
317
|
-
|
|
318
|
-
const profile = await repository.findByEmail(" ADA@EXAMPLE.COM ");
|
|
319
|
-
|
|
320
|
-
assert.deepEqual(calls, [{ email: "ada@example.com" }]);
|
|
321
|
-
assert.equal(profile?.id, "7");
|
|
322
|
-
assert.equal(profile?.email, "ada@example.com");
|
|
323
|
-
assert.equal(profile?.displayName, "Ada Example");
|
|
324
|
-
});
|
|
325
|
-
|
|
326
|
-
test("userProfilesRepository.findByIdentity reads existing profiles from collection documents", async () => {
|
|
327
|
-
const { api, calls } = createUserProfilesApiStub({
|
|
328
|
-
id: 7,
|
|
329
|
-
authProvider: "supabase",
|
|
330
|
-
authProviderUserSid: "supabase-user-7",
|
|
331
|
-
email: "ada@example.com",
|
|
332
|
-
username: "ada",
|
|
333
|
-
displayName: "Ada Example",
|
|
334
|
-
avatarStorageKey: null,
|
|
335
|
-
avatarVersion: null,
|
|
336
|
-
avatarUpdatedAt: null,
|
|
337
|
-
createdAt: "2026-04-20T00:00:00.000Z"
|
|
338
|
-
});
|
|
339
|
-
const repository = createUserProfilesRepository({
|
|
340
|
-
api
|
|
341
|
-
});
|
|
342
|
-
|
|
343
|
-
const profile = await repository.findByIdentity({
|
|
344
|
-
provider: "supabase",
|
|
345
|
-
providerUserId: "supabase-user-7"
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
assert.deepEqual(calls, [{
|
|
349
|
-
authProvider: "supabase",
|
|
350
|
-
authProviderUserSid: "supabase-user-7"
|
|
351
|
-
}]);
|
|
352
|
-
assert.equal(profile?.id, "7");
|
|
353
|
-
assert.equal(profile?.displayName, "Ada Example");
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
test("userProfilesRepository.findByEmail returns null when the row is missing", async () => {
|
|
357
|
-
const { api } = createUserProfilesApiStub(undefined);
|
|
358
|
-
const repository = createUserProfilesRepository({
|
|
359
|
-
api
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
const profile = await repository.findByEmail("missing@example.com");
|
|
363
|
-
|
|
364
|
-
assert.equal(profile, null);
|
|
365
|
-
});
|
|
366
|
-
|
|
367
|
-
for (const outcome of ["pending", "committed", "unknown"]) {
|
|
368
|
-
test(`user settings preserves a ${outcome} write failure instead of recovering as a duplicate`, async () => {
|
|
369
|
-
let reads = 0;
|
|
370
|
-
const failure = Object.assign(new Error("Write failed"), {
|
|
371
|
-
transactionOutcome: outcome,
|
|
372
|
-
cause: { code: "23505" }
|
|
373
|
-
});
|
|
374
|
-
const api = {
|
|
375
|
-
resources: {
|
|
376
|
-
userSettings: {
|
|
377
|
-
async query() { reads += 1; return asCollectionDocument([]); },
|
|
378
|
-
async post() { throw failure; }
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
};
|
|
382
|
-
const repository = createUserSettingsRepository({ api });
|
|
383
|
-
await assert.rejects(repository.ensureForUserId("7"), error => error === failure);
|
|
384
|
-
assert.equal(reads, 1);
|
|
385
|
-
});
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
test("user settings recovers a duplicate only after its standalone write rolled back", async () => {
|
|
389
|
-
let reads = 0;
|
|
390
|
-
const existing = { id: "7", ...DEFAULT_USER_SETTINGS };
|
|
391
|
-
const api = {
|
|
392
|
-
resources: {
|
|
393
|
-
userSettings: {
|
|
394
|
-
async query() { reads += 1; return asCollectionDocument(reads === 1 ? [] : [existing]); },
|
|
395
|
-
async post() {
|
|
396
|
-
throw Object.assign(new Error("Write failed"), {
|
|
397
|
-
transactionOutcome: "rolledBack", cause: { code: "23505" }
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
};
|
|
403
|
-
const repository = createUserSettingsRepository({ api });
|
|
404
|
-
assert.deepEqual(await repository.ensureForUserId("7"), existing);
|
|
405
|
-
assert.equal(reads, 2);
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
test("profile upsert performs duplicate recovery after its API transaction has finished", async () => {
|
|
409
|
-
const trx = { trxId: "managed-upsert" };
|
|
410
|
-
const profile = {
|
|
411
|
-
id: "7", authProvider: "local", authProviderUserSid: "local-7",
|
|
412
|
-
email: "ada@example.com", username: "ada", displayName: "Ada"
|
|
413
|
-
};
|
|
414
|
-
let active = false;
|
|
415
|
-
let rolledBack = false;
|
|
416
|
-
const api = {
|
|
417
|
-
async transaction(work) {
|
|
418
|
-
active = true;
|
|
419
|
-
try {
|
|
420
|
-
return await work(trx);
|
|
421
|
-
} catch (cause) {
|
|
422
|
-
active = false;
|
|
423
|
-
rolledBack = true;
|
|
424
|
-
throw Object.assign(new Error("Transaction failed", { cause }), { transactionOutcome: "rolledBack" });
|
|
425
|
-
}
|
|
426
|
-
},
|
|
427
|
-
resources: {
|
|
428
|
-
userProfiles: {
|
|
429
|
-
vars: {},
|
|
430
|
-
async query({ transaction, queryParams, format }) {
|
|
431
|
-
assert.equal(format, "plain");
|
|
432
|
-
if (active) {
|
|
433
|
-
assert.equal(transaction, trx);
|
|
434
|
-
return asCollectionDocument([]);
|
|
435
|
-
}
|
|
436
|
-
assert.equal(rolledBack, true);
|
|
437
|
-
assert.equal(transaction, null);
|
|
438
|
-
assert.equal(queryParams.filters.authProviderUserSid, "local-7");
|
|
439
|
-
return asCollectionDocument([profile]);
|
|
440
|
-
},
|
|
441
|
-
async post({ transaction }) {
|
|
442
|
-
assert.equal(transaction, trx);
|
|
443
|
-
throw Object.assign(new Error("Duplicate identity"), {
|
|
444
|
-
transactionOutcome: "pending", cause: { code: "23505", message: "unique provider identity" }
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
};
|
|
450
|
-
const repository = createUserProfilesRepository({ api });
|
|
451
|
-
assert.equal((await repository.upsert(profile)).id, "7");
|
|
452
|
-
assert.equal(active, false);
|
|
453
|
-
});
|
|
454
|
-
|
|
455
|
-
test("profile upsert never recovers inside a caller-owned failed transaction", async () => {
|
|
456
|
-
const trx = { trxId: "caller-managed" };
|
|
457
|
-
let reads = 0;
|
|
458
|
-
const failure = Object.assign(new Error("Duplicate identity"), {
|
|
459
|
-
transactionOutcome: "pending", cause: { code: "23505" }
|
|
460
|
-
});
|
|
461
|
-
const api = {
|
|
462
|
-
async transaction() { throw new Error("Must join the caller's owner"); },
|
|
463
|
-
resources: {
|
|
464
|
-
userProfiles: {
|
|
465
|
-
vars: {},
|
|
466
|
-
async query({ transaction }) {
|
|
467
|
-
assert.equal(transaction, trx);
|
|
468
|
-
reads += 1;
|
|
469
|
-
return asCollectionDocument([]);
|
|
470
|
-
},
|
|
471
|
-
async post() { throw failure; }
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
};
|
|
475
|
-
const repository = createUserProfilesRepository({ api });
|
|
476
|
-
await assert.rejects(repository.upsert({
|
|
477
|
-
authProvider: "local", authProviderUserSid: "local-7",
|
|
478
|
-
email: "ada@example.com", displayName: "Ada"
|
|
479
|
-
}, { trx }), error => error === failure);
|
|
480
|
-
assert.equal(reads, 2);
|
|
481
|
-
});
|
|
482
|
-
|
|
483
|
-
for (const outcome of ["pending", "committed", "unknown"]) {
|
|
484
|
-
test(`profile email conflict preserves the ${outcome} outcome from a caller-owned write`, async () => {
|
|
485
|
-
const trx = { trxId: "caller-managed" };
|
|
486
|
-
let reads = 0;
|
|
487
|
-
const failure = Object.assign(new Error("Resource write failed"), {
|
|
488
|
-
transactionOutcome: outcome,
|
|
489
|
-
cause: { code: "23505", message: "duplicate key in users_email_unique" }
|
|
490
|
-
});
|
|
491
|
-
const api = {
|
|
492
|
-
resources: {
|
|
493
|
-
userProfiles: {
|
|
494
|
-
vars: {},
|
|
495
|
-
async query() { reads += 1; return asCollectionDocument([]); },
|
|
496
|
-
async post() { throw failure; }
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
};
|
|
500
|
-
const repository = createUserProfilesRepository({ api });
|
|
501
|
-
await assert.rejects(repository.upsert({
|
|
502
|
-
authProvider: "local", authProviderUserSid: "local-7",
|
|
503
|
-
email: "ada@example.com", displayName: "Ada"
|
|
504
|
-
}, { trx }), error => {
|
|
505
|
-
if (outcome === "pending") {
|
|
506
|
-
assert.equal(error.code, "USER_PROFILE_EMAIL_CONFLICT");
|
|
507
|
-
assert.equal(error.cause, failure);
|
|
508
|
-
} else {
|
|
509
|
-
assert.equal(error, failure);
|
|
510
|
-
}
|
|
511
|
-
assert.equal(error.transactionOutcome, outcome);
|
|
512
|
-
return true;
|
|
513
|
-
});
|
|
514
|
-
assert.equal(reads, 2);
|
|
515
|
-
});
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
// The username upsert chooses for `email` while `holders` (username to profile id) are taken,
|
|
519
|
-
// and the username reads it made. `existing` is the identity's profile, when it has one.
|
|
520
|
-
async function chooseUsername(email, holders, { existing = null } = {}) {
|
|
521
|
-
const trx = { trxId: "caller-managed" };
|
|
522
|
-
const usernameReads = [];
|
|
523
|
-
let written = null;
|
|
524
|
-
const repository = createUserProfilesRepository({
|
|
525
|
-
api: {
|
|
526
|
-
resources: {
|
|
527
|
-
userProfiles: {
|
|
528
|
-
vars: {},
|
|
529
|
-
async query({ queryParams, transaction }) {
|
|
530
|
-
assert.equal(transaction, trx);
|
|
531
|
-
const filters = queryParams?.filters || {};
|
|
532
|
-
if (Object.hasOwn(filters, "usernames")) {
|
|
533
|
-
usernameReads.push(filters.usernames);
|
|
534
|
-
return asCollectionDocument(filters.usernames
|
|
535
|
-
.filter((username) => holders.has(username))
|
|
536
|
-
.map((username) => ({ id: holders.get(username), username })));
|
|
537
|
-
}
|
|
538
|
-
return asCollectionDocument(existing ? [existing] : []);
|
|
539
|
-
},
|
|
540
|
-
async post({ data }) {
|
|
541
|
-
written = data.username;
|
|
542
|
-
return { id: "11", ...data };
|
|
543
|
-
},
|
|
544
|
-
async patch({ id, data }) {
|
|
545
|
-
written = data.username;
|
|
546
|
-
return { ...existing, id, ...data };
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
});
|
|
552
|
-
await repository.upsert({
|
|
553
|
-
authProvider: "local", authProviderUserSid: "local-7", email, displayName: "Ada"
|
|
554
|
-
}, { trx });
|
|
555
|
-
return { username: written, usernameReads };
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
const takenBy = (usernames, id = "8") => new Map(usernames.map((username) => [username, id]));
|
|
559
|
-
|
|
560
|
-
test("profile upsert chooses the first free username of base, base-2 to base-1000 in one read", async () => {
|
|
561
|
-
const fresh = await chooseUsername("ada@example.com", new Map());
|
|
562
|
-
assert.equal(fresh.username, "ada");
|
|
563
|
-
assert.equal(fresh.usernameReads.length, 1);
|
|
564
|
-
assert.equal(fresh.usernameReads[0].length, 1000);
|
|
565
|
-
assert.deepEqual(fresh.usernameReads[0].slice(0, 3), ["ada", "ada-2", "ada-3"]);
|
|
566
|
-
assert.equal(fresh.usernameReads[0][999], "ada-1000");
|
|
567
|
-
|
|
568
|
-
assert.equal((await chooseUsername("ada@example.com", takenBy(["ada"]))).username, "ada-2");
|
|
569
|
-
const gap = await chooseUsername("ada@example.com", takenBy(["ada", "ada-2", "ada-4", "ada-5"]));
|
|
570
|
-
assert.equal(gap.username, "ada-3");
|
|
571
|
-
assert.equal(gap.usernameReads.length, 1);
|
|
572
|
-
});
|
|
573
|
-
|
|
574
|
-
test("profile upsert trims a long username base so the suffixed name fits", async () => {
|
|
575
|
-
const longBase = "a".repeat(120);
|
|
576
|
-
const longEmail = `${"a".repeat(130)}@example.com`;
|
|
577
|
-
assert.equal((await chooseUsername(longEmail, new Map())).username, longBase);
|
|
578
|
-
assert.equal((await chooseUsername(longEmail, takenBy([longBase]))).username, `${"a".repeat(118)}-2`);
|
|
579
|
-
const taken = [longBase, ...Array.from({ length: 8 }, (_, index) => `${"a".repeat(118)}-${index + 2}`)];
|
|
580
|
-
assert.equal((await chooseUsername(longEmail, takenBy(taken))).username, `${"a".repeat(117)}-10`);
|
|
581
|
-
});
|
|
582
|
-
|
|
583
|
-
test("profile upsert keeps a username the profile itself holds", async () => {
|
|
584
|
-
const existing = {
|
|
585
|
-
id: "7", authProvider: "local", authProviderUserSid: "local-7",
|
|
586
|
-
email: "ada@example.com", username: "", displayName: "Ada"
|
|
587
|
-
};
|
|
588
|
-
assert.equal((await chooseUsername("ada@example.com", takenBy(["ada"], "7"), { existing })).username, "ada");
|
|
589
|
-
assert.equal((await chooseUsername("ada@example.com", takenBy(["ada"], "8"), { existing })).username, "ada-2");
|
|
590
|
-
});
|
|
591
|
-
|
|
592
|
-
test("profile upsert fails the same way when base to base-1000 are all taken", async () => {
|
|
593
|
-
const taken = ["ada", ...Array.from({ length: 999 }, (_, index) => `ada-${index + 2}`)];
|
|
594
|
-
await assert.rejects(chooseUsername("ada@example.com", takenBy(taken)), /Unable to generate unique username\./);
|
|
595
|
-
assert.equal((await chooseUsername("ada@example.com", takenBy(taken.slice(0, -1)))).username, "ada-1000");
|
|
596
|
-
});
|