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-942: migrate away from reading stripes-config::okapi.url #1589

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions src/components/Login/LoginCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class LoginCtrl extends Component {
static propTypes = {
authFailure: PropTypes.arrayOf(PropTypes.object),
ssoEnabled: PropTypes.bool,
okapiUrl: PropTypes.string.isRequired,
tenant: PropTypes.string.isRequired,
autoLogin: PropTypes.shape({
username: PropTypes.string.isRequired,
password: PropTypes.string.isRequired,
Expand All @@ -35,9 +37,6 @@ class LoginCtrl extends Component {

constructor(props) {
super(props);
this.sys = require('stripes-config'); // eslint-disable-line global-require
this.okapiUrl = this.sys.okapi.url;
this.tenant = this.sys.okapi.tenant;
if (props.autoLogin && props.autoLogin.username) {
this.handleSubmit(props.autoLogin);
}
Expand All @@ -54,15 +53,15 @@ class LoginCtrl extends Component {
}

handleSubmit = (data) => {
return requestLogin(this.okapiUrl, this.context.store, this.tenant, data)
return requestLogin(this.props.okapiUrl, this.context.store, this.tenant, data)
.then(this.handleSuccessfulLogin)
.catch(e => {
console.error(e); // eslint-disable-line no-console
});
}

handleSSOLogin = () => {
requestSSOLogin(this.okapiUrl, this.tenant);
requestSSOLogin(this.props.okapiUrl, this.props.tenant);
}

render() {
Expand All @@ -82,6 +81,8 @@ class LoginCtrl extends Component {
const mapStateToProps = state => ({
authFailure: state.okapi.authFailure,
ssoEnabled: state.okapi.ssoEnabled,
okapiUrl: state.okapi.url,
tenant: state.okapi.tenant,
});
const mapDispatchToProps = dispatch => ({
clearAuthErrors: () => dispatch(setAuthError([])),
Expand Down
10 changes: 5 additions & 5 deletions src/components/Root/FFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
*/

import ms from 'ms';
import { okapi as okapiConfig } from 'stripes-config';
import {
setRtrTimeout,
setRtrFlsTimeout,
Expand Down Expand Up @@ -77,10 +76,11 @@ const OKAPI_FETCH_OPTIONS = {
};

export class FFetch {
constructor({ logger, store, rtrConfig }) {
constructor({ logger, store, rtrConfig, okapi }) {
this.logger = logger;
this.store = store;
this.rtrConfig = rtrConfig;
this.okapi = okapi;
}

/**
Expand Down Expand Up @@ -232,12 +232,12 @@ export class FFetch {
*/
ffetch = async (resource, options = {}) => {
// FOLIO API requests are subject to RTR
if (isFolioApiRequest(resource, okapiConfig.url)) {
if (isFolioApiRequest(resource, this.okapi.url)) {
this.logger.log('rtrv', 'will fetch', resource);

// on authentication, grab the response to kick of the rotation cycle,
// then return the response
if (isAuthenticationRequest(resource, okapiConfig.url)) {
if (isAuthenticationRequest(resource, this.okapi.url)) {
this.logger.log('rtr', 'authn request', resource);
return this.nativeFetch.apply(global, [resource, options && { ...options, ...OKAPI_FETCH_OPTIONS }])
.then(res => {
Expand Down Expand Up @@ -266,7 +266,7 @@ export class FFetch {
// tries to logout, the logout request will fail. And that's fine, just
// fine. We will let them fail, capturing the response and swallowing it
// to avoid getting stuck in an error loop.
if (isLogoutRequest(resource, okapiConfig.url)) {
if (isLogoutRequest(resource, this.okapi.url)) {
this.logger.log('rtr', 'logout request');

return this.nativeFetch.apply(global, [resource, options && { ...options, ...OKAPI_FETCH_OPTIONS }])
Expand Down
100 changes: 79 additions & 21 deletions src/components/Root/FFetch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ jest.mock('../../loginServices', () => ({
getTokenExpiry: jest.fn(() => Promise.resolve())
}));

jest.mock('stripes-config', () => ({
url: 'okapiUrl',
tenant: 'okapiTenant',
okapi: {
url: 'okapiUrl',
tenant: 'okapiTenant'
}
}),
{ virtual: true });

const log = jest.fn();

const mockFetch = jest.fn();
Expand All @@ -50,7 +40,11 @@ describe('FFetch class', () => {
describe('Calling a non-FOLIO API', () => {
it('calls native fetch once', async () => {
mockFetch.mockResolvedValueOnce('non-okapi-success');
const testFfetch = new FFetch({ logger: { log } });
const testFfetch = new FFetch({ logger: { log },
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
} });
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();

Expand All @@ -63,7 +57,11 @@ describe('FFetch class', () => {
describe('Calling a FOLIO API fetch', () => {
it('calls native fetch once', async () => {
mockFetch.mockResolvedValueOnce('okapi-success');
const testFfetch = new FFetch({ logger: { log } });
const testFfetch = new FFetch({ logger: { log },
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
} });
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();
const response = await global.fetch('okapiUrl/whatever', { testOption: 'test' });
Expand Down Expand Up @@ -91,6 +89,10 @@ describe('FFetch class', () => {
logger: { log },
store: {
dispatch: jest.fn(),
},
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
}
});
testFfetch.replaceFetch();
Expand All @@ -110,7 +112,11 @@ describe('FFetch class', () => {
describe('logging out', () => {
it('calls native fetch once to log out', async () => {
mockFetch.mockResolvedValueOnce('logged out');
const testFfetch = new FFetch({ logger: { log } });
const testFfetch = new FFetch({ logger: { log },
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
} });
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();

Expand All @@ -124,7 +130,11 @@ describe('FFetch class', () => {
it('fetch failure is silently trapped', async () => {
mockFetch.mockRejectedValueOnce('logged out FAIL');

const testFfetch = new FFetch({ logger: { log } });
const testFfetch = new FFetch({ logger: { log },
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
} });
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();

Expand All @@ -143,7 +153,11 @@ describe('FFetch class', () => {
describe('logging out', () => {
it('Calling an okapi fetch with valid token...', async () => {
mockFetch.mockResolvedValueOnce('okapi success');
const testFfetch = new FFetch({ logger: { log } });
const testFfetch = new FFetch({ logger: { log },
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
} });
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();

Expand Down Expand Up @@ -188,6 +202,10 @@ describe('FFetch class', () => {
rtrConfig: {
fixedLengthSessionWarningTTL: '1m',
},
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
}
});
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();
Expand Down Expand Up @@ -244,6 +262,10 @@ describe('FFetch class', () => {
rtrConfig: {
fixedLengthSessionWarningTTL: '1m',
},
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
}
});
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();
Expand Down Expand Up @@ -282,6 +304,10 @@ describe('FFetch class', () => {
rtrConfig: {
fixedLengthSessionWarningTTL: '1m',
},
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
}
});
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();
Expand Down Expand Up @@ -317,6 +343,10 @@ describe('FFetch class', () => {
logger: { log },
store: {
dispatch: jest.fn(),
},
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
}
});
testFfetch.replaceFetch();
Expand Down Expand Up @@ -361,6 +391,10 @@ describe('FFetch class', () => {
rtrConfig: {
fixedLengthSessionWarningTTL: '1m',
},
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
}
});
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();
Expand Down Expand Up @@ -389,7 +423,11 @@ describe('FFetch class', () => {
it('returns the error', async () => {
mockFetch.mockResolvedValue('success')
.mockResolvedValueOnce('failure');
const testFfetch = new FFetch({ logger: { log } });
const testFfetch = new FFetch({ logger: { log },
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
} });
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();

Expand All @@ -411,7 +449,11 @@ describe('FFetch class', () => {
},
}
));
const testFfetch = new FFetch({ logger: { log } });
const testFfetch = new FFetch({ logger: { log },
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
} });
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();

Expand All @@ -435,7 +477,11 @@ describe('FFetch class', () => {
}
))
.mockRejectedValueOnce(new Error('token error message'));
const testFfetch = new FFetch({ logger: { log } });
const testFfetch = new FFetch({ logger: { log },
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
} });
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();

Expand Down Expand Up @@ -470,7 +516,11 @@ describe('FFetch class', () => {
}
}
));
const testFfetch = new FFetch({ logger: { log } });
const testFfetch = new FFetch({ logger: { log },
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
} });
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();

Expand Down Expand Up @@ -500,7 +550,11 @@ describe('FFetch class', () => {
}
}
));
const testFfetch = new FFetch({ logger: { log } });
const testFfetch = new FFetch({ logger: { log },
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
} });
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();

Expand Down Expand Up @@ -530,7 +584,11 @@ describe('FFetch class', () => {
}
}
));
const testFfetch = new FFetch({ logger: { log } });
const testFfetch = new FFetch({ logger: { log },
okapi:{
url: 'okapiUrl',
tenant: 'okapiTenant'
} });
testFfetch.replaceFetch();
testFfetch.replaceXMLHttpRequest();

Expand Down
3 changes: 1 addition & 2 deletions src/components/Root/FXHR.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { okapi } from 'stripes-config';
import { getPromise, isFolioApiRequest } from './token-util';
import {
RTR_ERROR_EVENT,
Expand All @@ -15,7 +14,7 @@ export default (deps) => {

open = (method, url) => {
this.FFetchContext.logger?.log('rtr', 'capture XHR.open');
this.shouldEnsureToken = isFolioApiRequest(url, okapi.url);
this.shouldEnsureToken = isFolioApiRequest(url, this.FFetchContext.okapi.url);
super.open(method, url);
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/Root/FXHR.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('FXHR', () => {
let testXHR;
beforeEach(() => {
jest.clearAllMocks();
FakeXHR = FXHR({ tokenExpiration: { atExpires: Date.now(), rtExpires: Date.now() + 5000 }, logger: { log: () => {} } });
FakeXHR = FXHR({ tokenExpiration: { atExpires: Date.now(), rtExpires: Date.now() + 5000 }, logger: { log: () => {} }, okapi:{ url:'okapiUrl' } });
testXHR = new FakeXHR();
});

Expand Down
1 change: 1 addition & 0 deletions src/components/Root/Root.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Root extends Component {
logger: this.props.logger,
store,
rtrConfig,
okapi
});
this.ffetch.replaceFetch();
this.ffetch.replaceXMLHttpRequest();
Expand Down
4 changes: 2 additions & 2 deletions src/components/SSOLanding/useSSOSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useLocation } from 'react-router-dom';
import { useCookies } from 'react-cookie';
import queryString from 'query-string';

import { config, okapi } from 'stripes-config';
import { config } from 'stripes-config';

import { defaultErrors } from '../../constants';
import { setAuthError } from '../../okapiActions';
Expand Down Expand Up @@ -45,7 +45,7 @@ const useSSOSession = () => {
const tenant = getTenant(params, token, store);

useEffect(() => {
requestUserWithPerms(okapi.url, store, tenant, token)
requestUserWithPerms(store.getState().okapi.url, store, tenant, token)
.then(() => {
if (store.getState()?.okapi?.authFailure) {
return Promise.reject(new Error('SSO Failed'));
Expand Down
Loading