Skip to content

Commit

Permalink
[chocolatey powershellgallery] add service tests for NuGet v2 services (
Browse files Browse the repository at this point in the history
#1487)

* add service tests for NuGet v2 services

* fixup - test the correct endpoint

* remove superfluous try/catch blocks

* test the colors too

* use invalid json fixture
  • Loading branch information
chris48s authored and RedSparr0w committed Feb 13, 2018
1 parent 143ec95 commit 581546d
Show file tree
Hide file tree
Showing 5 changed files with 404 additions and 44 deletions.
73 changes: 29 additions & 44 deletions lib/nuget-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function mapNugetFeedv2({ camp, cache }, pattern, offset, getInfo) {
{ headers: { 'Accept': 'application/atom+json,application/json' } },
function(err, res, buffer) {
if (err != null) {
done(err);
done(new Error('inaccessible'));
return;
}

Expand All @@ -30,13 +30,13 @@ function mapNugetFeedv2({ camp, cache }, pattern, offset, getInfo) {
if (includePre === null) {
getNugetPackage(apiUrl, id, true, request, done);
} else {
done(new Error('Package not found in feed'));
done(new Error('not found'));
}
} else {
done(null, result);
}
} catch (e) {
done(e);
} catch (e) {
done(new Error('invalid'));
}
});
}
Expand All @@ -51,25 +51,20 @@ function mapNugetFeedv2({ camp, cache }, pattern, offset, getInfo) {
const badgeData = getBadgeData(site, data);
getNugetPackage(apiUrl, repo, null, request, function(err, data) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
badgeData.text[1] = err.message;
sendBadge(format, badgeData);
return;
}
try {
const version = data.NormalizedVersion || data.Version;
badgeData.text[1] = 'v' + version;
if (version.indexOf('-') !== -1) {
badgeData.colorscheme = 'yellow';
} else if (version[0] === '0') {
badgeData.colorscheme = 'orange';
} else {
badgeData.colorscheme = 'blue';
}
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
const version = data.NormalizedVersion || data.Version;
badgeData.text[1] = 'v' + version;
if (version.indexOf('-') !== -1) {
badgeData.colorscheme = 'yellow';
} else if (version[0] === '0') {
badgeData.colorscheme = 'orange';
} else {
badgeData.colorscheme = 'blue';
}
sendBadge(format, badgeData);
});
}));

Expand All @@ -83,25 +78,20 @@ function mapNugetFeedv2({ camp, cache }, pattern, offset, getInfo) {
const badgeData = getBadgeData(site, data);
getNugetPackage(apiUrl, repo, true, request, function(err, data) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
badgeData.text[1] = err.message;
sendBadge(format, badgeData);
return;
}
try {
const version = data.NormalizedVersion || data.Version;
badgeData.text[1] = 'v' + version;
if (version.indexOf('-') !== -1) {
badgeData.colorscheme = 'yellow';
} else if (version[0] === '0') {
badgeData.colorscheme = 'orange';
} else {
badgeData.colorscheme = 'blue';
}
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
const version = data.NormalizedVersion || data.Version;
badgeData.text[1] = 'v' + version;
if (version.indexOf('-') !== -1) {
badgeData.colorscheme = 'yellow';
} else if (version[0] === '0') {
badgeData.colorscheme = 'orange';
} else {
badgeData.colorscheme = 'blue';
}
sendBadge(format, badgeData);
});
}));

Expand All @@ -115,19 +105,14 @@ function mapNugetFeedv2({ camp, cache }, pattern, offset, getInfo) {
const badgeData = getBadgeData(site, data);
getNugetPackage(apiUrl, repo, null, request, function(err, data) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
badgeData.text[1] = err.message;
sendBadge(format, badgeData);
return;
}
try {
const downloads = data.DownloadCount;
badgeData.text[1] = metric(downloads);
badgeData.colorscheme = downloadCountColor(downloads);
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
const downloads = data.DownloadCount;
badgeData.text[1] = metric(downloads);
badgeData.colorscheme = downloadCountColor(downloads);
sendBadge(format, badgeData);
});
}));
}
Expand Down
171 changes: 171 additions & 0 deletions service-tests/chocolatey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
'use strict';

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const {
isMetric,
isVPlusDottedVersionNClauses
} = require('./helpers/validators');
const colorscheme = require('../lib/colorscheme.json');
const {
versionJsonWithDash,
versionJsonFirstCharZero,
versionJsonFirstCharNotZero
} = require('./helpers/nuget-fixtures.js');
const { invalidJSON } = require('./helpers/response-fixtures');

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


