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

feat: mfe multitenant config #3

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export async function runtimeConfig() {

const params = new URLSearchParams();
params.append('mfe', APP_ID);
params.append('site', window.location.hostname);
const url = `${MFE_CONFIG_API_URL}?${params.toString()}`;

const { data } = await apiService.get(url, apiConfig);
Expand Down
87 changes: 51 additions & 36 deletions src/initialize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,38 +40,52 @@ jest.mock('./auth/LocalForageCache');

let config = null;
const newConfig = {
common: {
SITE_NAME: 'Test Case',
LOGO_URL: 'http://test.example.com:18000/theme/logo.png',
LOGO_TRADEMARK_URL: 'http://test.example.com:18000/theme/logo.png',
LOGO_WHITE_URL: 'http://test.example.com:18000/theme/logo.png',
ACCESS_TOKEN_COOKIE_NAME: 'edx-jwt-cookie-header-payload',
FAVICON_URL: 'http://test.example.com:18000/theme/favicon.ico',
CSRF_TOKEN_API_PATH: '/csrf/api/v1/token',
DISCOVERY_API_BASE_URL: 'http://test.example.com:18381',
PUBLISHER_BASE_URL: 'http://test.example.com:18400',
ECOMMERCE_BASE_URL: 'http://test.example.com:18130',
LANGUAGE_PREFERENCE_COOKIE_NAME: 'openedx-language-preference',
LEARNING_BASE_URL: 'http://test.example.com:2000',
LMS_BASE_URL: 'http://test.example.com:18000',
LOGIN_URL: 'http://test.example.com:18000/login',
LOGOUT_URL: 'http://test.example.com:18000/logout',
STUDIO_BASE_URL: 'http://studio.example.com:18010',
MARKETING_SITE_BASE_URL: 'http://test.example.com:18000',
ORDER_HISTORY_URL: 'http://test.example.com:1996/orders',
REFRESH_ACCESS_TOKEN_ENDPOINT: 'http://test.example.com:18000/login_refresh',
SEGMENT_KEY: '',
USER_INFO_COOKIE_NAME: 'edx-user-info',
IGNORED_ERROR_REGEX: '',
CREDENTIALS_BASE_URL: 'http://test.example.com:18150',
localhost: {
common: {
SITE_NAME: 'Test Case',
LOGO_URL: 'http://test.example.com:18000/theme/logo.png',
LOGO_TRADEMARK_URL: 'http://test.example.com:18000/theme/logo.png',
LOGO_WHITE_URL: 'http://test.example.com:18000/theme/logo.png',
ACCESS_TOKEN_COOKIE_NAME: 'edx-jwt-cookie-header-payload',
FAVICON_URL: 'http://test.example.com:18000/theme/favicon.ico',
CSRF_TOKEN_API_PATH: '/csrf/api/v1/token',
DISCOVERY_API_BASE_URL: 'http://test.example.com:18381',
PUBLISHER_BASE_URL: 'http://test.example.com:18400',
ECOMMERCE_BASE_URL: 'http://test.example.com:18130',
LANGUAGE_PREFERENCE_COOKIE_NAME: 'openedx-language-preference',
LEARNING_BASE_URL: 'http://test.example.com:2000',
LMS_BASE_URL: 'http://test.example.com:18000',
LOGIN_URL: 'http://test.example.com:18000/login',
LOGOUT_URL: 'http://test.example.com:18000/logout',
STUDIO_BASE_URL: 'http://studio.example.com:18010',
MARKETING_SITE_BASE_URL: 'http://test.example.com:18000',
ORDER_HISTORY_URL: 'http://test.example.com:1996/orders',
REFRESH_ACCESS_TOKEN_ENDPOINT: 'http://test.example.com:18000/login_refresh',
SEGMENT_KEY: '',
USER_INFO_COOKIE_NAME: 'edx-user-info',
IGNORED_ERROR_REGEX: '',
CREDENTIALS_BASE_URL: 'http://test.example.com:18150',
},
auth: {
INFO_EMAIL: 'openedx@example.com',
ACTIVATION_EMAIL_SUPPORT_LINK: 'http//support.test.com',
},
learning: {
LEGACY_THEME_NAME: 'example',
DISCUSSIONS_MFE_BASE_URL: 'http://test.example.com:2002',
},
},
auth: {
INFO_EMAIL: 'openedx@example.com',
ACTIVATION_EMAIL_SUPPORT_LINK: 'http//support.test.com',
},
learning: {
LEGACY_THEME_NAME: 'example',
DISCUSSIONS_MFE_BASE_URL: 'http://test.example.com:2002',
site2: {
common: {
SITE_NAME: 'Site 2',
LOGO_URL: 'http://test.example-site2.com:18000/theme/logo.png',
LOGO_TRADEMARK_URL: 'http://test.example-site2.com:18000/theme/logo.png',
LOGO_WHITE_URL: 'http://test.example-site2.com:18000/theme/logo.png',
LMS_BASE_URL: 'http://test.example-site2.com:18000',
LOGIN_URL: 'http://test.example-site2.com:18000/login',
LOGOUT_URL: 'http://test.example-site2.com:18000/logout',

},
},
};

Expand Down Expand Up @@ -279,14 +293,15 @@ describe('initialize', () => {
expect(overrideHandlers.initError).toHaveBeenCalledWith(new Error('uhoh!'));
});

it('should initialize the app with runtime configuration', async () => {
it('should initialize the app with runtime configuration by site', async () => {
config.MFE_CONFIG_API_URL = 'http://localhost:18000/api/mfe/v1/config';
config.APP_ID = 'auth';
configureCache.mockReturnValueOnce(Promise.resolve({
get: (url) => {
const params = new URL(url).search;
const mfe = new URLSearchParams(params).get('mfe');
return ({ data: { ...newConfig.common, ...newConfig[mfe] } });
const site = new URLSearchParams(params).get('site');
return ({ data: { ...newConfig[site].common, ...newConfig[site][mfe] } });
},
}));

Expand Down Expand Up @@ -315,9 +330,9 @@ describe('initialize', () => {
expect(ensureAuthenticatedUser).not.toHaveBeenCalled();
expect(hydrateAuthenticatedUser).not.toHaveBeenCalled();
expect(logError).not.toHaveBeenCalled();
expect(config.SITE_NAME).toBe(newConfig.common.SITE_NAME);
expect(config.INFO_EMAIL).toBe(newConfig.auth.INFO_EMAIL);
expect(Object.values(config).includes(newConfig.learning.DISCUSSIONS_MFE_BASE_URL)).toBeFalsy();
expect(config.SITE_NAME).toBe(newConfig.localhost.common.SITE_NAME);
expect(config.INFO_EMAIL).toBe(newConfig.localhost.auth.INFO_EMAIL);
expect(Object.values(config).includes(newConfig.localhost.learning.DISCUSSIONS_MFE_BASE_URL)).toBeFalsy();
});

it('should initialize the app with the build config when runtime configuration fails', async () => {
Expand Down