Skip to content

Commit

Permalink
STCOR-942: migrate away from reading stripes-config::okapi.url
Browse files Browse the repository at this point in the history
  • Loading branch information
aidynoJ committed Feb 4, 2025
1 parent b6883f6 commit e849fda
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 60 deletions.
6 changes: 3 additions & 3 deletions src/components/Login/LoginCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ 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;
// store is already available on login page with okapi data
this.okapiUrl = this.constructor.contextType.store.getState().okapi.url;
this.tenant = this.constructor.contextType.store.getState().okapi.tenant;
if (props.autoLogin && props.autoLogin.username) {
this.handleSubmit(props.autoLogin);
}
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
16 changes: 7 additions & 9 deletions src/components/SSOLanding/useSSOSession.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useDispatch, useStore } from 'react-redux';
import { useLocation } from 'react-router-dom';
import { useCookies } from 'react-cookie';

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

import { defaultErrors } from '../../constants';
import { setAuthError } from '../../okapiActions';
Expand All @@ -30,13 +30,11 @@ jest.mock('stripes-config', () => ({
config: {
useSecureTokens: true,
},
okapi: {
url: 'okapiUrl',
tenant: 'okapiTenant'
}
}),
{ virtual: true });

jest.mock('');

jest.mock('../../loginServices', () => ({
requestUserWithPerms: jest.fn()
}));
Expand Down Expand Up @@ -75,7 +73,7 @@ describe('SSOLanding', () => {

renderHook(() => useSSOSession());

expect(requestUserWithPerms).toHaveBeenCalledWith(okapi.url, store, okapi.tenant, ssoTokenValue);
expect(requestUserWithPerms).toHaveBeenCalledWith(store.getState().okapi.url, store, store.getState().okapi.tenant, ssoTokenValue);
});

it('should request user session when RTR is disabled with token from cookies', () => {
Expand All @@ -86,7 +84,7 @@ describe('SSOLanding', () => {

renderHook(() => useSSOSession());

expect(requestUserWithPerms).toHaveBeenCalledWith(okapi.url, store, okapi.tenant, ssoTokenValue);
expect(requestUserWithPerms).toHaveBeenCalledWith(store.getState().okapi.url, store, 'okapiTenant', ssoTokenValue);
});

it('should request user session when RTR is disabled and right tenant from ssoToken', () => {
Expand All @@ -99,7 +97,7 @@ describe('SSOLanding', () => {

renderHook(() => useSSOSession());

expect(requestUserWithPerms).toHaveBeenCalledWith(okapi.url, store, tokenTenant, ssoTokenValue);
expect(requestUserWithPerms).toHaveBeenCalledWith(store.getState().okapi.url, store, tokenTenant, ssoTokenValue);
});

it('should request user session when RTR is enabled and right tenant from query params', () => {
Expand All @@ -111,7 +109,7 @@ describe('SSOLanding', () => {

renderHook(() => useSSOSession());

expect(requestUserWithPerms).toHaveBeenCalledWith(okapi.url, store, queryTenant, undefined);
expect(requestUserWithPerms).toHaveBeenCalledWith(store.getState().okapi.url, store, queryTenant, undefined);
});

it('should display error when session request failed', async () => {
Expand Down
Loading

0 comments on commit e849fda

Please sign in to comment.