diff --git a/src/constants.ts b/src/constants.ts index aacc3a981..92cc24744 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -22,10 +22,11 @@ export const CONTACT_SUPPORT_TEAM_MESSAGE = export const DETAILED_UPGRADES_VIEW_ENABLED = import.meta.env.VITE_DETAILED_UPGRADES_VIEW_ENABLED === "true"; export const IS_MSW_ENABLED = import.meta.env.VITE_MSW_ENABLED === "true"; -export const MSW_ENDPOINTS_TO_INTERCEPT = - (import.meta.env.VITE_MSW_ENDPOINTS_TO_INTERCEPT ?? "") - .split(",") - .filter(Boolean) ?? []; +export const MSW_ENDPOINTS_TO_INTERCEPT = ( + import.meta.env.VITE_MSW_ENDPOINTS_TO_INTERCEPT ?? "" +) + .split(",") + .filter(Boolean); export const HOMEPAGE_PATH = ROUTES.overview.root(); export const DEFAULT_ACCESS_GROUP_NAME = "global"; export const BREAKPOINT_PX = { diff --git a/src/tests/browser.ts b/src/tests/browser.ts index 7e486456b..57da499e7 100644 --- a/src/tests/browser.ts +++ b/src/tests/browser.ts @@ -1,18 +1,11 @@ import { API_URL, API_URL_OLD, MSW_ENDPOINTS_TO_INTERCEPT } from "@/constants"; import type { RequestHandler } from "msw"; -import { http, HttpResponse, passthrough } from "msw"; +import { http, passthrough } from "msw"; import { setupWorker } from "msw/browser"; import fallbackHandlers from "./server/handlers"; const handlers: RequestHandler[] = [ http.all("*", ({ request }) => { - if (request.url.includes("sentry.is.canonical.com")) { - return passthrough(); - } - - return; - }), - http.all("*", async ({ request }) => { if (!request.url.includes(API_URL) && !request.url.includes(API_URL_OLD)) { return passthrough(); } @@ -30,9 +23,9 @@ const handlers: RequestHandler[] = [ ...fallbackHandlers, - http.all("*", async ({ request }) => { - console.log("Request not handled:", request.url); - return new HttpResponse(null, { status: 404 }); + http.all("*", ({ request }) => { + console.warn("MSW: No handler matched, passing through:", request.url); + return passthrough(); }), ]; diff --git a/src/tests/endpointsToIntercept.json b/src/tests/endpointsToIntercept.json deleted file mode 100644 index fe51488c7..000000000 --- a/src/tests/endpointsToIntercept.json +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/src/tests/server/handlers/_constants.ts b/src/tests/server/handlers/_constants.ts index 4a056b2b0..575c235d1 100644 --- a/src/tests/server/handlers/_constants.ts +++ b/src/tests/server/handlers/_constants.ts @@ -1,10 +1,44 @@ import { HttpResponse } from "msw"; export const ENDPOINT_STATUS_API_ERROR_MESSAGE = `The endpoint status is set to "error".`; + +const DEFAULT_ERROR_STATUS = 500; + +/** + * @deprecated Use {@link createEndpointStatusError} instead. + * Keeping for backward compatibility during migration. + */ export const ENDPOINT_STATUS_API_ERROR = HttpResponse.json( { error: "EndpointStatusError", message: ENDPOINT_STATUS_API_ERROR_MESSAGE, }, - { status: 500 }, + { status: DEFAULT_ERROR_STATUS }, ); + +/** + * Creates a fresh JSON-body error response for endpoint-status-driven error + * simulation. Use this where tests assert on the parsed error body (e.g. + * package-profiles, ubuntu-pro, activities list). + * + * Prefer this over the static `ENDPOINT_STATUS_API_ERROR` constant because + * response objects should not be shared across handler invocations. + */ +export const createEndpointStatusError = (status = DEFAULT_ERROR_STATUS) => + HttpResponse.json( + { + error: "EndpointStatusError", + message: ENDPOINT_STATUS_API_ERROR_MESSAGE, + }, + { status }, + ); + +/** + * Creates a fresh null-body error response for endpoint-status-driven error + * simulation. Use this where the handler originally used + * `throw new HttpResponse(null, { status: 500 })`. + */ +export const createEndpointStatusNetworkError = ( + status = DEFAULT_ERROR_STATUS, +) => new HttpResponse(null, { status }); + diff --git a/src/tests/server/handlers/_helpers.ts b/src/tests/server/handlers/_helpers.ts index 2cf7f7a40..df3c5ad53 100644 --- a/src/tests/server/handlers/_helpers.ts +++ b/src/tests/server/handlers/_helpers.ts @@ -3,6 +3,7 @@ import type { HttpHandler } from "msw"; import { http, HttpResponse } from "msw"; import { API_URL } from "@/constants"; import { getEndpointStatus } from "@/tests/controllers/controller"; +import { createEndpointStatusNetworkError } from "./_constants"; interface GeneratePaginatedResponseProps { data: D[]; @@ -73,6 +74,19 @@ export const isAction = (request: Request, actionName: string | string[]) => { : actionName.includes(action); }; +/** + * Returns `true` when the global endpoint-status controller has a non-default + * status that applies to the given path. Pass the handler's own path so that + * a targeted `setEndpointStatus({ status: "error", path: "/features" })` only + * affects the matching handler. + */ +export function shouldApplyEndpointStatus(path?: string): boolean { + const { status, path: statusPath } = getEndpointStatus(); + if (status === "default") return false; + if (!statusPath) return true; + return !!path && statusPath.includes(path); +} + interface GenerateGetListEndpointParams { readonly path: string; readonly response: T[]; @@ -82,37 +96,31 @@ export function generateGetListEndpoint({ path, response, }: GenerateGetListEndpointParams): HttpHandler { - return http.get>( - `${API_URL}${path}`, - () => { - const endpointStatus = getEndpointStatus(); + return http.get(`${API_URL}${path}`, () => { + if (shouldApplyEndpointStatus(path)) { + const { status } = getEndpointStatus(); - if ( - !endpointStatus.path || - (endpointStatus.path && endpointStatus.path === path) - ) { - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); - } - - if (endpointStatus.status === "empty") { - return HttpResponse.json({ - results: [], - count: 0, - next: null, - previous: null, - }); - } + if (status === "error") { + throw createEndpointStatusNetworkError(); } - return HttpResponse.json({ - results: response, - count: response.length, - next: null, - previous: null, - }); - }, - ); + if (status === "empty") { + return HttpResponse.json({ + results: [], + count: 0, + next: null, + previous: null, + }); + } + } + + return HttpResponse.json({ + results: response, + count: response.length, + next: null, + previous: null, + }); + }); } export const parseArray = (paramValue: string | null): string[] => { diff --git a/src/tests/server/handlers/accessGroup.ts b/src/tests/server/handlers/accessGroup.ts index c0aff62f9..52392e4d1 100644 --- a/src/tests/server/handlers/accessGroup.ts +++ b/src/tests/server/handlers/accessGroup.ts @@ -20,13 +20,6 @@ export default [ return HttpResponse.json(accessGroups); }), - http.get(API_URL_OLD, ({ request }) => { - if (!isAction(request, "ChangeComputersAccessGroup")) { - return; - } - - return HttpResponse.json({ success: true }); - }), http.get(API_URL_OLD, ({ request }) => { if ( diff --git a/src/tests/server/handlers/activity.ts b/src/tests/server/handlers/activity.ts index 3d1515294..bca6f587e 100644 --- a/src/tests/server/handlers/activity.ts +++ b/src/tests/server/handlers/activity.ts @@ -1,15 +1,14 @@ import { API_URL, API_URL_OLD } from "@/constants"; -import type { Activity, GetActivitiesParams } from "@/features/activities"; +import type { Activity } from "@/features/activities"; import { getEndpointStatus } from "@/tests/controllers/controller"; import { activities, activityTypes, INVALID_ACTIVITY_SEARCH_QUERY, } from "@/tests/mocks/activity"; -import type { ApiPaginatedResponse } from "@/types/api/ApiPaginatedResponse"; import { http, HttpResponse } from "msw"; -import { ENDPOINT_STATUS_API_ERROR } from "./_constants"; -import { generatePaginatedResponse, isAction } from "./_helpers"; +import { createEndpointStatusError, createEndpointStatusNetworkError } from "./_constants"; +import { generatePaginatedResponse, isAction, shouldApplyEndpointStatus } from "./_helpers"; const STATUS_QUERY_REGEX = /(?:^|\s)status:([^\s]+)/; const TYPE_QUERY_REGEX = /(?:^|\s)type:([^\s]+)/; @@ -48,13 +47,14 @@ const parseActivitiesQuery = ( }; export default [ - http.get>( + http.get( `${API_URL}activities`, async ({ request }) => { - const endpointStatus = getEndpointStatus(); - - if (endpointStatus.status === "error") { - throw ENDPOINT_STATUS_API_ERROR; + if (shouldApplyEndpointStatus("activities")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusError(); + } } const url = new URL(request.url); @@ -97,17 +97,15 @@ export default [ }, ), - http.get<{ id: string }, GetActivitiesParams, Activity>( + http.get( `${API_URL}activities/:id`, async ({ params: { id } }) => { - const endpointStatus = getEndpointStatus(); - - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("activities/:id")) { + throw createEndpointStatusNetworkError(); } return HttpResponse.json( - activities.find((activity) => activity.id === parseInt(id)) ?? { + activities.find((activity) => activity.id === parseInt(id as string)) ?? { activity_status: "succeeded", approval_time: null, children: [], @@ -162,26 +160,22 @@ export default [ ]); }), - http.post( + http.post( `${API_URL}activities/revert`, async () => { - const endpointStatus = getEndpointStatus(); - - if (endpointStatus.status === "error") { - throw ENDPOINT_STATUS_API_ERROR; + if (shouldApplyEndpointStatus("activities/revert")) { + throw createEndpointStatusError(); } return HttpResponse.json([activities[0].id, activities[1].id]); }, ), - http.post( + http.post( `${API_URL}activities/reapply`, async () => { - const endpointStatus = getEndpointStatus(); - - if (endpointStatus.status === "error") { - throw ENDPOINT_STATUS_API_ERROR; + if (shouldApplyEndpointStatus("activities/reapply")) { + throw createEndpointStatusError(); } return HttpResponse.json([activities[0].id, activities[1].id]); diff --git a/src/tests/server/handlers/aptSource.ts b/src/tests/server/handlers/aptSource.ts index 0e7abde8d..1051089be 100644 --- a/src/tests/server/handlers/aptSource.ts +++ b/src/tests/server/handlers/aptSource.ts @@ -2,9 +2,9 @@ import { API_URL, API_URL_OLD } from "@/constants"; import type { APTSource, GetAPTSourcesParams } from "@/features/apt-sources"; import { getEndpointStatus } from "@/tests/controllers/controller"; import { aptSources } from "@/tests/mocks/apt-sources"; -import { isAction } from "@/tests/server/handlers/_helpers"; +import { isAction, shouldApplyEndpointStatus } from "@/tests/server/handlers/_helpers"; import { http, HttpResponse } from "msw"; -import { ENDPOINT_STATUS_API_ERROR } from "./_constants"; +import { createEndpointStatusNetworkError } from "./_constants"; export default [ http.get( @@ -19,15 +19,10 @@ export default [ ), http.delete(`${API_URL}repository/apt-source/:sourceId`, () => { - const endpointStatus = getEndpointStatus(); - - if ( - !endpointStatus.path || - (endpointStatus.path && - endpointStatus.path === "repository/apt-source/:sourceId") - ) { - if (endpointStatus.status === "error") { - throw HttpResponse.json(ENDPOINT_STATUS_API_ERROR, { status: 500 }); + if (shouldApplyEndpointStatus("repository/apt-source/:sourceId")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusNetworkError(); } } diff --git a/src/tests/server/handlers/distributions.ts b/src/tests/server/handlers/distributions.ts index b08d039d2..538a04fac 100644 --- a/src/tests/server/handlers/distributions.ts +++ b/src/tests/server/handlers/distributions.ts @@ -4,6 +4,7 @@ import { getEndpointStatus } from "@/tests/controllers/controller"; import { distributions } from "@/tests/mocks/distributions"; import { http, HttpResponse } from "msw"; import { isAction } from "@/tests/server/handlers/_helpers"; +import { createEndpointStatusNetworkError } from "./_constants"; export default [ http.get( @@ -16,7 +17,7 @@ export default [ const endpointStatus = getEndpointStatus(); if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + throw createEndpointStatusNetworkError(); } if (endpointStatus.status === "empty") { diff --git a/src/tests/server/handlers/eventsLog.ts b/src/tests/server/handlers/eventsLog.ts index d8bc0c529..191c9f3cf 100644 --- a/src/tests/server/handlers/eventsLog.ts +++ b/src/tests/server/handlers/eventsLog.ts @@ -2,7 +2,8 @@ import { API_URL } from "@/constants"; import type { EventLog, GetEventsLogParams } from "@/features/events-log"; import { getEndpointStatus } from "@/tests/controllers/controller"; import { eventsLog } from "@/tests/mocks/eventsLog"; -import { generatePaginatedResponse } from "@/tests/server/handlers/_helpers"; +import { generatePaginatedResponse, shouldApplyEndpointStatus } from "@/tests/server/handlers/_helpers"; +import { createEndpointStatusNetworkError } from "./_constants"; import type { ApiPaginatedResponse } from "@/types/api/ApiPaginatedResponse"; import { http, HttpResponse } from "msw"; @@ -12,8 +13,8 @@ export default [ async ({ request }) => { const endpointStatus = getEndpointStatus(); - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("events") && endpointStatus.status === "error") { + throw createEndpointStatusNetworkError(); } const url = new URL(request.url); diff --git a/src/tests/server/handlers/features.ts b/src/tests/server/handlers/features.ts index f9c91d963..7438dfa83 100644 --- a/src/tests/server/handlers/features.ts +++ b/src/tests/server/handlers/features.ts @@ -3,11 +3,11 @@ import { API_URL } from "@/constants"; import { features } from "@/tests/mocks/features"; import { getEndpointStatus } from "@/tests/controllers/controller"; import type { Feature } from "@/types/Feature"; -import type { ApiPaginatedResponse } from "@/types/api/ApiPaginatedResponse"; import { generatePaginatedResponse } from "@/tests/server/handlers/_helpers"; +import { createEndpointStatusNetworkError } from "./_constants"; export default [ - http.get>( + http.get( `${API_URL}features`, () => { const endpointStatus = getEndpointStatus(); @@ -23,7 +23,7 @@ export default [ } if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + throw createEndpointStatusNetworkError(); } return HttpResponse.json( diff --git a/src/tests/server/handlers/instance.ts b/src/tests/server/handlers/instance.ts index 1cb32e5e4..0a3267f84 100644 --- a/src/tests/server/handlers/instance.ts +++ b/src/tests/server/handlers/instance.ts @@ -2,8 +2,6 @@ import { API_URL, API_URL_OLD } from "@/constants"; import type { Activity } from "@/features/activities"; import type { DistributionUpgradeTarget, - GetInstanceParams, - GetInstancesParams, RemoveInstancesParams, SanitizeInstanceParams, } from "@/features/instances"; @@ -27,20 +25,21 @@ import { pendingInstances, } from "@/tests/mocks/instance"; import { userGroups } from "@/tests/mocks/userGroup"; -import type { ApiPaginatedResponse } from "@/types/api/ApiPaginatedResponse"; import type { Instance, PendingInstance } from "@/types/Instance"; import type { GroupsResponse } from "@/types/User"; import { delay, http, HttpResponse } from "msw"; -import { generatePaginatedResponse, isAction } from "./_helpers"; +import { generatePaginatedResponse, isAction, shouldApplyEndpointStatus } from "./_helpers"; +import { createEndpointStatusNetworkError } from "./_constants"; export default [ - http.get>( + http.get( `${API_URL}computers`, async ({ request }) => { - const endpointStatus = getEndpointStatus(); - - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("computers")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusNetworkError(); + } } const url = new URL(request.url); @@ -245,23 +244,25 @@ export default [ }), http.post(`${API_URL}computers/release-upgrades`, async () => { - const endpointStatus = getEndpointStatus(); - - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("computers/release-upgrades")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusNetworkError(); + } } const releaseUpgradeActivity = RELEASE_UPGRADE_ACTIVITY; return HttpResponse.json(releaseUpgradeActivity); }), - http.get( + http.get( `${API_URL}computers/:computerId`, async ({ request }) => { - const endpointStatus = getEndpointStatus(); - - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("computers/:computerId")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusNetworkError(); + } } const url = new URL(request.url); diff --git a/src/tests/server/handlers/organisationPreferences.ts b/src/tests/server/handlers/organisationPreferences.ts index 65488f5de..a62043f80 100644 --- a/src/tests/server/handlers/organisationPreferences.ts +++ b/src/tests/server/handlers/organisationPreferences.ts @@ -3,13 +3,16 @@ import { getEndpointStatus } from "@/tests/controllers/controller"; import { preferences } from "@/tests/mocks/organisationPreferences"; import type { Preferences } from "@/types/Preferences"; import { http, HttpResponse } from "msw"; +import { shouldApplyEndpointStatus } from "./_helpers"; +import { createEndpointStatusNetworkError } from "./_constants"; export default [ http.get(`${API_URL}preferences`, () => { - const endpointStatus = getEndpointStatus(); - - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("preferences")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusNetworkError(); + } } return HttpResponse.json(preferences); diff --git a/src/tests/server/handlers/packageProfile.ts b/src/tests/server/handlers/packageProfile.ts index 5d5dafe9a..0af09fd32 100644 --- a/src/tests/server/handlers/packageProfile.ts +++ b/src/tests/server/handlers/packageProfile.ts @@ -2,8 +2,8 @@ import { API_URL, API_URL_OLD } from "@/constants"; import { getEndpointStatus } from "@/tests/controllers/controller"; import { packageProfiles } from "@/tests/mocks/package-profiles"; import { http, HttpResponse } from "msw"; -import { ENDPOINT_STATUS_API_ERROR } from "./_constants"; -import { isAction } from "./_helpers"; +import { createEndpointStatusError } from "./_constants"; +import { isAction, shouldApplyEndpointStatus } from "./_helpers"; export default [ http.get(`${API_URL}packageprofiles`, ({ request }) => { @@ -42,15 +42,10 @@ export default [ ), http.put(`${API_URL}packageprofiles/:profileName`, () => { - const endpointStatus = getEndpointStatus(); - - if ( - !endpointStatus.path || - (endpointStatus.path && - endpointStatus.path.includes("packageprofiles/:profileName")) - ) { - if (endpointStatus.status === "error") { - throw ENDPOINT_STATUS_API_ERROR; + if (shouldApplyEndpointStatus("packageprofiles/:profileName")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusError(); } } @@ -58,14 +53,10 @@ export default [ }), http.post(`${API_URL}packageprofiles`, () => { - const endpointStatus = getEndpointStatus(); - - if ( - !endpointStatus.path || - (endpointStatus.path && endpointStatus.path.includes("packageprofiles")) - ) { - if (endpointStatus.status === "error") { - throw ENDPOINT_STATUS_API_ERROR; + if (shouldApplyEndpointStatus("packageprofiles")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusError(); } } @@ -73,17 +64,10 @@ export default [ }), http.post(`${API_URL}packageprofiles/:profileName/constraints`, () => { - const endpointStatus = getEndpointStatus(); - - if ( - !endpointStatus.path || - (endpointStatus.path && - endpointStatus.path.includes( - "packageprofiles/:profileName/constraints", - )) - ) { - if (endpointStatus.status === "error") { - throw ENDPOINT_STATUS_API_ERROR; + if (shouldApplyEndpointStatus("packageprofiles/:profileName/constraints")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusError(); } } @@ -91,17 +75,10 @@ export default [ }), http.delete(`${API_URL}packageprofiles/:profileName/constraints`, () => { - const endpointStatus = getEndpointStatus(); - - if ( - !endpointStatus.path || - (endpointStatus.path && - endpointStatus.path.includes( - "packageprofiles/:profileName/constraints", - )) - ) { - if (endpointStatus.status === "error") { - throw ENDPOINT_STATUS_API_ERROR; + if (shouldApplyEndpointStatus("packageprofiles/:profileName/constraints")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusError(); } } @@ -111,17 +88,10 @@ export default [ http.put( `${API_URL}packageprofiles/:profileName/constraints/:constraintId`, () => { - const endpointStatus = getEndpointStatus(); - - if ( - !endpointStatus.path || - (endpointStatus.path && - endpointStatus.path.includes( - "packageprofiles/:profileName/constraints/:constraintId", - )) - ) { - if (endpointStatus.status === "error") { - throw ENDPOINT_STATUS_API_ERROR; + if (shouldApplyEndpointStatus("packageprofiles/:profileName/constraints/:constraintId")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusError(); } } @@ -130,10 +100,11 @@ export default [ ), http.get(API_URL_OLD, ({ request }) => { - const endpointStatus = getEndpointStatus(); - - if (!endpointStatus.path && endpointStatus.status === "error") { - throw ENDPOINT_STATUS_API_ERROR; + if (shouldApplyEndpointStatus()) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusError(); + } } if (!isAction(request, "RemovePackageProfile")) { diff --git a/src/tests/server/handlers/packages.ts b/src/tests/server/handlers/packages.ts index 081205bbf..ad6268c79 100644 --- a/src/tests/server/handlers/packages.ts +++ b/src/tests/server/handlers/packages.ts @@ -10,7 +10,12 @@ import { } from "@/tests/mocks/packages"; import { activities } from "@/tests/mocks/activity"; import type { ApiPaginatedResponse } from "@/types/api/ApiPaginatedResponse"; -import { generatePaginatedResponse, isAction } from "./_helpers"; +import { + generatePaginatedResponse, + isAction, + shouldApplyEndpointStatus, +} from "./_helpers"; +import { createEndpointStatusNetworkError } from "./_constants"; const parseBooleanParam = (value: string | null): boolean | undefined => { if (value === "true") { @@ -28,10 +33,11 @@ export default [ http.get>( `${API_URL}packages`, async ({ request }) => { - const endpointStatus = getEndpointStatus(); - - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("packages")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusNetworkError(); + } } const url = new URL(request.url); diff --git a/src/tests/server/handlers/process.ts b/src/tests/server/handlers/process.ts index 0937a4382..a6711a49a 100644 --- a/src/tests/server/handlers/process.ts +++ b/src/tests/server/handlers/process.ts @@ -2,7 +2,8 @@ import { API_URL } from "@/constants"; import type { GetProcessesParams, Process } from "@/features/processes"; import { getEndpointStatus } from "@/tests/controllers/controller"; import { processes } from "@/tests/mocks/process"; -import { generatePaginatedResponse } from "@/tests/server/handlers/_helpers"; +import { generatePaginatedResponse, shouldApplyEndpointStatus } from "@/tests/server/handlers/_helpers"; +import { createEndpointStatusNetworkError } from "./_constants"; import type { ApiPaginatedResponse } from "@/types/api/ApiPaginatedResponse"; import { http, HttpResponse } from "msw"; @@ -12,8 +13,8 @@ export default [ async ({ request }) => { const endpointStatus = getEndpointStatus(); - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("computers/:instanceId/processes") && endpointStatus.status === "error") { + throw createEndpointStatusNetworkError(); } const url = new URL(request.url); diff --git a/src/tests/server/handlers/rebootProfiles.ts b/src/tests/server/handlers/rebootProfiles.ts index 4ab74e957..3665c36c0 100644 --- a/src/tests/server/handlers/rebootProfiles.ts +++ b/src/tests/server/handlers/rebootProfiles.ts @@ -2,26 +2,24 @@ import { API_URL } from "@/constants"; import { getEndpointStatus } from "@/tests/controllers/controller"; import { rebootProfiles } from "@/tests/mocks/rebootProfiles"; import { http, HttpResponse } from "msw"; -import { ENDPOINT_STATUS_API_ERROR } from "./_constants"; -import { generatePaginatedResponse } from "./_helpers"; +import { createEndpointStatusError, createEndpointStatusNetworkError } from "./_constants"; +import { generatePaginatedResponse, shouldApplyEndpointStatus } from "./_helpers"; export default [ http.get(`${API_URL}rebootprofiles`, ({ request }) => { const { searchParams } = new URL(request.url); - const endpointStatus = getEndpointStatus(); const search = searchParams.get("search")?.toLowerCase() ?? ""; const limit = parseInt(searchParams.get("limit") ?? "20"); const offset = parseInt(searchParams.get("offset") ?? "0"); - if ( - !endpointStatus.path || - endpointStatus.path.includes("rebootprofiles") - ) { - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("rebootprofiles")) { + const { status } = getEndpointStatus(); + + if (status === "error") { + throw createEndpointStatusNetworkError(); } - if (endpointStatus.status === "empty") { + if (status === "empty") { return HttpResponse.json({ results: [], count: 0, @@ -57,15 +55,11 @@ export default [ }), http.delete(`${API_URL}rebootprofiles/:id`, ({ params }) => { - const endpointStatus = getEndpointStatus(); + if (shouldApplyEndpointStatus("rebootprofiles/:id")) { + const { status } = getEndpointStatus(); - if ( - !endpointStatus.path || - (endpointStatus.path && - endpointStatus.path.includes("rebootprofiles/:id")) - ) { - if (endpointStatus.status === "error") { - throw ENDPOINT_STATUS_API_ERROR; + if (status === "error") { + throw createEndpointStatusError(); } } diff --git a/src/tests/server/handlers/removalProfiles.ts b/src/tests/server/handlers/removalProfiles.ts index a7b65fec4..651e747d7 100644 --- a/src/tests/server/handlers/removalProfiles.ts +++ b/src/tests/server/handlers/removalProfiles.ts @@ -1,7 +1,8 @@ import { API_URL_OLD } from "@/constants"; import { removalProfiles } from "@/tests/mocks/removalProfiles"; import { http, HttpResponse } from "msw"; -import { isAction } from "./_helpers"; +import { isAction, shouldApplyEndpointStatus } from "./_helpers"; +import { createEndpointStatusNetworkError } from "./_constants"; import { getEndpointStatus } from "@/tests/controllers/controller"; export default [ @@ -10,16 +11,13 @@ export default [ return; } - const endpointStatus = getEndpointStatus(); + if (shouldApplyEndpointStatus("GetRemovalProfiles")) { + const { status } = getEndpointStatus(); - if ( - !endpointStatus.path || - endpointStatus.path.includes("GetRemovalProfiles") - ) { - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (status === "error") { + throw createEndpointStatusNetworkError(); } - if (endpointStatus.status === "empty") { + if (status === "empty") { return HttpResponse.json([], { status: 200 }); } } diff --git a/src/tests/server/handlers/repositoryProfiles.ts b/src/tests/server/handlers/repositoryProfiles.ts index 42bf2bac1..cfcda2dea 100644 --- a/src/tests/server/handlers/repositoryProfiles.ts +++ b/src/tests/server/handlers/repositoryProfiles.ts @@ -2,16 +2,16 @@ import { API_URL } from "@/constants"; import { getEndpointStatus } from "@/tests/controllers/controller"; import { repositoryProfiles } from "@/tests/mocks/repositoryProfiles"; import { http, HttpResponse } from "msw"; -import { generatePaginatedResponse } from "./_helpers"; +import { generatePaginatedResponse, shouldApplyEndpointStatus } from "./_helpers"; +import { createEndpointStatusNetworkError } from "./_constants"; export default [ http.get(`${API_URL}repositoryprofiles`, ({ request }) => { - const endpointStatus = getEndpointStatus(); - if ( - endpointStatus.status === "error" && - (!endpointStatus.path || endpointStatus.path === "repositoryprofiles") - ) { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("repositoryprofiles")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusNetworkError(); + } } const { searchParams } = new URL(request.url); diff --git a/src/tests/server/handlers/script.ts b/src/tests/server/handlers/script.ts index c028a3eed..00393a7fc 100644 --- a/src/tests/server/handlers/script.ts +++ b/src/tests/server/handlers/script.ts @@ -7,27 +7,26 @@ import { scriptVersionsWithPagination, } from "@/tests/mocks/script"; import { scriptProfiles } from "@/tests/mocks/scriptProfiles"; -import { generatePaginatedResponse } from "@/tests/server/handlers/_helpers"; +import { generatePaginatedResponse, shouldApplyEndpointStatus } from "@/tests/server/handlers/_helpers"; +import { createEndpointStatusNetworkError } from "./_constants"; import { http, HttpResponse } from "msw"; export default [ http.get(`${API_URL}scripts`, async ({ request }) => { const DEFAULT_PAGE_SIZE = 20; - const endpointStatus = getEndpointStatus(); const url = new URL(request.url); const limit = Number(url.searchParams.get("limit")) || DEFAULT_PAGE_SIZE; const offset = Number(url.searchParams.get("offset")) || 0; const search = url.searchParams.get("search") || ""; - if ( - !endpointStatus.path || - (endpointStatus.path && endpointStatus.path.includes("scripts")) - ) { - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("scripts")) { + const { status } = getEndpointStatus(); + + if (status === "error") { + throw createEndpointStatusNetworkError(); } - if (endpointStatus.status === "empty") { + if (status === "empty") { return HttpResponse.json({ results: [], count: 0, diff --git a/src/tests/server/handlers/scriptProfiles.ts b/src/tests/server/handlers/scriptProfiles.ts index b66f8eb2c..c312182a3 100644 --- a/src/tests/server/handlers/scriptProfiles.ts +++ b/src/tests/server/handlers/scriptProfiles.ts @@ -3,13 +3,12 @@ import type { Activity } from "@/features/activities"; import { getEndpointStatus } from "@/tests/controllers/controller"; import { scriptProfiles } from "@/tests/mocks/scriptProfiles"; import { http, HttpResponse } from "msw"; -import { ENDPOINT_STATUS_API_ERROR } from "./_constants"; -import { generatePaginatedResponse } from "./_helpers"; +import { createEndpointStatusError, createEndpointStatusNetworkError } from "./_constants"; +import { generatePaginatedResponse, shouldApplyEndpointStatus } from "./_helpers"; export default [ http.get(`${API_URL}script-profiles`, ({ request }) => { const { searchParams } = new URL(request.url); - const endpointStatus = getEndpointStatus(); const search = searchParams.get("search") || ""; const limit = parseInt(searchParams.get("limit") || "20"); @@ -24,15 +23,14 @@ export default [ ); }); - if ( - !endpointStatus.path || - (endpointStatus.path && endpointStatus.path.includes("script-profiles")) - ) { - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("script-profiles")) { + const { status: endpointStatusValue } = getEndpointStatus(); + + if (endpointStatusValue === "error") { + throw createEndpointStatusNetworkError(); } - if (endpointStatus.status === "empty") { + if (endpointStatusValue === "empty") { return HttpResponse.json({ results: [], count: 0, @@ -54,18 +52,14 @@ export default [ http.get<{ profileId: string }>( `${API_URL}script-profiles/:profileId`, ({ params }) => { - const endpointStatus = getEndpointStatus(); - - if ( - !endpointStatus.path || - (endpointStatus.path && - endpointStatus.path.includes("script-profiles/:profileId")) - ) { - if (endpointStatus.status === "error") { - throw ENDPOINT_STATUS_API_ERROR; + if (shouldApplyEndpointStatus("script-profiles/:profileId")) { + const { status } = getEndpointStatus(); + + if (status === "error") { + throw createEndpointStatusError(); } - if (endpointStatus.status === "empty") { + if (status === "empty") { return HttpResponse.json(undefined); } } @@ -80,21 +74,18 @@ export default [ http.get(`${API_URL}script-profiles/:profileId/activities`, ({ request }) => { const { searchParams } = new URL(request.url); - const endpointStatus = getEndpointStatus(); const limit = parseInt(searchParams.get("limit") || "20"); const offset = parseInt(searchParams.get("offset") || "0"); - if ( - !endpointStatus.path || - (endpointStatus.path && - endpointStatus.path.includes("script-profiles/:profileId/activities")) - ) { - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("script-profiles/:profileId/activities")) { + const { status } = getEndpointStatus(); + + if (status === "error") { + throw createEndpointStatusNetworkError(); } - if (endpointStatus.status === "empty") { + if (status === "empty") { return HttpResponse.json({ results: [], count: 0, diff --git a/src/tests/server/handlers/snap.ts b/src/tests/server/handlers/snap.ts index 57f7b4514..a061522b8 100644 --- a/src/tests/server/handlers/snap.ts +++ b/src/tests/server/handlers/snap.ts @@ -12,7 +12,9 @@ import { http, HttpResponse } from "msw"; import { generateFilteredResponse, generatePaginatedResponse, + shouldApplyEndpointStatus, } from "./_helpers"; +import { createEndpointStatusNetworkError } from "./_constants"; export default [ http.get( @@ -87,8 +89,8 @@ export default [ const offset = Number(url.searchParams.get("offset")) || 0; const limit = Number(url.searchParams.get("limit")) || DEFAULT_PAGE_SIZE; - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("computers/:computerId/snaps/installed") && endpointStatus.status === "error") { + throw createEndpointStatusNetworkError(); } return HttpResponse.json( diff --git a/src/tests/server/handlers/ubuntuPro.ts b/src/tests/server/handlers/ubuntuPro.ts index 3d96c2753..3eecb8f9f 100644 --- a/src/tests/server/handlers/ubuntuPro.ts +++ b/src/tests/server/handlers/ubuntuPro.ts @@ -1,6 +1,7 @@ import { getEndpointStatus } from "@/tests/controllers/controller"; import { http, HttpResponse } from "msw"; -import { ENDPOINT_STATUS_API_ERROR } from "./_constants"; +import { createEndpointStatusError } from "./_constants"; +import { shouldApplyEndpointStatus } from "./_helpers"; import { API_URL } from "@/constants"; import { attachUbuntuProActivity, @@ -9,14 +10,10 @@ import { export default [ http.post(`${API_URL}attach-token`, () => { - const endpointStatus = getEndpointStatus(); - - if ( - !endpointStatus.path || - (endpointStatus.path && endpointStatus.path.includes("attach-token")) - ) { - if (endpointStatus.status === "error") { - throw ENDPOINT_STATUS_API_ERROR; + if (shouldApplyEndpointStatus("attach-token")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusError(); } } @@ -24,14 +21,10 @@ export default [ }), http.post(`${API_URL}detach-token`, () => { - const endpointStatus = getEndpointStatus(); - - if ( - !endpointStatus.path || - (endpointStatus.path && endpointStatus.path.includes("detach-token")) - ) { - if (endpointStatus.status === "error") { - throw ENDPOINT_STATUS_API_ERROR; + if (shouldApplyEndpointStatus("detach-token")) { + const { status } = getEndpointStatus(); + if (status === "error") { + throw createEndpointStatusError(); } } diff --git a/src/tests/server/handlers/wslProfiles.ts b/src/tests/server/handlers/wslProfiles.ts index 152aeeab9..cc3b72996 100644 --- a/src/tests/server/handlers/wslProfiles.ts +++ b/src/tests/server/handlers/wslProfiles.ts @@ -2,12 +2,12 @@ import { API_URL } from "@/constants"; import { getEndpointStatus } from "@/tests/controllers/controller"; import { wslProfiles } from "@/tests/mocks/wsl-profiles"; import { http, HttpResponse } from "msw"; -import { generatePaginatedResponse } from "./_helpers"; +import { generatePaginatedResponse, shouldApplyEndpointStatus } from "./_helpers"; +import { createEndpointStatusNetworkError } from "./_constants"; export default [ http.get(`${API_URL}child-instance-profiles`, ({ request }) => { const { searchParams } = new URL(request.url); - const endpointStatus = getEndpointStatus(); const search = searchParams.get("search") || ""; const limit = parseInt(searchParams.get("limit") || "20"); @@ -17,16 +17,14 @@ export default [ return wslProfile.title.includes(search); }); - if ( - !endpointStatus.path || - (endpointStatus.path && - endpointStatus.path.includes("child-instance-profiles")) - ) { - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (shouldApplyEndpointStatus("child-instance-profiles")) { + const { status } = getEndpointStatus(); + + if (status === "error") { + throw createEndpointStatusNetworkError(); } - if (endpointStatus.status === "empty") { + if (status === "empty") { return HttpResponse.json({ results: [], count: 0, @@ -46,18 +44,14 @@ export default [ }), http.get(`${API_URL}child-instance-profiles/:name`, ({ params }) => { - const endpointStatus = getEndpointStatus(); + if (shouldApplyEndpointStatus("child-instance-profiles/:name")) { + const { status } = getEndpointStatus(); - if ( - !endpointStatus.path || - (endpointStatus.path && - endpointStatus.path.includes("child-instance-profiles/:name")) - ) { - if (endpointStatus.status === "error") { - throw new HttpResponse(null, { status: 500 }); + if (status === "error") { + throw createEndpointStatusNetworkError(); } - if (endpointStatus.status === "empty") { + if (status === "empty") { return HttpResponse.json(undefined); } }