mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-30 10:56:53 -05:00
Added test cases for fetching related by user slug instead of ID
This commit is contained in:
parent
cc7474538f
commit
0f3b049189
4 changed files with 65 additions and 12 deletions
|
@ -47,7 +47,7 @@ module.exports = class Handler
|
|||
|
||||
# sending functions
|
||||
sendUnauthorizedError: (res) -> errors.forbidden(res) #TODO: rename sendUnauthorizedError to sendForbiddenError
|
||||
sendNotFoundError: (res) -> errors.notFound(res)
|
||||
sendNotFoundError: (res, message) -> errors.notFound(res, message)
|
||||
sendMethodNotAllowed: (res) -> errors.badMethod(res)
|
||||
sendBadInputError: (res, message) -> errors.badInput(res, message)
|
||||
sendDatabaseError: (res, err) ->
|
||||
|
|
|
@ -31,8 +31,7 @@ module.exports.NamedPlugin = (schema) ->
|
|||
schema.index({'slug': 1}, {unique: true, sparse: true, name: 'slug index'})
|
||||
|
||||
schema.statics.getBySlug = (slug, done) ->
|
||||
@findOne {slug: slug}, (err, doc) ->
|
||||
if err then done err else done doc
|
||||
@findOne {slug: slug}, done
|
||||
|
||||
schema.pre('save', (next) ->
|
||||
if schema.uses_coco_versions
|
||||
|
|
|
@ -240,13 +240,13 @@ UserHandler = class UserHandler extends Handler
|
|||
@sendSuccess(res, documents)
|
||||
|
||||
IDify: (idOrSlug, done) ->
|
||||
if Handler.isID idOrSlug
|
||||
done idOrSlug
|
||||
else
|
||||
User.getBySlug idOrSlug, (user) -> done user?.get '_id'
|
||||
return done null, idOrSlug if Handler.isID idOrSlug
|
||||
User.getBySlug idOrSlug, (err, user) -> done err, user?.get '_id'
|
||||
|
||||
getLevelSessions: (req, res, userIDOrSlug) ->
|
||||
@IDify userIDOrSlug, (userID) =>
|
||||
@IDify userIDOrSlug, (err, userID) =>
|
||||
return @sendDatabaseError res, err if err
|
||||
return @sendNotFoundError res unless userID?
|
||||
query = creator: userID + ''
|
||||
isAuthorized = req.user._id+'' is userID or req.user.isAdmin()
|
||||
projection = {}
|
||||
|
@ -261,7 +261,9 @@ UserHandler = class UserHandler extends Handler
|
|||
@sendSuccess(res, documents)
|
||||
|
||||
getEarnedAchievements: (req, res, userIDOrSlug) ->
|
||||
@IDify userIDOrSlug, (userID) =>
|
||||
@IDify userIDOrSlug, (err, userID) =>
|
||||
return @sendDatabaseError res, err if err
|
||||
return @sendNotFoundError res unless userID?
|
||||
query = user: userID + ''
|
||||
query.notified = false if req.query.notified is 'false'
|
||||
EarnedAchievement.find(query).sort(changed: -1).exec (err, documents) =>
|
||||
|
|
|
@ -1,12 +1,64 @@
|
|||
require '../common'
|
||||
|
||||
describe 'LevelFeedback', ->
|
||||
describe '/db/level.session', ->
|
||||
|
||||
url = getURL('/db/level.session')
|
||||
url = getURL('/db/level.session/')
|
||||
session =
|
||||
permissions: simplePermissions
|
||||
|
||||
it 'get schema', (done) ->
|
||||
request.get {uri: url+'/schema'}, (err, res, body) ->
|
||||
request.get {uri: url+'schema'}, (err, res, body) ->
|
||||
expect(res.statusCode).toBe(200)
|
||||
body = JSON.parse(body)
|
||||
expect(body.type).toBeDefined()
|
||||
done()
|
||||
|
||||
it 'clears things first', (done) ->
|
||||
clearModels [LevelSession], (err) ->
|
||||
expect(err).toBeNull()
|
||||
done()
|
||||
|
||||
# TODO Tried to mimic what happens on the site. Why is this even so hard to do.
|
||||
# Right now it's even possible to create ownerless sessions through POST
|
||||
xit 'allows users to create level sessions through PATCH', (done) ->
|
||||
loginJoe (joe) ->
|
||||
console.log url + mongoose.Types.ObjectId()
|
||||
request {method: 'patch', uri: url + mongoose.Types.ObjectId(), json: session}, (err, res, body) ->
|
||||
expect(err).toBeNull()
|
||||
expect(res.statusCode).toBe 200
|
||||
console.log body
|
||||
expect(body.creator).toEqual joe.get('_id').toHexString()
|
||||
done()
|
||||
|
||||
# Should remove this as soon as the PATCH test case above works
|
||||
it 'create a level session', (done) ->
|
||||
unittest.getNormalJoe (joe) ->
|
||||
session.creator = joe.get('_id').toHexString()
|
||||
session = new LevelSession session
|
||||
session.save (err) ->
|
||||
expect(err).toBeNull()
|
||||
done()
|
||||
|
||||
it 'GET /db/user/<ID>/level.sessions gets a user\'s level sessions', (done) ->
|
||||
unittest.getNormalJoe (joe) ->
|
||||
request.get {uri: getURL "/db/user/#{joe.get '_id'}/level.sessions"}, (err, res, body) ->
|
||||
expect(err).toBeNull()
|
||||
expect(res.statusCode).toBe 200
|
||||
sessions = JSON.parse body
|
||||
expect(sessions.length).toBe 1
|
||||
done()
|
||||
|
||||
it 'GET /db/user/<SLUG>/level.sessions gets a user\'s level sessions', (done) ->
|
||||
unittest.getNormalJoe (joe) ->
|
||||
request.get {uri: getURL "/db/user/#{joe.get 'slug'}/level.sessions"}, (err, res, body) ->
|
||||
expect(err).toBeNull()
|
||||
expect(res.statusCode).toBe 200
|
||||
sessions = JSON.parse body
|
||||
expect(sessions.length).toBe 1
|
||||
done()
|
||||
|
||||
it 'GET /db/user/<IDorSLUG>/level.sessions returns 404 if user not found', (done) ->
|
||||
request.get {uri: getURL "/db/user/misterschtroumpf/level.sessions"}, (err, res) ->
|
||||
expect(err).toBeNull()
|
||||
expect(res.statusCode).toBe 404
|
||||
done()
|
||||
|
|
Loading…
Reference in a new issue