[MM-44607] Only cleanse subpath if it occurs at the beginning (#2129)

* [MM-44607] Only cleanse subpath if it occurs at the beginning

* Fix tests
This commit is contained in:
Devin Binnie
2022-05-27 11:37:07 -04:00
committed by GitHub
parent 42a53e31b5
commit b4a779a666
4 changed files with 30 additions and 1 deletions

View File

@@ -303,6 +303,20 @@ describe('common/utils/url', () => {
}); });
}); });
describe('cleanPathName', () => {
it('should not clean path name if it occurs other than the beginning', () => {
expect(urlUtils.cleanPathName('/mattermost', '/home/channels/mattermost/test')).toBe('/home/channels/mattermost/test');
});
it('should clean path name if it occurs at the beginning', () => {
expect(urlUtils.cleanPathName('/mattermost', '/mattermost/channels/home/test')).toBe('/channels/home/test');
});
it('should do nothing if it doesnt occur', () => {
expect(urlUtils.cleanPathName('/mattermost', '/channels/home/test')).toBe('/channels/home/test');
});
});
describe('isCustomLoginURL', () => { describe('isCustomLoginURL', () => {
it('should match correct URL', () => { it('should match correct URL', () => {
expect(urlUtils.isCustomLoginURL( expect(urlUtils.isCustomLoginURL(

View File

@@ -208,6 +208,18 @@ function isChannelExportUrl(serverUrl: URL | string, inputUrl: URL | string): bo
return isUrlType('plugins/com.mattermost.plugin-channel-export/api/v1/export', serverUrl, inputUrl); return isUrlType('plugins/com.mattermost.plugin-channel-export/api/v1/export', serverUrl, inputUrl);
} }
function cleanPathName(basePathName: string, pathName: string) {
if (basePathName === '/') {
return pathName;
}
if (pathName.startsWith(basePathName)) {
return pathName.replace(basePathName, '');
}
return pathName;
}
export default { export default {
isValidURL, isValidURL,
isValidURI, isValidURI,
@@ -224,4 +236,5 @@ export default {
isCustomLoginURL, isCustomLoginURL,
isChannelExportUrl, isChannelExportUrl,
isUrlType, isUrlType,
cleanPathName,
}; };

View File

@@ -46,6 +46,7 @@ jest.mock('common/utils/url', () => ({
isTeamUrl: jest.fn(), isTeamUrl: jest.fn(),
isAdminUrl: jest.fn(), isAdminUrl: jest.fn(),
getView: jest.fn(), getView: jest.fn(),
cleanPathName: jest.fn(),
})); }));
jest.mock('common/tabs/TabView', () => ({ jest.mock('common/tabs/TabView', () => ({
getTabViewName: jest.fn(), getTabViewName: jest.fn(),
@@ -770,6 +771,7 @@ describe('main/windows/windowManager', () => {
], ],
}, },
]; ];
urlUtils.cleanPathName.mockImplementation((base, path) => path);
}); });
afterEach(() => { afterEach(() => {

View File

@@ -588,7 +588,7 @@ export class WindowManager {
log.debug('WwindowManager.handleBrowserHistoryPush', {viewName, pathName}); log.debug('WwindowManager.handleBrowserHistoryPush', {viewName, pathName});
const currentView = this.viewManager?.views.get(viewName); const currentView = this.viewManager?.views.get(viewName);
const cleanedPathName = currentView?.tab.server.url.pathname === '/' ? pathName : pathName.replace(currentView?.tab.server.url.pathname || '', ''); const cleanedPathName = urlUtils.cleanPathName(currentView?.tab.server.url.pathname || '', pathName);
const redirectedViewName = urlUtils.getView(`${currentView?.tab.server.url}${cleanedPathName}`, Config.teams)?.name || viewName; const redirectedViewName = urlUtils.getView(`${currentView?.tab.server.url}${cleanedPathName}`, Config.teams)?.name || viewName;
if (this.viewManager?.closedViews.has(redirectedViewName)) { if (this.viewManager?.closedViews.has(redirectedViewName)) {
// If it's a closed view, just open it and stop // If it's a closed view, just open it and stop