Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
got todos endpoints mostly done, ready to work on front-end I think
- Loading branch information
Warner Onstine
committed
Jan 13, 2015
1 parent
a8ec1ce
commit dc0e0d2
Showing
7 changed files
with
325 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,96 @@ | |||
var UsersController = function(){ | |||
|
|||
var | |||
settings = require("../config/settings"), | |||
mongoose = require("mongoose"), | |||
async = require('async'), | |||
restify = require("restify"), | |||
restifyErrors = require("../config/errors")(restify), | |||
_ = require("lodash"), | |||
Todo = require("../models/todo"); | |||
|
|||
var _add = function(req, res, next) { | |||
var | |||
_user = req.user; | |||
|
|||
var todo = new Todo.Model(req.params); | |||
todo.user = _user._id | |||
todo.save(function(err, saved){ | |||
if(err && err.errors) { | |||
return next(new restifyErrors.InvalidInputError(err.errors)); | |||
} | |||
else if(err) return next(err); | |||
return res.send(saved); | |||
}); | |||
|
|||
}; | |||
|
|||
var _list = function(req, res, next) { | |||
var | |||
_user = req.user; | |||
Todo.Model | |||
.find({ | |||
user: _user._id, | |||
completed:false | |||
}) | |||
.lean() | |||
.exec(function(err, todos){ | |||
if(err) return next(err); | |||
res.send(todos); | |||
}); | |||
}; | |||
|
|||
var _get = function(req, res, next) { | |||
var | |||
_user = req.user, | |||
id = req.params.id; | |||
|
|||
Todo.Model | |||
.findOne({ | |||
_id:id, | |||
user:_user._id | |||
}) | |||
.lean() | |||
.exec(function(err, todo){ | |||
if(err) return next(err); | |||
if(!todo) { | |||
return next(new restify.ResourceNotFoundError("Todo not found")); | |||
} | |||
res.send(todo); | |||
}); | |||
}; | |||
|
|||
var _update = function(req, res, next) { | |||
var | |||
_user = req.user, | |||
id = req.params.id; | |||
|
|||
Todo.Model | |||
.findOne({ | |||
_id:id, | |||
user:_user._id | |||
}) | |||
.exec(function(err, todo){ | |||
if(err) return next(err); | |||
if(!todo) { | |||
return next(new restify.ResourceNotFoundError("Todo not found")); | |||
} | |||
console.log("params %j", req.params); | |||
todo = _.assign(todo, req.params); | |||
console.log("modified todo %j", todo); | |||
todo.save(function(err, saved){ | |||
if(err) next(err); | |||
return res.send(saved); | |||
}); | |||
}); | |||
}; | |||
|
|||
return { | |||
add: _add, | |||
list: _list, | |||
get: _get, | |||
update:_update | |||
}; | |||
}(); | |||
|
|||
module.exports = UsersController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
module.exports = [ | |||
{ | |||
text: "Todo 1", | |||
notes: "Nothing to speak of" | |||
}, | |||
{ | |||
text: "Todo 2", | |||
notes: "Nothing to speak of two" | |||
} | |||
|
|||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,194 @@ | |||
var | |||
request = require("supertest"), | |||
should = require("should"), | |||
mongoose = require("mongoose"), | |||
conf = require("../../config/settings"), | |||
User = require("../../models/user").Model, | |||
Todo = require("../../models/todo").Model; | |||
|
|||
function logUserIn(email, done) { | |||
User | |||
.findOne({ | |||
email: email | |||
}) | |||
.exec(function(err, usr){ | |||
if(err) throw err; | |||
|
|||
var data = { | |||
username: usr.email, | |||
password: "password", | |||
grant_type: "password" | |||
}; | |||
|
|||
request(server) | |||
.post("/token") | |||
.send(data) | |||
.auth("foo","bar") | |||
.end(function(err, res){ | |||
if(err) throw err; | |||
return done(err,{ | |||
user: usr, | |||
token: res.body | |||
}); | |||
}); | |||
}); | |||
} | |||
|
|||
describe("Todos", function(){ | |||
var todo_to_change; | |||
|
|||
describe("POST /todos", function(){ | |||
var | |||
user, | |||
user_token; | |||
|
|||
before(function(done){ | |||
this.timeout(10000); | |||
var emit = function(){ | |||
if ( | |||
!user || | |||
!user_token | |||
) return; | |||
return done(); | |||
}; | |||
|
|||
|
|||
logUserIn("warner2@test.com", function(err, data){ | |||
if(err) throw err; | |||
user = data.user; | |||
user_token = data.token; | |||
return emit(); | |||
}); | |||
}); | |||
it("should add a new todo for the currently logged in user", function(done) { | |||
var data = { | |||
text: "This is a todo", | |||
notes: "Nothing to speak of" | |||
}; | |||
request(server) | |||
.post("/todos") | |||
.set("Authorization", user_token.token_type + " " + user_token.access_token) | |||
.send(data) | |||
.end(function(err, res){ | |||
should.not.exist(err); | |||
should.exist(res.body); | |||
|
|||
console.log(res.body); | |||
|
|||
res.statusCode.should.equal(200); | |||
should.exist(res.body._id); | |||
res.body.text.should.equal(data.text); | |||
res.body.notes.should.equal(data.notes); | |||
res.body.completed.should.equal(false); | |||
|
|||
return done(); | |||
}); | |||
}); | |||
}); | |||
describe("GET /todos", function(){ | |||
var | |||
user, | |||
user_token, | |||
todo_id; | |||
|
|||
before(function(done){ | |||
this.timeout(10000); | |||
var emit = function(){ | |||
if ( | |||
!user || | |||
!user_token | |||
) return; | |||
return done(); | |||
}; | |||
|
|||
|
|||
logUserIn("warner@test.com", function(err, data){ | |||
if(err) throw err; | |||
user = data.user; | |||
user_token = data.token; | |||
return emit(); | |||
}); | |||
}); | |||
it("should retrieve all todos for currently logged in user", function(done) { | |||
request(server) | |||
.get("/todos") | |||
.set("Authorization", user_token.token_type + " " + user_token.access_token) | |||
.end(function(err, res){ | |||
should.not.exist(err); | |||
should.exist(res.body); | |||
|
|||
console.log(res.body); | |||
|
|||
res.statusCode.should.equal(200); | |||
res.body.length.should.equal(2); | |||
todo_id = res.body[0]._id; | |||
todo_to_change = res.body[1]._id; | |||
return done(); | |||
}); | |||
}); | |||
it("should retrieve a specific todo", function(done) { | |||
request(server) | |||
.get("/todos/" + todo_id) | |||
.set("Authorization", user_token.token_type + " " + user_token.access_token) | |||
.end(function(err, res){ | |||
should.not.exist(err); | |||
should.exist(res.body); | |||
|
|||
console.log(res.body); | |||
|
|||
res.statusCode.should.equal(200); | |||
should.exist(res.body.text); | |||
return done(); | |||
}); | |||
}); | |||
}); | |||
|
|||
describe("PUT /todos/:id", function(){ | |||
var | |||
user, | |||
user_token, | |||
todo_id; | |||
|
|||
before(function(done){ | |||
this.timeout(10000); | |||
var emit = function(){ | |||
if ( | |||
!user || | |||
!user_token | |||
) return; | |||
return done(); | |||
}; | |||
|
|||
|
|||
logUserIn("warner@test.com", function(err, data){ | |||
if(err) throw err; | |||
user = data.user; | |||
user_token = data.token; | |||
return emit(); | |||
}); | |||
}); | |||
it("should update a specific todo", function(done) { | |||
var data = { | |||
text: "Updated Text", | |||
notes: "New notes." | |||
}; | |||
request(server) | |||
.put("/todos/" + todo_to_change) | |||
.set("Authorization", user_token.token_type + " " + user_token.access_token) | |||
.send(data) | |||
.end(function(err, res){ | |||
should.not.exist(err); | |||
should.exist(res.body); | |||
|
|||
console.log(res.body); | |||
|
|||
res.statusCode.should.equal(200); | |||
should.exist(res.body.text); | |||
res.body.text.should.equal(data.text); | |||
res.body.notes.should.equal(data.notes); | |||
return done(); | |||
}); | |||
}); | |||
}); | |||
|
|||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters