From f9f57a6b91ffb736f9b2ba16d8ae5aba8c3f7287 Mon Sep 17 00:00:00 2001 From: Daniel Saewitz Date: Tue, 26 Feb 2013 02:36:51 -0500 Subject: [PATCH] Cleanup and more checks --- server.js | 4 +- src/server/api.coffee | 26 +++++++++---- test/api.mocha.coffee | 26 ++++++++++++- test/request/local.coffee | 25 ------------- test/request/request.coffee | 74 ------------------------------------- 5 files changed, 46 insertions(+), 109 deletions(-) delete mode 100644 test/request/local.coffee delete mode 100644 test/request/request.coffee diff --git a/server.js b/server.js index 98b139b185..d7fc162cf7 100644 --- a/server.js +++ b/server.js @@ -19,7 +19,7 @@ process.env.SMTP_PASS = conf.get("SMTP_PASS"); process.env.SMTP_SERVICE = conf.get("SMTP_SERVICE"); process.env.STRIPE_API_KEY = conf.get("STRIPE_API_KEY"); process.env.STRIPE_PUB_KEY = conf.get("STRIPE_PUB_KEY"); - +/* process.on('uncaughtException', function (error) { function sendEmail(mailData) { @@ -55,7 +55,7 @@ process.on('uncaughtException', function (error) { }); console.log(error.stack); }); - +*/ require('coffee-script') // remove intermediate compilation requirement module.exports = server = require('./src/server') diff --git a/src/server/api.coffee b/src/server/api.coffee index e6aa77476d..88bf0cae77 100644 --- a/src/server/api.coffee +++ b/src/server/api.coffee @@ -15,7 +15,9 @@ NO_USER_FOUND = err: "No user found." # Every url added beneath router is prefaced by /api/v1 ### - v1 API. Requires user-id and apiToken, task-id, direction. Test with: + v1 API. Requires api-v1-user (user id) and api-v1-key (api key) headers, Test with: + $ cd node_modules/racer && npm install && cd ../.. + $ mocha test/api.mocha.coffee ### auth = (req, res, next) -> @@ -28,26 +30,36 @@ auth = (req, res, next) -> query.fetch (err, user) -> return res.json err: err if err - req.user = user.at(0) + user = user.at(0) + req.user = user + req.userObj = user.get() + return res.json 500, NO_USER_FOUND if !req.userObj || _.isEmpty(req.userObj) next() router.get '/status', (req, res) -> res.json status: 'up' router.get '/user', auth, (req, res) -> - self = req.user.get() - return res.json 500, NO_USER_FOUND if !self || _.isEmpty(self) + self = req.userObj + + delete self[val] for val in ['tasks', 'apiToken', 'flags', 'lastCron'] return res.json self +router.get '/task/:id', auth, (req, res) -> + task = req.user.get("tasks.#{req.params.id}") + console.log task + return res.json 500, err: "No task found." if !task || _.isEmpty(task) + + return res.json 200, task + router.post '/user/task', auth, (req, res) -> task = { title, text, type, value, note } = req.body return res.json 500, err: "type must be habit, todo, daily, or reward" unless /habit|todo|daily|reward/.test type return res.json 500, err: "must have a title" unless check(title).notEmpty() return res.json 500, err: "must have text" unless check(text).notEmpty() - self = req.user.get() - return res.json 500, NO_USER_FOUND if !self || _.isEmpty(self) + self = req.userObj value ||= 0 @@ -59,7 +71,7 @@ router.post '/user/task', auth, (req, res) -> return res.json 201, task router.get '/user/tasks', auth, (req, res) -> - self = req.user.get() + self = req.userObj return res.json 500, NO_USER_FOUND if !self || _.isEmpty(self) model = req.getModel() diff --git a/test/api.mocha.coffee b/test/api.mocha.coffee index d9d6e95a67..fc9a403e24 100644 --- a/test/api.mocha.coffee +++ b/test/api.mocha.coffee @@ -101,6 +101,7 @@ modificationsLookup = (direction, options = {}) -> ###### Specs ###### describe 'API', -> + describe 'Without token or user id', -> it '/api/v1/status', (done) -> @@ -124,6 +125,7 @@ describe 'API', -> currentUser = null user = null model = null + uid = null before -> #store.flush() @@ -141,6 +143,7 @@ describe 'API', -> type: 'habit' beforeEach -> + model = store.createModel() currentUser = user.get() it 'GET /api/v1/user', (done) -> @@ -152,9 +155,28 @@ describe 'API', -> expect(res.body.err).to.be undefined expect(res.statusCode).to.be 200 expect(res.body.id).not.to.be.empty() + model.set '_user', currentUser + ### + currentUser.tasks = [] + for type in ['habit','todo','daily','reward'] + model.refList "_#{type}List", "_user.tasks", "_user.#{type}Ids" + currentUser.tasks = currentUser.tasks.concat model.get("_#{type}List") + ### expect(res.body).to.eql(currentUser) done() + it 'GET /api/v1/task/:id', (done) -> + tid = _.values(currentUser.tasks)[0].id + request.post("#{baseURL}/task/#{tid}") + .set('Accept', 'application/json') + .set('X-API-User', currentUser.id) + .set('X-API-Key', currentUser.apiToken) + .end (res) -> + expect(res.body.err).to.be undefined + expect(res.statusCode).to.be 200 + expect(res.body).to.eql currentUser.tasks[tid] + done() + it 'POST /api/v1/user/task', (done) -> request.post("#{baseURL}/user/task") .set('Accept', 'application/json') @@ -166,7 +188,7 @@ describe 'API', -> expect(res.statusCode).to.be 201 expect(res.body.id).not.to.be.empty() # Ensure that user owns the newly created object - console.log _.size(user.get().tasks) + console.log 'test', _.size(user.get().tasks) expect(user.get().tasks[res.body.id]).to.be.an('object') done() @@ -180,6 +202,8 @@ describe 'API', -> expect(res.statusCode).to.be 200 currentUser = user.get() console.log _.size(currentUser.tasks) + console.log uid + console.log 'hellomate', model.get("users.#{uid}") model.ref '_user', user tasks = [] for type in ['habit','todo','daily','reward'] diff --git a/test/request/local.coffee b/test/request/local.coffee deleted file mode 100644 index 1431d12cd5..0000000000 --- a/test/request/local.coffee +++ /dev/null @@ -1,25 +0,0 @@ -Request = require './request' -qs = require 'querystring' - -makeUrl = (path, params) -> - if typeof params is "object" - params = qs.stringify(params) - else - params = '' - return "http://localhost:3000/#{path}?#{params}" - -# url, params (optional), callback -get = (obj, callback) -> - obj.params ||= {} - obj.path ||= '' - - requestUrl = makeUrl obj.path, obj.params - - request = new Request requestUrl - - request.done (res) -> - callback null, JSON.parse(res) - - request.fire() - -module.exports = { get } diff --git a/test/request/request.coffee b/test/request/request.coffee deleted file mode 100644 index 5eadd9178b..0000000000 --- a/test/request/request.coffee +++ /dev/null @@ -1,74 +0,0 @@ -http = require 'http' - -# ### Request -# -# The Request class is just a wrapper -# around the `http.request` function, -# providing a cleaner and more reusable -# interface. -# -# `constructor(options)` -# -# `options` can be anything `http.request` takes. -# -# Callbacks can be queued up by calling `request.done` -# and passing it a callback function to be invoked when the -# http request has completed -# -# Example: -# -# callback = (json) -> -# doSomethingWithJSON( json ) -# -# request = new Request('http://api.phish.net/api.js?') -# request.done (response) -> -# callback JSON.parse(response)[0] -# -# request.fire(data) # data refers to the post data -# -class Request - constructor: (@options) -> - @callbacks = {} - - fire: (data) -> - request = http.request(@options, @_handler) - request.on 'error', (err) => - for callback in @callbacks['fail'] - callback(err) - - # Send data with request - if data - request.write data - - request.end() - - this - - done: (callback) -> - push.call(this, 'done', callback) - - this - - fail: (callback) -> - push.call(this, 'fail', callback) - - this - - ########### - # PRIVATE # - ########### - - _handler: (response) => - data = '' - response.on 'data', (chunk) -> - data += chunk - response.on 'end', => - for callback in @callbacks['done'] - callback(data) - - - push = (type, callback) -> - @callbacks[type] ?= [] - @callbacks[type].push callback - -module.exports = Request