From 730ac4b3dfdc5ccfd5ed85efc87834b79e226c7e Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Fri, 10 Apr 2015 18:27:58 -0500 Subject: [PATCH] correct chat api tests; prevent sending a blank message via the api --- test/api.mocha.coffee | 13 +++++++++++-- website/src/controllers/groups.js | 14 +++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/test/api.mocha.coffee b/test/api.mocha.coffee index 53d301595a..d5f971bfda 100644 --- a/test/api.mocha.coffee +++ b/test/api.mocha.coffee @@ -285,12 +285,13 @@ describe "API", -> describe "Chat", -> chat = undefined it "Posts a message to party chat", (done) -> - request.post(baseURL + "/groups/" + group._id + "/chat").send( - message: "Test MSG" + msg = "TestMsg" + request.post(baseURL + "/groups/" + group._id + "/chat?message=" + msg).send( ).end (res) -> expectCode res, 200 chat = res.body.message expect(chat.id).to.be.ok + expect(chat.text).to.be.eql msg expect(chat.timestamp).to.be.ok expect(chat.likes).to.be.empty expect(chat.flags).to.be.empty @@ -302,6 +303,14 @@ describe "API", -> expect(chat.user).to.be user.profile.name done() + it "Does not post an empty message", (done) -> + msg = "" + request.post(baseURL + "/groups/" + group._id + "/chat?message=" + msg).send( + ).end (res) -> + expectCode res, 400 + expect(res.body.err).to.be.eql 'You cannot send a blank message' + done() + it "can not like own chat message", (done) -> request.post(baseURL + "/groups/" + group._id + "/chat/" + chat.id + "/like").send( ).end (res) -> diff --git a/website/src/controllers/groups.js b/website/src/controllers/groups.js index 924b701622..e515cd95a7 100644 --- a/website/src/controllers/groups.js +++ b/website/src/controllers/groups.js @@ -31,7 +31,7 @@ var guildPopulate = {path: 'members', select: nameFields, options: {limit: 15} } * limited fields - and only a sampling of the members, beacuse they can be in the thousands * @param type: 'party' or otherwise * @param q: the Mongoose query we're building up - * @param additionalFields: if we want to populate some additional field not fetched normally + * @param additionalFields: if we want to populate some additional field not fetched normally * pass it as a string, parties only */ var populateQuery = function(type, q, additionalFields){ @@ -237,8 +237,8 @@ api.getChat = function(req, res, next) { * TODO make this it's own ngResource so we don't have to send down group data with each chat post */ api.postChat = function(req, res, next) { - if(typeof req.query.message === 'undefined' || req.query.message == '') { - return res.json(204,{err:'No content in the message.'}); + if(!req.query.message) { + return res.json(400,{err:'You cannot send a blank message'}); } else { var user = res.locals.user var group = res.locals.group; @@ -310,7 +310,7 @@ api.flagChatMessage = function(req, res, next){ group.save(function(err,_saved){ if(err) return next(err); var addressesToSendTo = JSON.parse(nconf.get('FLAG_REPORT_EMAIL')); - + if(Array.isArray(addressesToSendTo)){ addressesToSendTo = addressesToSendTo.map(function(email){ return {email: email, canSend: true} @@ -363,7 +363,7 @@ api.clearFlagCount = function(req, res, next){ }else{ return res.json(401, {err: "Only an admin can clear the flag count!"}) } - + } api.seenMessage = function(req,res,next){ @@ -582,7 +582,7 @@ var inviteByUUIDs = function(uuids, group, req, res, next){ cb(); }); } - }); + }); }, function(err){ if(err) return err.code ? res.json(err.code, {err: err.err}) : next(err); @@ -648,7 +648,7 @@ var inviteByEmails = function(invites, group, req, res, next){ inviteByUUIDs(usersAlreadyRegistered, group, req, res, next); }else{ - // Send only status code down the line because it doesn't need + // Send only status code down the line because it doesn't need // info on invited users since they are not yet registered res.send(200); }