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

STCOR-773 show legacy or application-based discovery info #1385

Merged
merged 8 commits into from
Jan 4, 2024
75 changes: 39 additions & 36 deletions src/RootWithIntl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import {
Router,
Switch,
Redirect as InternalRedirect
} from 'react-router-dom';

import { Provider } from 'react-redux';
Expand Down Expand Up @@ -43,13 +44,47 @@ import { CalloutContext } from './CalloutContext';
import PreLoginLanding from './components/PreLoginLanding';
import { setOkapiTenant } from './okapiActions';

export const renderLogoutComponent = (stripes) => {
const { okapi } = stripes;

if (okapi.authnUrl) {
return <Redirect to={`${okapi.authnUrl}/realms/${okapi.tenant}/protocol/openid-connect/logout?client_id=${okapi.clientId}&post_logout_redirect_uri=${window.location.protocol}//${window.location.host}`} />;
}

return <InternalRedirect to="/" />;
};

export const renderLoginComponent = (stripes) => {
const { config, okapi } = stripes;

if (okapi.authnUrl) {
if (config.isSingleTenant) {
const redirectUri = `${window.location.protocol}//${window.location.host}/oidc-landing`;
const authnUri = `${okapi.authnUrl}/realms/${okapi.tenant}/protocol/openid-connect/auth?client_id=${okapi.clientId}&response_type=code&redirect_uri=${redirectUri}&scope=openid`;
return <Redirect to={authnUri} />;
}

const handleSelectTenant = (tenant, clientId) => {
localStorage.setItem('tenant', JSON.stringify({ tenantName: tenant, clientId }));
stripes.store.dispatch(setOkapiTenant({ tenant, clientId }));
};

return <PreLoginLanding onSelectTenant={handleSelectTenant} />;
}

return <Login
autoLogin={config.autoLogin}
stripes={stripes}
/>;
};

class RootWithIntl extends React.Component {
static propTypes = {
stripes: PropTypes.shape({
clone: PropTypes.func.isRequired,
config: PropTypes.object,
epics: PropTypes.object,
logger: PropTypes.object.isRequired,
clone: PropTypes.func.isRequired,
config: PropTypes.object.isRequired,
okapi: PropTypes.object.isRequired,
store: PropTypes.object.isRequired
}).isRequired,
Expand All @@ -65,42 +100,12 @@ class RootWithIntl extends React.Component {

state = { callout: null };

handleSelectTenant = (tenant, clientId) => {
localStorage.setItem('tenant', JSON.stringify({ tenantName: tenant, clientId }));
this.props.stripes.store.dispatch(setOkapiTenant({ clientId, tenant }));
}

setCalloutRef = (ref) => {
this.setState({
callout: ref,
});
}

singleTenantAuthnUrl = () => {
const { okapi } = this.props.stripes;
const redirectUri = `${window.location.protocol}//${window.location.host}/oidc-landing`;

return `${okapi.authnUrl}/realms/${okapi.tenant}/protocol/openid-connect/auth?client_id=${okapi.clientId}&response_type=code&redirect_uri=${redirectUri}&scope=openid`;
}

renderLoginComponent() {
const { config, okapi } = this.props.stripes;

if (okapi.authnUrl) {
if (config.isSingleTenant) {
return <Redirect to={this.singleTenantAuthnUrl()} />;
}
return <PreLoginLanding
onSelectTenant={this.handleSelectTenant}
/>;
}

return <Login
autoLogin={config.autoLogin}
stripes={this.props.stripes}
/>;
}

render() {
const {
token,
Expand All @@ -111,8 +116,6 @@ class RootWithIntl extends React.Component {
const connect = connectFor('@folio/core', this.props.stripes.epics, this.props.stripes.logger);
const stripes = this.props.stripes.clone({ connect });

const logoutUrl = `${stripes.okapi.authnUrl}/realms/${stripes.okapi.tenant}/protocol/openid-connect/logout?client_id=${stripes.okapi.clientId}&post_logout_redirect_uri=${window.location.protocol}//${window.location.host}`;

return (
<StripesContext.Provider value={stripes}>
<CalloutContext.Provider value={this.state.callout}>
Expand Down Expand Up @@ -208,11 +211,11 @@ class RootWithIntl extends React.Component {
<TitledRoute
name="logout"
path="/logout"
component={<Redirect to={logoutUrl} />}
component={renderLogoutComponent(this.props.stripes)}
/>
<TitledRoute
name="login"
component={this.renderLoginComponent()}
component={renderLoginComponent(this.props.stripes)}
/>
</Switch>
}
Expand Down
74 changes: 74 additions & 0 deletions src/RootWithIntl.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* shhhh, eslint, it's ok. we need "unused" imports for mocks */
/* eslint-disable no-unused-vars */

import { render, screen } from '@folio/jest-config-stripes/testing-library/react';

import { Redirect as InternalRedirect } from 'react-router-dom';
import Redirect from './components/Redirect';
import { Login } from './components';
import PreLoginLanding from './components/PreLoginLanding';

import {
renderLoginComponent,
renderLogoutComponent
} from './RootWithIntl';

jest.mock('react-router-dom', () => ({
Redirect: () => '<internalredirect>',
withRouter: (Component) => Component,
}));
jest.mock('./components/Redirect', () => () => '<redirect>');
jest.mock('./components/Login', () => () => '<login>');
jest.mock('./components/PreLoginLanding', () => () => '<preloginlanding>');

describe('RootWithIntl', () => {
describe('renderLoginComponent', () => {
it('handles legacy login', () => {
const stripes = { okapi: {}, config: {} };
render(renderLoginComponent(stripes));

expect(screen.getByText(/<login>/)).toBeInTheDocument();
});

describe('handles third-party login', () => {
it('handles single-tenant', () => {
const stripes = {
okapi: { authnUrl: 'https://barbie.com' },
config: { isSingleTenant: true }
};
render(renderLoginComponent(stripes));

expect(screen.getByText(/<redirect>/)).toBeInTheDocument();
});

it('handles multi-tenant', () => {
const stripes = {
okapi: { authnUrl: 'https://oppie.com' },
config: { },
};
render(renderLoginComponent(stripes));

expect(screen.getByText(/<preloginlanding>/)).toBeInTheDocument();
});
});
});

describe('renderLogoutComponent', () => {
it('handles legacy logout', () => {
const stripes = { okapi: {}, config: {} };
render(renderLogoutComponent(stripes));

expect(screen.getByText(/<internalredirect>/)).toBeInTheDocument();
});

it('handles third-party logout', () => {
const stripes = {
okapi: { authnUrl: 'https://oppie.com' },
config: { },
};
render(renderLogoutComponent(stripes));

expect(screen.getByText(/<redirect>/)).toBeInTheDocument();
});
});
});
Loading
Loading