@jskit-ai/users-core 0.1.260 → 0.2.0

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 ADDED
@@ -0,0 +1,48 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0 - 2026-10-29
4
+
5
+ - Translation ready: every user-visible text is a message key with its English in `locales/en.json`, so an app translates it by supplying translation files only. With no translations, texts and behaviour are unchanged. See the guide "Translating a JSKIT app".
6
+ - Saved locales are canonical BCP 47 codes (`en-AU`); the language list comes from `config.languages` when the app sets it. Exports `./shared/settings`.
7
+
8
+ ### Upgrading
9
+
10
+ - Success responses that had `meta.message` also carry `meta.messageCode` and `meta.messageParams`.
11
+
12
+ ### New message keys (11)
13
+
14
+ All keys in `locales/en.json` (exported as `./locales/en.json`) are new in this version.
15
+
16
+ <details><summary>Key list</summary>
17
+
18
+ - `jskit.users.profile.notFound`
19
+ - `jskit.users.avatar.notFound`
20
+ - `jskit.users.avatar.uploadResultMissing`
21
+ - `jskit.users.security.passwordChangeUnavailable`
22
+ - `jskit.users.security.passwordMethodToggleUnavailable`
23
+ - `jskit.users.security.oauthLinkUnavailable`
24
+ - `jskit.users.security.oauthUnlinkUnavailable`
25
+ - `jskit.users.security.logoutOtherSessionsUnavailable`
26
+ - `jskit.users.security.passwordConfirmationMismatch`
27
+ - `jskit.users.security.passwordUpdated`
28
+ - `jskit.users.bootstrap.authUnavailable`
29
+
30
+ </details>
31
+
32
+ ### Codes (11)
33
+
34
+ Error, field-error and message codes this package sends, with their English. Undotted codes kept their existing values and render from `jskit.errors.<code>`.
35
+
36
+ | Code | English |
37
+ | --- | --- |
38
+ | `users.profile.notFound` | User profile was not found. |
39
+ | `users.avatar.notFound` | Avatar not found. |
40
+ | `users.avatar.uploadResultMissing` | Avatar upload completed without a profile result. |
41
+ | `users.security.passwordChangeUnavailable` | Password change is not available. |
42
+ | `users.security.passwordMethodToggleUnavailable` | Password method toggle is not available. |
43
+ | `users.security.oauthLinkUnavailable` | OAuth linking is not available. |
44
+ | `users.security.oauthUnlinkUnavailable` | OAuth unlink is not available. |
45
+ | `users.security.logoutOtherSessionsUnavailable` | Logout other sessions is not available. |
46
+ | `users.security.passwordConfirmationMismatch` | Password confirmation does not match. |
47
+ | `users.security.passwordUpdated` | Password updated. |
48
+ | `users.bootstrap.authUnavailable` | Authentication service temporarily unavailable. Please retry. |
@@ -0,0 +1,25 @@
1
+ {
2
+ "jskit": {
3
+ "users": {
4
+ "profile": {
5
+ "notFound": "User profile was not found."
6
+ },
7
+ "avatar": {
8
+ "notFound": "Avatar not found.",
9
+ "uploadResultMissing": "Avatar upload completed without a profile result."
10
+ },
11
+ "security": {
12
+ "passwordChangeUnavailable": "Password change is not available.",
13
+ "passwordMethodToggleUnavailable": "Password method toggle is not available.",
14
+ "oauthLinkUnavailable": "OAuth linking is not available.",
15
+ "oauthUnlinkUnavailable": "OAuth unlink is not available.",
16
+ "logoutOtherSessionsUnavailable": "Logout other sessions is not available.",
17
+ "passwordConfirmationMismatch": "Password confirmation does not match.",
18
+ "passwordUpdated": "Password updated."
19
+ },
20
+ "bootstrap": {
21
+ "authUnavailable": "Authentication service temporarily unavailable. Please retry."
22
+ }
23
+ }
24
+ }
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/users-core",
3
- "version": "0.1.260",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "node --test --test-concurrency=1"
@@ -10,15 +10,17 @@
10
10
  "./server/UsersIdentityProvider": "./src/server/UsersIdentityProvider.js",
11
11
  "./server/usersExtensions": "./src/server/usersExtensions.js",
12
12
  "./shared/resources/userProfileResource": "./src/shared/resources/userProfileResource.js",
13
- "./shared/resources/userSettingsResource": "./src/shared/resources/userSettingsResource.js"
13
+ "./shared/resources/userSettingsResource": "./src/shared/resources/userSettingsResource.js",
14
+ "./shared/settings": "./src/shared/settings.js",
15
+ "./locales/en.json": "./locales/en.json"
14
16
  },
