@proteinjs/user-server 1.28.0 → 1.28.2

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.
@@ -1,5 +1,6 @@
1
1
  import sha256 from 'crypto-js/sha256';
2
- import { SourceRecordSyncRunner, getDbAsSystem } from '@proteinjs/db';
2
+ import { getDbAsSystem } from '@proteinjs/db';
3
+ import { SourceRecordSyncRunner } from '@proteinjs/db/source';
3
4
  import { SourceRepository } from '@proteinjs/reflection';
4
5
  // Deep import (test-only): ServiceAuth is the real RPC door but is not index-exported by
5
6
  // @proteinjs/service — the door test below runs the actual gate rather than pinning metadata.
@@ -21,6 +22,12 @@ class OpsRole implements RoleCatalogEntry {
21
22
  description = 'Operational machinery';
22
23
  }
23
24
 
25
+ /** A second catalog role, so a declaration can come to equal a runtime-edited roles column. */
26
+ class AuditRole implements RoleCatalogEntry {
27
+ role = 'audit';
28
+ description = 'Audit reads';
29
+ }
30
+
24
31
  class TestOpsMachineAccount extends MachineAccount {
25
32
  id = 'machine-test-ops';
26
33
  email = 'machine-ops@test.local';
@@ -68,10 +75,10 @@ describe('Machine accounts as source records', () => {
68
75
  }));
69
76
  };
70
77
 
71
- /** One boot of Db.init's source-record leg with these MachineAccount declarations. */
78
+ /** One boot of Db.init's source-record leg with these MachineAccount declarations — what it did, per table. */
72
79
  const boot = async (declarations: MachineAccount[]) => {
73
80
  seedLoaders(declarations);
74
- await new SourceRecordSyncRunner().load();
81
+ return await new SourceRecordSyncRunner().load();
75
82
  };
76
83
 
77
84
  const machineRow = async () => await getDbAsSystem().get(tables.User, { email: 'machine-ops@test.local' });
