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

Hot Fix: Added cases for ensureProtocol method; Fix project participant editing #999

Merged
merged 4 commits into from
Apr 13, 2023
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
4 changes: 0 additions & 4 deletions api/src/repositories/project-participation-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ export class ProjectParticipationRepository extends BaseRepository {
project_role pr
ON
pr.project_role_id = pp.project_role_id
LEFT JOIN
project_role pr
ON
pr.project_role_id = pp.project_role_id
WHERE
pp.project_id = ${projectId};
`;
Expand Down
17 changes: 13 additions & 4 deletions app/src/utils/Utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import {
} from './Utils';

describe('ensureProtocol', () => {
it('does nothing if string already has `http://`', async () => {
const url = 'http://someurl.com';
const urlWithProtocol = ensureProtocol(url);
expect(urlWithProtocol).toEqual(url);
it('upgrades the URL if string begins with `http://`', async () => {
const urlWithProtocol = ensureProtocol('http://someurl.com');
expect(urlWithProtocol).toEqual('https://someurl.com');
});

it('does nothing if string already has `https://`', async () => {
Expand All @@ -24,6 +23,16 @@ describe('ensureProtocol', () => {
expect(urlWithProtocol).toEqual(url);
});

it('adds http if string begins with `localhost`', async () => {
const urlWithProtocol = ensureProtocol('localhost:1234/test');
expect(urlWithProtocol).toEqual('http://localhost:1234/test');
});

it('does nothing if string begins with `http://localhost`', async () => {
const urlWithProtocol = ensureProtocol('http://localhost:1234/test');
expect(urlWithProtocol).toEqual('http://localhost:1234/test');
});

it('adds `https://` when no protocol param is provided', async () => {
const url = 'someurl.com';
const urlWithProtocol = ensureProtocol(url);
Expand Down
20 changes: 18 additions & 2 deletions app/src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,30 @@ import { LatLngBounds } from 'leaflet';
import moment from 'moment';

/**
* Checks if a url string starts with an `http(s)://` protocol, and adds `https://` if it does not.
* Checks if a url string starts with an `http[s]://` protocol, and adds `https://` if it does not. If the url
* begins with `localhost`, the `http` protocol is used.
*
* @param {string} url
* @param {('http://' | 'https://')} [protocol='https://'] The protocol to add, if necessary. Defaults to `https://`.
* @return {*} {string} the url which is guaranteed to have an `http(s)://` protocol.
*/
export const ensureProtocol = (url: string, protocol: 'http://' | 'https://' = 'https://'): string => {
return ((url.startsWith('http://') || url.startsWith('https://')) && url) || `${protocol}${url}`;
if (url.startsWith('localhost')) {
return `${'http://'}${url}`;
}

if (url.startsWith('https://') || url.startsWith('http://localhost')) {
return url;
}

if (url.startsWith('http://')) {
// If protocol is HTTPS, upgrade the URL
if (protocol === 'https://') {
return `${'https://'}${url.slice(7)}`;
}
}

return `${protocol}${url}`;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ services:
- NODE_ENV=${NODE_ENV}
- REACT_APP_NODE_ENV=${NODE_ENV}
- PORT=${APP_PORT}
- REACT_APP_API_HOST=http://${API_HOST}
- REACT_APP_API_HOST=${API_HOST}
- REACT_APP_API_PORT=${API_PORT}
- REACT_APP_MAX_UPLOAD_NUM_FILES=${MAX_UPLOAD_NUM_FILES}
- REACT_APP_MAX_UPLOAD_FILE_SIZE=${MAX_UPLOAD_FILE_SIZE}
Expand Down