15
17
  "dependencies": {
16
- "@jskit-ai/database-runtime": "0.1.247",
17
- "@jskit-ai/json-rest-api-core": "0.1.191",
18
- "@jskit-ai/resource-core": "0.1.189",
19
- "@jskit-ai/resource-crud-core": "0.1.189",
20
- "@jskit-ai/storage-runtime": "0.1.244",
21
- "@jskit-ai/uploads-runtime": "0.1.222",
18
+ "@jskit-ai/database-runtime": "0.1.249",
19
+ "@jskit-ai/json-rest-api-core": "0.2.0",
20
+ "@jskit-ai/resource-core": "0.1.191",
21
+ "@jskit-ai/resource-crud-core": "0.1.191",
22
+ "@jskit-ai/storage-runtime": "0.1.246",
23
+ "@jskit-ai/uploads-runtime": "0.2.0",
22
24
  "json-rest-schema": "^1.0.17"
23
25
  },
24
26
  "description": "Users/account runtime plus HTTP routes for account features.",
@@ -166,8 +168,8 @@
166
168
  }
167
169
  },
168
170
  "peerDependencies": {
169
- "@jskit-ai/auth-core": "0.1.245",
170
- "@jskit-ai/http-runtime": "0.1.245",
171
- "@jskit-ai/kernel": "0.1.247"
171
+ "@jskit-ai/auth-core": "0.2.0",
172
+ "@jskit-ai/http-runtime": "0.2.0",
173
+ "@jskit-ai/kernel": "0.2.0"
172
174
  }
173
175
  }
@@ -9,8 +9,8 @@
9
9
  "./shared": "./src/shared/index.js"
10
10
  },
11
11
  "dependencies": {
12
- "@jskit-ai/crud-core": "0.1.258",
13
- "@jskit-ai/resource-crud-core": "0.1.189"
12
+ "@jskit-ai/crud-core": "0.2.0",
13
+ "@jskit-ai/resource-crud-core": "0.1.191"
14
14
  },
15
15
  "jskit": {
16
16
  "kind": "runtime",
@@ -9,9 +9,9 @@
9
9
  "./shared": "./src/shared/index.js"
10
10
  },
11
11
  "dependencies": {
12
- "@jskit-ai/crud-core": "0.1.258",
13
- "@jskit-ai/resource-crud-core": "0.1.189",
14
- "@jskit-ai/workspaces-core": "0.1.225"
12
+ "@jskit-ai/crud-core": "0.2.0",
13
+ "@jskit-ai/resource-crud-core": "0.1.191",
14
+ "@jskit-ai/workspaces-core": "0.2.0"
15
15
  },
