diff --git a/server/middleware/levels.coffee b/server/middleware/levels.coffee index 0d04b1c81..a7c93560c 100644 --- a/server/middleware/levels.coffee +++ b/server/middleware/levels.coffee @@ -52,7 +52,9 @@ module.exports = classroomMap[classroom.id] = classroom for classroom in classrooms levelOriginal = level.get('original') for courseInstance in courseInstances - classroom = classroomMap[courseInstance.get('classroomID').toString()] + classroomID = courseInstance.get('classroomID') + continue unless classroomID + classroom = classroomMap[classroomID.toString()] courseID = courseInstance.get('courseID') classroomCourse = _.find(classroom.get('courses'), (c) -> c._id.equals(courseID)) for courseLevel in classroomCourse.levels diff --git a/spec/server/functional/level.spec.coffee b/spec/server/functional/level.spec.coffee index 27cfc9783..5a0e9cdb7 100644 --- a/spec/server/functional/level.spec.coffee +++ b/spec/server/functional/level.spec.coffee @@ -80,6 +80,12 @@ describe 'GET /db/level/:handle/session', -> expect(res.statusCode).toBe(200) expect(body.codeLanguage).toBe('python') done() + + it 'does not break if the user has a courseInstance without an associated classroom', utils.wrap (done) -> + yield @courseInstance.update({$unset: {classroomID: ''}}) + [res, body] = yield request.getAsync { uri: @url, json: true } + expect(res.statusCode).toBe(402) + done() it 'returns 402 if the user is not in a course with that level', utils.wrap (done) -> otherStudent = yield utils.initUser({role: 'student'})