Skip to content

Commit

Permalink
Merge pull request #938 from US-Trustee-Program/CAMS-283-add-mock-off…
Browse files Browse the repository at this point in the history
…ices-repo

CAMS-283 - Add MockOfficesRepository
  • Loading branch information
btposey authored Sep 30, 2024
2 parents 12eae17 + ab3dfd8 commit bff6d50
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
4 changes: 4 additions & 0 deletions backend/functions/lib/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { MockOfficesGateway } from './testing/mock-gateways/mock.offices.gateway
import { OfficesCosmosDbRepository } from './adapters/gateways/offices.cosmosdb.repository';
import OktaUserGroupGateway from './adapters/gateways/okta/okta-user-group-gateway';
import { UserSessionUseCase } from './use-cases/user-session/user-session';
import { MockOfficesRepository } from './testing/mock-gateways/mock-offices.repository';

export const getAttorneyGateway = (): AttorneyGatewayInterface => {
return MockAttorneysGateway;
Expand Down Expand Up @@ -117,6 +118,9 @@ export const getOfficesGateway = (
};

export const getOfficesRepository = (applicationContext: ApplicationContext): OfficesRepository => {
if (applicationContext.config.authConfig.provider === 'mock') {
return new MockOfficesRepository();
}
return new OfficesCosmosDbRepository(applicationContext);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createMockApplicationContext } from '../testing-utilities';
import { MockOfficesRepository } from './mock-offices.repository';

describe('MockOfficesRepository', () => {
test('should return expected attorney lists by office code', async () => {
const context = await createMockApplicationContext();
const repo = new MockOfficesRepository();

const nyAttys = await repo.getOfficeAttorneys(context, 'USTP_CAMS_Region_2_Office_Manhattan');
expect(nyAttys.map((atty) => atty.name)).toEqual([
'Jessica Pearson',
'Jack McCoy',
"Martha's Son",
]);

const buAttys = await repo.getOfficeAttorneys(context, 'USTP_CAMS_Region_2_Office_Buffalo');
expect(buAttys.map((atty) => atty.name)).toEqual([]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CamsRole } from '../../../../../common/src/cams/roles';
import MockUsers from '../../../../../common/src/cams/test-utilities/mock-user';
import { AttorneyUser, CamsUserReference } from '../../../../../common/src/cams/users';
import { ApplicationContext } from '../../adapters/types/basic';
import { getStorageGateway } from '../../factory';
import { OfficesRepository } from '../../use-cases/gateways.types';

export class MockOfficesRepository implements OfficesRepository {
async getOfficeAttorneys(
context: ApplicationContext,
officeCode: string,
): Promise<AttorneyUser[]> {
// TODO: Remap the office code to use the user.offices when user.offices is changed to use UstpOfficeDetail.
const storageGateway = getStorageGateway(context);
const ustpOffices = storageGateway.getUstpOffices();
if (!ustpOffices.has(officeCode)) {
return Promise.resolve([]);
}
const ustpOffice = ustpOffices.get(officeCode);
const users: AttorneyUser[] = MockUsers.filter(
(mockUser) =>
mockUser.user.roles.includes(CamsRole.TrialAttorney) &&
!!mockUser.user.offices.find(
(office) => !!ustpOffice.groups.find((o) => o.groupDesignator === office.groupDesignator),
),
).map<AttorneyUser>((mockUser) => mockUser.user);
return Promise.resolve(users);
}

putOfficeStaff(
_context: ApplicationContext,
_officeCode: string,
_user: CamsUserReference,
): Promise<void> {
throw new Error('Method not implemented.');
}
}
12 changes: 8 additions & 4 deletions backend/functions/lib/use-cases/offices/offices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OfficesUseCase } from './offices';
import { ApplicationContext } from '../../adapters/types/basic';
import { createMockApplicationContext } from '../../testing/testing-utilities';
import { OFFICES } from '../../../../../common/src/cams/test-utilities/offices.mock';
import { OfficesCosmosDbRepository } from '../../adapters/gateways/offices.cosmosdb.repository';
import * as factory from '../../factory';

describe('offices use case tests', () => {
let applicationContext: ApplicationContext;
Expand All @@ -21,9 +21,13 @@ describe('offices use case tests', () => {

test('should return attorneys', async () => {
const useCase = new OfficesUseCase();
const repoSpy = jest
.spyOn(OfficesCosmosDbRepository.prototype, 'getOfficeAttorneys')
.mockResolvedValue([]);
const repoSpy = jest.fn().mockResolvedValue([]);
jest.spyOn(factory, 'getOfficesRepository').mockImplementation(() => {
return {
putOfficeStaff: jest.fn(),
getOfficeAttorneys: repoSpy,
};
});

const officeCode = 'new-york';
const officeAttorneys = await useCase.getOfficeAttorneys(applicationContext, officeCode);
Expand Down

0 comments on commit bff6d50

Please sign in to comment.