Skip to content

Commit

Permalink
do auth server again, good practice (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
joehand authored Dec 16, 2016
1 parent c31dbc9 commit 422f5b8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 61 deletions.
29 changes: 3 additions & 26 deletions auth-server.js
Original file line number Diff line number Diff line change
@@ -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()
})
}
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/auth/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 0 additions & 7 deletions lib/township.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions tests/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -94,7 +95,7 @@ authServer(port, function (server) {
})

test.onFinish(function () {
server.close(function () {
closeServer(function () {
fs.unlink(config, function () {
// done!
})
Expand Down
54 changes: 31 additions & 23 deletions tests/helpers/auth-server.js
Original file line number Diff line number Diff line change
@@ -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()
})
}
})
}

0 comments on commit 422f5b8

Please sign in to comment.