Skip to content

Commit

Permalink
[npm bountysource cdnjs circleci clojars docker gem homebrew itunes m…
Browse files Browse the repository at this point in the history
…icrobadger nexus requires shippable suggest uptimerobot] Service tests for NPM total downloads + invalidJSON helper (#1471)

* Service tests for NPM total downloads

* Spread syntax removed

* service-tests/helpers/mocks.js -> service-tests/helpers/response-fixtures.js

* Another value an invalid JSON in response-fixtures

* Using invalidJSON helper in service tests

* Working example in a service test of microbadger

* isPercentage supports decimal and integer values
  • Loading branch information
platan authored and RedSparr0w committed Feb 12, 2018
1 parent 8ad176d commit 17b4a70
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 38 deletions.
3 changes: 1 addition & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1697,8 +1697,7 @@ cache(function (data, match, sendBadge, request) {
var apiUrl = 'https://api.npmjs.org/downloads/range/1000-01-01:3000-01-01/' + pkg; // use huge range, will need to fix this in year 3000 :)
var badgeData = getBadgeData('downloads', data);
request(apiUrl, function (err, res, buffer) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
if (checkErrorResponse(badgeData, err, res)) {
sendBadge(format, badgeData);
return;
}
Expand Down
3 changes: 2 additions & 1 deletion service-tests/bountysource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const { invalidJSON } = require('./helpers/response-fixtures');

const t = new ServiceTester({ id: 'bountysource', title: 'Bountysource' });
module.exports = t;
Expand Down Expand Up @@ -30,7 +31,7 @@ t.create('bounties (unexpected response)')
.get('/team/mozilla-core/activity.json')
.intercept(nock => nock('https://api.bountysource.com')
.get('/teams/mozilla-core')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'bounties', value: 'invalid'});

Expand Down
3 changes: 2 additions & 1 deletion service-tests/cdnjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const { isVPlusTripleDottedVersion } = require('./helpers/validators.js');
const { invalidJSON } = require('./helpers/response-fixtures');

const t = new ServiceTester({ id: 'cdnjs', title: 'CDNJs' });
module.exports = t;
Expand All @@ -28,7 +29,7 @@ t.create('cdnjs (unexpected response)')
.get('/v/jquery.json')
.intercept(nock => nock('https://api.cdnjs.com')
.get('/libraries/jquery?fields=version')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'cdnjs', value: 'invalid'});

Expand Down
4 changes: 2 additions & 2 deletions service-tests/circleci.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');

const { invalidJSON } = require('./helpers/response-fixtures');
const isBuildStatus = Joi.string().regex(/^(passing|failed|no tests|scheduled|not run)$/);

const t = new ServiceTester({ id: 'circleci', title: 'Circle CI' });
Expand Down Expand Up @@ -36,6 +36,6 @@ t.create('circle ci (unexpected response)')
.get('/project/github/RedSparr0w/node-csgo-parser.json')
.intercept(nock => nock('https://circleci.com')
.get('/api/v1.1/project/github/RedSparr0w/node-csgo-parser?filter=completed&limit=1')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'build', value: 'invalid'});
3 changes: 2 additions & 1 deletion service-tests/clojars.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const { invalidJSON } = require('./helpers/response-fixtures');

const t = new ServiceTester({ id: 'clojars', title: 'clojars' });
module.exports = t;
Expand All @@ -27,7 +28,7 @@ t.create('clojars (unexpected response)')
.get('/v/prismic.json')
.intercept(nock => nock('https://clojars.org')
.get('/prismic/latest-version.json')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'clojars', value: 'invalid'});

Expand Down
9 changes: 5 additions & 4 deletions service-tests/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');

const { isMetric } = require('./helpers/validators.js');
const { invalidJSON } = require('./helpers/response-fixtures');
const isAutomatedBuildStatus = Joi.string().valid('automated', 'manual');
const isBuildStatus = Joi.string().regex(/^(passing|failing|building)$/);

