From 709ffe8e396aef10191ae5ce9386db1d6333c7fd Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Wed, 25 Sep 2013 14:01:55 +0400 Subject: [PATCH] tests --- package.yaml | 3 --- test/lib/auth.js | 22 ----------------- test/lib/server.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++ test/run.js | 19 +++++++-------- 4 files changed, 67 insertions(+), 36 deletions(-) delete mode 100644 test/lib/auth.js create mode 100644 test/lib/server.js diff --git a/package.yaml b/package.yaml index 091a03114..96c226952 100644 --- a/package.yaml +++ b/package.yaml @@ -27,9 +27,6 @@ dependencies: minimatch: '*' through: '*' -devDependencies: - galaxy: '*' # and node 0.11.2+ for testing - keywords: # TODO - private - repository diff --git a/test/lib/auth.js b/test/lib/auth.js deleted file mode 100644 index f557f799d..000000000 --- a/test/lib/auth.js +++ /dev/null @@ -1,22 +0,0 @@ - -var request = require('request'); -var assert = require('assert'); - -module.exports.auth = function(user, pass, cb) { - request({ - url: 'http://localhost:55550/-/user/org.couchdb.user:'+escape(user)+'/-rev/undefined', - method: 'PUT', - headers: { - accept: 'application/json', - 'user-agent': 'node/v0.10.8 linux x64', - authorization: 'Basic '+(new Buffer(user+':'+pass)).toString('base64'), - }, - json: { - content: "doesn't matter, 'cause sinopia uses info from Authorization header anywayz", - } - }, function(req, res, body) { - assert.notEqual(body.ok.indexOf('"'+user+'"'), -1); - cb(); - }); -} - diff --git a/test/lib/server.js b/test/lib/server.js new file mode 100644 index 000000000..0d543b92f --- /dev/null +++ b/test/lib/server.js @@ -0,0 +1,59 @@ +var request = require('request'); +var assert = require('assert'); + +function Server(url) { + if (!(this instanceof Server)) return new Server(url); + this.url = url.replace(/\/$/, ''); + this.userAgent = 'node/v0.10.8 linux x64'; +} + +Server.prototype.request = function(options, cb) { + options.headers = options.headers || {}; + + return request({ + url: this.url + options.uri, + method: options.method || 'GET', + headers: { + accept: options.headers.accept || 'application/json', + 'user-agent': options.headers['user-agent'] || this.userAgent, + authorization: this.auth, + }, + json: options.json + }, cb); +} + +Server.prototype.auth = function(user, pass, cb) { + this.auth = 'Basic '+(new Buffer(user+':'+pass)).toString('base64'); + this.request({ + uri: '/-/user/org.couchdb.user:'+escape(user)+'/-rev/undefined', + method: 'PUT', + json: { + content: "doesn't matter, 'cause sinopia uses info from Authorization header anywayz", + } + }, function(req, res, body) { + assert.notEqual(body.ok.indexOf('"'+user+'"'), -1); + cb(); + }); +} + +Server.prototype.get_package = function(name, cb) { + request({ + uri: '/'+name, + method: 'GET', + }, function(req, res, body) { + cb(body); + }); +} + +Server.prototype.put_package = function(name, data, cb) { + request({ + uri: '/'+name, + method: 'PUT', + json: data, + }, function(req, res, body) { + cb(body); + }); +} + +module.exports = Server; + diff --git a/test/run.js b/test/run.js index f2a80550e..6f62fa401 100755 --- a/test/run.js +++ b/test/run.js @@ -1,17 +1,14 @@ -#!/usr/local/bin/node --harmony +#!/usr/bin/env node -var galaxy = require('galaxy'); -var Auth = galaxy.star(require('./lib/auth')); - -var sleep = galaxy.star(function(ms, cb) { - setTimeout(cb, ms); -}); +var Server = require('./lib/server'); process.argv = ['node', 'sinopia', '-c', './config.yaml']; require('../bin/sinopia'); -galaxy.unstar(function*() { - sleep(1000); - yield Auth.auth('test', 'test'); -})(); +setTimeout(function() { + var server = new Server('http://localhost:55550/'); + server.auth('test', 'test', function() { + console.log('ok'); + }); +}, 1000);