mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-23 15:48:11 -05:00
Make POST /db/classroom/:id/members use update to avoid race conditions
This commit is contained in:
parent
6de6479f3b
commit
f57cf3a83d
2 changed files with 8 additions and 4 deletions
|
@ -57,10 +57,11 @@ ClassroomHandler = class ClassroomHandler extends Handler
|
|||
members = _.clone(classroom.get('members'))
|
||||
if _.any(members, (memberID) -> memberID.equals(req.user.get('_id')))
|
||||
return @sendSuccess(res, @formatEntity(req, classroom))
|
||||
update = { $push: { members : req.user.get('_id')}}
|
||||
classroom.update update, (err) =>
|
||||
return @sendDatabaseError(res, err) if err
|
||||
members.push req.user.get('_id')
|
||||
classroom.set('members', members)
|
||||
classroom.save (err, classroom) =>
|
||||
return @sendDatabaseError(res, err) if err
|
||||
return @sendSuccess(res, @formatEntity(req, classroom))
|
||||
|
||||
formatEntity: (req, doc) ->
|
||||
|
|
|
@ -123,12 +123,15 @@ describe 'POST /db/classroom/:id/members', ->
|
|||
data = { name: 'Classroom 5' }
|
||||
request.post {uri: classroomsURL, json: data }, (err, res, body) ->
|
||||
classroomCode = body.code
|
||||
classroomID = body._id
|
||||
expect(res.statusCode).toBe(200)
|
||||
loginNewUser (user2) ->
|
||||
url = getURL("/db/classroom/~/members")
|
||||
data = { code: classroomCode }
|
||||
request.post { uri: url, json: data }, (err, res, body) ->
|
||||
expect(res.statusCode).toBe(200)
|
||||
Classroom.findById classroomID, (err, classroom) ->
|
||||
expect(classroom.get('members').length).toBe(1)
|
||||
done()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue