Skip to content

Commit

Permalink
Fix auth failure redirect (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandones authored Nov 23, 2020
1 parent 89ae255 commit 5fac2de
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export const defineConfigSchema = jest.fn();
export const getConfig = jest
.fn()
.mockResolvedValue({ redirectAuthFailure: { enabled: false } });

export const navigate = jest.fn();
3 changes: 1 addition & 2 deletions packages/esm-api/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ module.exports = {
"lodash-es": "lodash",
"@openmrs/esm-error-handling":
"<rootDir>/__mocks__/openmrs-esm-error-handling.mock.ts",
"@openmrs/esm-config":
"<rootDir>/__mocks__/openmrs-esm-module-config.mock.ts",
"@openmrs/esm-config": "<rootDir>/__mocks__/openmrs-esm-config.mock.ts",
"single-spa": "<rootDir>/__mocks__/single-spa.mock.ts",
},
};
36 changes: 7 additions & 29 deletions packages/esm-api/src/openmrs-fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { openmrsFetch, openmrsObservableFetch } from "./openmrs-fetch";
import { isObservable } from "rxjs";

import { navigateToUrl as mockNavigateToUrl } from "single-spa";
import { getConfig as mockGetConfig } from "@openmrs/esm-config";
import {
getConfig as mockGetConfig,
navigate as mockNavigate,
} from "@openmrs/esm-config";

describe("openmrsFetch", () => {
beforeEach(() => {
Expand Down Expand Up @@ -209,33 +211,9 @@ describe("openmrsFetch", () => {

return openmrsFetch("/ws/rest/v1/session").then((data) => {
//@ts-ignore
expect(mockNavigateToUrl.mock.calls[0][0]).toBe("/openmrs/spa/login");
});
});

it(`redirects to openmrs login page when the server responds with a 401`, () => {
(mockGetConfig as any).mockResolvedValueOnce({
redirectAuthFailure: {
enabled: true,
url: "/openmrs/login",
errors: [401],
resolvePromise: true,
},
});

// @ts-ignore
window.fetch.mockReturnValue(
Promise.resolve({
ok: false,
status: 401,
statusText: "You are not authorized",
text: () => Promise.resolve("a string response body"),
})
);

return openmrsFetch("/ws/rest/v1/session").then((data) => {
//@ts-ignore
expect(window.location.assign.mock.calls[0][0]).toBe("/openmrs/login");
expect(mockNavigate.mock.calls[0][0]).toStrictEqual({
to: "/openmrs/spa/login",
});
});
});
});
Expand Down
13 changes: 3 additions & 10 deletions packages/esm-api/src/openmrs-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Observable } from "rxjs";
import { navigateToUrl } from "single-spa";
import isPlainObject from "lodash-es/isPlainObject";
import { getConfig } from "@openmrs/esm-config";
import { getConfig, navigate } from "@openmrs/esm-config";
import { FetchResponse } from "./types";

export function openmrsFetch<T = any>(
Expand Down Expand Up @@ -94,16 +94,9 @@ export function openmrsFetch<T = any>(
const { redirectAuthFailure } = await getConfig("@openmrs/esm-api");
if (
redirectAuthFailure.enabled &&
redirectAuthFailure.errors.indexOf(response.status) >= 0
redirectAuthFailure.errors.includes(response.status)
) {
const navigatesWithInSpa = (url) => {
// @ts-ignore
return url.startsWith(window.getOpenmrsSpaBase());
};

navigatesWithInSpa(redirectAuthFailure.url)
? navigateToUrl(redirectAuthFailure.url)
: location.assign(redirectAuthFailure.url);
navigate({ to: redirectAuthFailure.url });

/* We sometimes don't really want this promise to resolve since there's no response data,
* nor do we want it to reject because that would trigger error handling. We instead
Expand Down

0 comments on commit 5fac2de

Please sign in to comment.