Commit 715c829c authored by Alexander Dietmüller's avatar Alexander Dietmüller

Fix bug: Signups for one course don't influence other course anymore

parent 19a1d6d0
......@@ -13,6 +13,7 @@ import json
from functools import wraps
from itertools import chain
from bson import ObjectId
from flask import current_app, abort
from eve.methods.get import getitem_internal
from eve.methods.patch import patch_internal
......@@ -94,7 +95,10 @@ def update_signups(course):
# Next, count current signups not on waiting list
collection = current_app.data.driver.db['signups']
taken_spots = collection.count({'status': {'$ne': 'waiting'}})
taken_spots = collection.count({
'course': ObjectId(str(course)),
'status': {'$ne': 'waiting'}
})
available_spots = total_spots - taken_spots
......@@ -108,17 +112,12 @@ def update_signups(course):
sort=[('_updated', 1), ('nethz', 1)],
limit=available_spots)
signups_to_update = [str(item['_id']) for item in signups
if item['status'] == 'waiting']
signup_ids = [item['_id'] for item in signups]
for signup_id in signups_to_update:
patch_internal('signups',
_id=signup_id,
payload={'status': 'reserved'},
concurrency_check=False,
skip_validation=True)
collection.update_many({'_id': {'$in': signup_ids}},
{'$set': {'status': 'reserved'}})
return signups_to_update
return [str(item) for item in signup_ids]
def mark_as_paid(payments):
......
......@@ -15,12 +15,14 @@ from signups import update_signups
def test_success(app):
"""If there are enough spots, the status will be 'reserved'."""
with app.admin(): # Admin so we don't need to care about nethz
# Create fake courses to sign up to
# Create fake courses to sign up to
with app.admin():
course_id = str(app.data.driver.db['courses'].insert({'spots': 10}))
# Sign up as user
with app.user(nethz='nethz'):
signup = {
'nethz': 'Something',
'nethz': 'nethz',
'course': course_id,
}
......@@ -96,6 +98,35 @@ def test_not_enough_spots(app):
assert second_response['status'] == 'waiting'
def test_course_independence(app):
"""Test that signups of one course don't influence another.
There was a bug in the code that lead to counting any signup for any
course. This test should ensure it doesn't happen again.
"""
with app.admin():
# Course with a free spot
main_course = app.data.driver.db['courses'].insert({'spots': 1})
# Other course that will get signups
other_course = app.data.driver.db['courses'].insert({'spots': 5})
for _ in range(5):
app.data.driver.db['signups'].insert({'course': other_course})
# Sign up to course
signup = {
'nethz': 'Something',
'course': str(main_course),
}
response = app.client.post('/signups',
data=signup,
assert_status=201)
# The other signups should have no influence
assert response['status'] == 'reserved'
def test_update_spots(app):
"""Test the main update function.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment