soul-cli 0.8.2 → 0.10.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/.env.sample +11 -2
- package/.github/dependabot.yml +9 -0
- package/.github/pull_request_template.md +8 -0
- package/.github/workflows/ci.yml +33 -0
- package/.github/workflows/pr-title.yml +27 -0
- package/.github/workflows/release.yml +44 -0
- package/.husky/pre-commit +0 -3
- package/.release-please-manifest.json +3 -0
- package/AGENTS.md +31 -0
- package/CHANGELOG.md +35 -0
- package/CLAUDE.md +1 -0
- package/README.md +16 -8
- package/ROADMAP.md +154 -0
- package/SECURITY.md +19 -0
- package/docs/extensions-examples.md +96 -16
- package/docs/ws-examples.md +4 -0
- package/eslint.config.js +29 -0
- package/package.json +44 -24
- package/release-please-config.json +10 -0
- package/scripts/dev.sh +64 -0
- package/src/cli.test.js +128 -0
- package/src/config/index.js +10 -2
- package/src/config/index.test.js +120 -0
- package/src/controllers/auth/common.js +1 -16
- package/src/controllers/auth/common.test.js +23 -0
- package/src/controllers/auth/tables.js +1 -1
- package/src/controllers/auth/token.js +30 -7
- package/src/controllers/auth/user.js +20 -20
- package/src/controllers/auth/user.test.js +232 -0
- package/src/controllers/auth.test.js +445 -0
- package/src/controllers/rows.js +159 -112
- package/src/controllers/rows.test.js +88 -1
- package/src/controllers/tables.js +63 -17
- package/src/controllers/tables.test.js +185 -0
- package/src/db/index.test.js +21 -0
- package/src/extensions.js +105 -36
- package/src/extensions.test.js +340 -0
- package/src/index.js +37 -20
- package/src/index.test.js +118 -0
- package/src/middlewares/api.js +1 -1
- package/src/middlewares/api.test.js +29 -0
- package/src/middlewares/auth.js +11 -48
- package/src/middlewares/auth.test.js +105 -0
- package/src/middlewares/validation.test.js +60 -0
- package/src/schemas/index.js +3 -1
- package/src/schemas/rows.js +15 -5
- package/src/schemas/tables.js +28 -4
- package/src/server.js +85 -4
- package/src/services/authService.js +33 -3
- package/src/services/authService.test.js +115 -0
- package/src/services/rowService.js +25 -18
- package/src/services/rowService.test.js +153 -0
- package/src/services/tableService.js +15 -17
- package/src/services/tableService.test.js +190 -0
- package/src/swagger/swagger.json +4 -7
- package/src/tests/index.js +1 -1
- package/src/utils/index.js +25 -2
- package/src/utils/index.test.js +135 -0
- package/src/utils/logger.js +15 -0
- package/src/utils/sql.js +112 -0
- package/src/utils/sql.test.js +115 -0
- package/src/websocket.js +47 -8
- package/src/websocket.test.js +121 -0
- package/.eslintignore +0 -2
- package/.eslintrc.js +0 -24
- package/.github/workflows/npm-publish.yml +0 -23
- package/src/middlewares/logger.js +0 -13
|
@@ -4,6 +4,8 @@ const app = require('../index');
|
|
|
4
4
|
const config = require('../config');
|
|
5
5
|
const { generateToken } = require('../utils');
|
|
6
6
|
const { testData } = require('../tests/testData');
|
|
7
|
+
const { removeRevokedRefreshTokens } = require('./auth');
|
|
8
|
+
const { authService } = require('../services');
|
|
7
9
|
|
|
8
10
|
const requestWithSupertest = supertest(app);
|
|
9
11
|
|
|
@@ -59,6 +61,46 @@ describe('Auth Endpoints', () => {
|
|
|
59
61
|
expect(res.body).not.toHaveProperty('salt');
|
|
60
62
|
});
|
|
61
63
|
|
|
64
|
+
it('POST /tables/_users/rows should throw 400 error if password is not passed', async () => {
|
|
65
|
+
const accessToken = await generateToken(
|
|
66
|
+
{ username: 'John', isSuperuser: true },
|
|
67
|
+
config.tokenSecret,
|
|
68
|
+
'1H',
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
const res = await requestWithSupertest
|
|
72
|
+
.post('/api/tables/_users/rows')
|
|
73
|
+
.set('Cookie', [`accessToken=${accessToken}`])
|
|
74
|
+
.send({
|
|
75
|
+
fields: { username: 'NoPasswordUser' },
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
expect(res.status).toEqual(400);
|
|
79
|
+
expect(res.body.message).toBe('password is required');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('POST /tables/_users/rows should return 500 for an unexpected database error', async () => {
|
|
83
|
+
const accessToken = await generateToken(
|
|
84
|
+
{ username: 'John', isSuperuser: true },
|
|
85
|
+
config.tokenSecret,
|
|
86
|
+
'1H',
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const res = await requestWithSupertest
|
|
90
|
+
.post('/api/tables/_users/rows')
|
|
91
|
+
.set('Cookie', [`accessToken=${accessToken}`])
|
|
92
|
+
.send({
|
|
93
|
+
fields: {
|
|
94
|
+
username: 'BogusFieldUser',
|
|
95
|
+
password: testData.strongPassword,
|
|
96
|
+
thisColumnDoesNotExist: 'x',
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
expect(res.status).toEqual(500);
|
|
101
|
+
expect(res.body.message).toBe('Server error');
|
|
102
|
+
});
|
|
103
|
+
|
|
62
104
|
it('POST /tables/_users/rows should throw 400 error if the password is not strong', async () => {
|
|
63
105
|
const accessToken = await generateToken(
|
|
64
106
|
{ username: 'John', isSuperuser: true },
|
|
@@ -368,5 +410,408 @@ describe('Auth Endpoints', () => {
|
|
|
368
410
|
expect(res.body).not.toHaveProperty('hashed_password');
|
|
369
411
|
expect(res.body).not.toHaveProperty('salt');
|
|
370
412
|
});
|
|
413
|
+
|
|
414
|
+
it('PUT /auth/change-password/ should throw 401 if the user id in the token does not exist', async () => {
|
|
415
|
+
const accessToken = await generateToken(
|
|
416
|
+
{ username: 'ghost', userId: 999999, isSuperuser: true },
|
|
417
|
+
config.tokenSecret,
|
|
418
|
+
'1H',
|
|
419
|
+
);
|
|
420
|
+
|
|
421
|
+
const res = await requestWithSupertest
|
|
422
|
+
.put('/api/auth/change-password')
|
|
423
|
+
.set('Cookie', [`accessToken=${accessToken}`])
|
|
424
|
+
.send({
|
|
425
|
+
fields: {
|
|
426
|
+
currentPassword: testData.strongPassword2,
|
|
427
|
+
newPassword: testData.strongPassword,
|
|
428
|
+
},
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
expect(res.status).toEqual(401);
|
|
432
|
+
expect(res.body.message).toBe('User not found');
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it('PUT /auth/change-password/ should throw 400 if the new password is weak', async () => {
|
|
436
|
+
const accessToken = await generateToken(
|
|
437
|
+
{ username: 'John', userId: 2, isSuperuser: true },
|
|
438
|
+
config.tokenSecret,
|
|
439
|
+
'1H',
|
|
440
|
+
);
|
|
441
|
+
|
|
442
|
+
const res = await requestWithSupertest
|
|
443
|
+
.put('/api/auth/change-password')
|
|
444
|
+
.set('Cookie', [`accessToken=${accessToken}`])
|
|
445
|
+
.send({
|
|
446
|
+
fields: {
|
|
447
|
+
currentPassword: testData.strongPassword2,
|
|
448
|
+
newPassword: testData.weakPassword,
|
|
449
|
+
},
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
expect(res.status).toEqual(400);
|
|
453
|
+
expect(res.body.message).toBe(
|
|
454
|
+
'This password is weak, it should be at least 8 characters long and contain a combination of lowercase letters, uppercase letters, numbers, and special characters',
|
|
455
|
+
);
|
|
456
|
+
});
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
describe('Extended error paths', () => {
|
|
460
|
+
let superuserToken;
|
|
461
|
+
let noRoleUserId;
|
|
462
|
+
|
|
463
|
+
beforeAll(async () => {
|
|
464
|
+
superuserToken = await generateToken(
|
|
465
|
+
{ username: 'John', isSuperuser: true },
|
|
466
|
+
config.tokenSecret,
|
|
467
|
+
'1H',
|
|
468
|
+
);
|
|
469
|
+
|
|
470
|
+
// register a fresh non-superuser user, then strip their role
|
|
471
|
+
// assignment so downstream tests can exercise the "role not found"
|
|
472
|
+
// paths in both obtainAccessToken and refreshAccessToken.
|
|
473
|
+
await requestWithSupertest
|
|
474
|
+
.post('/api/tables/_users/rows')
|
|
475
|
+
.set('Cookie', [`accessToken=${superuserToken}`])
|
|
476
|
+
.send({
|
|
477
|
+
fields: {
|
|
478
|
+
username: 'NoRoleUser',
|
|
479
|
+
password: testData.strongPassword,
|
|
480
|
+
},
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
const lookup = await requestWithSupertest
|
|
484
|
+
.get('/api/tables/_users/rows?_filters=username__eq:NoRoleUser')
|
|
485
|
+
.set('Cookie', [`accessToken=${superuserToken}`]);
|
|
486
|
+
noRoleUserId = lookup.body.data[0].id;
|
|
487
|
+
|
|
488
|
+
await requestWithSupertest
|
|
489
|
+
.delete(
|
|
490
|
+
`/api/tables/_users_roles/rows/${noRoleUserId}?_lookup_field=user_id`,
|
|
491
|
+
)
|
|
492
|
+
.set('Cookie', [`accessToken=${superuserToken}`]);
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
it('POST /auth/token/obtain returns 401 ROLE_NOT_FOUND_ERROR for a user with no role assigned', async () => {
|
|
496
|
+
const res = await requestWithSupertest
|
|
497
|
+
.post('/api/auth/token/obtain')
|
|
498
|
+
.send({
|
|
499
|
+
fields: { username: 'NoRoleUser', password: testData.strongPassword },
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
expect(res.status).toEqual(401);
|
|
503
|
+
expect(res.body.message).toBe('Role not found for this user');
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it('GET /auth/token/refresh returns 401 USER_NOT_FOUND_ERROR for a nonexistent user id', async () => {
|
|
507
|
+
const refreshToken = await generateToken(
|
|
508
|
+
{ username: 'ghost', userId: 999999, isSuperuser: false },
|
|
509
|
+
config.tokenSecret,
|
|
510
|
+
'1H',
|
|
511
|
+
);
|
|
512
|
+
|
|
513
|
+
const res = await requestWithSupertest
|
|
514
|
+
.get('/api/auth/token/refresh')
|
|
515
|
+
.set('Cookie', [`refreshToken=${refreshToken}`]);
|
|
516
|
+
|
|
517
|
+
expect(res.status).toEqual(401);
|
|
518
|
+
expect(res.body.message).toBe('User not found');
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
it('GET /auth/token/refresh returns 401 ROLE_NOT_FOUND_ERROR for a user with no role assigned', async () => {
|
|
522
|
+
const refreshToken = await generateToken(
|
|
523
|
+
{ username: 'NoRoleUser', userId: noRoleUserId, isSuperuser: false },
|
|
524
|
+
config.tokenSecret,
|
|
525
|
+
'1H',
|
|
526
|
+
);
|
|
527
|
+
|
|
528
|
+
const res = await requestWithSupertest
|
|
529
|
+
.get('/api/auth/token/refresh')
|
|
530
|
+
.set('Cookie', [`refreshToken=${refreshToken}`]);
|
|
531
|
+
|
|
532
|
+
expect(res.status).toEqual(401);
|
|
533
|
+
expect(res.body.message).toBe('Role not found for this user');
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
it('GET /auth/token/refresh returns 403 for a malformed refresh token', async () => {
|
|
537
|
+
const res = await requestWithSupertest
|
|
538
|
+
.get('/api/auth/token/refresh')
|
|
539
|
+
.set('Cookie', ['refreshToken=not-a-valid-token']);
|
|
540
|
+
|
|
541
|
+
expect(res.status).toEqual(403);
|
|
542
|
+
expect(res.body.message).toBe('Invalid refresh token');
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
it('GET /auth/logout clears cookies and revokes the refresh token', async () => {
|
|
546
|
+
const loginRes = await requestWithSupertest
|
|
547
|
+
.post('/api/auth/token/obtain')
|
|
548
|
+
.send({
|
|
549
|
+
fields: {
|
|
550
|
+
username: testData.users.user1.username,
|
|
551
|
+
password: testData.strongPassword2,
|
|
552
|
+
},
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
const cookies = loginRes.headers['set-cookie'];
|
|
556
|
+
const refreshTokenCookie = cookies.find((c) =>
|
|
557
|
+
c.startsWith('refreshToken='),
|
|
558
|
+
);
|
|
559
|
+
const accessTokenCookie = cookies.find((c) =>
|
|
560
|
+
c.startsWith('accessToken='),
|
|
561
|
+
);
|
|
562
|
+
|
|
563
|
+
const logoutRes = await requestWithSupertest
|
|
564
|
+
.get('/api/auth/logout')
|
|
565
|
+
.set('Cookie', [refreshTokenCookie, accessTokenCookie]);
|
|
566
|
+
|
|
567
|
+
expect(logoutRes.status).toEqual(200);
|
|
568
|
+
expect(logoutRes.body.message).toBe('Logout successful');
|
|
569
|
+
expect(logoutRes.headers['set-cookie']).toEqual(
|
|
570
|
+
expect.arrayContaining([
|
|
571
|
+
expect.stringContaining('accessToken=;'),
|
|
572
|
+
expect.stringContaining('refreshToken=;'),
|
|
573
|
+
]),
|
|
574
|
+
);
|
|
575
|
+
|
|
576
|
+
// using the now-revoked refresh token should be rejected
|
|
577
|
+
const refreshRes = await requestWithSupertest
|
|
578
|
+
.get('/api/auth/token/refresh')
|
|
579
|
+
.set('Cookie', [refreshTokenCookie]);
|
|
580
|
+
|
|
581
|
+
expect(refreshRes.status).toEqual(403);
|
|
582
|
+
expect(refreshRes.body.message).toBe('Invalid refresh token');
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
it('GET /auth/logout returns 500 for a malformed refresh token', async () => {
|
|
586
|
+
const res = await requestWithSupertest
|
|
587
|
+
.get('/api/auth/logout')
|
|
588
|
+
.set('Cookie', ['refreshToken=garbage', 'accessToken=garbage']);
|
|
589
|
+
|
|
590
|
+
expect(res.status).toEqual(500);
|
|
591
|
+
expect(res.body.message).toBe('Server error');
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
it('POST /tables/_roles_permissions/rows validates fields via customValidator', async () => {
|
|
595
|
+
const res = await requestWithSupertest
|
|
596
|
+
.post('/api/tables/_roles_permissions/rows')
|
|
597
|
+
.set('Cookie', [`accessToken=${superuserToken}`])
|
|
598
|
+
.send({
|
|
599
|
+
fields: {
|
|
600
|
+
role_id: 1,
|
|
601
|
+
table_name: 'users',
|
|
602
|
+
// missing create/read/update/delete
|
|
603
|
+
},
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
expect(res.status).toEqual(400);
|
|
607
|
+
expect(res.body.error).toBeDefined();
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
it('PUT /tables/_roles_permissions/rows/:pks validates fields via customValidator', async () => {
|
|
611
|
+
const res = await requestWithSupertest
|
|
612
|
+
.put('/api/tables/_roles_permissions/rows/1')
|
|
613
|
+
.set('Cookie', [`accessToken=${superuserToken}`])
|
|
614
|
+
.send({
|
|
615
|
+
fields: {
|
|
616
|
+
role_id: 1,
|
|
617
|
+
table_name: 'users',
|
|
618
|
+
// missing create/read/update/delete
|
|
619
|
+
},
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
expect(res.status).toEqual(400);
|
|
623
|
+
expect(res.body.error).toBeDefined();
|
|
624
|
+
});
|
|
625
|
+
|
|
626
|
+
it('POST /tables/_roles_permissions/rows accepts a fully-specified permission row', async () => {
|
|
627
|
+
// a table created after the default role already has its baseline
|
|
628
|
+
// permissions assigned, so it won't collide with the (role_id,
|
|
629
|
+
// table_name) unique constraint the way an existing table would.
|
|
630
|
+
await requestWithSupertest
|
|
631
|
+
.post('/api/tables')
|
|
632
|
+
.set('Cookie', [`accessToken=${superuserToken}`])
|
|
633
|
+
.send({
|
|
634
|
+
name: 'perm_test_table',
|
|
635
|
+
schema: [{ name: 'x', type: 'TEXT' }],
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
const res = await requestWithSupertest
|
|
639
|
+
.post('/api/tables/_roles_permissions/rows')
|
|
640
|
+
.set('Cookie', [`accessToken=${superuserToken}`])
|
|
641
|
+
.send({
|
|
642
|
+
fields: {
|
|
643
|
+
role_id: 1,
|
|
644
|
+
table_name: 'perm_test_table',
|
|
645
|
+
create: 0,
|
|
646
|
+
read: 1,
|
|
647
|
+
update: 0,
|
|
648
|
+
delete: 0,
|
|
649
|
+
},
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
expect(res.status).toEqual(201);
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
it('PUT /tables/_users/rows/:id strips is_superuser/hashed_password/salt from the request body', async () => {
|
|
656
|
+
const before = await requestWithSupertest
|
|
657
|
+
.get(`/api/tables/_users/rows/${noRoleUserId}`)
|
|
658
|
+
.set('Cookie', [`accessToken=${superuserToken}`]);
|
|
659
|
+
const originalIsSuperuser = before.body.data[0].is_superuser;
|
|
660
|
+
|
|
661
|
+
const res = await requestWithSupertest
|
|
662
|
+
.put(`/api/tables/_users/rows/${noRoleUserId}`)
|
|
663
|
+
.set('Cookie', [`accessToken=${superuserToken}`])
|
|
664
|
+
.send({
|
|
665
|
+
fields: {
|
|
666
|
+
username: 'NoRoleUserRenamed',
|
|
667
|
+
is_superuser: 'true',
|
|
668
|
+
hashed_password: 'attacker-controlled',
|
|
669
|
+
salt: 'attacker-controlled',
|
|
670
|
+
},
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
expect(res.status).toEqual(200);
|
|
674
|
+
|
|
675
|
+
const after = await requestWithSupertest
|
|
676
|
+
.get(`/api/tables/_users/rows/${noRoleUserId}`)
|
|
677
|
+
.set('Cookie', [`accessToken=${superuserToken}`]);
|
|
678
|
+
|
|
679
|
+
expect(after.body.data[0].is_superuser).toEqual(originalIsSuperuser);
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
it('removeRevokedRefreshTokens deletes only expired revoked tokens', () => {
|
|
683
|
+
const expiredToken = 'expired-revoked-token';
|
|
684
|
+
const freshToken = 'fresh-revoked-token';
|
|
685
|
+
|
|
686
|
+
authService.saveRevokedRefreshToken({
|
|
687
|
+
refreshToken: expiredToken,
|
|
688
|
+
expiresAt: Math.floor(Date.now() / 1000) - 3600,
|
|
689
|
+
});
|
|
690
|
+
authService.saveRevokedRefreshToken({
|
|
691
|
+
refreshToken: freshToken,
|
|
692
|
+
expiresAt: Math.floor(Date.now() / 1000) + 3600,
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
removeRevokedRefreshTokens();
|
|
696
|
+
|
|
697
|
+
expect(
|
|
698
|
+
authService.getRevokedRefreshToken({ refreshToken: expiredToken }),
|
|
699
|
+
).toHaveLength(0);
|
|
700
|
+
expect(
|
|
701
|
+
authService.getRevokedRefreshToken({ refreshToken: freshToken }),
|
|
702
|
+
).toHaveLength(1);
|
|
703
|
+
});
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
describe('Cookie SameSite/Secure configuration', () => {
|
|
707
|
+
const freshAppWith = (configOverrides) => {
|
|
708
|
+
jest.resetModules();
|
|
709
|
+
jest.doMock('../config', () => ({
|
|
710
|
+
...jest.requireActual('../config'),
|
|
711
|
+
...configOverrides,
|
|
712
|
+
}));
|
|
713
|
+
return require('../index');
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
afterEach(() => {
|
|
717
|
+
jest.dontMock('../config');
|
|
718
|
+
jest.resetModules();
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
it('defaults to SameSite=Lax with no Secure flag (unchanged from unset-attribute behavior)', async () => {
|
|
722
|
+
const res = await supertest(app)
|
|
723
|
+
.post('/api/auth/token/obtain')
|
|
724
|
+
.send({
|
|
725
|
+
fields: {
|
|
726
|
+
username: testData.users.user1.username,
|
|
727
|
+
password: testData.strongPassword2,
|
|
728
|
+
},
|
|
729
|
+
});
|
|
730
|
+
|
|
731
|
+
expect(res.status).toEqual(201);
|
|
732
|
+
const cookies = res.headers['set-cookie'];
|
|
733
|
+
expect(cookies.every((cookie) => /SameSite=Lax/i.test(cookie))).toBe(
|
|
734
|
+
true,
|
|
735
|
+
);
|
|
736
|
+
expect(cookies.some((cookie) => /Secure/i.test(cookie))).toBe(false);
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
it('honors a configured SameSite=None and Secure=true (cross-origin dev mode)', async () => {
|
|
740
|
+
const crossOriginApp = freshAppWith({
|
|
741
|
+
cookie: { sameSite: 'none', secure: true },
|
|
742
|
+
});
|
|
743
|
+
|
|
744
|
+
const res = await supertest(crossOriginApp)
|
|
745
|
+
.post('/api/auth/token/obtain')
|
|
746
|
+
.send({
|
|
747
|
+
fields: {
|
|
748
|
+
username: testData.users.user1.username,
|
|
749
|
+
password: testData.strongPassword2,
|
|
750
|
+
},
|
|
751
|
+
});
|
|
752
|
+
|
|
753
|
+
expect(res.status).toEqual(201);
|
|
754
|
+
const cookies = res.headers['set-cookie'];
|
|
755
|
+
expect(cookies.every((cookie) => /SameSite=None/i.test(cookie))).toBe(
|
|
756
|
+
true,
|
|
757
|
+
);
|
|
758
|
+
expect(cookies.every((cookie) => /Secure/i.test(cookie))).toBe(true);
|
|
759
|
+
});
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
describe('Multiple roles per user', () => {
|
|
763
|
+
// Regression test for a bug where _users_roles' unique constraint was
|
|
764
|
+
// declared as UNIQUE(user_id, user_id) instead of
|
|
765
|
+
// UNIQUE(user_id, role_id), which made the DB reject a second role
|
|
766
|
+
// assignment for any user -- contradicting docs/auth.md's "a user can
|
|
767
|
+
// belong to any number of roles."
|
|
768
|
+
it('allows assigning a second role to a user that already has one', async () => {
|
|
769
|
+
const superuserToken = await generateToken(
|
|
770
|
+
{ username: 'John', isSuperuser: true },
|
|
771
|
+
config.tokenSecret,
|
|
772
|
+
'1H',
|
|
773
|
+
);
|
|
774
|
+
|
|
775
|
+
const userRes = await requestWithSupertest
|
|
776
|
+
.post('/api/tables/_users/rows')
|
|
777
|
+
.set('Cookie', [`accessToken=${superuserToken}`])
|
|
778
|
+
.send({
|
|
779
|
+
fields: {
|
|
780
|
+
username: 'MultiRoleUser',
|
|
781
|
+
password: testData.strongPassword,
|
|
782
|
+
},
|
|
783
|
+
});
|
|
784
|
+
expect(userRes.status).toEqual(201);
|
|
785
|
+
|
|
786
|
+
const userLookup = await requestWithSupertest
|
|
787
|
+
.get('/api/tables/_users/rows?_filters=username__eq:MultiRoleUser')
|
|
788
|
+
.set('Cookie', [`accessToken=${superuserToken}`]);
|
|
789
|
+
const userId = userLookup.body.data[0].id;
|
|
790
|
+
|
|
791
|
+
const roleRes = await requestWithSupertest
|
|
792
|
+
.post('/api/tables/_roles/rows')
|
|
793
|
+
.set('Cookie', [`accessToken=${superuserToken}`])
|
|
794
|
+
.send({ fields: { name: 'second-role' } });
|
|
795
|
+
expect(roleRes.status).toEqual(201);
|
|
796
|
+
|
|
797
|
+
const roleLookup = await requestWithSupertest
|
|
798
|
+
.get('/api/tables/_roles/rows?_filters=name__eq:second-role')
|
|
799
|
+
.set('Cookie', [`accessToken=${superuserToken}`]);
|
|
800
|
+
const secondRoleId = roleLookup.body.data[0].id;
|
|
801
|
+
|
|
802
|
+
// The user already has the default role assigned automatically on
|
|
803
|
+
// creation -- this second assignment previously violated the buggy
|
|
804
|
+
// UNIQUE(user_id, user_id) constraint.
|
|
805
|
+
const assignRes = await requestWithSupertest
|
|
806
|
+
.post('/api/tables/_users_roles/rows')
|
|
807
|
+
.set('Cookie', [`accessToken=${superuserToken}`])
|
|
808
|
+
.send({ fields: { user_id: userId, role_id: secondRoleId } });
|
|
809
|
+
expect(assignRes.status).toEqual(201);
|
|
810
|
+
|
|
811
|
+
const userRoles = await requestWithSupertest
|
|
812
|
+
.get(`/api/tables/_users_roles/rows?_filters=user_id__eq:${userId}`)
|
|
813
|
+
.set('Cookie', [`accessToken=${superuserToken}`]);
|
|
814
|
+
expect(userRoles.body.data).toHaveLength(2);
|
|
815
|
+
});
|
|
371
816
|
});
|
|
372
817
|
});
|