// downloads

t.create('total downloads (valid)')
.get('/dt/scriptcs.json')
.expectJSONTypes(Joi.object().keys({
name: 'chocolatey',
value: isMetric,
}));

t.create('total downloads (not found)')
.get('/dt/not-a-real-package.json')
.expectJSON({name: 'chocolatey', value: 'not found'});

t.create('total downloads (connection error)')
.get('/dt/scriptcs.json')
.networkOff()
.expectJSON({name: 'chocolatey', value: 'inaccessible'});

t.create('total downloads (unexpected response)')
.get('/dt/scriptcs.json')
.intercept(nock => nock('https://www.chocolatey.org')
.get("/api/v2/Packages()?$filter=Id%20eq%20%27scriptcs%27%20and%20IsLatestVersion%20eq%20true")
.reply(invalidJSON)
)
.expectJSON({name: 'chocolatey', value: 'invalid'});


// version

t.create('version (valid)')
.get('/v/scriptcs.json')
.expectJSONTypes(Joi.object().keys({
name: 'chocolatey',
value: isVPlusDottedVersionNClauses,
}));

t.create('version (mocked, yellow badge)')
.get('/v/scriptcs.json?style=_shields_test')
.intercept(nock => nock('https://www.chocolatey.org')
.get("/api/v2/Packages()?$filter=Id%20eq%20%27scriptcs%27%20and%20IsLatestVersion%20eq%20true")
.reply(200, versionJsonWithDash)
)
.expectJSON({
name: 'chocolatey',
value: 'v1.2-beta',
colorB: colorscheme.yellow.colorB
});

t.create('version (mocked, orange badge)')
.get('/v/scriptcs.json?style=_shields_test')
.intercept(nock => nock('https://www.chocolatey.org')
.get("/api/v2/Packages()?$filter=Id%20eq%20%27scriptcs%27%20and%20IsLatestVersion%20eq%20true")
.reply(200, versionJsonFirstCharZero)
)
.expectJSON({
name: 'chocolatey',
value: 'v0.35',
colorB: colorscheme.orange.colorB
});

t.create('version (mocked, blue badge)')
.get('/v/scriptcs.json?style=_shields_test')
.intercept(nock => nock('https://www.chocolatey.org')
.get("/api/v2/Packages()?$filter=Id%20eq%20%27scriptcs%27%20and%20IsLatestVersion%20eq%20true")
.reply(200, versionJsonFirstCharNotZero)
)
.expectJSON({
name: 'chocolatey',
value: 'v1.2.7',
colorB: colorscheme.blue.colorB
});

t.create('version (not found)')
.get('/v/not-a-real-package.json')
.expectJSON({name: 'chocolatey', value: 'not found'});

t.create('version (connection error)')
.get('/v/scriptcs.json')
.networkOff()
.expectJSON({name: 'chocolatey', value: 'inaccessible'});

t.create('version (unexpected response)')
.get('/v/scriptcs.json')
.intercept(nock => nock('https://www.chocolatey.org')
.get("/api/v2/Packages()?$filter=Id%20eq%20%27scriptcs%27%20and%20IsLatestVersion%20eq%20true")
.reply(invalidJSON)
)
.expectJSON({name: 'chocolatey', value: 'invalid'});


// version (pre)

t.create('version (pre) (valid)')
.get('/vpre/scriptcs.json')
.expectJSONTypes(Joi.object().keys({
name: 'chocolatey',
value: isVPlusDottedVersionNClauses,
}));

