Skip to content

Commit

Permalink
Merge pull request #239 from codecov/fix-uuid-token
Browse files Browse the repository at this point in the history
fix: Validate UUID tokens
  • Loading branch information
thomasrockhu authored Jul 22, 2021
2 parents b3d01c0 + 1756b26 commit 300eb11
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/helpers/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const validator = require('validator')
* @returns boolean
*/
function validateToken(token) {
return validator.isAlphanumeric(token)
return validator.isAlphanumeric(token) || validator.isUUID(token)
}

function validateURL(url) {
Expand Down
6 changes: 5 additions & 1 deletion test/helpers/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ const realEnv = { ...process.env }

describe('Input Validators', () => {
describe('Tokens', () => {
it('Returns true with a valid token', () => {
it('Returns true with a valid alphanumeric token', () => {
expect(validate.validateToken('1bc123')).toBe(true)
})
it('Returns true with a valid UUID token', () => {
// Use a randomly generated UUIDv4
expect(validate.validateToken('5becd1a9-efa8-4bd8-8f94-e9f8613820c3')).toBe(true)
})
it('Returns false with an invalid token', () => {
expect(validate.validateToken('1bc1 23')).toBe(false)
})
Expand Down
31 changes: 22 additions & 9 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const { version } = require('../package.json')
const nock = require('nock')
const fs = require('fs')

// Backup the env
const realEnv = { ...process.env }

describe('Uploader Core', () => {
const env = process.env

Expand Down Expand Up @@ -68,16 +71,26 @@ describe('Uploader Core', () => {
expect(log).toHaveBeenCalledWith(expect.stringMatching(/<<<<<< ENV/))
})

it('Can upload without token', async () => {
jest.spyOn(process, 'exit').mockImplementation(() => {})
const log = jest.spyOn(console, 'log').mockImplementation(() => {})
await app.main({
name: 'customname',
url: 'https://codecov.io',
dryRun: true,
env: 'SOMETHING,ANOTHER',
describe('Token', () => {
beforeEach(() => {
delete process.env.CODECOV_TOKEN
})

afterEach(() => {
process.env = realEnv
})

it('Can upload without token', async () => {
jest.spyOn(process, 'exit').mockImplementation(() => {})
const log = jest.spyOn(console, 'log').mockImplementation(() => {})
await app.main({
name: 'customname',
url: 'https://codecov.io',
dryRun: true,
env: 'SOMETHING,ANOTHER',
})
expect(log).toHaveBeenCalledWith(expect.stringMatching('-> No token specified or token is empty'))
})
expect(log).toHaveBeenCalledWith(expect.stringMatching('-> No token specified or token is empty'))
})

describe('Flags', () => {
Expand Down

0 comments on commit 300eb11

Please sign in to comment.