@proteinjs/user-server 1.23.1 → 1.24.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 +11 -0
- package/dist/generated/index.js +1 -1
- package/dist/src/services/AccessInvite.d.ts.map +1 -1
- package/dist/src/services/AccessInvite.js +25 -17
- package/dist/src/services/AccessInvite.js.map +1 -1
- package/dist/test/AccessGrantProvenance.integration.test.d.ts +2 -0
- package/dist/test/AccessGrantProvenance.integration.test.d.ts.map +1 -0
- package/dist/test/AccessGrantProvenance.integration.test.js +322 -0
- package/dist/test/AccessGrantProvenance.integration.test.js.map +1 -0
- package/generated/index.ts +1 -1
- package/package.json +3 -3
- package/src/services/AccessInvite.ts +11 -1
- package/test/AccessGrantProvenance.integration.test.ts +159 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Reference, StringColumn, Table, getDb, getDbAsSystem, getTables } from '@proteinjs/db';
|
|
2
|
+
import { getDropTestTable } from '@proteinjs/db-driver-spanner/test';
|
|
3
|
+
import { AccessGrant, SharedRecord, User, getSharedDb, tables, withSharedRecordColumns } from '@proteinjs/user';
|
|
4
|
+
import { AccessInvite } from '../src/services/AccessInvite';
|
|
5
|
+
import { UserServerTestEnvironment } from './UserServerTestEnvironment';
|
|
6
|
+
|
|
7
|
+
/** A shared record a plain user may read — the resource every invite and grant below hangs on. */
|
|
8
|
+
interface ProvenanceNote extends SharedRecord {
|
|
9
|
+
title: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class ProvenanceNoteTable extends Table<ProvenanceNote> {
|
|
13
|
+
name = 'user_test_provenance_note';
|
|
14
|
+
auth: Table<ProvenanceNote>['auth'] = {
|
|
15
|
+
db: { all: 'authenticated' },
|
|
16
|
+
service: { all: 'authenticated' },
|
|
17
|
+
};
|
|
18
|
+
columns = withSharedRecordColumns<ProvenanceNote>({
|
|
19
|
+
title: new StringColumn('title'),
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The provenance columns as a reader sees them — typed loosely so the suite compiles before the columns exist. */
|
|
24
|
+
type Provenance = { createdBy?: Reference<User> | null; grantedBy?: Reference<User> | null };
|
|
25
|
+
|
|
26
|
+
const testEnv = new UserServerTestEnvironment();
|
|
27
|
+
const noteTable = new ProvenanceNoteTable() as Table<ProvenanceNote>;
|
|
28
|
+
const dropTestTable = getDropTestTable(testEnv.spannerDriver);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* WHO CONFERRED A GRANT is a stored fact, never a guess. An access invite remembers the account
|
|
32
|
+
* that minted it (`createdBy`, stamped at the mint from the caller); a grant remembers the account
|
|
33
|
+
* that conferred it (`grantedBy`): an accepted invite's grant carries the invite's MINTER — the
|
|
34
|
+
* person who shared — never the acceptor; a grant a caller inserts directly carries that caller;
|
|
35
|
+
* the grant the system confers with no one behind it (the creator's own owner grant at a shared
|
|
36
|
+
* record's birth) carries nobody.
|
|
37
|
+
*
|
|
38
|
+
* Before these columns the only "who" on an invite was last-accept telemetry on a multi-use link
|
|
39
|
+
* and a grant had principal / resource / level alone — "who sent me this" was unrecoverable.
|
|
40
|
+
*/
|
|
41
|
+
describe('AccessGrant provenance — the minter on the invite, the granter on the grant', () => {
|
|
42
|
+
let alice: User;
|
|
43
|
+
let bob: User;
|
|
44
|
+
let carol: User;
|
|
45
|
+
|
|
46
|
+
beforeAll(async () => {
|
|
47
|
+
await testEnv.beforeAll();
|
|
48
|
+
// Name-based resolution (the accept's final `resource.get()` resolves the table by name) and the schema.
|
|
49
|
+
(getTables() as Table<any>[]).push(noteTable);
|
|
50
|
+
await testEnv.spannerDriver.getTableManager().loadTable(noteTable);
|
|
51
|
+
}, 120_000);
|
|
52
|
+
|
|
53
|
+
afterAll(async () => {
|
|
54
|
+
await dropTestTable(noteTable);
|
|
55
|
+
await testEnv.afterAll();
|
|
56
|
+
}, 120_000);
|
|
57
|
+
|
|
58
|
+
beforeEach(async () => {
|
|
59
|
+
const db = getDbAsSystem();
|
|
60
|
+
await db.delete(noteTable, {});
|
|
61
|
+
await db.delete(tables.AccessGrant, {});
|
|
62
|
+
await db.delete(tables.AccessInvite, {});
|
|
63
|
+
await db.delete(tables.User, {});
|
|
64
|
+
alice = await testEnv.createUser({ name: 'Alice Owner', email: 'alice@test.local' });
|
|
65
|
+
bob = await testEnv.createUser({ name: 'Bob Recipient', email: 'bob@test.local' });
|
|
66
|
+
carol = await testEnv.createUser({ name: 'Carol Reader', email: 'carol@test.local' });
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
/** Alice's note: the shared-record insert confers her owner grant as system — the real bootstrap. */
|
|
70
|
+
const aliceNote = async (): Promise<ProvenanceNote> => {
|
|
71
|
+
testEnv.actAs(alice);
|
|
72
|
+
return await getSharedDb().insert(noteTable, { title: 'Trip budget' });
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const grantsOf = async (principal: User, note: ProvenanceNote): Promise<(AccessGrant & Provenance)[]> =>
|
|
76
|
+
(await getDbAsSystem().query(tables.AccessGrant, {
|
|
77
|
+
principal: principal.id,
|
|
78
|
+
resource: note.id,
|
|
79
|
+
resourceTable: noteTable.name,
|
|
80
|
+
})) as (AccessGrant & Provenance)[];
|
|
81
|
+
|
|
82
|
+
/** `minter` (an owner on the note) mints an invite at `accessLevel` and hands back its token. */
|
|
83
|
+
const mint = async (minter: User, note: ProvenanceNote, accessLevel: AccessGrant['accessLevel']): Promise<string> => {
|
|
84
|
+
testEnv.actAs(minter);
|
|
85
|
+
const { token } = await new AccessInvite().createAccessInvite({
|
|
86
|
+
resourceTable: noteTable.name,
|
|
87
|
+
resourceId: note.id,
|
|
88
|
+
accessLevel,
|
|
89
|
+
});
|
|
90
|
+
return token;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
it('an invite carries the account that minted it', async () => {
|
|
94
|
+
const note = await aliceNote();
|
|
95
|
+
const token = await mint(alice, note, 'read');
|
|
96
|
+
|
|
97
|
+
const invite = (await getDbAsSystem().get(tables.AccessInvite, { token })) as Provenance;
|
|
98
|
+
expect(invite.createdBy?._id).toBe(alice.id);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("an accepted invite's grant carries the invite's minter as grantedBy — the sharer, never the acceptor", async () => {
|
|
102
|
+
const note = await aliceNote();
|
|
103
|
+
const token = await mint(alice, note, 'read');
|
|
104
|
+
|
|
105
|
+
testEnv.actAs(bob);
|
|
106
|
+
await new AccessInvite().acceptAccessInvite(token);
|
|
107
|
+
|
|
108
|
+
const [grant] = await grantsOf(bob, note);
|
|
109
|
+
expect(grant.accessLevel).toBe('read');
|
|
110
|
+
expect(grant.grantedBy?._id).toBe(alice.id);
|
|
111
|
+
expect(grant.grantedBy?._id).not.toBe(bob.id);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('an upgrade through a higher invite restamps the grant with that invite’s minter', async () => {
|
|
115
|
+
const note = await aliceNote();
|
|
116
|
+
const readToken = await mint(alice, note, 'read');
|
|
117
|
+
testEnv.actAs(bob);
|
|
118
|
+
await new AccessInvite().acceptAccessInvite(readToken);
|
|
119
|
+
|
|
120
|
+
// A second owner mints the write link; Bob's upgrade through it names that minter.
|
|
121
|
+
testEnv.actAs(alice);
|
|
122
|
+
await getDb().insert(tables.AccessGrant, {
|
|
123
|
+
principal: new Reference<User>(tables.User.name, carol.id),
|
|
124
|
+
resource: new Reference<ProvenanceNote>(noteTable.name, note.id),
|
|
125
|
+
resourceTable: noteTable.name,
|
|
126
|
+
accessLevel: 'owner',
|
|
127
|
+
});
|
|
128
|
+
const writeToken = await mint(carol, note, 'write');
|
|
129
|
+
testEnv.actAs(bob);
|
|
130
|
+
await new AccessInvite().acceptAccessInvite(writeToken);
|
|
131
|
+
|
|
132
|
+
const grants = await grantsOf(bob, note);
|
|
133
|
+
expect(grants).toHaveLength(1);
|
|
134
|
+
expect(grants[0].accessLevel).toBe('write');
|
|
135
|
+
expect(grants[0].grantedBy?._id).toBe(carol.id);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('a grant a caller confers directly carries that caller', async () => {
|
|
139
|
+
const note = await aliceNote();
|
|
140
|
+
testEnv.actAs(alice);
|
|
141
|
+
await getDb().insert(tables.AccessGrant, {
|
|
142
|
+
principal: new Reference<User>(tables.User.name, carol.id),
|
|
143
|
+
resource: new Reference<ProvenanceNote>(noteTable.name, note.id),
|
|
144
|
+
resourceTable: noteTable.name,
|
|
145
|
+
accessLevel: 'read',
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const [grant] = await grantsOf(carol, note);
|
|
149
|
+
expect(grant.grantedBy?._id).toBe(alice.id);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("the creator's own owner grant — conferred by the system at the record's birth — carries no granter", async () => {
|
|
153
|
+
const note = await aliceNote();
|
|
154
|
+
|
|
155
|
+
const [grant] = await grantsOf(alice, note);
|
|
156
|
+
expect(grant.accessLevel).toBe('owner');
|
|
157
|
+
expect(grant.grantedBy?._id ?? undefined).toBeUndefined();
|
|
158
|
+
});
|
|
159
|
+
});
|