-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add stricter checks in link for
profile
(#422)
Co-authored-by: Roshan Jossy <roshanjossey@gmail.com>
- Loading branch information
1 parent
deef897
commit 629c5b8
Showing
3 changed files
with
74 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); | ||
}); | ||
}); |
629c5b8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
app – ./
all-contributors.vercel.app
app-all-contributors.vercel.app
all-contributors-app.vercel.app
app-git-master-all-contributors.vercel.app