Merge branch 'adi2412-anonNameCollide'

This commit is contained in:
Scott Erickson 2014-05-05 17:47:22 -07:00
commit 8865a9035f
5 changed files with 66 additions and 8 deletions

View file

@ -1,4 +1,4 @@
{backboneFailure, genericFailure} = require 'lib/errors' {backboneFailure, genericFailure, parseServerError} = require 'lib/errors'
User = require 'models/User' User = require 'models/User'
storage = require 'lib/storage' storage = require 'lib/storage'
BEEN_HERE_BEFORE_KEY = 'beenHereBefore' BEEN_HERE_BEFORE_KEY = 'beenHereBefore'
@ -16,7 +16,14 @@ init = ->
module.exports.createUser = (userObject, failure=backboneFailure, nextURL=null) -> module.exports.createUser = (userObject, failure=backboneFailure, nextURL=null) ->
user = new User(userObject) user = new User(userObject)
user.save({}, { 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() success: -> if nextURL then window.location.href = nextURL else window.location.reload()
}) })

View file

@ -46,7 +46,7 @@
"mongoose": "3.8.x", "mongoose": "3.8.x",
"mongoose-text-search": "~0.0.2", "mongoose-text-search": "~0.0.2",
"request": "2.12.x", "request": "2.12.x",
"tv4": "1.0.x", "tv4": "~1.0.16",
"lodash": "~2.0.0", "lodash": "~2.0.0",
"underscore.string": "2.3.x", "underscore.string": "2.3.x",
"async": "0.2.x", "async": "0.2.x",
@ -92,7 +92,8 @@
"karma-phantomjs-launcher": "~0.1.1", "karma-phantomjs-launcher": "~0.1.1",
"karma": "~0.10.9", "karma": "~0.10.9",
"karma-coverage": "~0.1.4", "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", "license": "MIT for the code, and CC-BY for the art and music",
"private": true, "private": true,

View file

@ -105,11 +105,12 @@ UserHandler = class UserHandler extends Handler
(req, user, callback) -> (req, user, callback) ->
return callback(null, req, user) unless req.body.name return callback(null, req, user) unless req.body.name
nameLower = req.body.name?.toLowerCase() nameLower = req.body.name?.toLowerCase()
return callback(null, req, user) if nameLower is user.get('nameLower') # return callback(null, req, user) if nameLower is user.get('nameLower')
User.findOne({nameLower:nameLower}).exec (err, otherUser) -> User.findOne({nameLower:nameLower,anonymous:false}).exec (err, otherUser) ->
log.error "Database error setting user name: #{err}" if err log.error "Database error setting user name: #{err}" if err
return callback(res:'Database error.', code:500) if err return callback(res:'Database error.', code:500) if err
r = {message:'is already used by another account', property:'name'} 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 return callback({res:r, code:409}) if otherUser
user.set('name', req.body.name) user.set('name', req.body.name)
callback(null, req, user) callback(null, req, user)
@ -127,7 +128,7 @@ UserHandler = class UserHandler extends Handler
@getPropertiesFromMultipleDocuments res, User, properties, ids @getPropertiesFromMultipleDocuments res, User, properties, ids
nameToID: (req, res, name) -> 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.send(if otherUser then otherUser._id else JSON.stringify(''))
res.end() res.end()

View file

@ -3,7 +3,12 @@
console.log 'IT BEGINS' 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') GLOBAL._ = require('lodash')
_.str = require('underscore.string') _.str = require('underscore.string')
_.mixin(_.str.exports()) _.mixin(_.str.exports())

View file

@ -44,6 +44,20 @@ describe 'User.updateMailChimp', ->
describe 'POST /db/user', -> 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) -> it 'preparing test : clears the db first', (done) ->
clearModels [User], (err) -> clearModels [User], (err) ->
throw err if err throw err if err
@ -90,6 +104,36 @@ describe 'POST /db/user', ->
expect(user.passwordHash).toBeUndefined() expect(user.passwordHash).toBeUndefined()
done() 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', -> describe 'PUT /db/user', ->