mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-07-14 07:52:15 +00:00
Cleanup and more checks
This commit is contained in:
parent
e4f4227072
commit
f9f57a6b91
5 changed files with 46 additions and 109 deletions
|
|
@ -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')
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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']
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
@ -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
|
||||
Loading…
Reference in a new issue