Skip to content

Commit

Permalink
mockBCeIDAccountUserFound and not found
Browse files Browse the repository at this point in the history
  • Loading branch information
lewischen-aot committed Feb 11, 2025
1 parent ac1ab54 commit db01406
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
getAuthRelatedEntities,
getInstitutionToken,
InstitutionTokenTypes,
mockBCeIDAccountDetails,
mockBCeIDAccountUserFound,
mockBCeIDAccountUserNotFound,
} from "../../../../testHelpers";
import {
E2EDataSources,
Expand Down Expand Up @@ -70,7 +71,6 @@ describe("InstitutionUserInstitutionsController(e2e)-createInstitutionUserWithAu

it("Should throw an UnprocessableEntityException error when the user passed in the payload is not found on BCeID.", async () => {
// Arrange
const user = createFakeUser();
const payload = {
bceidUserId: faker.random.alpha({ count: 5 }),
permissions: [
Expand All @@ -82,7 +82,7 @@ describe("InstitutionUserInstitutionsController(e2e)-createInstitutionUserWithAu
};

// Mock BCeID account method to return null response.
await mockBCeIDAccountDetails(appModule, user, collegeFInstitution, true);
await mockBCeIDAccountUserNotFound(appModule);

// Institution token.
const token = await getInstitutionToken(InstitutionTokenTypes.CollegeFUser);
Expand Down Expand Up @@ -121,7 +121,7 @@ describe("InstitutionUserInstitutionsController(e2e)-createInstitutionUserWithAu
});

// Mock BCeID account details.
await mockBCeIDAccountDetails(
await mockBCeIDAccountUserFound(
appModule,
user,
collegeDLocation.institution,
Expand Down Expand Up @@ -163,7 +163,7 @@ describe("InstitutionUserInstitutionsController(e2e)-createInstitutionUserWithAu
};

// Mock get BCeID account details with the initial userName.
await mockBCeIDAccountDetails(
await mockBCeIDAccountUserFound(
appModule,
{ ...user, userName: userGuid },
collegeFInstitution,
Expand Down Expand Up @@ -203,7 +203,7 @@ describe("InstitutionUserInstitutionsController(e2e)-createInstitutionUserWithAu
};

// Mock get BCeID account details
await mockBCeIDAccountDetails(appModule, user, collegeFInstitution);
await mockBCeIDAccountUserFound(appModule, user, collegeFInstitution);
// Institution token.
const token = await getInstitutionToken(InstitutionTokenTypes.CollegeFUser);
const endpoint = "/institutions/institution-user";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import { BCeIDService } from "../../services";
* @param user a persisted user object.
* @param displayName a persisted display name for user login.
* @param institution a persisted institution object.
* @param returnsNull indicates whether mocks a null response.
* @returns a persisted BCeID account object.
*/
export async function mockBCeIDAccountDetails(
export async function mockBCeIDAccountUserFound(
testingModule: TestingModule,
user: User,
institution: Institution,
returnsNull?: boolean,
): Promise<jest.SpyInstance> {
const bceIDService = await getProviderInstanceForModule(
testingModule,
Expand All @@ -27,21 +25,43 @@ export async function mockBCeIDAccountDetails(
return jest
.spyOn(bceIDService, "getAccountDetails")
.mockImplementation(() => {
const response = returnsNull
? null
: {
user: {
guid: user.userName,
displayName: `${user.firstName}_${user.lastName}`,
firstname: user.firstName,
surname: user.lastName,
email: user.email,
},
institution: {
guid: institution.businessGuid,
legalName: institution.legalOperatingName,
},
};
const response = {
user: {
guid: user.userName,
displayName: `${user.firstName}_${user.lastName}`,
firstname: user.firstName,
surname: user.lastName,
email: user.email,
},
institution: {
guid: institution.businessGuid,
legalName: institution.legalOperatingName,
},
};
return Promise.resolve(response);
});
}

/**
* Mocks BCeID account info from BCeID service to return null.
* @param testingModule nest testing module.
* @param user a persisted user object.
* @param displayName a persisted display name for user login.
* @param institution a persisted institution object.
* @returns a persisted BCeID account object.
*/
export async function mockBCeIDAccountUserNotFound(
testingModule: TestingModule,
): Promise<jest.SpyInstance> {
const bceIDService = await getProviderInstanceForModule(
testingModule,
AppInstitutionsModule,
BCeIDService,
);
return jest
.spyOn(bceIDService, "getAccountDetails")
.mockImplementation(() => {
const response = null;
return Promise.resolve(response);
});
}

0 comments on commit db01406

Please sign in to comment.