-
Notifications
You must be signed in to change notification settings - Fork 213
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ffb6690
On logout, the default locale of the application should override the …
vasharma05 38721b9
Added tests and updated documentation
vasharma05 e7de64c
Reverted km.json
vasharma05 9670339
Fixed km.json
vasharma05 dca4d3e
Merge branch 'main' of https://www.github.com/openmrs/openmrs-esm-cor…
vasharma05 266c8a1
Updated the attribute name for storing default locale
vasharma05 d14dd55
Updated test description
vasharma05 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
106 changes: 106 additions & 0 deletions
106
packages/apps/esm-login-app/src/redirect-logout/redirect-logout.test.tsx
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,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" }); | ||
}); | ||
}); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 therefetchCurrentUser()
logic), but this is only a suggestion.There was a problem hiding this comment.
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!