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

Models+api #14

Merged
merged 2 commits into from Oct 30, 2014
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -1,4 +1,3 @@
var level = require('level')
var jsonBody = require("body/json")

module.exports = EMail
@@ -10,10 +9,7 @@ function EMail(models) {
EMail.prototype.create = function (req, res, opts, cb) {
var self = this
jsonBody(req, res, function (err, body) {
if (err) {
res.statusCode = 500
return res.end("NO U")
}
if (err) throw err
self.models.users.create({
handle: body.handle,
password: body.password,
@@ -27,10 +23,7 @@ EMail.prototype.create = function (req, res, opts, cb) {

EMail.prototype.login = function (req, res, opts, cb) {
jsonBody(req, res, function (err, body) {
if (err) {
res.statusCode = 500
return res.end("NO U")
}
if (err) throw err
})
}

@@ -14,7 +14,7 @@ module.exports = function(models, overrides) {
callbackURI: '/auth/callback',
scope: 'user' // optional, default scope is set to user
}

var gh = githubOAuth(extend({}, options, overrides))

gh.on('error', function(err) {
@@ -35,7 +35,7 @@ module.exports = function(models, overrides) {
} else {
request(params, callback)
}

function callback(err, response, body) {
debug('token verification response', {status: response.statusCode, body: body})
// TODO don't throw
@@ -44,7 +44,7 @@ module.exports = function(models, overrides) {
handle: body.login,
password: token.access_token,
email: body.email,
github: {
data: {
token: token.access_token,
account: body
}
@@ -54,6 +54,6 @@ module.exports = function(models, overrides) {
})
}
})

return gh
}
@@ -6,10 +6,10 @@ var githubProvider = require('./github.js')
module.exports = function(models) {
var provider = githubProvider(models)
var emailProvider = new EMailProvider(models)

// also make the email provider accessible to the consumer (TODO restructure this)
provider.emailProvider = emailProvider

return {
create: emailProvider.create.bind(emailProvider),
login: function(req, res, opts) {
@@ -1,17 +1,40 @@
var Models = require('level-orm')
var level = require('level')
var RestModels = require('level-restful')
var util = require('util')
var bcrypt = require('bcrypt')
var debug = require('debug')('users')


module.exports = Users

function Users(db) {
// users is the sublevel name to user
// handle is the primary key to user for insertion
Models.call(this, { db: db }, 'users', 'handle');
fields = [
{
'name': 'handle',
'type': 'string'
},
{
'name': 'password',
'type': 'string'
},
{
'name': 'email',
'type': 'string',
'optional': true
},
{
'name': 'data',
'type': 'object',
'optional': true
}
]
opts = {
rest: false
}
RestModels.call(this, db, 'users', 'handle', fields, opts);
}
util.inherits(Users, Models);
util.inherits(Users, RestModels);

Users.prototype.create = function(data, cb, insecure) {
// Creates a user given some data
@@ -25,7 +48,7 @@ Users.prototype.create = function(data, cb, insecure) {
// This is for benchmarking without bcrypt hit
// DO NOT USE FOR ANY OTHER PURPOSE
var self = this
debug("creating user", data)
debug("creating user", data);

if(!data['handle'] || !data['password']) {
return cb("can not create user without handle and password in data", false)
@@ -5,24 +5,29 @@ var extend = require('extend')
var Router = require('routes-router')
var jsonBody = require("body")
var debug = require('debug')('server')
var concat = require('concat-stream')
require('string.prototype.startswith')

var auth = require(path.join(__dirname, 'auth', 'index.js'))
var defaults = require(path.join(__dirname, 'defaults.js'))
var createModels = require(path.join(__dirname, 'models.js'))
var auth = require('./auth/index.js')
var defaults = require('./defaults.js')
var createModels = require('./models.js')

module.exports = Server

function Server(overrides) {
// allow either new Server() or just Server()
if (!(this instanceof Server)) return new Server(overrides)
this.options = extend({}, defaults, overrides)

this.models = createModels()
this.router = this.createRoutes()
this.server = http.createServer(this.router)
var self = this

if (!(self instanceof Server)) return new Server(overrides)
self.options = extend({}, defaults, overrides)

self.models = createModels()
self.router = self.createRoutes()
self.server = http.createServer(self.router)
}

Server.prototype.createRoutes = function() {

var router = Router({
errorHandler: function (req, res) {
res.statusCode = 500
@@ -31,18 +36,28 @@ Server.prototype.createRoutes = function() {
notFound: function (req, res) {
res.statusCode = 404
res.end("oh noes")
},
tearDown: function (req, res) {

}
})

router.addRoute('/', this.index)

// Authentication
var provider = auth(this.models)
router.addRoute('/auth/create/', {POST: provider.create})
router.addRoute('/auth/login/', provider.login)
router.addRoute('/auth/callback', provider.callback)
router.addRoute('/auth/logout/', provider.logout)

// Wire up API endpoints
var self = this
router.addRoute('/api/:model/:id?', function(req, res, opts) {
var id = opts.params.id || ''
var model = opts.params.model
self.models[model].dispatch(req, res, id)
})

return router
}

@@ -1,5 +1,9 @@
var level = require('level')
var RestModels = require('level-restful')
var bytewise = require('bytewise/hex')
var util = require('util')
var debug = require('debug')('models')
var timestamp = require('monotonic-timestamp')

var defaults = require('./defaults.js')
var Users = require('./auth/users.js')
@@ -8,8 +12,44 @@ var Users = require('./auth/users.js')
module.exports = function() {
var db = level(defaults['DAT_REGISTRY_DB'],
{ keyEncoding: bytewise, valueEncoding: 'json' })

return {
users: new Users(db)
db: db,
users: new Users(db),
metadat: new MetaDat(db)
}
}
}

function MetaDat(db) {
// MetaDat is the metadata for a particular dat instance.
// id is the primary key, auto incremented by timestamp
fields = [
{
'name': 'owner_id',
'type': 'number'
},
{
'name': 'name',
'type': 'string'
},
{
'name': 'url',
'type': 'string'
},
{
'name': 'schema',
'type': 'string',
'optional': true
},
{
'name': 'license',
'type': 'string',
'optional': true,
'default': 'BSD'
}
]
RestModels.call(this, db, 'metadat', 'id', fields, opts);
}

util.inherits(MetaDat, RestModels);
MetaDat.prototype.keyfn = timestamp
@@ -1,24 +1,61 @@
var path = require('path')
var debug = require('debug')('dat.test-common')
var debug = require('debug')('test-common')
var st = require("st")
var request = require('request').defaults({json: true})
var rimraf = require('rimraf')

var Server = require('../../api')
var defaults = require('../../api/defaults.js')

module.exports = function() {
var common = {}


common.testGET = function (t, path, data, cb) {
this.getRegistry(t, function(err, api, done) {
params = {
method: 'GET',
uri: 'http://localhost:' + api.options.PORT + path
}
debug('requesting', params)
request(params, function get(err, res, json) {
cb(err, api, res, json, done)
})
})
}

common.testPOST = function (t, path, data, cb) {
this.getRegistry(t, function(err, api, done) {
params = {
method: 'POST',
uri: 'http://localhost:' + api.options.PORT + path,
json: data,
'content-type': 'application/json'
}
debug('requesting', params)
request(params, function get(err, res, json) {
cb(err, api, res, json, done)
})
})
}

common.getRegistry = function (t, cb) {


var dbPath = defaults.DAT_REGISTRY_DB
rimraf.sync(dbPath);

var api = Server()
var port = api.options.PORT

api.server.listen(port, function() {
console.log('listening on port', port)
cb(null, api, done)
})

function done() {
api.server.close()
api.models.db.close()
t.end()
}

}
@@ -6,10 +6,16 @@ function test(name, testFunction) {
return tape(common.testPrefix + name, testFunction)
}

var tests = [
require(path.join(__dirname, 'tests', 'authTests.js')),
var testFiles = [
'authTests.js',
'metadatTests.js'
]

var tests = []
for (i in testFiles) {
tests.push(require(path.join(__dirname, 'tests', testFiles[i])))
}

// var finish = require(path.join(__dirname, 'tests', 'finish.js'))

var specificTestFile = process.argv[2]
@@ -1,37 +1,3 @@
var request = require('request').defaults({json: true})
var level = require('level')

var url = 'http://localhost:5000'

module.exports.createUser = function(test, common) {
test('creates a user via POST', function(t) {
var testUser = {
'handle': 'testuser',
'password': 'password123',
'email': 'testuser@email.com'
}
common.getRegistry(test, function(err, api, done) {
params = {
method: 'POST',
uri: url + '/auth/create/',
json: testUser
}
request(params, function get(err, res, json) {
t.ifError(err)
t.equal(res.statusCode, 200)
t.equal(json.handle, testUser.handle)
api.models.users.get(json.handle, function(err, user) {
t.ifError(err)
t.equal(user.handle, testUser.handle)
t.equal(user.email, testUser.email)
t.end()
done()
}, true)
})
})
})
}

module.exports.loginUser = function(test, common) {
test('login a user', function(t) {
var testData = {
@@ -43,6 +9,5 @@ module.exports.loginUser = function(test, common) {
}

module.exports.all = function(test, common) {
module.exports.createUser(test, common);
module.exports.loginUser(test, common);
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.