mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-30 10:56:53 -05:00
Whoop whoop test for recalculation of gamesCompleted
This commit is contained in:
parent
7a4c6daec8
commit
7a07e1feb0
6 changed files with 88 additions and 5 deletions
|
@ -21,7 +21,7 @@ class EarnedAchievementHandler extends Handler
|
||||||
EarnedAchievementHandler.recalculate achievementSlugsOrIDs, onSuccess
|
EarnedAchievementHandler.recalculate achievementSlugsOrIDs, onSuccess
|
||||||
else
|
else
|
||||||
EarnedAchievementHandler.recalculate onSuccess
|
EarnedAchievementHandler.recalculate onSuccess
|
||||||
@sendSuccess res, {}
|
@sendAccepted res, {}
|
||||||
|
|
||||||
# Returns success: boolean
|
# Returns success: boolean
|
||||||
# TODO call onFinished
|
# TODO call onFinished
|
||||||
|
|
|
@ -47,6 +47,7 @@ module.exports = class Handler
|
||||||
|
|
||||||
# sending functions
|
# sending functions
|
||||||
sendUnauthorizedError: (res) -> errors.forbidden(res) #TODO: rename sendUnauthorizedError to sendForbiddenError
|
sendUnauthorizedError: (res) -> errors.forbidden(res) #TODO: rename sendUnauthorizedError to sendForbiddenError
|
||||||
|
sendForbiddenError: (res) -> errors.forbidden(res)
|
||||||
sendNotFoundError: (res) -> errors.notFound(res)
|
sendNotFoundError: (res) -> errors.notFound(res)
|
||||||
sendMethodNotAllowed: (res) -> errors.badMethod(res)
|
sendMethodNotAllowed: (res) -> errors.badMethod(res)
|
||||||
sendBadInputError: (res, message) -> errors.badInput(res, message)
|
sendBadInputError: (res, message) -> errors.badInput(res, message)
|
||||||
|
@ -62,6 +63,18 @@ module.exports = class Handler
|
||||||
res.send(message)
|
res.send(message)
|
||||||
res.end()
|
res.end()
|
||||||
|
|
||||||
|
sendCreated: (res, message) ->
|
||||||
|
res.send 201, message
|
||||||
|
res.end()
|
||||||
|
|
||||||
|
sendAccepted: (res, message) ->
|
||||||
|
res.send 202, message
|
||||||
|
res.end()
|
||||||
|
|
||||||
|
sendNoContent: (res, message) ->
|
||||||
|
res.send 204, message
|
||||||
|
res.end()
|
||||||
|
|
||||||
# generic handlers
|
# generic handlers
|
||||||
get: (req, res) ->
|
get: (req, res) ->
|
||||||
@sendUnauthorizedError(res) if not @hasAccess(req)
|
@sendUnauthorizedError(res) if not @hasAccess(req)
|
||||||
|
|
|
@ -13,7 +13,7 @@ module.exports.setup = (app) ->
|
||||||
parts = module.split('/')
|
parts = module.split('/')
|
||||||
module = parts[0]
|
module = parts[0]
|
||||||
|
|
||||||
return errors.unauthorized(res, 'Must be admin to access this area.') unless req.user?.isAdmin()
|
return errors.forbidden(res, 'Admins only') unless req.user?.isAdmin()
|
||||||
|
|
||||||
try
|
try
|
||||||
moduleName = module.replace '.', '_'
|
moduleName = module.replace '.', '_'
|
||||||
|
|
|
@ -375,5 +375,28 @@ UserHandler = class UserHandler extends Handler
|
||||||
return @sendNotFoundError res unless remark?
|
return @sendNotFoundError res unless remark?
|
||||||
@sendSuccess res, remark
|
@sendSuccess res, remark
|
||||||
|
|
||||||
|
statHandlers:
|
||||||
|
gamesCompleted: (done) ->
|
||||||
|
LevelSession = require '../levels/sessions/LevelSession'
|
||||||
|
|
||||||
|
User.find {}, (err, users) ->
|
||||||
|
async.eachSeries users, ((user, doneWithUser) ->
|
||||||
|
userID = user.get('_id').toHexString()
|
||||||
|
|
||||||
|
LevelSession.find {creator: userID, 'state.completed': true}, (err, sessions) ->
|
||||||
|
completedCount = sessions.length
|
||||||
|
User.findOneAndUpdate {_id: user.get '_id'}, {$set: 'stats.gamesCompleted': completedCount}, (err) ->
|
||||||
|
log.error err if err?
|
||||||
|
doneWithUser()
|
||||||
|
), done
|
||||||
|
|
||||||
|
recalculate: (req, res, statName) ->
|
||||||
|
return @sendForbiddenError(res) unless req.user.isAdmin()
|
||||||
|
|
||||||
|
if statName of @statHandlers
|
||||||
|
@statHandlers[statName] -> log.debug "Finished recalculating stats"
|
||||||
|
return @sendAccepted res, {}
|
||||||
|
else return @sendNotFoundError(res)
|
||||||
|
|
||||||
|
|
||||||
module.exports = new UserHandler()
|
module.exports = new UserHandler()
|
||||||
|
|
23
test/server/functional/admin.spec.coffee
Normal file
23
test/server/functional/admin.spec.coffee
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
common = require '../common'
|
||||||
|
|
||||||
|
describe 'recalculate statistics', ->
|
||||||
|
url = getURL '/admin/user/recalculate/'
|
||||||
|
|
||||||
|
it 'does not allow regular users', (done) ->
|
||||||
|
loginJoe ->
|
||||||
|
request.post {uri:url + 'gamesCompleted'}, (err, res, body) ->
|
||||||
|
expect(res.statusCode).toBe 403
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'responds with a 202 Accepted', (done) ->
|
||||||
|
loginAdmin ->
|
||||||
|
request.post {uri:url + 'gamesCompleted'}, (err, res, body) ->
|
||||||
|
expect(res.statusCode).toBe 202
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'responds with a 404 when not found', (done) ->
|
||||||
|
loginAdmin ->
|
||||||
|
request.post {uri:url + 'ballsKicked'}, (err, res, body) ->
|
||||||
|
expect(res.statusCode).toBe 404
|
||||||
|
done()
|
||||||
|
|
|
@ -270,9 +270,11 @@ describe 'GET /db/user', ->
|
||||||
)
|
)
|
||||||
|
|
||||||
describe 'statistics', ->
|
describe 'statistics', ->
|
||||||
it 'games completed', (done) ->
|
|
||||||
LevelSession = require '../../../server/levels/sessions/LevelSession'
|
LevelSession = require '../../../server/levels/sessions/LevelSession'
|
||||||
User = require '../../../server/users/User'
|
User = require '../../../server/users/User'
|
||||||
|
UserHandler = require '../../../server/users/user_handler'
|
||||||
|
|
||||||
|
it 'games completed', (done) ->
|
||||||
|
|
||||||
session = new LevelSession
|
session = new LevelSession
|
||||||
name: 'Beat Gandalf'
|
name: 'Beat Gandalf'
|
||||||
|
@ -286,9 +288,31 @@ describe 'statistics', ->
|
||||||
session.save (err) ->
|
session.save (err) ->
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
|
|
||||||
User.findOne {_id: joe.get 'id'}, (err, guy) ->
|
User.findById joe.get('id'), (err, guy) ->
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
expect(guy.get 'id').toBe joe.get 'id'
|
expect(guy.get 'id').toBe joe.get 'id'
|
||||||
expect(guy.get 'stats.gamesCompleted').toBe 1
|
expect(guy.get 'stats.gamesCompleted').toBe 1
|
||||||
|
|
||||||
done()
|
done()
|
||||||
|
|
||||||
|
it 'recalculates games completed', (done) ->
|
||||||
|
unittest.getNormalJoe (joe) ->
|
||||||
|
loginAdmin ->
|
||||||
|
User.findByIdAndUpdate joe.get('id'), {$set:'stats.gamesCompleted':0}, (err, guy) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(guy.get 'id').toBe joe.get 'id'
|
||||||
|
expect(guy.get 'stats.gamesCompleted').toBe 0
|
||||||
|
|
||||||
|
UserHandler.statHandlers.gamesCompleted ->
|
||||||
|
User.findById joe.get('id'), (err, guy) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(guy.get 'id').toBe joe.get 'id'
|
||||||
|
expect(guy.get 'stats.gamesCompleted').toBe 1
|
||||||
|
done()
|
||||||
|
|
||||||
|
|
||||||
|
xit 'cleans up', (done) ->
|
||||||
|
clearModels [LevelSession], (err) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
|
||||||
|
done()
|
||||||
|
|
Loading…
Reference in a new issue