Skip to content

Commit

Permalink
fix: add stricter checks in link for profile (#422)
Browse files Browse the repository at this point in the history
Co-authored-by: Roshan Jossy <roshanjossey@gmail.com>
  • Loading branch information
tenshiAMD and Roshanjossey committed Sep 25, 2022
1 parent deef897 commit 629c5b8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lib/get-user-details.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = getUserDetails;

const { UserNotFoundError } = require("./modules/errors");
const { generateValidLink } = require("./modules/helpers");
const { generateValidProfileLink } = require("./modules/helpers");

async function getUserDetails({ octokit, username }) {
// TODO: optimization, if commenting user is the user we're adding we can avoid an api call
Expand Down Expand Up @@ -33,6 +33,6 @@ async function getUserDetails({ octokit, username }) {
login,
name: name || username,
avatar_url,
profile: generateValidLink(blog || html_url, username),
profile: generateValidProfileLink(blog, html_url),
};
}
16 changes: 7 additions & 9 deletions lib/modules/helpers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
function generateValidLink(url, username = '') {
let validLink = url
const validRegex = /^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$/

if (!validLink.startsWith("http")) validLink = `http://${url}/`
if (!validLink.match(validRegex)) validLink = `https://github.com/${username}/`

return validLink
function generateValidProfileLink(blog, githubProfileURL) {
const validRegexWithScheme = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
const validRegexWithoutScheme = /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/
if (validRegexWithScheme.test(blog)) return blog;
if (validRegexWithoutScheme.test(blog)) return `http://${blog}`;
return githubProfileURL || ''
}

module.exports = {
generateValidLink
generateValidProfileLink
}
74 changes: 65 additions & 9 deletions test/unit/helpers.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,75 @@
const { generateValidLink } = require('../../lib/modules/helpers');
const { generateValidProfileLink } = require('../../lib/modules/helpers');

describe('generateValidLink', () => {
const username = 'tenshiAMD'
describe('generateValidProfileLink', () => {
const githubProfileUrl = 'https://github.com/tenshiAMD'

test('return valid link - no protocol', async () => {
test('returns valid link - valid URL format having `https` protocol', async () => {
let url = 'https://tenshiamd.com';
let validUrl = generateValidProfileLink(url, githubProfileUrl);

expect(validUrl).toEqual(url);
});

test('returns valid link - valid URL format having `http` protocol', async () => {
let url = 'http://tenshiamd.com';
let validUrl = generateValidProfileLink(url, githubProfileUrl);

expect(validUrl).toEqual(url);
});

test('returns valid link - valid URL format with `null` githubProfileUrl', async () => {
let url = 'https://tenshiamd.com';
let validUrl = generateValidProfileLink(url, null);

expect(validUrl).toEqual(url);
});

test('returns valid link - valid URL format with `http` in between', async () => {
let url = 'tenshhttpiamd.com';
let validUrl = generateValidProfileLink(url, githubProfileUrl);

expect(validUrl).toEqual(url);
});

test('returns valid link - valid URL format with `https` in between', async () => {
let url = 'tenshhttpsiamd.com';
let validUrl = generateValidProfileLink(url, githubProfileUrl);

expect(validUrl).toEqual(url);
});

test('returns valid link - no protocol', async () => {
let url = 'tenshiamd.com';
let validUrl = generateValidLink(url, username);
let validUrl = generateValidProfileLink(url, githubProfileUrl);

expect(validUrl).toEqual(url);
});

test('returns valid link - no protocol and starting with `http`', async () => {
let url = 'httptenshiamd.com';
let validUrl = generateValidProfileLink(url, githubProfileUrl);

expect(validUrl).toEqual(url);
});

test('returns valid link - no protocol and starting with `https`', async () => {
let url = 'httpstenshiamd.com';
let validUrl = generateValidProfileLink(url, githubProfileUrl);

expect(validUrl).toEqual(url);
});

test('returns valid link - incomplete URL format', async () => {
let url = 'contributor';
let validUrl = generateValidProfileLink(url, githubProfileUrl);

expect(validUrl).toEqual(`http://${url}/`);
expect(validUrl).toEqual(githubProfileUrl);
});

test('return valid link - incomplete URL format', async () => {
test('returns valid link - incomplete URL format with `null` githubProfileUrl', async () => {
let url = 'contributor';
let validUrl = generateValidLink(url, username);
let validUrl = generateValidProfileLink(url, null);

expect(validUrl).toEqual(`https://github.com/${username}/`);
expect(validUrl).toEqual('');
});
});

1 comment on commit 629c5b8

@vercel
Copy link

@vercel vercel bot commented on 629c5b8 Sep 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.