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

[BACKPORT] fix: internal routing ignores public path #313

Merged
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
3 changes: 2 additions & 1 deletion src/components/ProgramRecord/ProgramRecord.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ProgramRecordTable from './ProgramRecordTable';
import RecordsHelp from './RecordsHelp';
import ProgramRecordAlert from '../ProgramRecordAlert';
import SendLearnerRecordModal from '../ProgramRecordSendModal';
import createCorrectInternalRoute from '../../utils';

import getProgramDetails from './data/service';

Expand Down Expand Up @@ -81,7 +82,7 @@ function ProgramRecord({ isPublic }) {
className="back-to-records"
>
<Hyperlink
destination="/"
destination={createCorrectInternalRoute('/')}
variant="muted"
>
<FormattedMessage
Expand Down
7 changes: 6 additions & 1 deletion src/components/ProgramRecordsList/ProgramRecordsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import NavigationBar from '../NavigationBar/NavigationBar';

import createCorrectInternalRoute from '../../utils';
import getProgramRecords from './data/service';

function ProgramRecordsList() {
Expand Down Expand Up @@ -115,7 +116,11 @@
<div className="d-flex align-items-center pt-3 pt-lg-0">
<Hyperlink
variant="muted"
destination={getConfig().USE_LR_MFE ? `/${record.uuid}` : `${getConfig().CREDENTIALS_BASE_URL}/records/programs/${record.uuid}/`}
destination={
getConfig().USE_LR_MFE
? createCorrectInternalRoute(`/${record.uuid}`)

Check warning on line 121 in src/components/ProgramRecordsList/ProgramRecordsList.jsx

View check run for this annotation

Codecov / codecov/patch

src/components/ProgramRecordsList/ProgramRecordsList.jsx#L121

Added line #L121 was not covered by tests
: `${getConfig().CREDENTIALS_BASE_URL}/records/programs/${record.uuid}/`
}
>
<Button variant="outline-primary">
<FormattedMessage
Expand Down
20 changes: 20 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getConfig } from '@edx/frontend-platform';

/**
* Create a correct inner path depend on config PUBLIC_PATH.
* @param {string} checkPath - the internal route path that is validated
* @returns {string} - the correct internal route path
*/
export default function createCorrectInternalRoute(checkPath) {
let basePath = getConfig().PUBLIC_PATH;

if (basePath.endsWith('/')) {
basePath = basePath.slice(0, -1);
}

if (!checkPath.startsWith(basePath)) {
return `${basePath}${checkPath}`;
}

return checkPath;
}
43 changes: 43 additions & 0 deletions src/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { getConfig } from '@edx/frontend-platform';

import createCorrectInternalRoute from './utils';

jest.mock('@edx/frontend-platform', () => ({
getConfig: jest.fn(),
ensureConfig: jest.fn(),
}));

describe('LearnerRecord utils', () => {
describe('createCorrectInternalRoute', () => {
beforeEach(() => {
getConfig.mockReset();
});

it('returns the correct internal route when checkPath is not prefixed with basePath', () => {
getConfig.mockReturnValue({ PUBLIC_PATH: '/' });

const checkPath = '/some/path';
const result = createCorrectInternalRoute(checkPath);

expect(result).toBe('/some/path');
});

it('returns the input checkPath when it is already prefixed with basePath', () => {
getConfig.mockReturnValue({ PUBLIC_PATH: '/learner-record' });

const checkPath = '/learner-record/some/path';
const result = createCorrectInternalRoute(checkPath);

expect(result).toBe('/learner-record/some/path');
});

it('handles basePath ending with a slash correctly', () => {
getConfig.mockReturnValue({ PUBLIC_PATH: '/learner-record/' });

const checkPath = '/some/path';
const result = createCorrectInternalRoute(checkPath);

expect(result).toBe('/learner-record/some/path');
});
});
});
Loading