forked from Xeio/IdleCodeRedeemer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuserManager.test.ts
More file actions
434 lines (379 loc) · 18.5 KB
/
userManager.test.ts
File metadata and controls
434 lines (379 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import { describe, test, expect, beforeAll, beforeEach } from 'bun:test';
import { db, initializeDatabase } from './db';
import { userManager } from './userManager';
import { users, redeemedCodes, pendingCodes } from './schema/index';
import { isEncrypted, encrypt } from '../utils/crypto';
beforeAll(() => {
initializeDatabase();
});
beforeEach(() => {
// Clear in FK-safe order so the users delete never violates constraints
db.delete(pendingCodes).run();
db.delete(redeemedCodes).run();
db.delete(users).run();
});
// ---------------------------------------------------------------------------
// saveCredentials
// ---------------------------------------------------------------------------
describe('saveCredentials', () => {
test('inserts a new user', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
const rows = db.select().from(users).all();
expect(rows).toHaveLength(1);
expect(rows[0]!.discordId).toBe('user-1');
// Credentials are encrypted at rest — raw DB values must not be plaintext
expect(rows[0]!.userId).not.toBe('111');
expect(rows[0]!.userHash).not.toBe('hash-a');
// Decrypted values match originals
const creds = await userManager.getCredentials('user-1');
expect(creds?.userId).toBe('111');
expect(creds?.userHash).toBe('hash-a');
});
test('upserts existing user credentials', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.saveCredentials({ discordId: 'user-1', userId: '222', userHash: 'hash-b' });
const rows = db.select().from(users).all();
expect(rows).toHaveLength(1);
// Decrypted values reflect the latest upsert
const creds = await userManager.getCredentials('user-1');
expect(creds?.userId).toBe('222');
expect(creds?.userHash).toBe('hash-b');
});
test('stores optional server field', async () => {
await userManager.saveCredentials({
discordId: 'user-1',
userId: '111',
userHash: 'hash-a',
server: 'server1',
});
const rows = db.select().from(users).all();
expect(rows[0]!.server).toBe('server1');
});
});
// ---------------------------------------------------------------------------
// getCredentials
// ---------------------------------------------------------------------------
describe('getCredentials', () => {
test('returns null for an unknown discord ID', async () => {
expect(await userManager.getCredentials('unknown')).toBeNull();
});
test('returns the correct credentials for a known user', async () => {
await userManager.saveCredentials({
discordId: 'user-1',
userId: '111',
userHash: 'hash-a',
server: 'sv1',
});
const creds = await userManager.getCredentials('user-1');
expect(creds).not.toBeNull();
expect(creds?.discordId).toBe('user-1');
expect(creds?.userId).toBe('111');
expect(creds?.userHash).toBe('hash-a');
expect(creds?.server).toBe('sv1');
});
});
// ---------------------------------------------------------------------------
// hasCredentials
// ---------------------------------------------------------------------------
describe('hasCredentials', () => {
test('returns false for an unknown discord ID', async () => {
expect(await userManager.hasCredentials('unknown')).toBe(false);
});
test('returns true after saveCredentials', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
expect(await userManager.hasCredentials('user-1')).toBe(true);
});
});
// ---------------------------------------------------------------------------
// updateServer
// ---------------------------------------------------------------------------
describe('updateServer', () => {
test('updates the server field for an existing user', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.updateServer('user-1', 'server-new');
const creds = await userManager.getCredentials('user-1');
expect(creds?.server).toBe('server-new');
});
test('overrides a previously stored server', async () => {
await userManager.saveCredentials({
discordId: 'user-1',
userId: '111',
userHash: 'hash-a',
server: 'server-old',
});
await userManager.updateServer('user-1', 'server-new');
const creds = await userManager.getCredentials('user-1');
expect(creds?.server).toBe('server-new');
});
});
// ---------------------------------------------------------------------------
// deleteCredentials
// ---------------------------------------------------------------------------
describe('deleteCredentials', () => {
test('removes the user row', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.deleteCredentials('user-1');
expect(await userManager.hasCredentials('user-1')).toBe(false);
});
test('is a no-op for an unknown user', async () => {
await userManager.deleteCredentials('nobody');
expect(db.select().from(users).all()).toHaveLength(0);
});
});
// ---------------------------------------------------------------------------
// getAllUsers
// ---------------------------------------------------------------------------
describe('getAllUsers', () => {
test('returns empty array when no users exist', async () => {
expect(await userManager.getAllUsers()).toEqual([]);
});
test('returns all stored users', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.saveCredentials({ discordId: 'user-2', userId: '222', userHash: 'hash-b' });
const all = await userManager.getAllUsers();
expect(all).toHaveLength(2);
const ids = all.map((u) => u.discordId);
expect(ids).toContain('user-1');
expect(ids).toContain('user-2');
});
});
// ---------------------------------------------------------------------------
// autoRedeem default
// ---------------------------------------------------------------------------
describe('autoRedeem default', () => {
test('is true after saveCredentials without explicit value', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
const creds = await userManager.getCredentials('user-1');
expect(creds?.autoRedeem).toBe(true);
});
});
// ---------------------------------------------------------------------------
// setAutoRedeem
// ---------------------------------------------------------------------------
describe('setAutoRedeem', () => {
test('disables auto-redeem for a user', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.setAutoRedeem('user-1', false);
const creds = await userManager.getCredentials('user-1');
expect(creds?.autoRedeem).toBe(false);
});
test('re-enables auto-redeem after disabling', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.setAutoRedeem('user-1', false);
await userManager.setAutoRedeem('user-1', true);
const creds = await userManager.getCredentials('user-1');
expect(creds?.autoRedeem).toBe(true);
});
});
// ---------------------------------------------------------------------------
// getAllUsersWithAutoRedeem
// ---------------------------------------------------------------------------
describe('getAllUsersWithAutoRedeem', () => {
test('returns empty array when no users exist', async () => {
expect(await userManager.getAllUsersWithAutoRedeem()).toEqual([]);
});
test('returns only users with auto-redeem enabled', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.saveCredentials({ discordId: 'user-2', userId: '222', userHash: 'hash-b' });
await userManager.saveCredentials({ discordId: 'user-3', userId: '333', userHash: 'hash-c' });
await userManager.setAutoRedeem('user-2', false);
const enabled = await userManager.getAllUsersWithAutoRedeem();
expect(enabled).toHaveLength(2);
const ids = enabled.map((u) => u.discordId);
expect(ids).toContain('user-1');
expect(ids).toContain('user-3');
expect(ids).not.toContain('user-2');
});
test('returns all users when none have disabled auto-redeem', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.saveCredentials({ discordId: 'user-2', userId: '222', userHash: 'hash-b' });
const enabled = await userManager.getAllUsersWithAutoRedeem();
expect(enabled).toHaveLength(2);
});
test('returns empty when all users have disabled auto-redeem', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.saveCredentials({ discordId: 'user-2', userId: '222', userHash: 'hash-b' });
await userManager.setAutoRedeem('user-1', false);
await userManager.setAutoRedeem('user-2', false);
expect(await userManager.getAllUsersWithAutoRedeem()).toEqual([]);
});
});
// ---------------------------------------------------------------------------
// migratePlaintextCredentials
// ---------------------------------------------------------------------------
describe('migratePlaintextCredentials', () => {
test('is a no-op when the table is empty', async () => {
await userManager.migratePlaintextCredentials();
expect(db.select().from(users).all()).toHaveLength(0);
});
test('encrypts plaintext userId and userHash, preserving decrypted values', async () => {
// Seed a row with plaintext credentials directly (bypassing saveCredentials)
db.insert(users).values({ discordId: 'user-1', userId: '111', userHash: 'hash-a' }).run();
await userManager.migratePlaintextCredentials();
const row = db.select().from(users).get();
// Both fields must now be in encrypted format
expect(isEncrypted(row!.userId)).toBe(true);
expect(isEncrypted(row!.userHash)).toBe(true);
// Decrypted values must match the original plaintext
const creds = await userManager.getCredentials('user-1');
expect(creds?.userId).toBe('111');
expect(creds?.userHash).toBe('hash-a');
});
test('migrates multiple plaintext rows independently', async () => {
db.insert(users).values([
{ discordId: 'user-1', userId: '111', userHash: 'hash-a' },
{ discordId: 'user-2', userId: '222', userHash: 'hash-b' },
]).run();
await userManager.migratePlaintextCredentials();
const creds1 = await userManager.getCredentials('user-1');
const creds2 = await userManager.getCredentials('user-2');
expect(creds1?.userId).toBe('111');
expect(creds1?.userHash).toBe('hash-a');
expect(creds2?.userId).toBe('222');
expect(creds2?.userHash).toBe('hash-b');
});
test('leaves already-encrypted rows unchanged (no double-encryption)', async () => {
// Seed via saveCredentials so the row is already encrypted
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
const before = db.select().from(users).get();
await userManager.migratePlaintextCredentials();
const after = db.select().from(users).get();
// Raw stored values must be identical — no re-encryption occurred
expect(after!.userId).toBe(before!.userId);
expect(after!.userHash).toBe(before!.userHash);
// Decrypted values still correct
const creds = await userManager.getCredentials('user-1');
expect(creds?.userId).toBe('111');
expect(creds?.userHash).toBe('hash-a');
});
test('encrypts only the plaintext field in a mixed-state row', async () => {
// Seed a row where userId is already encrypted but userHash is still plaintext
const encryptedUserId = encrypt('111');
db.insert(users).values({ discordId: 'user-1', userId: encryptedUserId, userHash: 'hash-a' }).run();
await userManager.migratePlaintextCredentials();
const row = db.select().from(users).get();
// userId must be the same encrypted value (not double-encrypted)
expect(row!.userId).toBe(encryptedUserId);
// userHash must now be encrypted
expect(isEncrypted(row!.userHash)).toBe(true);
// Both decrypt correctly
const creds = await userManager.getCredentials('user-1');
expect(creds?.userId).toBe('111');
expect(creds?.userHash).toBe('hash-a');
});
});
// ---------------------------------------------------------------------------
// notification preference defaults
// ---------------------------------------------------------------------------
describe('notification preference defaults', () => {
test('dmOnCode defaults to false after saveCredentials', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
const creds = await userManager.getCredentials('user-1');
expect(creds?.dmOnCode).toBe(false);
});
test('dmOnSuccess defaults to true after saveCredentials', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
const creds = await userManager.getCredentials('user-1');
expect(creds?.dmOnSuccess).toBe(true);
});
test('dmOnFailure defaults to false after saveCredentials', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
const creds = await userManager.getCredentials('user-1');
expect(creds?.dmOnFailure).toBe(false);
});
});
// ---------------------------------------------------------------------------
// setNotificationPreferences
// ---------------------------------------------------------------------------
describe('setNotificationPreferences', () => {
test('enables dmOnCode', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.setNotificationPreferences('user-1', { dmOnCode: true });
const creds = await userManager.getCredentials('user-1');
expect(creds?.dmOnCode).toBe(true);
});
test('disables dmOnSuccess', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.setNotificationPreferences('user-1', { dmOnSuccess: false });
const creds = await userManager.getCredentials('user-1');
expect(creds?.dmOnSuccess).toBe(false);
});
test('enables dmOnFailure', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.setNotificationPreferences('user-1', { dmOnFailure: true });
const creds = await userManager.getCredentials('user-1');
expect(creds?.dmOnFailure).toBe(true);
});
test('updates multiple prefs at once', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.setNotificationPreferences('user-1', {
dmOnCode: true,
dmOnSuccess: false,
dmOnFailure: true,
});
const creds = await userManager.getCredentials('user-1');
expect(creds?.dmOnCode).toBe(true);
expect(creds?.dmOnSuccess).toBe(false);
expect(creds?.dmOnFailure).toBe(true);
});
test('does not affect unspecified prefs', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.setNotificationPreferences('user-1', { dmOnCode: true });
const creds = await userManager.getCredentials('user-1');
// dmOnSuccess and dmOnFailure must remain at their defaults
expect(creds?.dmOnSuccess).toBe(true);
expect(creds?.dmOnFailure).toBe(false);
});
});
// ---------------------------------------------------------------------------
// getDiscordIdsWithDmOnCode
// ---------------------------------------------------------------------------
describe('getDiscordIdsWithDmOnCode', () => {
test('returns empty array when no users exist', async () => {
expect(await userManager.getDiscordIdsWithDmOnCode()).toEqual([]);
});
test('returns only discord IDs of users with dmOnCode enabled', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.saveCredentials({ discordId: 'user-2', userId: '222', userHash: 'hash-b' });
await userManager.saveCredentials({ discordId: 'user-3', userId: '333', userHash: 'hash-c' });
await userManager.setNotificationPreferences('user-1', { dmOnCode: true });
await userManager.setNotificationPreferences('user-3', { dmOnCode: true });
const ids = await userManager.getDiscordIdsWithDmOnCode();
expect(ids).toHaveLength(2);
expect(ids).toContain('user-1');
expect(ids).toContain('user-3');
expect(ids).not.toContain('user-2');
});
test('returns empty when no users have dmOnCode enabled', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
expect(await userManager.getDiscordIdsWithDmOnCode()).toEqual([]);
});
});
// ---------------------------------------------------------------------------
// getUserCount
// ---------------------------------------------------------------------------
describe('getUserCount', () => {
test('returns 0 when no users exist', async () => {
expect(await userManager.getUserCount()).toBe(0);
});
test('returns 1 after one user is saved', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
expect(await userManager.getUserCount()).toBe(1);
});
test('counts multiple users', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.saveCredentials({ discordId: 'user-2', userId: '222', userHash: 'hash-b' });
expect(await userManager.getUserCount()).toBe(2);
});
test('decreases after a user is deleted', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.saveCredentials({ discordId: 'user-2', userId: '222', userHash: 'hash-b' });
await userManager.deleteCredentials('user-1');
expect(await userManager.getUserCount()).toBe(1);
});
test('upsert does not increase the count', async () => {
await userManager.saveCredentials({ discordId: 'user-1', userId: '111', userHash: 'hash-a' });
await userManager.saveCredentials({ discordId: 'user-1', userId: '222', userHash: 'hash-b' });
expect(await userManager.getUserCount()).toBe(1);
});
});