Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) O3-2484: The application should revert to default locale on logout #782

Merged
merged 7 commits into from
Oct 29, 2023
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect } from "react";
import {
navigate,
setUserLanguage,
useConfig,
useConnectivity,
useSession,
Expand All @@ -19,6 +20,13 @@ const RedirectLogout: React.FC<RedirectLogoutProps> = () => {
navigate({ to: "${openmrsSpaBase}/login" });
} else {
performLogout().then(() => {
const defaultLang =
document.documentElement.getAttribute("data-default-lang");
setUserLanguage({
locale: defaultLang,
authenticated: false,
sessionId: "",
});
Comment on lines +23 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to make this part of the performLogout() or even the logic that happens here, which might solve similar issues (for instance, when a user's session times out, should the language be reset? If yes, it probably belongs in the refetchCurrentUser() logic), but this is only a suggestion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right @ibacher , I'll look into that too.
Thanks!

if (config.provider.type === "oauth2") {
navigate({ to: config.provider.logoutUrl });
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React from "react";
import { cleanup, render, waitFor } from "@testing-library/react";
import RedirectLogout from "./redirect-logout.component";
import { performLogout } from "./logout.resource";
import {
Session,
clearCurrentUser,
navigate,
openmrsFetch,
refetchCurrentUser,
setUserLanguage,
useConfig,
useConnectivity,
useSession,
} from "@openmrs/esm-framework";
import { mutate } from "swr";

jest.mock("@openmrs/esm-framework", () => ({
navigate: jest.fn(),
setUserLanguage: jest.fn(),
useConfig: jest.fn(),
useConnectivity: jest.fn(),
useSession: jest.fn(),
clearCurrentUser: jest.fn(),
openmrsFetch: jest.fn(),
refetchCurrentUser: jest.fn(),
}));

jest.mock("swr", () => ({
mutate: jest.fn(),
}));

Object.defineProperty(document, "documentElement", {
value: {
getAttribute: jest.fn().mockReturnValue("km"),
},
});

describe("Testing Logout", () => {
beforeEach(() => {
cleanup();
jest.clearAllMocks();
(useConnectivity as jest.Mock).mockReturnValue(true);
(openmrsFetch as jest.Mock).mockResolvedValue({});
(useSession as jest.Mock).mockReturnValue({
authenticated: true,
sessionId: "xyz",
} as Session);
(useConfig as jest.Mock).mockReturnValue({
provider: {
type: "",
},
});
});
it("should render Logout and redirect to login page", async () => {
render(<RedirectLogout />);
expect(openmrsFetch).toBeCalledWith("/ws/rest/v1/session", {
method: "DELETE",
});
await waitFor(() => expect(mutate).toBeCalled());
expect(clearCurrentUser).toBeCalled();
expect(refetchCurrentUser).toBeCalled();
expect(setUserLanguage).toBeCalledWith({
locale: "km",
authenticated: false,
sessionId: "",
});
expect(navigate).toBeCalledWith({ to: "${openmrsSpaBase}/login" });
});

it("should render Logout and redirect to provider.logoutUrl if provider.type === oauth2", async () => {
(useConfig as jest.Mock).mockReturnValue({
provider: {
type: "oauth2",
logoutUrl: "/oauth/logout",
},
});
render(<RedirectLogout />);
expect(openmrsFetch).toBeCalledWith("/ws/rest/v1/session", {
method: "DELETE",
});
await waitFor(() => expect(mutate).toBeCalled());
expect(clearCurrentUser).toBeCalled();
expect(refetchCurrentUser).toBeCalled();
expect(setUserLanguage).toBeCalledWith({
locale: "km",
authenticated: false,
sessionId: "",
});
expect(navigate).toBeCalledWith({ to: "/oauth/logout" });
});

it("should redirect to login if the session is already unauthenticated", async () => {
(useSession as jest.Mock).mockReturnValue({
authenticated: false,
} as Session);
render(<RedirectLogout />);
expect(navigate).toBeCalledWith({ to: "${openmrsSpaBase}/login" });
});

it("should redirect to login if the application is Offline", async () => {
(useConnectivity as jest.Mock).mockReturnValue(false);
render(<RedirectLogout />);
expect(navigate).toBeCalledWith({ to: "${openmrsSpaBase}/login" });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function isValidLocale(locale: unknown): locale is string {
return true;
}

function setUserLanguage(data: Session) {
export function setUserLanguage(data: Session) {
const locale = data?.user?.userProperties?.defaultLocale ?? data.locale;
const htmlLang = document.documentElement.getAttribute("lang");

Expand Down
21 changes: 21 additions & 0 deletions packages/framework/esm-framework/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [saveVisit](API.md#savevisit)
- [setCurrentVisit](API.md#setcurrentvisit)
- [setSessionLocation](API.md#setsessionlocation)
- [setUserLanguage](API.md#setuserlanguage)
- [toLocationObject](API.md#tolocationobject)
- [toVisitTypeObject](API.md#tovisittypeobject)
- [updateVisit](API.md#updatevisit)
Expand Down Expand Up @@ -1316,6 +1317,26 @@ ___

___

### setUserLanguage

▸ **setUserLanguage**(`data`): `void`

#### Parameters

| Name | Type |
| :------ | :------ |
| `data` | [`Session`](interfaces/Session.md) |

#### Returns

`void`

#### Defined in

[packages/framework/esm-api/src/shared-api-objects/current-user.ts:131](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L131)

___

### toLocationObject

▸ **toLocationObject**(`openmrsRestForm`): [`Location`](interfaces/Location.md)
Expand Down
2 changes: 1 addition & 1 deletion packages/shell/esm-app-shell/src/index.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="<%= openmrsDefaultLocale %>">
<html lang="<%= openmrsDefaultLocale %>" data-default-lang="<%= openmrsDefaultLocale %>">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
Expand Down
Loading