Validating team size on join, server-side
By doing this, we can prevent the bug where multiple users can join a team simutaneously and push its enrollment over the defined maximum value. Tests have also been added to confirm this behavior. TNL-3061
This commit is contained in:
committed by
Renzo Lucioni
parent
08b9b03b9f
commit
da3ac3e65b
@@ -143,7 +143,8 @@ class TeamAPITestCase(APITestCase, SharedModuleStoreTestCase):
|
||||
'name': 'Public Profiles',
|
||||
'description': 'Description for topic 6.'
|
||||
},
|
||||
]
|
||||
],
|
||||
'max_team_size': 1
|
||||
}
|
||||
cls.test_course_2 = CourseFactory.create(
|
||||
org='MIT',
|
||||
@@ -185,6 +186,13 @@ class TeamAPITestCase(APITestCase, SharedModuleStoreTestCase):
|
||||
profile.year_of_birth = 1970
|
||||
profile.save()
|
||||
|
||||
# This student is enrolled in the other course, but not yet a member of a team. This is to allow
|
||||
# course_2 to use a max_team_size of 1 without breaking other tests on course_1
|
||||
self.create_and_enroll_student(
|
||||
courses=[self.test_course_2],
|
||||
username='student_enrolled_other_course_not_on_team'
|
||||
)
|
||||
|
||||
# 'solar team' is intentionally lower case to test case insensitivity in name ordering
|
||||
self.test_team_1 = CourseTeamFactory.create(
|
||||
name=u'sólar team',
|
||||
@@ -956,6 +964,14 @@ class TestCreateMembershipAPI(TeamAPITestCase):
|
||||
)
|
||||
self.assertIn('not enrolled', json.loads(response.content)['developer_message'])
|
||||
|
||||
def test_over_max_team_size_in_course_2(self):
|
||||
response = self.post_create_membership(
|
||||
400,
|
||||
self.build_membership_data('student_enrolled_other_course_not_on_team', self.test_team_5),
|
||||
user='student_enrolled_other_course_not_on_team'
|
||||
)
|
||||
self.assertIn('full', json.loads(response.content)['developer_message'])
|
||||
|
||||
|
||||
@ddt.ddt
|
||||
class TestDetailMembershipAPI(TeamAPITestCase):
|
||||
|
||||
@@ -914,6 +914,13 @@ class MembershipListView(ExpandableFieldViewMixin, GenericAPIView):
|
||||
except User.DoesNotExist:
|
||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
course_module = modulestore().get_course(team.course_id)
|
||||
if course_module.teams_max_size is not None and team.users.count() >= course_module.teams_max_size:
|
||||
return Response(
|
||||
build_api_error(ugettext_noop("This team is already full.")),
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
try:
|
||||
membership = team.add_user(user)
|
||||
except AlreadyOnTeamInCourse:
|
||||
|
||||
Reference in New Issue
Block a user