Cleanup and more checks
This commit is contained in:
+25
-1
@@ -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
|
||||
Reference in New Issue
Block a user