Strip spaces in classCode on fetch and join

This commit is contained in:
phoenixeliot 2016-05-27 16:03:58 -07:00
parent 5da85621c6
commit 0d4a88a957
3 changed files with 27 additions and 3 deletions

View file

@ -84,7 +84,7 @@ ClassroomHandler = class ClassroomHandler extends Handler
return @sendDatabaseError(res, err) if err
return @sendSuccess(res, (@formatEntity(req, classroom) for classroom in classrooms))
else if code = req.query.code
code = code.toLowerCase()
code = code.toLowerCase().replace(/ /g, '')
Classroom.findOne {code: code}, (err, classroom) =>
return @sendDatabaseError(res, err) if err
return @sendNotFoundError(res) unless classroom

View file

@ -22,7 +22,7 @@ module.exports =
fetchByCode: wrap (req, res, next) ->
code = req.query.code
return next() unless code
classroom = yield Classroom.findOne({ code: code.toLowerCase() }).select('name ownerID aceConfig')
classroom = yield Classroom.findOne({ code: code.toLowerCase().replace(/ /g, '') }).select('name ownerID aceConfig')
if not classroom
log.debug("classrooms.fetchByCode: Couldn't find Classroom with code: #{code}")
throw new errors.NotFound('Classroom not found.')
@ -170,7 +170,7 @@ module.exports =
if req.user.isTeacher()
log.debug("classrooms.join: Cannot join a classroom as a teacher: #{req.user.id}")
throw new errors.Forbidden('Cannot join a classroom as a teacher')
code = req.body.code.toLowerCase()
code = req.body.code.toLowerCase().replace(/ /g, '')
classroom = yield Classroom.findOne({code: code})
if not classroom
log.debug("classrooms.join: Classroom not found with code #{code}")

View file

@ -60,6 +60,18 @@ describe 'GET /db/classroom/:id', ->
expect(body._id).toBe(classroomID = body._id)
done()
describe 'GET /db/classroom by classCode', ->
it 'Returns the class if you include spaces', utils.wrap (done) ->
user = yield utils.initUser()
yield utils.loginUser(user)
teacher = yield utils.initUser()
classroom = new Classroom({ name: "some class", ownerID: teacher.id, camelCode: "FooBarBaz", code: "foobarbaz" })
yield classroom.save()
[res, body] = yield request.getAsync(getURL('/db/classroom?code=foo bar baz'), { json: true })
expect(res.statusCode).toBe(200)
expect(res.body.data?.name).toBe(classroom.get('name'))
done()
describe 'POST /db/classroom', ->
beforeEach utils.wrap (done) ->
@ -295,6 +307,18 @@ describe 'POST /db/classroom/-/members', ->
fail('student should be added to the free course instance.')
done()
it 'joins the class even with spaces in the classcode', utils.wrap (done) ->
yield utils.loginUser(@student)
url = getURL("/db/classroom/anything-here/members")
code = @classroom.get('code')
codeWithSpaces = code.split("").join(" ")
[res, body] = yield request.postAsync { uri: url, json: { code: codeWithSpaces } }
expect(res.statusCode).toBe(200)
classroom = yield Classroom.findById(@classroom.id)
if classroom.get('members').length isnt 1
fail 'expected classCode with spaces to work too'
done()
it 'returns 403 if the user is a teacher', utils.wrap (done) ->
yield utils.loginUser(@teacher)
url = getURL("/db/classroom/~/members")