Skip to content

Commit

Permalink
Clean up service tests (#1127)
Browse files Browse the repository at this point in the history
- Consolidate regexes
- Use slimmer syntax where possible
- Fix bug caught by failing amo tests
  • Loading branch information
paulmelnikow authored Oct 9, 2017
1 parent 08524b6 commit c3636e7
Show file tree
Hide file tree
Showing 24 changed files with 250 additions and 344 deletions.
6 changes: 3 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6180,19 +6180,19 @@ cache(function(query_data, match, sendBadge, request) {
break;
case 'rating':
rating = parseInt(data.addon.rating, 10);
badgeData.text[0] = getLabel('downloads', query_data);
badgeData.text[0] = getLabel('rating', query_data);
badgeData.text[1] = rating + '/5';
badgeData.colorscheme = floorCountColor(rating, 2, 3, 4);
break;
case 'stars':
rating = parseInt(data.addon.rating, 10);
badgeData.text[0] = getLabel('downloads', query_data);
badgeData.text[0] = getLabel('stars', query_data);
badgeData.text[1] = starRating(rating);
badgeData.colorscheme = floorCountColor(rating, 2, 3, 4);
break;
case 'users':
var dailyUsers = parseInt(data.addon.daily_users[0], 10);
badgeData.text[0] = getLabel('downloads', query_data);
badgeData.text[0] = getLabel('users', query_data);
badgeData.text[1] = metric(dailyUsers);
badgeData.colorscheme = 'brightgreen';
break;
Expand Down
2 changes: 1 addition & 1 deletion service-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Here's what our first test looks like:
t.create('build status on default branch')
.get('/rust-lang/rust.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('build'),
name: 'build',
value: Joi.equal('failing', 'passing', 'unknown')
}));
```
Expand Down
27 changes: 13 additions & 14 deletions service-tests/mozilla.js → service-tests/amo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,50 @@

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const {
isMetric,
isStarRating,
isVPlusDottedVersionAtLeastOne,
} = require('./helpers/validators');

const t = new ServiceTester({ id: 'amo', title: 'Mozilla Addons' });
module.exports = t;

t.create('Downloads')
.get('/d/IndieGala-Helper.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('downloads'),
value: Joi.string().regex(/^\d+k$/)
}));
.expectJSONTypes(Joi.object().keys({ name: 'downloads', value: isMetric }));

t.create('Version')
.get('/v/IndieGala-Helper.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('mozilla add-on'),
value: Joi.string().regex(/^v\d+(\.\d+)?(\.\d+)?$/)
name: 'mozilla add-on',
value: isVPlusDottedVersionAtLeastOne
}));

t.create('Version - Custom label')
.get('/v/IndieGala-Helper.json?label=IndieGala Helper')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('IndieGala Helper'),
value: Joi.string().regex(/^v\d+(\.\d+)?(\.\d+)?$/)
name: 'IndieGala Helper',
value: isVPlusDottedVersionAtLeastOne
}));

t.create('Users')
.get('/users/IndieGala-Helper.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('users'),
name: 'users',
value: Joi.string().regex(/^\d+$/)
}));

t.create('Rating')
.get('/rating/IndieGala-Helper.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('rating'),
name: 'rating',
value: Joi.string().regex(/^\d\/\d$/)
}));

t.create('Stars')
.get('/stars/IndieGala-Helper.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('rating'),
value: Joi.string().regex(/^[\u2605\u2606]{5}$/)
}));
.expectJSONTypes(Joi.object().keys({ name: 'stars', value: isStarRating }));

t.create('Invalid addon')
.get('/d/invalid-name-of-addon.json')
Expand Down
16 changes: 4 additions & 12 deletions service-tests/ansible.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,22 @@

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

const t = new ServiceTester({ id: 'ansible', title: 'Ansible Galaxy' });
module.exports = t;

t.create('ansible role name')
.get('/role/14542.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('role'),
value: Joi.equal('openwisp.openwisp2')
}));
.expectJSON({ name: 'role', value: 'openwisp.openwisp2' });

t.create('ansible role download counts')
.get('/role/d/14542.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('role downloads'),
value: Joi.string().regex(/^[0-9]+[kMG]?$/)
}));
.expectJSONTypes(Joi.object().keys({ name: 'role downloads', value: isMetric }));

t.create('unkown role')
.get('/role/000.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('role'),
value: Joi.equal('not found')
}));
.expectJSON({ name: 'role', value: 'not found' });

t.create('connection error')
.get('/role/14542.json')
Expand Down
37 changes: 11 additions & 26 deletions service-tests/appveyor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,39 @@
const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');

const isAppveyorBuildState = Joi.equal('failing', 'passing', 'running', 'queued');
const isAppveyorTestTotals =
Joi.string().regex(/^(?:[0-9]+ (?:passed|skipped|failed)(?:, )?)+$/);

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

// Test AppVeyor build status badge
t.create('CI build status')
.get('/ci/gruntjs/grunt.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('build'),
value: Joi.equal('failing', 'passing', 'running', 'queued')
}));
.expectJSONTypes(Joi.object().keys({ name: 'build', value: isAppveyorBuildState }));

// Test AppVeyor branch build status badge
t.create('CI build status on master branch')
.get('/ci/gruntjs/grunt/master.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('build'),
value: Joi.equal('failing', 'passing', 'running', 'queued')
}));
.expectJSONTypes(Joi.object().keys({ name: 'build', value: isAppveyorBuildState }));

// Test AppVeyor build status badge on a non-existing project
t.create('CI 404')
.get('/ci/somerandomproject/thatdoesntexits.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('build'),
value: Joi.string().regex(/^project not found or access denied$/),
}));
.expectJSON({ name: 'build', value: 'project not found or access denied' });

// Test AppVeyor tests status badge
t.create('tests status')
.get('/tests/NZSmartie/coap-net-iu0to.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('tests'),
value: Joi.string().regex(/^(?:[0-9]+ (?:passed|skipped|failed)(?:, )?)+$/),
}));
.expectJSONTypes(Joi.object().keys({ name: 'tests', value: isAppveyorTestTotals }));

// Test AppVeyor branch tests status badge
t.create('tests status on master branch')
.get('/tests/NZSmartie/coap-net-iu0to/master.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('tests'),
value: Joi.string().regex(/^(?:[0-9]+ (?:passed|skipped|failed)(?:, )?)+$/),
}));
.expectJSONTypes(Joi.object().keys({ name: 'tests', value: isAppveyorTestTotals }));

// Test AppVeyor tests status badge for a non-existing project
t.create('tests 404')
.get('/tests/somerandomproject/thatdoesntexits.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('tests'),
value: Joi.string().regex(/^project not found or access denied$/),
}));

.get('/tests/somerandomproject/thatdoesntexits.json')
.expectJSON({ name: 'tests', value: 'project not found or access denied' });
83 changes: 34 additions & 49 deletions service-tests/bower.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,66 @@

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

const isBowerPrereleaseVersion = Joi.string().regex(/^v\d+(\.\d+)?(\.\d+)?(\-?\w)+?$/);

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

t.create('licence. eg. bower|MIT')
.get('/l/bootstrap.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('bower'),
value: Joi.equal('MIT')
}));
.get('/l/bootstrap.json')
.expectJSON({ name: 'bower', value: 'MIT' });

t.create('custom label for licence. eg. my licence|MIT')
.get('/l/bootstrap.json?label="my licence"')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('my licence'),
value: Joi.equal('MIT')
}));
.get('/l/bootstrap.json?label="my licence"')
.expectJSON({ name: 'my licence', value: 'MIT' });

t.create('version. eg. bower|v0.2.5')
.get('/v/bootstrap.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('bower'),
value: Joi.string().regex(/^v\d+(\.\d+)?(\.\d+)?$/)
}));
.get('/v/bootstrap.json')
.expectJSONTypes(Joi.object().keys({
name: 'bower',
value: isVPlusDottedVersionAtLeastOne
}));

t.create('custom label for version. eg. my verison|v0.2.5')
.get('/v/bootstrap.json?label="my verison"')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('my verison'),
value: Joi.string().regex(/^v\d+(\.\d+)?(\.\d+)?$/)
}));
.get('/v/bootstrap.json?label="my version"')
.expectJSONTypes(Joi.object().keys({
name: 'my version',
value: isVPlusDottedVersionAtLeastOne
}));

t.create('pre version. eg. bower|v0.2.5-alpha-rc-pre')
.get('/vpre/bootstrap.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('bower'),
value: Joi.string().regex(/^v\d+(\.\d+)?(\.\d+)?(-?\w)+?$/)
}));
.get('/vpre/bootstrap.json')
.expectJSONTypes(Joi.object().keys({
name: 'bower',
value: isBowerPrereleaseVersion
}));

t.create('custom label for pre version. eg. pre verison|v0.2.5-alpha-rc-pre')
.get('/vpre/bootstrap.json?label="pre verison"')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('pre verison'),
value: Joi.string().regex(/^v\d+(\.\d+)?(\.\d+)?(-?\w)+?$/)
}));
.get('/vpre/bootstrap.json?label="pre verison"')
.expectJSONTypes(Joi.object().keys({
name: 'pre verison',
value: isBowerPrereleaseVersion
}));


t.create('Version for Invaild Package. eg. bower|invalid')
.get('/v/it-is-a-invalid-package-should-error.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('bower'),
value: Joi.equal('invalid')
}));
.get('/v/it-is-a-invalid-package-should-error.json')
.expectJSON({ name: 'bower', value: 'invalid' });

t.create('Pre Version for Invaild Package. eg. bower|invalid')
.get('/vpre/it-is-a-invalid-package-should-error.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('bower'),
value: Joi.equal('invalid')
}));
.get('/vpre/it-is-a-invalid-package-should-error.json')
.expectJSON({ name: 'bower', value: 'invalid' });

t.create('licence for Invaild Package. eg. bower|invalid')
.get('/l/it-is-a-invalid-package-should-error.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('bower'),
value: Joi.equal('invalid')
}));
.get('/l/it-is-a-invalid-package-should-error.json')
.expectJSON({ name: 'bower', value: 'invalid' });


t.create('Version label should be `no releases` if no offical version. eg. bower|no releases')
.get('/v/bootstrap.json')
.intercept(nock => nock('https://libraries.io')
.get('/api/bower/bootstrap')
.reply(200, { latest_stable_release: { name: null } })) //or just `{}`
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('bower'),
value: Joi.equal('no releases')
}));
.expectJSON({ name: 'bower', value: 'no releases' });
31 changes: 14 additions & 17 deletions service-tests/chrome-web-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,47 @@
const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');

const {
isVPlusDottedVersionAtLeastOne,
isStarRating,
isMetric
} = require('./helpers/validators');

const t = new ServiceTester({ id: 'chrome-web-store', title: 'Chrome Web Store' });
module.exports = t;

t.create('Downloads (now users)')
.get('/d/alhjnofcnnpeaphgeakdhkebafjcpeae.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('users'),
value: Joi.string().regex(/^\d+k?$/)
}));
.expectJSONTypes(Joi.object().keys({ name: 'users', value: isMetric }));

t.create('Users')
.get('/users/alhjnofcnnpeaphgeakdhkebafjcpeae.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('users'),
value: Joi.string().regex(/^\d+k?$/)
}));
.expectJSONTypes(Joi.object().keys({ name: 'users', value: isMetric }));

t.create('Version')
.get('/v/alhjnofcnnpeaphgeakdhkebafjcpeae.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('chrome web store'),
value: Joi.string().regex(/^v\d+(\.\d+)?(\.\d+)?$/)
name: 'chrome web store',
value: isVPlusDottedVersionAtLeastOne
}));

t.create('Version - Custom label')
.get('/v/alhjnofcnnpeaphgeakdhkebafjcpeae.json?label=IndieGala Helper')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('IndieGala Helper'),
value: Joi.string().regex(/^v\d+(\.\d+)?(\.\d+)?$/)
name: 'IndieGala Helper',
value: isVPlusDottedVersionAtLeastOne
}));

t.create('Rating')
.get('/rating/alhjnofcnnpeaphgeakdhkebafjcpeae.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('rating'),
name: 'rating',
value: Joi.string().regex(/^\d\.?\d+?\/5$/)
}));

t.create('Stars')
.get('/stars/alhjnofcnnpeaphgeakdhkebafjcpeae.json')
.expectJSONTypes(Joi.object().keys({
name: Joi.equal('rating'),
value: Joi.string().regex(/^[\u2605\u2606]{5}$/)
}));
.expectJSONTypes(Joi.object().keys({ name: 'rating', value: isStarRating }));

t.create('Invalid addon')
.get('/d/invalid-name-of-addon.json')
Expand Down
Loading

0 comments on commit c3636e7

Please sign in to comment.