Skip to content

Commit

Permalink
Finish up testing
Browse files Browse the repository at this point in the history
Jira ticket: CAMS-461

Co-authored-by: Fritz Madden <[email protected]>
Co-authored-by: Arthur Morrow <[email protected]>
Co-authored-by: Brian Posey <[email protected]>
  • Loading branch information
4 people committed Dec 19, 2024
1 parent c5ad025 commit 94d2f1d
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 30 deletions.
54 changes: 53 additions & 1 deletion backend/function-apps/api/admin/admin.function.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@
import { InvocationContext } from '@azure/functions';
import { AdminController } from '../../../lib/controllers/admin/admin.controller';
import {
buildTestResponseError,
createMockAzureFunctionRequest,
} from '../../azure/testing-helpers';
import handler from './admin.function';
import { UnauthorizedError } from '../../../lib/common-errors/unauthorized-error';

const ADMIN_KEY = 'good-key';

describe('Admin function tests', () => {
const originalEnv = { ...process.env };
const context = new InvocationContext();

beforeEach(() => {
process.env = {
...originalEnv,
ADMIN_KEY,
};
});

afterEach(() => {
process.env = originalEnv;
});

// TODO: test the function
test('testing Migration Delete function', () => {});
test('admin handler will return a 401 response code if api key is missing from body', async () => {
const badRequest = createMockAzureFunctionRequest({
method: 'DELETE',
body: { apiKey: 'bad-key' }, //pragma: allowlist secret
});
const error = new UnauthorizedError('ADMIN-FUNCTION', {
message: 'API key was missing or did not match.',
});
const { azureHttpResponse } = buildTestResponseError(error);
const response = await handler(badRequest, context);
expect(response).toEqual(azureHttpResponse);
});

test('admin handler should call controller when invoked', async () => {
const request = createMockAzureFunctionRequest({
method: 'DELETE',
body: { apiKey: ADMIN_KEY },
});

const adminControllerSpy = jest
.spyOn(AdminController.prototype, 'handleRequest')
.mockResolvedValue({ statusCode: 204 });

const result = await handler(request, context);

expect(adminControllerSpy).toHaveBeenCalled();
expect(result.status).toEqual(204);
});
});
2 changes: 1 addition & 1 deletion backend/function-apps/azure/application-context-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function getApplicationContext<B = unknown>(args: {
session: undefined,
closables: [],
releasables: [],
} satisfies ApplicationContext<B>;
} satisfies ApplicationContext<B | unknown>;
}

