Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chocolatey powershellgallery] add service tests for NuGet v2 services #1487

Merged
merged 5 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few lines below there is a try-catch block. Catch block does not have tests. But in my opinion try-catch is unnecessary and can be removed. What do you think about it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah the JSON.parse() error is caught in getNugetPackage() so I've removed the others

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"version" and "pre version" badge can have different colors (yellow, orange or blue). Would you like to test these cases? Color value can be check using this approach

.get('/license/badges/badger.json?style=_shields_test')
.expectJSON({ name: 'license', value: 'missing', colorB: colorsB.red });

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had not realised that it we had an endpoint to test the colours but I now see you added this in #1190 - this would probably be helpful in some other tests.

I think it would also be helpful to add an example to the testing docs to explain this feature for other contributors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. I will try to add such example.


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