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

Better unit tests #100

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 16 additions & 14 deletions bin/web.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-console */

var express = require('express');
var uuid = require('uuid');
var basicAuth = require('basic-auth');
@@ -18,15 +20,15 @@ if (process.env.ANALYTICS_TOKEN) {
}

var myNuts = nuts.Nuts({
repository: process.env.GITHUB_REPO,
token: process.env.GITHUB_TOKEN,
endpoint: process.env.GITHUB_ENDPOINT,
username: process.env.GITHUB_USERNAME,
password: process.env.GITHUB_PASSWORD,
timeout: process.env.VERSIONS_TIMEOUT,
cache: process.env.VERSIONS_CACHE,
repository: process.env.GITHUB_REPO,
token: process.env.GITHUB_TOKEN,
endpoint: process.env.GITHUB_ENDPOINT,
username: process.env.GITHUB_USERNAME,
password: process.env.GITHUB_PASSWORD,
timeout: process.env.VERSIONS_TIMEOUT,
cache: process.env.VERSIONS_CACHE,
refreshSecret: process.env.GITHUB_SECRET,
proxyAssets: !Boolean(process.env.DONT_PROXY_ASSETS)
proxyAssets: !process.env.DONT_PROXY_ASSETS
});

// Control access to API
@@ -35,28 +37,28 @@ myNuts.before('api', function(access, next) {

function unauthorized() {
next(new Error('Invalid username/password for API'));
};
}

var user = basicAuth(access.req);
if (!user || !user.name || !user.pass) {
return unauthorized();
};
}

if (user.name === apiAuth.username && user.pass === apiAuth.password) {
return next();
} else {
return unauthorized();
};
}
});

// Log download
myNuts.before('download', function(download, next) {
console.log('download', download.platform.filename, "for version", download.version.tag, "on channel", download.version.channel, "for", download.platform.type);
console.log('download', download.platform.filename, 'for version', download.version.tag, 'on channel', download.version.channel, 'for', download.platform.type);

next();
});
myNuts.after('download', function(download, next) {
console.log('downloaded', download.platform.filename, "for version", download.version.tag, "on channel", download.version.channel, "for", download.platform.type);
console.log('downloaded', download.platform.filename, 'for version', download.version.tag, 'on channel', download.version.channel, 'for', download.platform.type);

// Track on segment if enabled
if (analytics) {
@@ -92,7 +94,7 @@ app.use(myNuts.router);

// Error handling
app.use(function(req, res, next) {
res.status(404).send("Page not found");
res.status(404).send('Page not found');
});
app.use(function(err, req, res, next) {
var msg = err.message || err;
33 changes: 20 additions & 13 deletions lib/backends/github.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
var _ = require('lodash');
var Q = require('q');
var util = require('util');
var destroy = require('destroy');
var GitHub = require('octocat');
var request = require('request');
var Buffer = require('buffer').Buffer;
var githubWebhook = require('github-webhook-handler');

var Backend = require('./backend');


function GitHubBackend() {
var that = this;
Backend.apply(this, arguments);
@@ -17,19 +16,15 @@ function GitHubBackend() {
proxyAssets: true
});

if ((!this.opts.username || !this.opts.password) && (!this.opts.token)) {
throw new Error('GitHub backend require "username" and "token" options');
}

this.client = new GitHub({
token: this.opts.token,
token: this.opts.token,
endpoint: this.opts.endpoint,
username: this.opts.username,
password: this.opts.password
});

this.ghrepo = this.client.repo(this.opts.repository);
this.releases = this.memoize(this._releases);
this.releases = this.memoize(this.releases);

// GitHub webhook to refresh list of versions
this.webhookHandler = githubWebhook({
@@ -45,15 +40,24 @@ function GitHubBackend() {
}
util.inherits(GitHubBackend, Backend);

// List all releases for this repository
GitHubBackend.prototype._releases = function() {
/**
* List all releases for this repository
* @return {Promise<Array<Release>>}
*/
GitHubBackend.prototype.releases = function() {
return this.ghrepo.releases()
.then(function(page) {
return page.all();
});
};

// Return stream for an asset
/**
* Return stream for an asset
* @param {Asset} asset
* @param {Request} req
* @param {Response} res
* @return {Promise}?
*/
GitHubBackend.prototype.serveAsset = function(asset, req, res) {
if (!this.opts.proxyAssets) {
res.redirect(asset.raw.browser_download_url);
@@ -62,7 +66,11 @@ GitHubBackend.prototype.serveAsset = function(asset, req, res) {
}
};

// Return stream for an asset
/**
* Return stream for an asset
* @param {Asset} asset
* @return {Promise<Stream>}
*/
GitHubBackend.prototype.getAssetStream = function(asset) {
var headers = {
'User-Agent': 'nuts',
@@ -88,5 +96,4 @@ GitHubBackend.prototype.getAssetStream = function(asset) {
}));
};


module.exports = GitHubBackend;
32 changes: 29 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
var express = require('express');

var Nuts = require('./nuts');
var platforms = require('./utils/platforms');
var winReleases = require('./utils/win-releases');

/**
* Create an express application with Nuts binded to it.
* This is mostly used for unit testing.
*
* @param {Object} options
* @return {Express.Application} app
*/
function createApp(options) {
var app = express();
var nuts = Nuts(options);

app.use(nuts.router);

app.use(function(err, req, res, next) {
res.status(err.statusCode || 500);
res.send({
message: err.message
});
});

return app;
}

module.exports = {
Nuts: Nuts,
platforms: platforms,
winReleases: winReleases
Nuts: Nuts,
platforms: platforms,
winReleases: winReleases,
createApp: createApp
};
Loading