Skip to content

Commit

Permalink
O3-4436: Navigation occasionally breaks
Browse files Browse the repository at this point in the history
O3-4436: Navigation occasionally breaks

O3-4436: Applied review changes
  • Loading branch information
wluyima committed Feb 18, 2025
1 parent 55973fa commit 84d59df
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
8 changes: 1 addition & 7 deletions packages/apps/esm-login-app/src/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@ export const configSchema = {
_default: 'basic',
_description:
"Selects the login mechanism to use. Choices are 'basic' and 'oauth2'. " +
"For 'oauth2' you'll also need to set the 'loginUrl' and 'logoutUrl'.",
"For 'oauth2' you'll also need to set the 'loginUrl'",
},
loginUrl: {
_type: Type.String,
_default: '${openmrsSpaBase}/login',
_description: 'The URL to use for an OAuth2 login.',
_validators: [validators.isUrl],
},
logoutUrl: {
_type: Type.String,
_default: '${openmrsSpaBase}/logout',
_description: 'The URL to use for an OAuth2 logout.',
_validators: [validators.isUrl],
},
},
chooseLocation: {
enabled: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const RedirectLogout: React.FC = () => {
useEffect(() => {
clearHistory();
if (!session.authenticated || !isLoginEnabled) {
navigate({ to: '${openmrsSpaBase}/login' });
if (config.provider.type !== 'oauth2') {
navigate({ to: '${openmrsSpaBase}/login' });
}
} else {
performLogout()
.then(() => {
Expand All @@ -24,9 +26,7 @@ const RedirectLogout: React.FC = () => {
sessionId: '',
});

if (config.provider.type === 'oauth2') {
navigate({ to: config.provider.logoutUrl });
} else {
if (config.provider.type !== 'oauth2') {
navigate({ to: '${openmrsSpaBase}/login' });
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ describe('RedirectLogout', () => {
expect(mockNavigate).toHaveBeenCalledWith({ to: '${openmrsSpaBase}/login' });
});

it('should redirect to `provider.logoutUrl` if the configured provider is `oauth2`', async () => {
it('should not redirect if the configured provider is `oauth2`', async () => {
mockUseConfig.mockReturnValue({
provider: {
type: 'oauth2',
logoutUrl: '/oauth/logout',
},
});

Expand All @@ -95,7 +94,7 @@ describe('RedirectLogout', () => {
authenticated: false,
sessionId: '',
});
expect(mockNavigate).toHaveBeenCalledWith({ to: '/oauth/logout' });
expect(mockNavigate).toHaveBeenCalledTimes(0);
});

it('should redirect to login if the session is already unauthenticated', async () => {
Expand Down Expand Up @@ -153,15 +152,29 @@ describe('RedirectLogout', () => {

mockUseConfig.mockReturnValue({
provider: {
type: 'oauth2',
logoutUrl: '/new/logout/url',
type: 'testProvider',
},
});

rerender(<RedirectLogout />);

await waitFor(() => {
expect(mockNavigate).toHaveBeenCalledWith({ to: '/new/logout/url' });
expect(mockNavigate).toHaveBeenCalledWith({ to: '${openmrsSpaBase}/login' });
});
});

it('should not redirect to login if user is not authenticated and the provider is oauth2', async () => {
mockUseSession.mockReturnValue({
authenticated: false,
} as Session);
mockUseConfig.mockReturnValue({
provider: {
type: 'oauth2',
},
});

render(<RedirectLogout />);

expect(mockNavigate).toHaveBeenCalledTimes(0);
});
});

0 comments on commit 84d59df

Please sign in to comment.