Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"single-spa-react": "^4.3.1",
"styled-components": "^5.3.7",
"twin.macro": "^3.4.0",
"yaml": "^2.9.0",
"yup": "^0.32.11"
},
"babelMacros": {
Expand Down
92 changes: 92 additions & 0 deletions src/api/useCqlLibraryServiceApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,98 @@ describe("useCqlLibraryServiceApi", () => {
).rejects.toThrow("canceled");
});

it("should search Cql Libraries for an admin-selected user", async () => {
const mockedResponse = { data: { content: [{ id: "1" }] } };
const searchCriteria = {
searchField: "helper",
optionalSearchProperties: ["library"],
};
const controller = new AbortController();
mockedAxios.put.mockResolvedValueOnce(mockedResponse);

const result = await cqlLibraryServiceApi.adminSearchCqlLibrariesForUser(
"profile user/special",
OwnershipType.SHARED,
10,
2,
searchCriteria,
"cqlLibraryName,false",
controller.signal
);

expect(mockedAxios.put).toHaveBeenCalledWith(
`${mockBaseUrl}/cql-libraries/admin/userProfile/profile%20user%2Fspecial/searches`,
searchCriteria,
{
headers: { Authorization: `Bearer ${mockToken}` },
params: {
ownershipType: OwnershipType.SHARED,
limit: 10,
page: 2,
sortInfo: "cqlLibraryName,false",
},
signal: controller.signal,
}
);
expect(result).toEqual(mockedResponse.data);
});

it("should use admin search defaults and convert an All limit", async () => {
const mockedResponse = { data: { content: [] } };
mockedAxios.put.mockResolvedValueOnce(mockedResponse);

const result = await cqlLibraryServiceApi.adminSearchCqlLibrariesForUser(
"profile-user",
OwnershipType.OWNED,
"All"
);

expect(mockedAxios.put).toHaveBeenCalledWith(
`${mockBaseUrl}/cql-libraries/admin/userProfile/profile-user/searches`,
{},
{
headers: { Authorization: `Bearer ${mockToken}` },
params: {
ownershipType: OwnershipType.OWNED,
limit: 1000,
page: 0,
sortInfo: undefined,
},
signal: undefined,
}
);
expect(result).toEqual(mockedResponse.data);
});

it("should preserve cancellation errors from admin library searches", async () => {
mockedAxios.put.mockRejectedValueOnce(new Error("canceled"));

await expect(
cqlLibraryServiceApi.adminSearchCqlLibrariesForUser(
"profile-user",
OwnershipType.OWNED
)
).rejects.toThrow("canceled");
});

it("should provide context when an admin library search fails", async () => {
const error = new Error("Forbidden");
const consoleErrorMock = jest.spyOn(console, "error").mockImplementation();
mockedAxios.put.mockRejectedValueOnce(error);

await expect(
cqlLibraryServiceApi.adminSearchCqlLibrariesForUser(
"profile-user",
OwnershipType.SHARED
)
).rejects.toThrow("Unable to search Cql Libraries for user profile-user");
expect(consoleErrorMock).toHaveBeenCalledWith(
"Unable to search Cql Libraries for user profile-user",
error
);
consoleErrorMock.mockRestore();
});

it("should fetch a single Cql Library", async () => {
const mockedResponse = { data: { id: "1" } };
mockedAxios.get.mockResolvedValueOnce(mockedResponse);
Expand Down
40 changes: 40 additions & 0 deletions src/api/useCqlLibraryServiceApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,46 @@ export class CqlLibraryServiceApi {
}
}

async adminSearchCqlLibrariesForUser(
harpId: string,
ownershipType: OwnershipType,
limit: string | number = 25,
page: number = 0,
searchCriteria?,
sortInfo?,
signal?: AbortSignal
): Promise<any> {
try {
limit = limit === "All" ? 1000 : limit;
const response = await axios.put<any>(
`${this.baseUrl}/cql-libraries/admin/userProfile/${encodeURIComponent(
harpId
)}/searches`,
searchCriteria ?? {},
{
headers: {
Authorization: `Bearer ${this.getAccessToken()}`,
},
params: {
ownershipType,
limit,
page,
sortInfo: sortInfo || undefined,
},
signal,
}
);
return response.data;
} catch (err) {
if (err.message === "canceled") {
throw new Error(err.message);
}
const message = `Unable to search Cql Libraries for user ${harpId}`;
console.error(message, err);
throw new Error(message);
}
}

async fetchCqlLibrary(id: string): Promise<CqlLibrary> {
try {
const response = await axios.get<CqlLibrary>(
Expand Down
12 changes: 12 additions & 0 deletions src/util/wafIntercept.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ describe("wafIntercept", () => {
await expect(wafIntercept(error)).rejects.toEqual(error);
});

it("should pass through 403 responses without a content-type header", async () => {
const error = {
response: {
status: 403,
headers: {},
data: "Forbidden",
},
};

await expect(wafIntercept(error)).rejects.toEqual(error);
});

it("should handle WAF block with different case variations", async () => {
const error = {
response: {
Expand Down
9 changes: 6 additions & 3 deletions src/util/wafIntercept.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import DOMPurify from "dompurify";

const wafIntercept = (error) => {
const contentType =
error?.response?.headers?.["content-type"] ??
error?.response?.headers?.get?.("content-type") ??
"";

// Check for WAF block
if (
error?.response?.status === 403 &&
error?.response?.headers["content-type"]
.toLowerCase()
.includes("text/html") &&
contentType.toLowerCase().includes("text/html") &&
(JSON.stringify(error.response.data)
.toLocaleLowerCase()
.includes("soc@hcqis.org") ||
Expand Down
Loading