Expand Down Expand Up @@ -40,7 +41,7 @@ t.create('docker stars (unexpected response)')
.get('/stars/_/ubuntu.json')
.intercept(nock => nock('https://hub.docker.com/')
.get('/v2/repositories/library/ubuntu/stars/count/')
.reply(200, "some kind of error")
.reply(invalidJSON)
)
.expectJSON({name: 'docker stars', value: 'invalid'});

Expand Down Expand Up @@ -74,7 +75,7 @@ t.create('docker pulls (unexpected response)')
.get('/pulls/_/ubuntu.json')
.intercept(nock => nock('https://hub.docker.com/')
.get('/v2/repositories/library/ubuntu')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'docker pulls', value: 'invalid'});

Expand Down Expand Up @@ -108,7 +109,7 @@ t.create('docker automated build (unexpected response)')
.get('/automated/_/ubuntu.json')
.intercept(nock => nock('https://registry.hub.docker.com/')
.get('/v2/repositories/library/ubuntu')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'docker build', value: 'invalid'});

Expand All @@ -135,6 +136,6 @@ t.create('docker build status (unexpected response)')
.get('/build/_/ubuntu.json')
.intercept(nock => nock('https://registry.hub.docker.com/')
.get('/v2/repositories/library/ubuntu/buildhistory')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'docker build', value: 'invalid'});
13 changes: 7 additions & 6 deletions service-tests/gem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
isVPlusDottedVersionAtLeastOne,
isMetric
} = require('./helpers/validators');
const { invalidJSON } = require('./helpers/response-fixtures');
const isOrdinalNumber = Joi.string().regex(/^[1-9][0-9]+(ʰ|ˢ||ʳ)$/);
const isOrdinalNumberDaily = Joi.string().regex(/^[1-9][0-9]+(ʰ|ˢ||ʳ) daily$/);

Expand Down Expand Up @@ -36,7 +37,7 @@ t.create('version (unexpected response)')
.get('/v/formatador.json')
.intercept(nock => nock('https://rubygems.org')
.get('/api/v1/gems/formatador.json')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'gem', value: 'invalid'});

Expand Down Expand Up @@ -64,7 +65,7 @@ t.create('total downloads (unexpected response)')
.get('/dt/rails.json')
.intercept(nock => nock('https://rubygems.org')
.get('/api/v1/gems/rails.json')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'downloads', value: 'invalid'});

Expand Down Expand Up @@ -105,7 +106,7 @@ t.create('version downloads (unexpected response)')
.get('/dv/rails/4.1.0.json')
.intercept(nock => nock('https://rubygems.org')
.get('/api/v1/versions/rails.json')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: '[email protected]', value: 'invalid'});

Expand All @@ -131,7 +132,7 @@ t.create('latest version downloads (unexpected response)')
.get('/dtv/rails.json')
.intercept(nock => nock('https://rubygems.org')
.get('/api/v1/gems/rails.json')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'downloads@latest', value: 'invalid'});

Expand All @@ -158,7 +159,7 @@ t.create('users (unexpected response)')
.get('/u/raphink.json')
.intercept(nock => nock('https://rubygems.org')
.get('/api/v1/owners/raphink/gems.json')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'gems', value: 'invalid'});

Expand Down Expand Up @@ -192,6 +193,6 @@ t.create('rank (unexpected response)')
.get('/rt/rspec-puppet-facts.json')
.intercept(nock => nock('http://bestgems.org')
.get('/api/v1/gems/rspec-puppet-facts/total_ranking.json')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'rank', value: 'invalid'});
13 changes: 13 additions & 0 deletions service-tests/helpers/response-fixtures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const invalidJSON = function() {
return [
200,
'{{{{{invalid json}}',
{ 'Content-Type': 'application/json' }
];
};