t.create('version (pre) (mocked, yellow badge)')
.get('/vpre/scriptcs.json?style=_shields_test')
.intercept(nock => nock('https://www.chocolatey.org')
.get("/api/v2/Packages()?$filter=Id%20eq%20%27scriptcs%27%20and%20IsAbsoluteLatestVersion%20eq%20true")
.reply(200, versionJsonWithDash)
)
.expectJSON({
name: 'chocolatey',
value: 'v1.2-beta',
colorB: colorscheme.yellow.colorB
});

t.create('version (pre) (mocked, orange badge)')
.get('/vpre/scriptcs.json?style=_shields_test')
.intercept(nock => nock('https://www.chocolatey.org')
.get("/api/v2/Packages()?$filter=Id%20eq%20%27scriptcs%27%20and%20IsAbsoluteLatestVersion%20eq%20true")
.reply(200, versionJsonFirstCharZero)
)
.expectJSON({
name: 'chocolatey',
value: 'v0.35',
colorB: colorscheme.orange.colorB
});

t.create('version (pre) (mocked, blue badge)')
.get('/vpre/scriptcs.json?style=_shields_test')
.intercept(nock => nock('https://www.chocolatey.org')
.get("/api/v2/Packages()?$filter=Id%20eq%20%27scriptcs%27%20and%20IsAbsoluteLatestVersion%20eq%20true")
.reply(200, versionJsonFirstCharNotZero)
)
.expectJSON({
name: 'chocolatey',
value: 'v1.2.7',
colorB: colorscheme.blue.colorB
});

t.create('version (pre) (not found)')
.get('/vpre/not-a-real-package.json')
.expectJSON({name: 'chocolatey', value: 'not found'});

t.create('version (pre) (connection error)')
.get('/vpre/scriptcs.json')
.networkOff()
.expectJSON({name: 'chocolatey', value: 'inaccessible'});

t.create('version (pre) (unexpected response)')
.get('/vpre/scriptcs.json')
.intercept(nock => nock('https://www.chocolatey.org')
.get("/api/v2/Packages()?$filter=Id%20eq%20%27scriptcs%27%20and%20IsAbsoluteLatestVersion%20eq%20true")
.reply(invalidJSON)
)
.expectJSON({name: 'chocolatey', value: 'invalid'});
29 changes: 29 additions & 0 deletions service-tests/helpers/nuget-fixtures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const versionJsonWithDash = JSON.stringify({
d: {
results: [
{ NormalizedVersion: '1.2-beta' }
]
}
});
const versionJsonFirstCharZero = JSON.stringify({
d: {
results: [
{ NormalizedVersion: '0.35' }
]
}
});
const versionJsonFirstCharNotZero = JSON.stringify({
d: {
results: [
{ NormalizedVersion: '1.2.7' }
]
}
});

module.exports = {
versionJsonWithDash,
versionJsonFirstCharZero,
versionJsonFirstCharNotZero
};
4 changes: 4 additions & 0 deletions service-tests/helpers/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const isVPlusTripleDottedVersion = withRegex(/^v[0-9]+.[0-9]+.[0-9]+$/);

const isVPlusDottedVersionAtLeastOne = withRegex(/^v\d+(\.\d+)?(\.\d+)?$/);

// matches a version number with N 'clauses' e.g: v1.2 or v1.22.7.392 are valid
const isVPlusDottedVersionNClauses = withRegex(/^v\d+(\.\d+)*$/);

// Simple regex for test Composer versions rule
// https://getcomposer.org/doc/articles/versions.md
// Examples:
Expand Down Expand Up @@ -63,6 +66,7 @@ module.exports = {
isSemver,
isVPlusTripleDottedVersion,
isVPlusDottedVersionAtLeastOne,
isVPlusDottedVersionNClauses,
isComposerVersion,
isPhpVersionReduction,
isStarRating,
Expand Down
Loading

0 comments on commit 581546d

Please sign in to comment.