Skip to content

Commit 276356f

Browse files
committed
server/types/PublicUser: update with accurate PublicUser apiKeys, should be sanitisedApiKeys. Add method for sanitisingApiKeys
1 parent 90335b3 commit 276356f

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
lines changed

server/controllers/user.controller/__testUtils__.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,15 @@ export const mockBaseUserFull: Omit<User, 'createdAt'> = {
5959
export function createMockUser(
6060
overrides: Partial<UserDocument> = {},
6161
unSanitised: boolean = false
62-
): (PublicUser | UserDocument) & Record<string, any> {
62+
): PublicUser | UserDocument {
63+
if (unSanitised) {
64+
return {
65+
...mockBaseUserFull,
66+
...overrides
67+
} as UserDocument;
68+
}
6369
return {
64-
...(unSanitised ? mockBaseUserFull : mockBaseUserSanitised),
70+
...mockBaseUserSanitised,
6571
...overrides
66-
};
72+
} as PublicUser;
6773
}

server/controllers/user.controller/__tests__/authManagement/3rdPartyManagement.test.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Request, Response } from 'express';
55
import { unlinkGithub, unlinkGoogle } from '../../authManagement';
66
import { saveUser } from '../../helpers';
77
import { createMockUser } from '../../__testUtils__';
8+
import { UserDocument } from '../../../../types';
89

910
jest.mock('../../helpers', () => ({
1011
...jest.requireActual('../../helpers'),
@@ -50,10 +51,13 @@ describe('user.controller > auth management > 3rd party auth', () => {
5051
});
5152
});
5253
describe('and when there is a user in the request', () => {
53-
const user = createMockUser({
54-
github: 'testuser',
55-
tokens: [{ kind: 'github' }, { kind: 'google' }]
56-
});
54+
const user = createMockUser(
55+
{
56+
github: 'testuser',
57+
tokens: [{ kind: 'github' }, { kind: 'google' }]
58+
},
59+
true
60+
) as UserDocument;
5761

5862
beforeEach(async () => {
5963
request.user = user;
@@ -96,10 +100,13 @@ describe('user.controller > auth management > 3rd party auth', () => {
96100
});
97101
});
98102
describe('and when there is a user in the request', () => {
99-
const user = createMockUser({
100-
google: 'testuser',
101-
tokens: [{ kind: 'github' }, { kind: 'google' }]
102-
});
103+
const user = createMockUser(
104+
{
105+
google: 'testuser',
106+
tokens: [{ kind: 'github' }, { kind: 'google' }]
107+
},
108+
true
109+
) as UserDocument;
103110

104111
beforeEach(async () => {
105112
request.user = user;

server/controllers/user.controller/__tests__/helpers.test.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ import { UserDocument } from '../../../types';
1010

1111
jest.mock('../../../models/user');
1212

13-
const mockFullUser = createMockUser({
14-
// sensitive fields to be removed:
15-
name: 'bob dylan',
16-
tokens: [],
17-
password: 'password12314',
18-
resetPasswordToken: 'wijroaijwoer',
19-
banned: true
20-
});
13+
const mockFullUser = createMockUser(
14+
{
15+
// sensitive fields to be removed:
16+
name: 'bob dylan',
17+
tokens: [],
18+
password: 'password12314',
19+
resetPasswordToken: 'wijroaijwoer',
20+
banned: true
21+
},
22+
true
23+
) as UserDocument;
2124

2225
const {
2326
name,

server/controllers/user.controller/helpers.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
import crypto from 'crypto';
22
import type { Response } from 'express';
33
import { User } from '../../models/user';
4-
import { PublicUser, UserDocument } from '../../types';
4+
import {
5+
ApiKeyDocument,
6+
PublicUser,
7+
SanitisedApiKey,
8+
UserDocument
9+
} from '../../types';
10+
11+
export function sanitiseApiKey(key: ApiKeyDocument): SanitisedApiKey {
12+
return {
13+
id: key.id,
14+
label: key.label,
15+
lastUsedAt: key.lastUsedAt,
16+
createdAt: key.createdAt
17+
};
18+
}
519

620
/**
721
* Sanitise user objects to remove sensitive fields
822
* @param user
923
* @returns Sanitised user
1024
*/
11-
export function userResponse(user: PublicUser | UserDocument): PublicUser {
25+
export function userResponse(user: UserDocument): PublicUser {
1226
return {
1327
email: user.email,
1428
username: user.username,
1529
preferences: user.preferences,
16-
apiKeys: user.apiKeys,
30+
apiKeys: user.apiKeys.map((el) => sanitiseApiKey(el)),
1731
verified: user.verified,
1832
id: user.id,
1933
totalSize: user.totalSize,

0 commit comments

Comments
 (0)