module.exports = {
invalidJSON
};
2 changes: 2 additions & 0 deletions service-tests/helpers/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const isMetricOverTimePeriod = withRegex(/^[1-9][0-9]*[kMGTPEZY]?\/(year|month|4

const isIntegerPercentage = withRegex(/^[0-9]+%$/);
const isDecimalPercentage = withRegex(/^[0-9]+\.[0-9]*%$/);
const isPercentage = Joi.alternatives().try(isIntegerPercentage, isDecimalPercentage);

const isFileSize = withRegex(/^[0-9]*[.]?[0-9]+\s(B|kB|MB|GB|TB|PB|EB|ZB|YB)$/);

Expand All @@ -68,6 +69,7 @@ module.exports = {
isMetric,
isMetricOpenIssues,
isMetricOverTimePeriod,
isPercentage,
isIntegerPercentage,
isDecimalPercentage,
isFileSize,
Expand Down
3 changes: 2 additions & 1 deletion service-tests/homebrew.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const { isVPlusTripleDottedVersion } = require('./helpers/validators.js');
const { invalidJSON } = require('./helpers/response-fixtures');

const t = new ServiceTester({ id: 'homebrew', title: 'homebrew' });
module.exports = t;
Expand Down Expand Up @@ -36,6 +37,6 @@ t.create('homebrew (unexpected response)')
.get('/v/cake.json')
.intercept(nock => nock('http://formulae.brew.sh')
.get('/formula/cake/version')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'homebrew', value: 'invalid'});
3 changes: 2 additions & 1 deletion service-tests/itunes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');

const { isVPlusDottedVersionAtLeastOne } = require('./helpers/validators');
const { invalidJSON } = require('./helpers/response-fixtures');

const t = new ServiceTester({ id: 'itunes', title: 'iTunes' });
module.exports = t;
Expand Down Expand Up @@ -33,7 +34,7 @@ t.create('iTunes version (unexpected response)')
.get('/v/324684580.json')
.intercept(nock => nock('https://itunes.apple.com')
.get('/lookup?id=324684580')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'itunes app store', value: 'invalid'});

9 changes: 5 additions & 4 deletions service-tests/microbadger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const { isFileSize } = require('./helpers/validators')
const { isFileSize } = require('./helpers/validators');
const { invalidJSON } = require('./helpers/response-fixtures');

const t = new ServiceTester({ id: 'microbadger', title: 'MicroBadger' })
const t = new ServiceTester({ id: 'microbadger', title: 'MicroBadger' });
module.exports = t;

t.create('image size without a specified tag')
.get('/image-size/_/hello-world.json')
.get('/image-size/_/centos.json')
.expectJSONTypes(Joi.object().keys({
name: 'image size',
value: isFileSize
Expand Down Expand Up @@ -72,6 +73,6 @@ t.create('unexpected response')
.get('/image-size/_/hello-world.json')
.intercept(nock => nock('https://api.microbadger.com')
.get('/v1/images/library/hello-world')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({ name: 'image size', value: 'error' });
4 changes: 3 additions & 1 deletion service-tests/nexus.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const { invalidJSON } = require('./helpers/response-fixtures');

const t = new ServiceTester({ id: 'nexus', title: 'Nexus' });
module.exports = t;
Expand Down Expand Up @@ -68,5 +69,6 @@ t.create('json parsing error')
.intercept(nock => nock('https://repository.jboss.org')
.get('/nexus/service/local/lucene/search')
.query({g: 'jboss', a: 'jboss-client'})
.reply(200, 'this should be a valid json'))
.reply(invalidJSON)
)
.expectJSON({ name: 'nexus', value: 'invalid' });
58 changes: 54 additions & 4 deletions service-tests/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,66 @@

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const { isSemver } = require('./helpers/validators');
const { isMetric, isSemver } = require('./helpers/validators');
const colorscheme = require('../lib/colorscheme.json');
const { invalidJSON } = require('./helpers/response-fixtures');
const mapValues = require('lodash.mapvalues');

const t = new ServiceTester({ id: 'npm', title: 'NPM' });
module.exports = t;
const colorsB = mapValues(colorscheme, 'colorB');

t.create('total downloads of left-pad')
.get('/dt/left-pad.json?style=_shields_test')
.expectJSONTypes(Joi.object().keys({ name: 'downloads', value: isMetric, colorB: colorsB.brightgreen }));

t.create('total downloads of @cycle/core')
.get('/dt/@cycle/core.json')
.expectJSONTypes(Joi.object().keys({ name: 'downloads', value: isMetric }));

t.create('total downloads of package with zero downloads')
.get('/dt/package-no-downloads.json?style=_shields_test')
.intercept(nock => nock('https://api.npmjs.org')
.get('/downloads/range/1000-01-01:3000-01-01/package-no-downloads')
.reply(200, {
downloads: [{
downloads: 0,
day: '2018-01-01'
}]
}))
.expectJSON({ name: 'downloads', value: '0', colorB: colorsB.red });

t.create('exact total downloads value')
.get('/dt/exact-value.json')
.intercept(nock => nock('https://api.npmjs.org')
.get('/downloads/range/1000-01-01:3000-01-01/exact-value')
.reply(200, {
downloads: [{
downloads: 2,
day: '2018-01-01'
}, {
downloads: 3,
day: '2018-01-02'
}]
}))
.expectJSON({ name: 'downloads', value: '5' });

t.create('total downloads when network is off')
.get('/dt/@cycle/core.json?style=_shields_test')
.networkOff()
.expectJSON({ name: 'downloads', value: 'inaccessible' , colorB: colorsB.red });

t.create('total downloads when API returns an invalid JSON')
.get('/dt/invalid-json.json?style=_shields_test')
.intercept(nock => nock('https://api.npmjs.org')
.get('/downloads/range/1000-01-01:3000-01-01/invalid-json')
.reply(invalidJSON))
.expectJSON({ name: 'downloads', value: 'invalid', colorB: colorsB.lightgrey });

t.create('total downloads of unknown package')
.get('/dt/npm-api-does-not-have-this-package.json?style=_shields_test')
.expectJSON({ name: 'downloads', value: 'not found' , colorB: colorsB.lightgrey });

t.create('gets the package version of left-pad')
.get('/v/left-pad.json')
.expectJSONTypes(Joi.object().keys({ name: 'npm', value: isSemver }));
Expand Down Expand Up @@ -106,9 +158,7 @@ t.create('license when registry returns an invalid JSON')
.get('/l/invalid-json.json?style=_shields_test')
.intercept(nock => nock('https://registry.npmjs.org')
.get('/invalid-json/latest')
.reply(200, 'invalid json'), {
'Content-Type': 'application/json'
})
.reply(invalidJSON))
.expectJSON({ name: 'license', value: 'invalid', colorB: colorsB.lightgrey });

t.create('license when network is off')
Expand Down
3 changes: 2 additions & 1 deletion service-tests/requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const { invalidJSON } = require('./helpers/response-fixtures');

const isRequireStatus = Joi.string().regex(/^(up to date|outdated|insecure|unknown)$/);

Expand Down Expand Up @@ -36,6 +37,6 @@ t.create('requirements (unexpected response)')
.get('/github/celery/celery.json')
.intercept(nock => nock('https://requires.io/')
.get('/api/v1/status/github/celery/celery')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'requirements', value: 'invalid'});
3 changes: 2 additions & 1 deletion service-tests/shippable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const { invalidJSON } = require('./helpers/response-fixtures');

const isBuildStatus = Joi.string().regex(/^(waiting|queued|processing|success|skipped|unstable|timeout|cancelled|failed|stopped)$/);

Expand Down Expand Up @@ -40,7 +41,7 @@ t.create('build status (unexpected response)')
.get('/5444c5ecb904a4b21567b0ff.json')
.intercept(nock => nock('https://api.shippable.com/')
.get('/projects/5444c5ecb904a4b21567b0ff/branchRunStatus')
.reply(200, "{{{{{invalid json}}")
.reply(invalidJSON)
)
.expectJSON({name: 'build', value: 'invalid'});

Expand Down
Loading

0 comments on commit 17b4a70

Please sign in to comment.