-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3260 - CAS Integration 3A - Create new Supplier and Site - Name Fix …
…And Error Log Improvement (#3843) - Fixed the order that first and last names were provided to the formatter. - Added the HTTP bad request logs for better troubleshooting while the final error handling ticket is pending. ### Sample logged error (Invalid SIN). ![image](https://github.com/user-attachments/assets/04626581-6a3b-4a9e-a312-2bb194e670e7)
- Loading branch information
1 parent
8040205
commit 4a2ac40
Showing
6 changed files
with
166 additions
and
37 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
sources/packages/backend/libs/integrations/src/cas/_tests_/unit/cas-test.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { HttpService } from "@nestjs/axios"; | ||
import { CASService } from "@sims/integrations/cas"; | ||
import { ConfigService } from "@sims/utilities/config"; | ||
import { Mocked } from "@suites/doubles.jest"; | ||
import { TestBed } from "@suites/unit"; | ||
|
||
/** | ||
* Default access token used for authentication. | ||
*/ | ||
const ACCESS_TOKEN = "access_token"; | ||
|
||
/** | ||
* Default axios headers for authentication. | ||
*/ | ||
export const DEFAULT_CAS_AXIOS_AUTH_HEADER = { | ||
headers: { Authorization: `Bearer ${ACCESS_TOKEN}` }, | ||
}; | ||
|
||
/** | ||
* Mock the first post call for authentication. | ||
* @param httpServiceMock mocked for HTTP service. | ||
* @returns mocked axios response. | ||
*/ | ||
export function mockAuthenticationResponseOnce( | ||
httpServiceMock: Mocked<HttpService>, | ||
): jest.Mock { | ||
const httpMethodMock = httpServiceMock.axiosRef.post as jest.Mock; | ||
return httpMethodMock.mockResolvedValueOnce({ | ||
data: { | ||
access_token: ACCESS_TOKEN, | ||
token_type: "bearer", | ||
expires_in: 3600, | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* Initializes the service under test. | ||
* @returns array of mocked services. | ||
*/ | ||
export async function initializeService(): Promise< | ||
[ | ||
casService: CASService, | ||
httpService: Mocked<HttpService>, | ||
configService: Mocked<ConfigService>, | ||
] | ||
> { | ||
const { unit, unitRef } = await TestBed.solitary(CASService).compile(); | ||
const configService = unitRef.get(ConfigService); | ||
const httpService = unitRef.get(HttpService); | ||
configService.casIntegration.baseUrl = "cas-url"; | ||
return [unit, httpService, configService]; | ||
} |
70 changes: 70 additions & 0 deletions
70
.../backend/libs/integrations/src/cas/_tests_/unit/cas.service.createSupplierAndSite.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { CASService, CreateSupplierAndSiteData } from "@sims/integrations/cas"; | ||
import { Mocked } from "@suites/unit"; | ||
import { HttpService } from "@nestjs/axios"; | ||
import { | ||
DEFAULT_CAS_AXIOS_AUTH_HEADER, | ||
initializeService, | ||
mockAuthenticationResponseOnce, | ||
} from "./cas-test.utils"; | ||
|
||
describe("CASService-createSupplierAndSite", () => { | ||
let casService: CASService; | ||
let httpService: Mocked<HttpService>; | ||
|
||
beforeAll(async () => { | ||
[casService, httpService] = await initializeService(); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it("Should invoke CAS API with formatted payload when all data was provided as expected.", async () => { | ||
// Arrange | ||
mockAuthenticationResponseOnce(httpService).mockResolvedValue({ | ||
data: { | ||
SUPPLIER_NUMBER: "123456", | ||
SUPPLIER_SITE_CODE: "001", | ||
}, | ||
}); | ||
|
||
const supplierData: CreateSupplierAndSiteData = { | ||
firstName: | ||
"First With Special Characters áÉíÓú and Very Extensive Number of Characters", | ||
lastName: "Some Last-Name With Length Over the Maximum", | ||
sin: "999999999", | ||
emailAddress: "[email protected]", | ||
supplierSite: { | ||
addressLine1: "Street-Special Characters-ãñè-Maximum", | ||
city: "City Name Over Maximum Length", | ||
provinceCode: "BC", | ||
postalCode: "h1h h2h", | ||
}, | ||
}; | ||
|
||
// Act | ||
await casService.createSupplierAndSite(supplierData); | ||
|
||
// Assert | ||
expect(httpService.axiosRef.post).toHaveBeenCalledWith( | ||
"cas-url/cfs/supplier/", | ||
{ | ||
SupplierName: | ||
"SOME LAST-NAME WITH LENGTH OVER THE MAXIMUM, FIRST WITH SPECIAL CHARACTERS AEIOU", | ||
SubCategory: "Individual", | ||
Sin: "999999999", | ||
SupplierAddress: [ | ||
{ | ||
AddressLine1: "STREET-SPECIAL CHARACTERS-ANE-MAXIM", | ||
City: "City Name Over Maximum Le", | ||
Province: "BC", | ||
Country: "CA", | ||
PostalCode: "H1HH2H", | ||
EmailAddress: supplierData.emailAddress, | ||
}, | ||
], | ||
}, | ||
DEFAULT_CAS_AXIOS_AUTH_HEADER, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletion
8
sources/packages/backend/libs/integrations/src/constants/error-code.constants.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
export const DOCUMENT_NUMBER_NOT_FOUND = "DOCUMENT_NUMBER_NOT_FOUND"; | ||
|
||
// Error code used when there is an CAS authentication error. | ||
/** | ||
* Error code used when there is an CAS authentication error. | ||
*/ | ||
export const CAS_AUTH_ERROR = "CAS_AUTH_ERROR"; | ||
/** | ||
* CAS bad request error. | ||
*/ | ||
export const CAS_BAD_REQUEST = "CAS_BAD_REQUEST"; |