From 422f5b82d4264d0e609b908ee36f18b8ddab2dcb Mon Sep 17 00:00:00 2001 From: Joe Hand Date: Fri, 16 Dec 2016 15:32:37 -0800 Subject: [PATCH] do auth server again, good practice (#39) --- auth-server.js | 29 ++----------------- lib/commands/auth/login.js | 2 +- lib/township.js | 7 ----- package.json | 3 +- tests/auth.js | 7 +++-- tests/helpers/auth-server.js | 54 +++++++++++++++++++++--------------- 6 files changed, 41 insertions(+), 61 deletions(-) diff --git a/auth-server.js b/auth-server.js index bb544c5..39fe26a 100644 --- a/auth-server.js +++ b/auth-server.js @@ -1,36 +1,13 @@ -var path = require('path') -var Server = require('dat.land/server') -var initDb = require('dat.land/server/database/init') -var rimraf = require('rimraf') +var createServer = require('./tests/helpers/auth-server') -var config = { - township: { - secret: 'very secret code', - db: path.join(__dirname, 'test-township.db') - }, - db: { - dialect: 'sqlite3', - connection: { filename: path.join(__dirname, 'test-sqlite.db') }, - useNullAsDefault: true - }, - whitelist: false, - port: process.env.PORT || 8888 -} - -initDb(config.db, function (err, db) { +createServer(process.env.PORT || 8888, function (err, server, closeServer) { if (err) throw err - const server = Server(config, db) - server.listen(config.port, function () { - console.log('listening', config.port) - }) process.on('exit', close) process.on('SIGINT', close) function close (cb) { - server.close(function () { - rimraf.sync(config.township.db) - rimraf.sync(config.db.connection.filename) + closeServer(function () { process.exit() }) } diff --git a/lib/commands/auth/login.js b/lib/commands/auth/login.js index 687c719..889b22c 100644 --- a/lib/commands/auth/login.js +++ b/lib/commands/auth/login.js @@ -36,7 +36,7 @@ function login (opts) { client.login({ email: user.email, password: user.password - }, function (err) { + }, function (err, resp, body) { if (err) ui.exitErr(err) console.log('Logged in successfully.') process.exit(0) diff --git a/lib/township.js b/lib/township.js index 488a56f..91f73da 100644 --- a/lib/township.js +++ b/lib/township.js @@ -12,13 +12,6 @@ module.exports = function (opts) { // xtend doesn't overwrite when key is present but undefined // If we want a default, make sure it's not going to passed as undefined } - if (!opts.server || opts['use-routes']) { - defaults.routes = { - register: '/auth/v1/register', - login: '/auth/v1/login', - updatePassword: '/auth/v1/updatepassword' - } - } var options = xtend(defaults, townshipOpts) return TownshipClient(options) } diff --git a/package.json b/package.json index 7b23c54..b89d1a7 100644 --- a/package.json +++ b/package.json @@ -30,11 +30,12 @@ "rimraf": "^2.5.4", "status-logger": "^3.0.0", "subcommand": "^2.0.4", - "township-client": "^1.2.1", + "township-client": "^1.3.1", "xtend": "^4.0.1" }, "devDependencies": { "appa": "^5.0.0", + "dat-land": "datproject/datfolder", "hypercore": "^4.15.1", "hyperdiscovery": "^1.0.1", "memdb": "^1.3.1", diff --git a/tests/auth.js b/tests/auth.js index c4f81e9..d807c69 100644 --- a/tests/auth.js +++ b/tests/auth.js @@ -9,13 +9,14 @@ var dat = path.resolve(path.join(__dirname, '..', 'bin', 'cli.js')) var baseTestDir = help.testFolder() var port = process.env.PORT || 3000 -var SERVER = 'http://localhost:' + port +var SERVER = 'http://localhost:' + port + '/api/v1' var config = path.join(__dirname, '.datrc-test') var opts = ' --server=' + SERVER + ' --config=' + config dat += opts -authServer(port, function (server) { +authServer(port, function (err, server, closeServer) { + if (err) throw err test('auth - whoami works when not logged in', function (t) { var cmd = dat + ' whoami ' var st = spawn(t, cmd, {cwd: baseTestDir}) @@ -94,7 +95,7 @@ authServer(port, function (server) { }) test.onFinish(function () { - server.close(function () { + closeServer(function () { fs.unlink(config, function () { // done! }) diff --git a/tests/helpers/auth-server.js b/tests/helpers/auth-server.js index a4274d5..c766176 100644 --- a/tests/helpers/auth-server.js +++ b/tests/helpers/auth-server.js @@ -1,33 +1,41 @@ -var memdb = require('memdb') -var http = require('http') -var createAppa = require('appa') -var township = require('township') +var path = require('path') +var Server = require('dat.land/server') +var initDb = require('dat.land/server/database/init') +var rimraf = require('rimraf') module.exports = createServer function createServer (port, cb) { - var app = createAppa({log: {level: 'silent'}}) - var db = memdb() - var ship = township({secret: 'test'}, db) + var config = { + township: { + secret: 'very secret code', + db: path.join(__dirname, '..', 'test-township.db') + }, + db: { + dialect: 'sqlite3', + connection: { filename: path.join(__dirname, '..', 'test-sqlite.db') }, + useNullAsDefault: true + }, + whitelist: false, + port: port || 8888 + } - app.on('/register', function (req, res, ctx) { - // appa provides `ctx` for us in the way we want out of the box - ship.register(req, res, ctx, function (err, code, data) { - if (err) return app.error(res, code, err.message) - app.send(res, code, data) - }) - }) + initDb(config.db, function (err, db) { + if (err) return cb(err) - app.on('/login', function (req, res, ctx) { - // appa provides `ctx` for us in the way we want out of the box - ship.login(req, res, ctx, function (err, code, data) { - if (err) return app.error(res, code, err.message) - app.send(res, code, data) + const server = Server(config, db) + server.listen(config.port, function () { + console.log('listening', config.port) }) - }) - var server = http.createServer(app) - server.listen(port, function () { - cb(server) + cb(null, server, close) + + function close (cb) { + server.close(function () { + rimraf.sync(config.township.db) + rimraf.sync(config.db.connection.filename) + process.exit() + }) + } }) }