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

fix: internal routing ignores public path #312

Merged
merged 2 commits into from
Mar 29, 2024
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
3 changes: 2 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 _ from 'lodash';

import NavigationBar from '../NavigationBar';

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

function ProgramRecordsList() {
Expand Down Expand Up @@ -115,7 +116,7 @@ function ProgramRecordsList() {
<div className="d-flex align-items-center pt-3 pt-lg-0">
<Hyperlink
variant="muted"
destination={`/${record.uuid}`}
destination={createCorrectInternalRoute(`/${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, getPath } 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 = getPath(getConfig().PUBLIC_PATH);

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

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

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

import createCorrectInternalRoute from './utils';

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

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

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

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: 'example.com' });
getPath.mockReturnValue('/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: 'example.com/' });
getPath.mockReturnValue('/learner-record/');

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

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