async function getApplicationContextSession(context: ApplicationContext) {
Expand Down
22 changes: 14 additions & 8 deletions backend/function-apps/azure/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { ApplicationContext } from '../../lib/adapters/types/basic';
import { getCamsError } from '../../lib/common-errors/error-utilities';
import { LoggerImpl } from '../../lib/adapters/services/logger.service';

const MODULE_NAME = 'FUNCTIONS-MODULE';

function azureToCamsDict(it: Iterable<[string, string]>): CamsDict {
if (!it) return {};
return Array.from(it).reduce((acc, record) => {
Expand All @@ -16,14 +18,18 @@ function azureToCamsDict(it: Iterable<[string, string]>): CamsDict {
export async function azureToCamsHttpRequest<B = unknown>(
request: HttpRequest,
): Promise<CamsHttpRequest<B>> {
return {
method: request.method as CamsHttpMethod,
url: request.url,
headers: azureToCamsDict(request.headers),
query: azureToCamsDict(request.query),
params: request.params,
body: request.body ? ((await request.json()) as unknown as B) : undefined,
};
try {
return {
method: request.method as CamsHttpMethod,
url: request.url,
headers: azureToCamsDict(request.headers),
query: azureToCamsDict(request.query),
params: request.params,
body: request.body ? ((await request.json()) as unknown as B) : undefined,
};
} catch (originalError) {
throw getCamsError(originalError, MODULE_NAME);
}
}

export function toAzureSuccess<T extends object = undefined>(
Expand Down
2 changes: 1 addition & 1 deletion backend/function-apps/azure/testing-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function createMockAzureFunctionRequest(
request: Partial<CamsHttpRequest> = {},
): HttpRequest {
const { headers, method, body, ...other } = request;
const requestBody = body ? JSON.stringify(body) : '';
const requestBody = body ? JSON.stringify(body) : undefined;

const requestInit = {
method: method ?? 'GET',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const MODULE_NAME = 'IMPORT_ACTION_GET_CONSOLIDATIONS';

const etlQueueOutput = output.storageQueue({
queueName: 'migration-task',
connection: 'AzureWebJobs',
connection: 'AzureWebJobsStorage',
});

async function queueMigrateConsolidation(
Expand Down
4 changes: 4 additions & 0 deletions backend/function-apps/migration/host.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"extensions": {
"durableTask": {
"hubName": "%MyTaskHub%"
},
"queues": {
"batchSize": 1,
"maxPollingInterval": "00:00:02"
}
},
"functionTimeout": "01:00:00"
Expand Down
8 changes: 4 additions & 4 deletions backend/function-apps/migration/loadConsolidations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ export const FLATTEN_BOUNDING_ARRAYS = 'flattenBoundingArrays';

const migrationQueue = output.storageQueue({
queueName: 'migration-task',
connection: 'AzureWebJobs',
connection: 'AzureWebJobsStorage',
});

const successQueue = output.storageQueue({
queueName: 'migration-task-success',
connection: 'AzureWebJobs',
connection: 'AzureWebJobsStorage',
});

const failQueue = output.storageQueue({
queueName: 'migration-task-fail',
connection: 'AzureWebJobs',
connection: 'AzureWebJobsStorage',
});

df.app.orchestration(MAIN_ORCHESTRATOR, main);
Expand All @@ -39,7 +39,7 @@ df.app.activity(FLATTEN_BOUNDING_ARRAYS, flattenBoundingArrays);

app.storageQueue(MIGRATE_CONSOLIDATION, {
queueName: 'migration-task',
connection: 'AzureWebJobs',
connection: 'AzureWebJobsStorage',
handler: migrateConsolidation,
extraOutputs: [successQueue, failQueue],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ const MODULE_NAME = 'IMPORT_ACTION_MIGRATE_CONSOLIDATION';

const successQueue = output.storageQueue({
queueName: 'migration-task-success',
connection: 'AzureWebJobs',
connection: 'AzureWebJobsStorage',
});

const failQueue = output.storageQueue({
queueName: 'migration-task-fail',
connection: 'AzureWebJobs',
connection: 'AzureWebJobsStorage',
});

async function migrateConsolidation(message: unknown, context: InvocationContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import { ApplicationContext } from '../../types/basic';
import { CaseAssignmentMongoRepository } from './case-assignment.mongo.repository';
import { MongoCollectionAdapter } from './utils/mongo-adapter';

describe('offices repo', () => {
describe('case assignment repo tests', () => {
let context: ApplicationContext;
let repo: CaseAssignmentMongoRepository;

beforeEach(async () => {
context = await createMockApplicationContext();
repo = new CaseAssignmentMongoRepository(context);
repo = CaseAssignmentMongoRepository.getInstance(context);
});

afterEach(async () => {
await closeDeferred(context);
jest.restoreAllMocks();
repo.release();
});

describe('test happy paths', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ describe('Consolidations Repository tests', () => {

beforeEach(async () => {
context = await createMockApplicationContext();
repo = new ConsolidationOrdersMongoRepository(context);
repo = ConsolidationOrdersMongoRepository.getInstance(context);
});

afterEach(async () => {
await closeDeferred(context);
repo.release();
jest.restoreAllMocks();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ describe('offices repo', () => {

beforeAll(async () => {
context = await createMockApplicationContext();
repo = new OfficesMongoRepository(context);
});

beforeEach(async () => {
repo = OfficesMongoRepository.getInstance(context);
});

afterEach(async () => {
await closeDeferred(context);
jest.restoreAllMocks();
repo.release();
});

test('getOfficeAttorneys', async () => {
Expand Down Expand Up @@ -77,20 +81,16 @@ describe('offices repo', () => {
const officeCode = 'office_code';
jest.spyOn(MongoCollectionAdapter.prototype, 'find').mockRejectedValue(error);

await expect(async () => await repo.getOfficeAttorneys(officeCode)).rejects.toThrow(
camsError,
);
await expect(() => repo.getOfficeAttorneys(officeCode)).rejects.toThrow(camsError);
});

test('putOfficeStaff error handling', async () => {
const session = await createMockApplicationContextSession();
const officeCode = 'test_office_code';

jest.spyOn(MongoCollectionAdapter.prototype, 'insertOne').mockRejectedValue(error);
jest.spyOn(MongoCollectionAdapter.prototype, 'replaceOne').mockRejectedValue(error);

expect(async () => await repo.putOfficeStaff(officeCode, session.user)).rejects.toThrow(
camsError,
);
await expect(() => repo.putOfficeStaff(officeCode, session.user)).rejects.toThrow(camsError);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ describe('User session cache Cosmos repository tests', () => {

beforeEach(async () => {
context = await createMockApplicationContext();
repo = new UserSessionCacheMongoRepository(context);
repo = UserSessionCacheMongoRepository.getInstance(context);
jest.resetAllMocks();
});

afterEach(async () => {
repo.release();
await closeDeferred(context);
});

Expand Down

0 comments on commit 94d2f1d

Please sign in to comment.