@@ -92,7 +99,7 @@ describe('Machine accounts as source records', () => {
92
99
  (SourceRepository.get() as unknown as SourceRepositoryInternals).objectCache[
93
100
  '@proteinjs/user-auth/AuthenticatedUserRepo'
94
101
  ] = [new UserRepo()];
95
- objectCache()['@proteinjs/user/RoleCatalogEntry'] = [new OpsRole()];
102
+ objectCache()['@proteinjs/user/RoleCatalogEntry'] = [new OpsRole(), new AuditRole()];
96
103
  // The two session watchers a real server registers, rebuilt from the seeded cache — and no
97
104
  // socket server exists in this process: the shape of a boot's sync (and of a migration Job,
98
105
  // which never starts one), where deactivations delete sessions before the server is up.
@@ -288,12 +295,11 @@ describe('Machine accounts as source records', () => {
288
295
  // re-declare-after-removal under a changed natural key) must adopt the row by its stable
289
296
  // declared id instead of INSERTing into a PK collision.
290
297
  await boot([new TestOpsMachineAccount()]);
291
- // Runtime-owned credential on the row, plus role drift the re-derivation must NOT resurrect.
292
- await getDbAsSystem().update(tables.User, {
293
- id: 'machine-test-ops',
294
- password: sha256('cred').toString(),
295
- roles: ['ops', 'stale-role'],
296
- });
298
+ // The runtime-owned credential on the row: `password` is a column no declaration covers, so
299
+ // the write leaves the row the sync's. (A DECLARED column written at runtime makes the row
300
+ // the product's — @proteinjs/db's authorship precedence, pinned by the next test — and a
301
+ // declaration never adopts a row that is the product's.)
302
+ await getDbAsSystem().update(tables.User, { id: 'machine-test-ops', password: sha256('cred').toString() });
297
303
  await insertSession('machine-session', 'machine-ops@test.local');
298
304
 
299
305
  class RenamedOps extends TestOpsMachineAccount {
@@ -302,7 +308,7 @@ describe('Machine accounts as source records', () => {
302
308
  await boot([new RenamedOps()]);
303
309
 
304
310
  // One row, adopted in place: id kept (references + credential survive), email re-derived,
305
- // reactivated, grants exactly the declaration's (no resurrection of stale grants).
311
+ // reactivated, grants exactly the declaration's.
306
312
  expect(await getDbAsSystem().query(tables.User, {})).toHaveLength(1);
307
313
  expect(await getDbAsSystem().get(tables.User, { email: 'machine-ops@test.local' })).toBeUndefined();
308
314
  const adopted = await getDbAsSystem().get(tables.User, { email: 'machine-ops-renamed@test.local' });
@@ -320,6 +326,41 @@ describe('Machine accounts as source records', () => {
320
326
  expect(await authenticate('machine-ops-renamed@test.local', 'cred')).toBe(true);
321
327
  });
322
328
 
329
+ it(`a declared column written at runtime makes the row the product's: the next boot leaves it as it is — never brought back, never a failed boot — and the declaration coming to equal it re-adopts it`, async () => {
330
+ // @proteinjs/db's authorship precedence: the sync stamps the declared columns it writes, and
331
+ // a row whose declared columns moved since is the product's until the declaration equals it
332
+ // again. The Roles service refuses machine targets, so the one road to a machine row's roles
333
+ // at runtime is a hand edit — the system write here.
334
+ await boot([new TestOpsMachineAccount()]);
335
+ await getDbAsSystem().update(tables.User, { id: 'machine-test-ops', roles: ['ops', 'audit'] });
336
+ const edited = await machineRow();
337
+
338
+ // The declaration stands still: the row is kept exactly as it is, nothing written.
339
+ expect((await boot([new TestOpsMachineAccount()]))[tables.User.name]).toMatchObject({
340
+ kept: 1,
341
+ updates: 0,
342
+ inserts: 0,
343
+ });
344
+ const kept = await machineRow();
345
+ expect(kept).toMatchObject({
346
+ id: 'machine-test-ops',
347
+ roles: ['ops', 'audit'],
348
+ status: 'active',
349
+ isLoadedFromSource: true,
350
+ });
351
+ expect(kept.updated.valueOf()).toBe(edited.updated.valueOf());
352
+
353
+ // The declaration comes to equal the row: re-adopted (its stamp written, nothing else)...
354
+ class OpsAndAudit extends TestOpsMachineAccount {
355
+ roles = ['ops', 'audit'];
356
+ }
357
+ expect((await boot([new OpsAndAudit()]))[tables.User.name]).toMatchObject({ adopted: 1, kept: 0 });
358
+ expect((await machineRow()).roles).toEqual(['ops', 'audit']);
359
+ // ...and from there the declaration moving again lands as before.
360
+ expect((await boot([new TestOpsMachineAccount()]))[tables.User.name]).toMatchObject({ updates: 1, kept: 0 });
361
+ expect((await machineRow()).roles).toEqual(['ops']);
362
+ });
363
+
323
364
  it(`the staff toggle rides the same watcher: SetUserStatus deactivation kills the target's sessions`, async () => {
324
365
  const admin = await testEnv.createUser({ name: 'Admin', email: 'admin@test.local', roles: ['admin'] });
325
366
  const target = await testEnv.createUser({ name: 'Target', email: 'target@test.local' });
@@ -1,6 +1,7 @@
1
1
  import moment from 'moment';
2
2
  import sha256 from 'crypto-js/sha256';
3
- import { SourceRecordSyncRunner, getDbAsSystem } from '@proteinjs/db';
3
+ import { getDbAsSystem } from '@proteinjs/db';
4
+ import { SourceRecordSyncRunner } from '@proteinjs/db/source';
4
5
  import { EmailSender, MailSink } from '@proteinjs/email-server';
5
6
  import { SourceRepository } from '@proteinjs/reflection';
6
7
  import { MachineAccount, RoleCatalogEntry, UserRepo, tables } from '@proteinjs/user';
@@ -80,9 +80,14 @@ describe('UpdateUserInfo avatar mutations', () => {
80
80
  expect(second.avatarFileId).not.toBe(first.avatarFileId);
81
81
 
82
82
  // The prior file is gone — row and bytes (the byte store checked directly through the
83
- // driver: the gated service read now refuses ids with no readable row by design).
83
+ // driver: the gated service read refuses ids with no readable row by design, and the driver's
84
+ // one answer for bytes that are not there is its not-found error — @proteinjs/db-file's
85
+ // FileStorageError, read by shape).
84
86
  expect(await getFileRow(first.avatarFileId!)).toBeFalsy();
85
- expect(await FileStorage.getDriver().getFileData(first.avatarFileId!)).toBe('');
87
+ await expect(FileStorage.getDriver().getFileData(first.avatarFileId!)).rejects.toMatchObject({
88
+ name: 'FileStorageError',
89
+ code: 'not-found',
90
+ });
86
91
  // The new one is intact.
87
92
  expect(await getFileRow(second.avatarFileId!)).toBeTruthy();
88
93
  });