|
| 1 | +import { APIGatewayProxyWithCognitoAuthorizerEvent, Context } from 'aws-lambda'; |
| 2 | +import { ConditionalCheckFailedException } from '@aws-sdk/client-dynamodb'; |
| 3 | + |
| 4 | +import { handler } from '../../../../../../src/app/controllers/easy-genomics/laboratory/user/add-laboratory-user.lambda'; |
| 5 | + |
| 6 | +jest.mock('../../../../../../src/app/services/easy-genomics/laboratory-service'); |
| 7 | +jest.mock('../../../../../../src/app/services/easy-genomics/laboratory-user-service'); |
| 8 | +jest.mock('../../../../../../src/app/services/easy-genomics/organization-user-service'); |
| 9 | +jest.mock('../../../../../../src/app/services/easy-genomics/platform-user-service'); |
| 10 | +jest.mock('../../../../../../src/app/services/easy-genomics/user-service'); |
| 11 | +jest.mock('../../../../../../src/app/utils/auth-utils'); |
| 12 | + |
| 13 | +import { LaboratoryService } from '../../../../../../src/app/services/easy-genomics/laboratory-service'; |
| 14 | +import { LaboratoryUserService } from '../../../../../../src/app/services/easy-genomics/laboratory-user-service'; |
| 15 | +import { OrganizationUserService } from '../../../../../../src/app/services/easy-genomics/organization-user-service'; |
| 16 | +import { PlatformUserService } from '../../../../../../src/app/services/easy-genomics/platform-user-service'; |
| 17 | +import { UserService } from '../../../../../../src/app/services/easy-genomics/user-service'; |
| 18 | +import { |
| 19 | + validateLaboratoryManagerAccess, |
| 20 | + validateOrganizationAdminAccess, |
| 21 | +} from '../../../../../../src/app/utils/auth-utils'; |
| 22 | + |
| 23 | +describe('add-laboratory-user.lambda', () => { |
| 24 | + let mockLabService: jest.MockedClass<typeof LaboratoryService>; |
| 25 | + let mockLabUserService: jest.MockedClass<typeof LaboratoryUserService>; |
| 26 | + let mockOrgUserService: jest.MockedClass<typeof OrganizationUserService>; |
| 27 | + let mockPlatformUserService: jest.MockedClass<typeof PlatformUserService>; |
| 28 | + let mockUserService: jest.MockedClass<typeof UserService>; |
| 29 | + let mockValidateOrgAdmin: jest.MockedFunction<typeof validateOrganizationAdminAccess>; |
| 30 | + let mockValidateLabManager: jest.MockedFunction<typeof validateLaboratoryManagerAccess>; |
| 31 | + |
| 32 | + const createEvent = (body: any, overrides: Partial<APIGatewayProxyWithCognitoAuthorizerEvent> = {}) => |
| 33 | + ({ |
| 34 | + body: JSON.stringify(body), |
| 35 | + isBase64Encoded: false, |
| 36 | + httpMethod: 'POST', |
| 37 | + path: '/laboratory/user/add', |
| 38 | + headers: {}, |
| 39 | + requestContext: { |
| 40 | + authorizer: { |
| 41 | + claims: { |
| 42 | + email: 'admin@example.com', |
| 43 | + 'cognito:username': 'admin-user', |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + resource: '', |
| 48 | + queryStringParameters: null, |
| 49 | + multiValueQueryStringParameters: null, |
| 50 | + pathParameters: null, |
| 51 | + stageVariables: null, |
| 52 | + multiValueHeaders: {}, |
| 53 | + ...overrides, |
| 54 | + }) as any; |
| 55 | + |
| 56 | + const createContext = (): Context => |
| 57 | + ({ |
| 58 | + functionName: 'add-laboratory-user', |
| 59 | + functionVersion: '$LATEST', |
| 60 | + invokedFunctionArn: 'arn:aws:lambda:region:acct:function:add-laboratory-user', |
| 61 | + memoryLimitInMB: '128', |
| 62 | + awsRequestId: 'req-id', |
| 63 | + logGroupName: '/aws/lambda/add-laboratory-user', |
| 64 | + logStreamName: '2026/03/11/[$LATEST]test', |
| 65 | + identity: undefined, |
| 66 | + clientContext: undefined, |
| 67 | + callbackWaitsForEmptyEventLoop: true, |
| 68 | + getRemainingTimeInMillis: () => 30000, |
| 69 | + done: jest.fn(), |
| 70 | + fail: jest.fn(), |
| 71 | + succeed: jest.fn(), |
| 72 | + }) as any; |
| 73 | + |
| 74 | + const baseRequest = { |
| 75 | + OrganizationId: 'org-1', |
| 76 | + LaboratoryId: 'lab-1', |
| 77 | + UserId: 'user-1', |
| 78 | + LabManager: true, |
| 79 | + LabTechnician: false, |
| 80 | + } as any; |
| 81 | + |
| 82 | + beforeEach(() => { |
| 83 | + jest.clearAllMocks(); |
| 84 | + mockLabService = LaboratoryService as jest.MockedClass<typeof LaboratoryService>; |
| 85 | + mockLabUserService = LaboratoryUserService as jest.MockedClass<typeof LaboratoryUserService>; |
| 86 | + mockOrgUserService = OrganizationUserService as jest.MockedClass<typeof OrganizationUserService>; |
| 87 | + mockPlatformUserService = PlatformUserService as jest.MockedClass<typeof PlatformUserService>; |
| 88 | + mockUserService = UserService as jest.MockedClass<typeof UserService>; |
| 89 | + mockValidateOrgAdmin = validateOrganizationAdminAccess as any; |
| 90 | + mockValidateLabManager = validateLaboratoryManagerAccess as any; |
| 91 | + |
| 92 | + mockValidateOrgAdmin.mockReturnValue(true); |
| 93 | + mockValidateLabManager.mockReturnValue(false); |
| 94 | + }); |
| 95 | + |
| 96 | + it('adds existing user to laboratory when caller is org admin and mapping does not exist', async () => { |
| 97 | + (mockLabService.prototype.queryByLaboratoryId as jest.Mock).mockResolvedValue({ |
| 98 | + OrganizationId: 'org-1', |
| 99 | + LaboratoryId: 'lab-1', |
| 100 | + }); |
| 101 | + (mockUserService.prototype.get as jest.Mock).mockResolvedValue({ |
| 102 | + UserId: 'user-1', |
| 103 | + }); |
| 104 | + (mockOrgUserService.prototype.get as jest.Mock).mockResolvedValue({ |
| 105 | + OrganizationId: 'org-1', |
| 106 | + UserId: 'user-1', |
| 107 | + }); |
| 108 | + (mockLabUserService.prototype.get as jest.Mock).mockRejectedValueOnce( |
| 109 | + // simulate LaboratoryUserNotFoundError, which should be swallowed |
| 110 | + new (class extends Error {})(), |
| 111 | + ); |
| 112 | + (mockPlatformUserService.prototype.addExistingUserToLaboratory as jest.Mock).mockResolvedValue(true); |
| 113 | + |
| 114 | + const result = await handler(createEvent(baseRequest), createContext(), () => {}); |
| 115 | + |
| 116 | + expect(result.statusCode).toBe(200); |
| 117 | + const body = JSON.parse(result.body); |
| 118 | + expect(body.Status).toBe('Success'); |
| 119 | + expect(mockPlatformUserService.prototype.addExistingUserToLaboratory).toHaveBeenCalled(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('rejects invalid request body', async () => { |
| 123 | + const result = await handler(createEvent({}), createContext(), () => {}); |
| 124 | + |
| 125 | + expect(result.statusCode).toBe(400); |
| 126 | + expect(mockPlatformUserService.prototype.addExistingUserToLaboratory).not.toHaveBeenCalled(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('denies access when caller is neither org admin nor lab manager', async () => { |
| 130 | + mockValidateOrgAdmin.mockReturnValue(false); |
| 131 | + mockValidateLabManager.mockReturnValue(false); |
| 132 | + |
| 133 | + (mockLabService.prototype.queryByLaboratoryId as jest.Mock).mockResolvedValue({ |
| 134 | + OrganizationId: 'org-1', |
| 135 | + LaboratoryId: 'lab-1', |
| 136 | + }); |
| 137 | + (mockUserService.prototype.get as jest.Mock).mockResolvedValue({ |
| 138 | + UserId: 'user-1', |
| 139 | + }); |
| 140 | + |
| 141 | + const result = await handler(createEvent(baseRequest), createContext(), () => {}); |
| 142 | + |
| 143 | + expect(result.statusCode).toBe(403); |
| 144 | + }); |
| 145 | + |
| 146 | + it('returns error when user already has laboratory access mapping', async () => { |
| 147 | + (mockLabService.prototype.queryByLaboratoryId as jest.Mock).mockResolvedValue({ |
| 148 | + OrganizationId: 'org-1', |
| 149 | + LaboratoryId: 'lab-1', |
| 150 | + }); |
| 151 | + (mockUserService.prototype.get as jest.Mock).mockResolvedValue({ |
| 152 | + UserId: 'user-1', |
| 153 | + }); |
| 154 | + (mockOrgUserService.prototype.get as jest.Mock).mockResolvedValue({ |
| 155 | + OrganizationId: 'org-1', |
| 156 | + UserId: 'user-1', |
| 157 | + }); |
| 158 | + (mockLabUserService.prototype.get as jest.Mock).mockResolvedValue({ |
| 159 | + LaboratoryId: 'lab-1', |
| 160 | + UserId: 'user-1', |
| 161 | + }); |
| 162 | + |
| 163 | + const result = await handler(createEvent(baseRequest), createContext(), () => {}); |
| 164 | + |
| 165 | + expect(result.statusCode).toBe(409); |
| 166 | + }); |
| 167 | + |
| 168 | + it('maps ConditionalCheckFailedException from platformUserService to LaboratoryUserAlreadyExistsError', async () => { |
| 169 | + (mockLabService.prototype.queryByLaboratoryId as jest.Mock).mockResolvedValue({ |
| 170 | + OrganizationId: 'org-1', |
| 171 | + LaboratoryId: 'lab-1', |
| 172 | + }); |
| 173 | + (mockUserService.prototype.get as jest.Mock).mockResolvedValue({ |
| 174 | + UserId: 'user-1', |
| 175 | + }); |
| 176 | + (mockOrgUserService.prototype.get as jest.Mock).mockResolvedValue({ |
| 177 | + OrganizationId: 'org-1', |
| 178 | + UserId: 'user-1', |
| 179 | + }); |
| 180 | + (mockLabUserService.prototype.get as jest.Mock).mockRejectedValueOnce(new (class extends Error {})()); |
| 181 | + (mockPlatformUserService.prototype.addExistingUserToLaboratory as jest.Mock).mockRejectedValue( |
| 182 | + new ConditionalCheckFailedException({}), |
| 183 | + ); |
| 184 | + |
| 185 | + const result = await handler(createEvent(baseRequest), createContext(), () => {}); |
| 186 | + |
| 187 | + expect(result.statusCode).toBe(409); |
| 188 | + }); |
| 189 | +}); |
0 commit comments