Skip to content

Commit 3c610a3

Browse files
committed
enforce MANAGE perm on invite create
1 parent 97defba commit 3c610a3

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

app/src/controllers/invite.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const controller = {
6565
const { bucketId } = await objectService.read(resource);
6666

6767
// Check for manage permission
68-
if (req.currentUser?.AuthType === AuthType.BEARER) {
68+
if (req.currentUser?.authType === AuthType.BEARER) {
6969
let bucketPermissions = [];
7070
const objectPermissions = await objectPermissionService.searchPermissions({
7171
userId: userId,
@@ -97,7 +97,7 @@ const controller = {
9797
await bucketService.read(resource);
9898

9999
// Check for manage permission
100-
if (req.currentUser?.AuthType === AuthType.BEARER) {
100+
if (req.currentUser?.authType === AuthType.BEARER) {
101101
const bucketPermissions = await bucketPermissionService.searchPermissions({
102102
userId: userId,
103103
bucketId: resource,

app/tests/unit/controllers/invite.spec.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const {
1313
const utils = require('../../../src/components/utils');
1414
const { Permissions, ResourceType, AuthType } = require('../../../src/components/constants');
1515

16+
// Mock out utils library and use a spy to observe behavior
17+
jest.mock('../../../src/components/utils');
1618
const SYSTEM_TIME = new Date('2024-03-08T19:00:00.000Z');
1719
const mockResponse = () => {
1820
const res = {};
@@ -47,6 +49,7 @@ describe('createInvite', () => {
4749
const bucketSearchPermissionSpy = jest.spyOn(bucketPermissionService, 'searchPermissions');
4850
const getCurrentIdentitySpy = jest.spyOn(utils, 'getCurrentIdentity');
4951
const getCurrentUserIdSpy = jest.spyOn(userService, 'getCurrentUserId');
52+
const addDashesToUuidSpy = jest.spyOn(utils, 'addDashesToUuid');
5053
const inviteCreateSpy = jest.spyOn(inviteService, 'create');
5154
const objectReadSpy = jest.spyOn(objectService, 'read');
5255
const objectSearchPermissionSpy = jest.spyOn(objectPermissionService, 'searchPermissions');
@@ -58,6 +61,7 @@ describe('createInvite', () => {
5861
beforeEach(() => {
5962
getCurrentIdentitySpy.mockReturnValue(USR_IDENTITY);
6063
getCurrentUserIdSpy.mockResolvedValue(USR_ID);
64+
addDashesToUuidSpy.mockReturnValue(RESOURCE);
6165
});
6266

6367
it('should 422 when expiresAt is more than 7 days away', async () => {
@@ -113,7 +117,7 @@ describe('createInvite', () => {
113117
it('should 403 when no object manage permission found', async () => {
114118
const req = {
115119
body: { objectId: RESOURCE },
116-
currentUser: { AuthType: AuthType.BEARER }
120+
currentUser: { authType: AuthType.BEARER }
117121
};
118122

119123
objectReadSpy.mockResolvedValue({});
@@ -137,7 +141,7 @@ describe('createInvite', () => {
137141
it('should 403 when no object nor bucket manage permission found', async () => {
138142
const req = {
139143
body: { objectId: RESOURCE },
140-
currentUser: { AuthType: AuthType.BEARER }
144+
currentUser: { authType: AuthType.BEARER }
141145
};
142146

143147
bucketSearchPermissionSpy.mockResolvedValue([]);
@@ -165,7 +169,7 @@ describe('createInvite', () => {
165169
it('should 201 when object manage permission found', async () => {
166170
const req = {
167171
body: { objectId: RESOURCE },
168-
currentUser: { AuthType: AuthType.BEARER }
172+
currentUser: { authType: AuthType.BEARER }
169173
};
170174

171175
inviteCreateSpy.mockResolvedValue({ token: TOKEN });
@@ -195,7 +199,7 @@ describe('createInvite', () => {
195199
const email = '[email protected]';
196200
const req = {
197201
body: { objectId: RESOURCE, email: email },
198-
currentUser: { AuthType: AuthType.BEARER }
202+
currentUser: { authType: AuthType.BEARER }
199203
};
200204

201205
bucketSearchPermissionSpy.mockResolvedValue([{}]);
@@ -229,7 +233,7 @@ describe('createInvite', () => {
229233
const expiresAt = Math.floor(new Date('2024-03-09T19:00:00.000Z') / 1000);
230234
const req = {
231235
body: { objectId: RESOURCE, expiresAt: expiresAt },
232-
currentUser: { AuthType: AuthType.BASIC }
236+
currentUser: { authType: AuthType.BASIC }
233237
};
234238

235239
inviteCreateSpy.mockResolvedValue({ token: TOKEN });
@@ -277,7 +281,7 @@ describe('createInvite', () => {
277281
it('should 403 when no bucket manage permission found', async () => {
278282
const req = {
279283
body: { bucketId: RESOURCE },
280-
currentUser: { AuthType: AuthType.BEARER }
284+
currentUser: { authType: AuthType.BEARER }
281285
};
282286

283287
bucketReadSpy.mockResolvedValue({});
@@ -302,7 +306,7 @@ describe('createInvite', () => {
302306
const email = '[email protected]';
303307
const req = {
304308
body: { bucketId: RESOURCE, email: email },
305-
currentUser: { AuthType: AuthType.BEARER }
309+
currentUser: { authType: AuthType.BEARER }
306310
};
307311

308312
bucketReadSpy.mockResolvedValue({});
@@ -332,7 +336,7 @@ describe('createInvite', () => {
332336
const expiresAt = Math.floor(new Date('2024-03-09T19:00:00.000Z') / 1000);
333337
const req = {
334338
body: { bucketId: RESOURCE, expiresAt: expiresAt },
335-
currentUser: { AuthType: AuthType.BASIC }
339+
currentUser: { authType: AuthType.BASIC }
336340
};
337341

338342
bucketReadSpy.mockResolvedValue({ bucketId: RESOURCE });
@@ -365,6 +369,7 @@ describe('useInvite', () => {
365369
const bucketReadSpy = jest.spyOn(bucketService, 'read');
366370
const getCurrentIdentitySpy = jest.spyOn(utils, 'getCurrentIdentity');
367371
const getCurrentUserIdSpy = jest.spyOn(userService, 'getCurrentUserId');
372+
const addDashesToUuidSpy = jest.spyOn(utils, 'addDashesToUuid');
368373
const inviteDeleteSpy = jest.spyOn(inviteService, 'delete');
369374
const inviteReadSpy = jest.spyOn(inviteService, 'read');
370375
const objectAddPermissionsSpy = jest.spyOn(objectPermissionService, 'addPermissions');
@@ -377,6 +382,7 @@ describe('useInvite', () => {
377382
beforeEach(() => {
378383
getCurrentIdentitySpy.mockReturnValue(USR_IDENTITY);
379384
getCurrentUserIdSpy.mockResolvedValue(USR_ID);
385+
addDashesToUuidSpy.mockReturnValue(TOKEN);
380386
});
381387

382388

0 commit comments

Comments
 (0)