16
16
  "jskit": {
17
17
  "kind": "runtime",
@@ -20,7 +20,7 @@ function createService({
20
20
  async function updateNotifications(request, user, payload = {}, options = {}) {
21
21
  const profile = await resolveUserProfile(userProfilesRepository, user);
22
22
  if (!profile) {
23
- throw new AppError(404, "User profile was not found.");
23
+ throw new AppError(404, "User profile was not found.", { code: "users.profile.notFound" });
24
24
  }
25
25
 
26
26
  const settings = await userSettingsRepository.updateNotifications(profile.id, payload);
@@ -20,7 +20,7 @@ function createService({
20
20
  async function updatePreferences(request, user, payload = {}, options = {}) {
21
21
  const profile = await resolveUserProfile(userProfilesRepository, user);
22
22
  if (!profile) {
23
- throw new AppError(404, "User profile was not found.");
23
+ throw new AppError(404, "User profile was not found.", { code: "users.profile.notFound" });
24
24
  }
25
25
 
26
26
  const settings = await userSettingsRepository.updatePreferences(profile.id, payload);
@@ -21,7 +21,7 @@ function createService({
21
21
  async function getForUser(request, user, options = {}) {
22
22
  const profile = await resolveUserProfile(userProfilesRepository, user);
23
23
  if (!profile) {
24
- throw new AppError(404, "User profile was not found.");
24
+ throw new AppError(404, "User profile was not found.", { code: "users.profile.notFound" });
25
25
  }
26
26
 
27
27
  const settings = await userSettingsRepository.ensureForUserId(profile.id);
@@ -38,7 +38,7 @@ function createService({
38
38
  async function updateProfile(request, user, payload = {}, options = {}) {
39
39
  const profile = await resolveUserProfile(userProfilesRepository, user);
40
40
  if (!profile) {
41
- throw new AppError(404, "User profile was not found.");
41
+ throw new AppError(404, "User profile was not found.", { code: "users.profile.notFound" });
42
42
  }
43
43
 
44
44
  let session = null;
@@ -73,7 +73,9 @@ function createService({
73
73
  const result = await avatarService.uploadForUser(user, avatarUpload);
74
74
  const profile = result?.profile || null;
75
75
  if (!profile) {
76
- throw new AppError(500, "Avatar upload completed without a profile result.");
76
+ throw new AppError(500, "Avatar upload completed without a profile result.", {
77
+ code: "users.avatar.uploadResultMissing"
78
+ });
77
79
  }
78
80
 
79
81
  const settings = await userSettingsRepository.ensureForUserId(profile.id);
@@ -107,7 +109,7 @@ function createService({
107
109
 
108
110
  const avatar = await avatarService.readForUser(user);
109
111
  if (!avatar) {
110
- throw new AppError(404, "Avatar not found.");
112
+ throw new AppError(404, "Avatar not found.", { code: "users.avatar.notFound" });
111
113
  }
112
114
 
113
115
  return avatar;
@@ -34,7 +34,7 @@ function createService({ userProfilesRepository, avatarStorageService, avatarPol
34
34
  async function resolveProfile(user) {
35
35
  const profile = await resolveUserProfile(userProfilesRepository, user);
36
36
  if (!profile) {
37
- throw new AppError(404, "User profile was not found.");
37
+ throw new AppError(404, "User profile was not found.", { code: "users.profile.notFound" });
38
38
  }
39
39
  return profile;
40
40
  }
@@ -20,12 +20,17 @@ function createService({
20
20
 
21
21
  async function changePassword(request, user, payload = {}, options = {}) {
22
22
  if (!authService || typeof authService.changePassword !== "function") {
23
- throw new AppError(501, "Password change is not available.");
23
+ throw new AppError(501, "Password change is not available.", {
24
+ code: "users.security.passwordChangeUnavailable"
25
+ });
24
26
  }
25
27
 
26
28
  if (payload.confirmPassword !== payload.newPassword) {
27
29
  throw createValidationError({
28
- confirmPassword: "Password confirmation does not match."
30
+ confirmPassword: {
31
+ code: "users.security.passwordConfirmationMismatch",
32
+ message: "Password confirmation does not match."
33
+ }
29
34
  });
30
35
  }
31
36
 
@@ -38,19 +43,23 @@ function createService({
38
43
  return {
39
44
  session: result?.session || null,
40
45
  response: returnJsonApiMeta({
41
- message: "Password updated."
46
+ message: "Password updated.",
47
+ messageCode: "users.security.passwordUpdated",
48
+ messageParams: {}
42
49
  })
43
50
  };
44
51
  }
45
52
 
46
53
  async function setPasswordMethodEnabled(request, user, payload = {}, options = {}) {
47
54
  if (!authService || typeof authService.setPasswordSignInEnabled !== "function") {
48
- throw new AppError(501, "Password method toggle is not available.");
55
+ throw new AppError(501, "Password method toggle is not available.", {
56
+ code: "users.security.passwordMethodToggleUnavailable"
57
+ });
49
58
  }
50
59
 
51
60
  const profile = await resolveUserProfile(userProfilesRepository, user);
52
61
  if (!profile) {
53
- throw new AppError(404, "User profile was not found.");
62
+ throw new AppError(404, "User profile was not found.", { code: "users.profile.notFound" });
54
63
  }
55
64
 
56
65
  const enabled = payload.enabled === true;
@@ -72,7 +81,9 @@ function createService({
72
81
 
73
82
  async function startOAuthProviderLink(request, user, payload = {}, options = {}) {
74
83
  if (!authService || typeof authService.startProviderLink !== "function") {
75
- throw new AppError(501, "OAuth linking is not available.");
84
+ throw new AppError(501, "OAuth linking is not available.", {
85
+ code: "users.security.oauthLinkUnavailable"
86
+ });
76
87
  }
77
88
 
78
89
  return authService.startProviderLink(request, {
@@ -83,7 +94,9 @@ function createService({
83
94
 
84
95
  async function unlinkOAuthProvider(request, user, payload = {}, options = {}) {
85
96
  if (!authService || typeof authService.unlinkProvider !== "function") {
86
- throw new AppError(501, "OAuth unlink is not available.");
97
+ throw new AppError(501, "OAuth unlink is not available.", {
98
+ code: "users.security.oauthUnlinkUnavailable"
99
+ });
87
100
  }
88
101
 
89
102
  return returnJsonApiData(await authService.unlinkProvider(request, {
@@ -93,7 +106,9 @@ function createService({
93
106
 
94
107
  async function logoutOtherSessions(request, _user, options = {}) {
95
108
  if (!authService || typeof authService.signOutOtherSessions !== "function") {
96
- throw new AppError(501, "Logout other sessions is not available.");
109
+ throw new AppError(501, "Logout other sessions is not available.", {
110
+ code: "users.security.logoutOtherSessionsUnavailable"
111
+ });
97
112
  }
98
113
 
99
114
  await authService.signOutOtherSessions(request);
@@ -11,6 +11,16 @@ const passwordChangeMetaOutputValidator = deepFreeze({
11
11
  type: "string",
12
12
  required: true,
13
13
  minLength: 1
14
+ },
15
+ // The English message's code, so a client can show it in the user's language.
16
+ messageCode: {
17
+ type: "string",
18
+ required: false,
19
+ minLength: 1
20
+ },
21
+ messageParams: {
22
+ type: "object",
23
+ required: false
14
24
  }
15
25
  }),
16
26
  mode: "replace"
@@ -6,7 +6,7 @@ import {
6
6
  createJsonRestContext,
7
7
  extractJsonRestCollectionRows
8
8
  } from "@jskit-ai/json-rest-api-core/server/jsonRestApiHost";
9
- import { DEFAULT_USER_SETTINGS } from "../../../shared/settings.js";
9
+ import { DEFAULT_USER_SETTINGS, normalizeLocaleCode } from "../../../shared/settings.js";
10
10
 
11
11
  const USER_SETTINGS_PATCH_FIELDS = Object.freeze([
12
12
  "theme",
@@ -73,7 +73,9 @@ function createRepository({ api } = {}) {
73
73
  )
74
74
  );
75
75
 
76
- return rows[0] || null;
76
+ const row = rows[0] || null;
77
+ // Rows saved before locales were canonical hold lowercased tags ("en-us").
78
+ return typeof row?.locale === "string" ? { ...row, locale: normalizeLocaleCode(row.locale) } : row;
77
79
  }
78
80
 
79
81
  async function findByUserId(userId, options = {}) {
@@ -85,7 +85,9 @@ function createUsersBootstrapContributor({
85
85
  authService.writeSessionCookies(reply, authResult.session);
86
86
  }
87
87
  if (authResult?.transientFailure === true) {
88
- throw new AppError(503, "Authentication service temporarily unavailable. Please retry.");
88
+ throw new AppError(503, "Authentication service temporarily unavailable. Please retry.", {
89
+ code: "users.bootstrap.authUnavailable"
90
+ });
89
91
  }
90
92
 
91
93
  const normalizedUser = authResult?.authenticated === true ? authResult?.actor || null : null;
@@ -1,12 +1,12 @@
1
1
  import { createSchema } from "json-rest-schema";
2
2
  import { deepFreeze } from "@jskit-ai/kernel/shared/support/deepFreeze";
3
- import { normalizeLowerText, normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
3
+ import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
4
4
  import {
5
5
  createSchemaDefinition
6
6
  } from "@jskit-ai/resource-core/shared/resource";
7
7
  import { defineCrudResource } from "@jskit-ai/resource-crud-core/shared/crudResource";
8
8
  import { createOperationMessages } from "../operationMessages.js";
9
- import { DEFAULT_USER_SETTINGS } from "../settings.js";
9
+ import { DEFAULT_USER_SETTINGS, normalizeLocaleCode } from "../settings.js";
10
10
  import {
11
11
  accountSecurityStatusSchema,
12
12
  userProfileOutputSchema
@@ -41,7 +41,7 @@ function normalizePositiveInteger(value) {
41
41
 
42
42
  const userSettingsBodySchema = createSchema({
43
43
  theme: { type: "string", required: true, minLength: 1, maxLength: 32 },
44
- locale: { type: "string", required: true, minLength: 1, maxLength: 24, lowercase: true },
44
+ locale: { type: "string", required: true, minLength: 1, maxLength: 24 },
45
45
  timeZone: { type: "string", required: true, minLength: 1, maxLength: 64 },
46
46
  dateFormat: { type: "string", required: true, minLength: 1, maxLength: 32 },
47
47
  numberFormat: { type: "string", required: true, minLength: 1, maxLength: 32 },
@@ -82,7 +82,7 @@ const userSettingsBodySchema = createSchema({
82
82
 
83
83
  const userSettingsPreferencesSchema = createSchema({
84
84
  theme: { type: "string", required: true, minLength: 1, maxLength: 32 },
85
- locale: { type: "string", required: true, minLength: 1, maxLength: 24, lowercase: true },
85
+ locale: { type: "string", required: true, minLength: 1, maxLength: 24 },
86
86
  timeZone: { type: "string", required: true, minLength: 1, maxLength: 64 },
87
87
  dateFormat: { type: "string", required: true, minLength: 1, maxLength: 32 },
88
88
  numberFormat: { type: "string", required: true, minLength: 1, maxLength: 32 },
@@ -281,7 +281,7 @@ const userSettingsResource = defineCrudResource({
281
281
  required: true,
282
282
  max: 24,
283
283
  defaultTo: DEFAULT_USER_SETTINGS.locale,
284
- setter: (value) => normalizeLowerText(value)
284
+ setter: (value) => normalizeLocaleCode(value)
285
285
  },
286
286
  timeZone: {
287
287
  type: "string",
@@ -1,3 +1,5 @@
1
+ import { normalizeLowerText } from "@jskit-ai/kernel/shared/support/normalize";
2
+
1
3
  const DEFAULT_USER_SETTINGS = Object.freeze({
2
4
  theme: "system",
3
5
  locale: "en",
@@ -12,6 +14,23 @@ const DEFAULT_USER_SETTINGS = Object.freeze({
12
14
  passwordSignInEnabled: true,
13
15
  passwordSetupRequired: false
14
16
  });
17
+
18
+ // A saved locale is a canonical BCP 47 tag ("en-US"). Older rows were stored
19
+ // lowercased ("en-us"), so reads canonicalize too; a value that is not a valid tag
20
+ // keeps the lowercased form it always had.
21
+ function normalizeLocaleCode(value) {
22
+ const locale = normalizeLowerText(value);
23
+ if (!locale) {
24
+ return "";
25
+ }
26
+ try {
27
+ return Intl.getCanonicalLocales(locale)[0] || locale;
28
+ } catch {
29
+ return locale;
30
+ }
31
+ }
32
+
15
33
  export {
16
- DEFAULT_USER_SETTINGS
34
+ DEFAULT_USER_SETTINGS,
35
+ normalizeLocaleCode
17
36
  };
@@ -16,7 +16,9 @@ test("users-core exports are explicit and aligned with production/template usage
16
16
  requiredExports: [
17
17
  "./server/UsersFeature",
18
18
  "./server/UsersIdentityProvider",
19
- "./server/usersExtensions"
19
+ "./server/usersExtensions",
20
+ // The generated client bootstrap registers the package's English catalog.
21
+ "./locales/en.json"
20
22
  ]
21
23
  });
22
24
 
@@ -0,0 +1,16 @@
1
+ import assert from "node:assert/strict";
2
+ import path from "node:path";
3
+ import test from "node:test";
4
+ import { fileURLToPath } from "node:url";
5
+ import { evaluateI18nCatalogContract } from "../../../tooling/test-support/i18nCatalogContract.mjs";
6
+
7
+ const TEST_DIRECTORY = path.dirname(fileURLToPath(import.meta.url));
8
+ const REPO_ROOT = path.resolve(TEST_DIRECTORY, "..", "..", "..");
9
+
10
+ test("users-core English catalog matches its source", () => {
11
+ const { problems } = evaluateI18nCatalogContract({
12
+ repoRoot: REPO_ROOT,
13
+ packageDir: path.join(REPO_ROOT, "packages", "users-core")
14
+ });
15
+ assert.deepEqual(problems, []);
16
+ });
@@ -0,0 +1,114 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { catalogMessage, formatMessage, messageKeyForCode } from "@jskit-ai/kernel/shared/i18n";
4
+ import { validateOperationSection } from "@jskit-ai/http-runtime/shared/validators/operationValidation";
5
+ import en from "../locales/en.json" with { type: "json" };
6
+ import { createService as createAccountProfileService } from "../src/server/accountProfile/accountProfileService.js";
7
+ import { createService as createAccountSecurityService } from "../src/server/accountSecurity/accountSecurityService.js";
8
+ import { createRepository as createUserSettingsRepository } from "../src/server/common/repositories/userSettingsRepository.js";
9
+ import { userSettingsResource } from "../src/shared/resources/userSettingsResource.js";
10
+ import { normalizeLocaleCode } from "../src/shared/settings.js";
11
+
12
+ // A code's catalog English, rendered with its params, is exactly the English sent.
13
+ function catalogEnglish(code, params = {}) {
14
+ return formatMessage(catalogMessage(en, messageKeyForCode(code)), params);
15
+ }
16
+
17
+ function assertCodedError(code) {
18
+ return (error) => {
19
+ assert.equal(error.code, code);
20
+ assert.equal(catalogEnglish(error.code, error.params), error.message);
21
+ return true;
22
+ };
23
+ }
24
+
25
+ const emptyProfiles = {
26
+ async findByIdentity() { return null; },
27
+ async findById() { return null; }
28
+ };
29
+
30
+ test("account services throw coded errors whose catalog English matches", async () => {
31
+ const profileService = createAccountProfileService({
32
+ userProfilesRepository: emptyProfiles,
33
+ userSettingsRepository: {},
34
+ avatarService: { async readForUser() { return null; } }
35
+ });
36
+ await assert.rejects(() => profileService.getForUser({}, { id: 7 }), assertCodedError("users.profile.notFound"));
37
+ await assert.rejects(() => profileService.readAvatar({}, { id: 7 }), assertCodedError("users.avatar.notFound"));
38
+
39
+ const securityService = createAccountSecurityService({ userSettingsRepository: {}, userProfilesRepository: emptyProfiles });
40
+ await assert.rejects(() => securityService.changePassword({}, { id: 7 }), assertCodedError("users.security.passwordChangeUnavailable"));
41
+ await assert.rejects(() => securityService.setPasswordMethodEnabled({}, { id: 7 }), assertCodedError("users.security.passwordMethodToggleUnavailable"));
42
+ await assert.rejects(() => securityService.startOAuthProviderLink({}, { id: 7 }), assertCodedError("users.security.oauthLinkUnavailable"));
43
+ await assert.rejects(() => securityService.unlinkOAuthProvider({}, { id: 7 }), assertCodedError("users.security.oauthUnlinkUnavailable"));
44
+ await assert.rejects(() => securityService.logoutOtherSessions({}, { id: 7 }), assertCodedError("users.security.logoutOtherSessionsUnavailable"));
45
+ });
46
+
47
+ test("password change codes its field error and its success message", async () => {
48
+ const service = createAccountSecurityService({
49
+ userSettingsRepository: {},
50
+ userProfilesRepository: emptyProfiles,
51
+ authService: { async changePassword() { return { session: null }; } }
52
+ });
53
+
54
+ await assert.rejects(
55
+ () => service.changePassword({}, { id: 7 }, { newPassword: "new-password", confirmPassword: "other" }),
56
+ (error) => {
57
+ const coded = error.details.fieldErrorCodes.confirmPassword;
58
+ assert.equal(coded.code, "users.security.passwordConfirmationMismatch");
59
+ assert.equal(catalogEnglish(coded.code, coded.params), error.details.fieldErrors.confirmPassword);
60
+ return true;
61
+ }
62
+ );
63
+
64
+ const { response } = await service.changePassword({}, { id: 7 }, { newPassword: "new-password", confirmPassword: "new-password" });
65
+ assert.deepEqual(response.value, { message: "Password updated.", messageCode: "users.security.passwordUpdated", messageParams: {} });
66
+ assert.equal(catalogEnglish(response.value.messageCode), response.value.message);
67
+ });
68
+
69
+ test("saved locales are canonical BCP 47 tags", () => {
70
+ assert.equal(normalizeLocaleCode("en-us"), "en-US");
71
+ assert.equal(normalizeLocaleCode(" EN-gb "), "en-GB");
72
+ assert.equal(normalizeLocaleCode("zh-hant-tw"), "zh-Hant-TW");
73
+ assert.equal(normalizeLocaleCode("en"), "en");
74
+ assert.equal(normalizeLocaleCode(""), "");
75
+ // Not a language tag: kept in the lowercased form it was always stored in.
76
+ assert.equal(normalizeLocaleCode("English_US"), "english_us");
77
+ });
78
+
79
+ test("preference input keeps the locale's case and storage canonicalizes it", async () => {
80
+ const parsed = validateOperationSection({
81
+ operation: userSettingsResource.operations.preferencesUpdate,
82
+ section: "body",
83
+ value: {
84
+ theme: "system",
85
+ locale: "en-US",
86
+ timeZone: "UTC",
87
+ dateFormat: "system",
88
+ numberFormat: "system",
89
+ currencyCode: "USD",
90
+ avatarSize: 64
91
+ }
92
+ });
93
+ assert.equal(parsed.ok, true);
94
+ assert.equal(parsed.value.locale, "en-US");
95
+ assert.equal(userSettingsResource.schema.locale.setter("it-it"), "it-IT");
96
+ });
97
+
98
+ test("user settings reads canonicalize locales saved lowercased", async () => {
99
+ const repository = createUserSettingsRepository({
100
+ api: {
101
+ resources: {
102
+ userSettings: {
103
+ async query() {
104
+ return { data: [{ id: "7", locale: "en-us", theme: "dark" }] };
105
+ }
106
+ }
107
+ }
108
+ }
109
+ });
110
+
111
+ const settings = await repository.findByUserId("7");
112
+ assert.equal(settings.locale, "en-US");
113
+ assert.equal(settings.theme, "dark");
114
+ });