Skip to content

Commit

Permalink
feat(registry): Add unit tests for audit --registry
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnturner committed Mar 6, 2019
1 parent f2f64ed commit 6895850
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 22 deletions.
57 changes: 46 additions & 11 deletions test/npm-auditer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-expressions */
/*
* Copyright IBM All Rights Reserved.
*
Expand All @@ -8,20 +9,36 @@ const path = require('path');
const { audit } = require('../lib/npm-auditer');

function config(additions) {
return Object.assign({}, { whitelist: [], advisories: [] }, additions);
const defaultConfig = {
levels: {
low: false,
moderate: false,
high: false,
critical: false,
},
report: {},
advisories: [],
whitelist: [],
directory: './',
registry: undefined,
};
return Object.assign({}, defaultConfig, additions);
}

function testDir(s) {
return path.resolve(__dirname, s);
}

describe('npm-auditer', () => {
// To modify what slow times are, need to use
// function() {} instead of () => {}
// eslint-disable-next-line func-names
describe('npm-auditer', function() {
this.slow(6000);
it('reports critical severity', () => {
return audit(
config({
directory: testDir('npm-critical'),
levels: { critical: true },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -38,7 +55,6 @@ describe('npm-auditer', () => {
config({
directory: testDir('npm-critical'),
levels: { critical: false },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -55,7 +71,6 @@ describe('npm-auditer', () => {
config({
directory: testDir('npm-high'),
levels: { high: true },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -72,7 +87,6 @@ describe('npm-auditer', () => {
config({
directory: testDir('npm-moderate'),
levels: { moderate: true },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -89,7 +103,6 @@ describe('npm-auditer', () => {
config({
directory: testDir('npm-moderate'),
levels: { moderate: false },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -107,7 +120,6 @@ describe('npm-auditer', () => {
directory: testDir('npm-moderate'),
levels: { moderate: true },
advisories: [658],
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -125,7 +137,6 @@ describe('npm-auditer', () => {
directory: testDir('npm-moderate'),
levels: { moderate: true },
advisories: [659],
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -142,7 +153,6 @@ describe('npm-auditer', () => {
config({
directory: testDir('npm-low'),
levels: { low: true },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -159,7 +169,6 @@ describe('npm-auditer', () => {
config({
directory: testDir('npm-none'),
levels: { low: true },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -171,4 +180,30 @@ describe('npm-auditer', () => {
});
});
});
// eslint-disable-next-line func-names
it('fails with error code ETIMEDOUT on an invalid site', function(cb) {
this.slow(25000);
audit(
config({
directory: testDir('npm-low'),
levels: { low: true },
registry: 'https://registry.npmjs.co',
})
).catch(err => {
expect(err.message).to.include('code ETIMEDOUT');
cb();
});
});
it('fails errors with code ENOAUDIT on a valid site with no audit', cb => {
audit(
config({
directory: testDir('npm-low'),
levels: { low: true },
registry: 'https://example.com',
})
).catch(err => {
expect(err.message).to.include('code ENOAUDIT');
cb();
});
});
});
40 changes: 29 additions & 11 deletions test/yarn-auditer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,36 @@ const path = require('path');
const { audit } = require('../lib/yarn-auditer');

function config(additions) {
return Object.assign({}, { whitelist: [], advisories: [] }, additions);
const defaultConfig = {
levels: {
low: false,
moderate: false,
high: false,
critical: false,
},
report: {},
advisories: [],
whitelist: [],
directory: './',
registry: undefined,
};
return Object.assign({}, defaultConfig, additions);
}

function testDir(s) {
return path.resolve(__dirname, s);
}

describe('yarn-auditer', () => {
// To modify what slow times are, need to use
// function() {} instead of () => {}
// eslint-disable-next-line func-names
describe('yarn-auditer', function() {
this.slow(3000);
it('reports critical severity', () => {
return audit(
config({
directory: testDir('yarn-critical'),
levels: { critical: true },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -38,7 +54,6 @@ describe('yarn-auditer', () => {
config({
directory: testDir('yarn-critical'),
levels: { critical: false },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -55,7 +70,6 @@ describe('yarn-auditer', () => {
config({
directory: testDir('yarn-high'),
levels: { high: true },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -72,7 +86,6 @@ describe('yarn-auditer', () => {
config({
directory: testDir('yarn-moderate'),
levels: { moderate: true },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -89,7 +102,6 @@ describe('yarn-auditer', () => {
config({
directory: testDir('yarn-moderate'),
levels: { moderate: false },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -107,7 +119,6 @@ describe('yarn-auditer', () => {
directory: testDir('yarn-moderate'),
levels: { moderate: true },
advisories: [658],
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -125,7 +136,6 @@ describe('yarn-auditer', () => {
directory: testDir('yarn-moderate'),
levels: { moderate: true },
advisories: [659],
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -142,7 +152,6 @@ describe('yarn-auditer', () => {
config({
directory: testDir('yarn-low'),
levels: { low: true },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -159,7 +168,6 @@ describe('yarn-auditer', () => {
config({
directory: testDir('yarn-none'),
levels: { low: true },
report: {},
}),
summary => summary
).then(summary => {
Expand All @@ -171,4 +179,14 @@ describe('yarn-auditer', () => {
});
});
});
it("doesn't use the registry flag since it's not supported in Yarn yet", () => {
return audit(
config({
directory: testDir('yarn-low'),
levels: { low: true },
registry: 'https://example.com',
}),
summary => summary
);
});
});

0 comments on commit 6895850

Please sign in to comment.