Permalink
Browse files

got todos endpoints mostly done, ready to work on front-end I think

  • Loading branch information...
Warner Onstine
Warner Onstine committed Jan 13, 2015
1 parent a8ec1ce commit dc0e0d24176080883c6c8429a669a7373ebb2488
Showing with 325 additions and 4 deletions.
  1. +9 −1 config/routes.js
  2. +96 −0 controllers/todos.js
  3. +1 −1 gulpfile.js
  4. +3 −1 models/todo.js
  5. +11 −0 test/data/todos.data.js
  6. +194 −0 test/specs/todo.test.js
  7. +11 −1 test/utils/data.js
View
@@ -2,12 +2,20 @@ var Routes = function(server) {
var
auth = require("../controllers/auth"),
users = require("../controllers/users");
users = require("../controllers/users"),
todos = require("../controllers/todos");
var UserRoutes = function(){
server.get("/user", auth.general, auth.refreshSessionData, users.current);
}();
var TodoRoutes = function(){
server.post("/todos", auth.general, todos.add);
server.get("/todos", auth.general, todos.list);
server.get("/todos/:id", auth.general, todos.get);
server.put("/todos/:id", auth.general, todos.update);
}();
return this;
};
View
@@ -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;
View
@@ -6,7 +6,7 @@ var
Q = require('q'),
TestData = require("./test/utils/data");
gulp.task('setup-test-data',['clear-test-data'], function(callback){
gulp.task('setup-test-data', function(callback){
TestData.createTestData(function(err){
return callback(err);
});
View
@@ -7,10 +7,12 @@ var TodoModel = function(){
var _jsonSchema = {
_id: {type:Schema.Types.ObjectId, "default":mongoose.Types.ObjectId},
text: String,
notes: String,
created: {type: Date, "default": Date.now},
completed: {type:Boolean, "default": false},
completedOn: Date,
position: {type:Number, "default":0}
position: {type:Number, "default":0},
user: {type: String, ref: "User"}
};
var _schema = mongoose.Schema(_jsonSchema);
View
@@ -0,0 +1,11 @@
module.exports = [
{
text: "Todo 1",
notes: "Nothing to speak of"
},
{
text: "Todo 2",
notes: "Nothing to speak of two"
}
];
View
@@ -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();
});
});
});
});
View
@@ -5,11 +5,13 @@ var TestData = function(){
async = require("async"),
conf = require("../../config/settings"),
User = require("../../models/user").Model,
Todo = require("../../models/todo").Model,
AuthUtils = require("../../utils/auth");
var _clearDatabase = function(done){
async.parallel([
function(cb) { User.remove({}, cb); }
function(cb) { User.remove({}, cb); },
function(cb) { Todo.remove({}, cb);}
], function(err, res){
if(err) throw err;
return done(err, res);
@@ -20,13 +22,21 @@ var TestData = function(){
var
users = require("../data/users.data").map(function(usr){
usr.hashedPassword = AuthUtils.hashPassword("password");
usr._id = new mongoose.Types.ObjectId;
return usr;
}),
todos = require("../data/todos.data").map(function(todo){
todo.user = users[0]._id;
return todo;
});
async.parallel([
function(cb) {
User.create(users, cb);
},
function(cb) {
Todo.create(todos, cb);
}
], function(err, res){
if(err) { console.log(err); return done(err); }

0 comments on commit dc0e0d2

Please sign in to comment.