diff --git a/app/lib/auth.coffee b/app/lib/auth.coffee index 9a75af184..53cef7ced 100644 --- a/app/lib/auth.coffee +++ b/app/lib/auth.coffee @@ -1,4 +1,4 @@ -{backboneFailure, genericFailure} = require 'lib/errors' +{backboneFailure, genericFailure, parseServerError} = require 'lib/errors' User = require 'models/User' storage = require 'lib/storage' BEEN_HERE_BEFORE_KEY = 'beenHereBefore' @@ -16,7 +16,14 @@ init = -> module.exports.createUser = (userObject, failure=backboneFailure, nextURL=null) -> user = new User(userObject) user.save({}, { - error: failure, + error: (model,jqxhr,options) -> + error = parseServerError(jqxhr.responseText) + property = error.property if error.property + if jqxhr.status is 409 and property is 'name' + anonUserObject = _.omit(userObject, 'name') + module.exports.createUser anonUserObject, failure, nextURL + else + genericFailure(jqxhr) success: -> if nextURL then window.location.href = nextURL else window.location.reload() }) diff --git a/package.json b/package.json index d4c3fb6c9..10c966fd8 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "mongoose": "3.8.x", "mongoose-text-search": "~0.0.2", "request": "2.12.x", - "tv4": "1.0.x", + "tv4": "~1.0.16", "lodash": "~2.0.0", "underscore.string": "2.3.x", "async": "0.2.x", @@ -92,7 +92,8 @@ "karma-phantomjs-launcher": "~0.1.1", "karma": "~0.10.9", "karma-coverage": "~0.1.4", - "compressible": "~1.0.1" + "compressible": "~1.0.1", + "jasmine-spec-reporter":"~0.3.0" }, "license": "MIT for the code, and CC-BY for the art and music", "private": true, diff --git a/server/users/user_handler.coffee b/server/users/user_handler.coffee index b720f1e5c..caddfedc0 100644 --- a/server/users/user_handler.coffee +++ b/server/users/user_handler.coffee @@ -105,11 +105,12 @@ UserHandler = class UserHandler extends Handler (req, user, callback) -> return callback(null, req, user) unless req.body.name nameLower = req.body.name?.toLowerCase() - return callback(null, req, user) if nameLower is user.get('nameLower') - User.findOne({nameLower:nameLower}).exec (err, otherUser) -> + # return callback(null, req, user) if nameLower is user.get('nameLower') + User.findOne({nameLower:nameLower,anonymous:false}).exec (err, otherUser) -> log.error "Database error setting user name: #{err}" if err return callback(res:'Database error.', code:500) if err r = {message:'is already used by another account', property:'name'} + console.log 'Another user exists' if otherUser return callback({res:r, code:409}) if otherUser user.set('name', req.body.name) callback(null, req, user) @@ -127,7 +128,7 @@ UserHandler = class UserHandler extends Handler @getPropertiesFromMultipleDocuments res, User, properties, ids nameToID: (req, res, name) -> - User.findOne({nameLower:name.toLowerCase()}).exec (err, otherUser) -> + User.findOne({nameLower:name.toLowerCase(),anonymous:false}).exec (err, otherUser) -> res.send(if otherUser then otherUser._id else JSON.stringify('')) res.end() diff --git a/test/server/common.coffee b/test/server/common.coffee index 80aa6e47e..dc01d8389 100644 --- a/test/server/common.coffee +++ b/test/server/common.coffee @@ -3,7 +3,12 @@ console.log 'IT BEGINS' - +require('jasmine-spec-reporter') +jasmine.getEnv().reporter.subReporters_ = [] +jasmine.getEnv().addReporter(new jasmine.SpecReporter({ + displaySuccessfulSpec: true, + displayFailedSpec: true + })) GLOBAL._ = require('lodash') _.str = require('underscore.string') _.mixin(_.str.exports()) diff --git a/test/server/functional/user.spec.coffee b/test/server/functional/user.spec.coffee index 46c98fc24..7ce89886d 100644 --- a/test/server/functional/user.spec.coffee +++ b/test/server/functional/user.spec.coffee @@ -44,6 +44,20 @@ describe 'User.updateMailChimp', -> describe 'POST /db/user', -> + createAnonNameUser = (done)-> + request.post getURL('/auth/logout'), -> + request.get getURL('/auth/whoami'), -> + req = request.post(getURL('/db/user'), (err, response) -> + expect(response.statusCode).toBe(200) + request.get getURL('/auth/whoami'), (request, response, body) -> + res = JSON.parse(response.body) + expect(res.anonymous).toBeTruthy() + expect(res.name).toEqual('Jim') + done() + ) + form = req.form() + form.append('name', 'Jim') + it 'preparing test : clears the db first', (done) -> clearModels [User], (err) -> throw err if err @@ -90,6 +104,36 @@ describe 'POST /db/user', -> expect(user.passwordHash).toBeUndefined() done() + it 'should allow setting anonymous user name', (done) -> + createAnonNameUser(done) + + it 'should allow multiple anonymous users with same name', (done) -> + createAnonNameUser(done) + + + it 'should not allow setting existing user name to anonymous user', (done) -> + + createAnonUser = -> + request.post getURL('/auth/logout'), -> + request.get getURL('/auth/whoami'), -> + req = request.post(getURL('/db/user'), (err, response) -> + expect(response.statusCode).toBe(409) + done() + ) + form = req.form() + form.append('name', 'Jim') + + req = request.post(getURL('/db/user'), (err,response,body) -> + expect(response.statusCode).toBe(200) + request.get getURL('/auth/whoami'), (request, response, body) -> + res = JSON.parse(response.body) + expect(res.anonymous).toBeFalsy() + createAnonUser() + ) + form = req.form() + form.append('email', 'new@user.com') + form.append('password', 'new') + describe 'PUT /db/user', ->