@proteinjs/user-server 1.4.4 → 1.5.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/generated/index.js.map +1 -1
- package/dist/src/authentication/DbSessionStore.d.ts +1 -0
- package/dist/src/authentication/DbSessionStore.d.ts.map +1 -1
- package/dist/src/services/Signup.d.ts +21 -3
- package/dist/src/services/Signup.d.ts.map +1 -1
- package/dist/src/services/Signup.js +66 -66
- package/dist/src/services/Signup.js.map +1 -1
- package/dist/test/SignupInvite.test.d.ts +2 -0
- package/dist/test/SignupInvite.test.d.ts.map +1 -0
- package/dist/test/SignupInvite.test.js +239 -0
- package/dist/test/SignupInvite.test.js.map +1 -0
- package/generated/index.ts +1 -1
- package/package.json +4 -4
- package/src/services/Signup.ts +46 -16
- package/test/SignupInvite.test.ts +175 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import moment from 'moment';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Covers invite token lifetime and the expired-token user experience.
|
|
5
|
+
*
|
|
6
|
+
* Two things are asserted here:
|
|
7
|
+
* 1. `sendInvite` stamps `tokenExpiresAt` from the `INVITE_TOKEN_TTL_DAYS` knob (not a literal),
|
|
8
|
+
* on both the create and the refresh path.
|
|
9
|
+
* 2. An expired token produces an honest, actionable message at `initializeSignup` — the surface
|
|
10
|
+
* the signup page reads — and is distinguishable from a token that never existed / was revoked.
|
|
11
|
+
* Previously both collapsed into "not found or has expired", and outside invite-only mode an
|
|
12
|
+
* expired token wasn't reported at all: signup rendered with the email field hidden and the user
|
|
13
|
+
* could only reach a generic "Sign up failed." on submit.
|
|
14
|
+
*
|
|
15
|
+
* The service's collaborators are stubbed at the module boundary; no db or mail server needed.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const dbGet = jest.fn();
|
|
19
|
+
const dbInsert = jest.fn();
|
|
20
|
+
const dbUpdate = jest.fn();
|
|
21
|
+
const dbDelete = jest.fn();
|
|
22
|
+
const sendEmail = jest.fn();
|
|
23
|
+
|
|
24
|
+
const db = { get: dbGet, insert: dbInsert, update: dbUpdate, delete: dbDelete };
|
|
25
|
+
|
|
26
|
+
// Only the db accessors are stubbed; the table/column classes stay real because the table
|
|
27
|
+
// definitions in @proteinjs/user extend them at import time.
|
|
28
|
+
jest.mock('@proteinjs/db', () => ({
|
|
29
|
+
...jest.requireActual('@proteinjs/db'),
|
|
30
|
+
getDb: () => db,
|
|
31
|
+
getDbAsSystem: () => db,
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
jest.mock('@proteinjs/email-server', () => ({
|
|
35
|
+
EmailSender: jest.fn().mockImplementation(() => ({ sendEmail })),
|
|
36
|
+
getDefaultInviteEmailConfigFactory: () => ({
|
|
37
|
+
getConfig: () => ({ getEmailContent: () => ({ text: 'invite', html: '<p>invite</p>' }) }),
|
|
38
|
+
}),
|
|
39
|
+
getDefaultSignupConfirmationEmailConfigFactory: () => ({
|
|
40
|
+
getConfig: () => ({ getNewUserEmailContent: () => ({ text: 'welcome', html: '<p>welcome</p>' }) }),
|
|
41
|
+
}),
|
|
42
|
+
}));
|
|
43
|
+
|
|
44
|
+
// `merge` is kept real so the generated source graphs still load; only lookup is stubbed, so
|
|
45
|
+
// `getDefaultInviteConfigFactory` falls back to its invite-optional default.
|
|
46
|
+
jest.mock('@proteinjs/reflection', () => ({
|
|
47
|
+
...jest.requireActual('@proteinjs/reflection'),
|
|
48
|
+
SourceRepository: {
|
|
49
|
+
...jest.requireActual('@proteinjs/reflection').SourceRepository,
|
|
50
|
+
get: () => ({ object: () => undefined }),
|
|
51
|
+
},
|
|
52
|
+
}));
|
|
53
|
+
|
|
54
|
+
import { UserRepo } from '@proteinjs/user';
|
|
55
|
+
import { INVITE_TOKEN_TTL_DAYS, Signup } from '../src/services/Signup';
|
|
56
|
+
|
|
57
|
+
const EXPECTED_TTL_DAYS = 90;
|
|
58
|
+
|
|
59
|
+
/** Tolerance for the clock advancing between the service stamping the token and the assertion. */
|
|
60
|
+
const CLOCK_SKEW_TOLERANCE_SECONDS = 60;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Compares against the same `moment().add(days, 'days')` arithmetic the service uses, so a DST
|
|
64
|
+
* boundary inside the window (90 days crosses one) cancels out instead of showing up as an hour of
|
|
65
|
+
* drift. A wrong TTL is still off by days, so this stays a biting assertion.
|
|
66
|
+
*/
|
|
67
|
+
const secondsFromExpectedTtl = (tokenExpiresAt: any, ttlDays: number) =>
|
|
68
|
+
Math.abs(moment(tokenExpiresAt).diff(moment().add(ttlDays, 'days'), 'seconds'));
|
|
69
|
+
|
|
70
|
+
describe('invite token lifetime', () => {
|
|
71
|
+
beforeEach(() => {
|
|
72
|
+
jest.restoreAllMocks();
|
|
73
|
+
jest.clearAllMocks();
|
|
74
|
+
dbInsert.mockImplementation(async (table: any, record: any) => record);
|
|
75
|
+
// `sendInvite` stamps the inviter from the session; there is no session in a unit test.
|
|
76
|
+
jest.spyOn(UserRepo.prototype, 'getUser').mockReturnValue({ id: 'inviter-1' } as any);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('is configured by the INVITE_TOKEN_TTL_DAYS knob', () => {
|
|
80
|
+
expect(INVITE_TOKEN_TTL_DAYS).toBe(EXPECTED_TTL_DAYS);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('stamps tokenExpiresAt from the knob when creating an invite', async () => {
|
|
84
|
+
dbGet.mockResolvedValue(undefined); // no existing user, no existing invite
|
|
85
|
+
|
|
86
|
+
const response = await new Signup().sendInvite('Invitee@Test.local');
|
|
87
|
+
|
|
88
|
+
expect(response.sent).toBe(true);
|
|
89
|
+
expect(dbInsert).toHaveBeenCalledTimes(1);
|
|
90
|
+
const [, inserted] = dbInsert.mock.calls[0];
|
|
91
|
+
expect(inserted.email).toBe('invitee@test.local');
|
|
92
|
+
expect(inserted.invitedBy).toBe('inviter-1');
|
|
93
|
+
expect(secondsFromExpectedTtl(inserted.tokenExpiresAt, INVITE_TOKEN_TTL_DAYS)).toBeLessThan(
|
|
94
|
+
CLOCK_SKEW_TOLERANCE_SECONDS
|
|
95
|
+
);
|
|
96
|
+
// Pin the value too, so changing the constant is a deliberate act rather than a drive-by.
|
|
97
|
+
expect(secondsFromExpectedTtl(inserted.tokenExpiresAt, EXPECTED_TTL_DAYS)).toBeLessThan(
|
|
98
|
+
CLOCK_SKEW_TOLERANCE_SECONDS
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('stamps tokenExpiresAt from the knob when refreshing an existing invite', async () => {
|
|
103
|
+
dbGet
|
|
104
|
+
.mockResolvedValueOnce(undefined) // user lookup
|
|
105
|
+
.mockResolvedValueOnce({ id: 'invite-1', email: 'invitee@test.local', token: 'old-token' });
|
|
106
|
+
|
|
107
|
+
await new Signup().sendInvite('invitee@test.local');
|
|
108
|
+
|
|
109
|
+
expect(dbUpdate).toHaveBeenCalledTimes(1);
|
|
110
|
+
const [, updated] = dbUpdate.mock.calls[0];
|
|
111
|
+
expect(updated.token).not.toBe('old-token');
|
|
112
|
+
expect(secondsFromExpectedTtl(updated.tokenExpiresAt, INVITE_TOKEN_TTL_DAYS)).toBeLessThan(
|
|
113
|
+
CLOCK_SKEW_TOLERANCE_SECONDS
|
|
114
|
+
);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('expired invite user experience', () => {
|
|
119
|
+
beforeEach(() => {
|
|
120
|
+
jest.clearAllMocks();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('tells the user the invite expired, and what to do about it', async () => {
|
|
124
|
+
dbGet.mockResolvedValue({
|
|
125
|
+
id: 'invite-1',
|
|
126
|
+
email: 'invitee@test.local',
|
|
127
|
+
token: 'expired-token',
|
|
128
|
+
tokenExpiresAt: moment().subtract(1, 'day'),
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const response = await new Signup().initializeSignup('expired-token');
|
|
132
|
+
|
|
133
|
+
expect(response.isReady).toBe(false);
|
|
134
|
+
expect(response.error).toMatch(/expired/i);
|
|
135
|
+
// The actionable half — an honest error still has to tell the user their way out.
|
|
136
|
+
expect(response.error).toMatch(/new one/i);
|
|
137
|
+
// It must not read as a generic failure or leave signup half-rendered.
|
|
138
|
+
expect(response.error).not.toMatch(/not found/i);
|
|
139
|
+
expect(response.invite).toBeUndefined();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('reports an unknown or revoked token distinctly from an expired one', async () => {
|
|
143
|
+
dbGet.mockResolvedValue(undefined);
|
|
144
|
+
|
|
145
|
+
const response = await new Signup().initializeSignup('unknown-token');
|
|
146
|
+
|
|
147
|
+
expect(response.isReady).toBe(false);
|
|
148
|
+
expect(response.error).toMatch(/no longer valid/i);
|
|
149
|
+
expect(response.error).not.toMatch(/expired/i);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('accepts a token that has not expired yet', async () => {
|
|
153
|
+
const invite = {
|
|
154
|
+
id: 'invite-1',
|
|
155
|
+
email: 'invitee@test.local',
|
|
156
|
+
token: 'good-token',
|
|
157
|
+
tokenExpiresAt: moment().add(1, 'day'),
|
|
158
|
+
};
|
|
159
|
+
dbGet.mockResolvedValue(invite);
|
|
160
|
+
|
|
161
|
+
const response = await new Signup().initializeSignup('good-token');
|
|
162
|
+
|
|
163
|
+
expect(response.isReady).toBe(true);
|
|
164
|
+
expect(response.error).toBeUndefined();
|
|
165
|
+
expect(response.invite?.email).toBe('invitee@test.local');
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('still blocks tokenless signup when invite-only, without an expiry message', async () => {
|
|
169
|
+
const response = await new Signup().initializeSignup(undefined);
|
|
170
|
+
|
|
171
|
+
// Default config is invite-optional, so this is the open case.
|
|
172
|
+
expect(response.isReady).toBe(true);
|
|
173
|
+
expect(dbGet).not.toHaveBeenCalled();
|
|
174
|
+
});
|
|
175
|
+
});
|