-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change behavior of cohort addition interface #3228
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
import json | ||
|
||
from django.test.client import RequestFactory | ||
from django.test.utils import override_settings | ||
from factory import post_generation, Sequence | ||
from factory.django import DjangoModelFactory | ||
|
||
from course_groups.models import CourseUserGroup | ||
from course_groups.views import add_users_to_cohort | ||
from courseware.tests.tests import TEST_DATA_MIXED_MODULESTORE | ||
from student.tests.factories import UserFactory | ||
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase | ||
from xmodule.modulestore.tests.factories import CourseFactory | ||
|
||
|
||
class CohortFactory(DjangoModelFactory): | ||
FACTORY_FOR = CourseUserGroup | ||
|
||
name = Sequence("cohort{}".format) | ||
course_id = "dummy_id" | ||
group_type = CourseUserGroup.COHORT | ||
|
||
@post_generation | ||
def users(self, create, extracted, **kwargs): # pylint: disable=W0613 | ||
self.users.add(*extracted) | ||
|
||
|
||
@override_settings(MODULESTORE=TEST_DATA_MIXED_MODULESTORE) | ||
class AddUsersToCohortTestCase(ModuleStoreTestCase): | ||
def setUp(self): | ||
self.course = CourseFactory.create() | ||
self.staff_user = UserFactory.create(is_staff=True) | ||
self.cohort1_users = [UserFactory.create() for _ in range(3)] | ||
self.cohort2_users = [UserFactory.create() for _ in range(2)] | ||
self.cohort3_users = [UserFactory.create() for _ in range(2)] | ||
self.cohortless_users = [UserFactory.create() for _ in range(3)] | ||
self.cohort1 = CohortFactory.create(course_id=self.course.id, users=self.cohort1_users) | ||
self.cohort2 = CohortFactory.create(course_id=self.course.id, users=self.cohort2_users) | ||
self.cohort3 = CohortFactory.create(course_id=self.course.id, users=self.cohort3_users) | ||
|
||
def check_request( | ||
self, | ||
users_string, | ||
expected_added=None, | ||
expected_changed=None, | ||
expected_present=None, | ||
expected_unknown=None | ||
): | ||
""" | ||
Check that add_users_to_cohort returns the expected result and has the | ||
expected side effects. The given users will be added to cohort1. | ||
|
||
users_string is the string input entered by the client | ||
|
||
expected_added is a list of users | ||
|
||
expected_changed is a list of (user, previous_cohort) tuples | ||
|
||
expected_present is a list of (user, email/username) tuples where | ||
email/username corresponds to the input | ||
|
||
expected_unknown is a list of strings corresponding to the input | ||
""" | ||
expected_added = expected_added or [] | ||
expected_changed = expected_changed or [] | ||
expected_present = expected_present or [] | ||
expected_unknown = expected_unknown or [] | ||
request = RequestFactory().post("dummy_url", {"users": users_string}) | ||
request.user = self.staff_user | ||
response = add_users_to_cohort(request, self.course.id, self.cohort1.id) | ||
self.assertEqual(response.status_code, 200) | ||
result = json.loads(response.content) | ||
self.assertEqual(result.get("success"), True) | ||
self.assertItemsEqual( | ||
result.get("added"), | ||
[ | ||
{"username": user.username, "name": user.profile.name, "email": user.email} | ||
for user in expected_added | ||
] | ||
) | ||
self.assertItemsEqual( | ||
result.get("changed"), | ||
[ | ||
{ | ||
"username": user.username, | ||
"name": user.profile.name, | ||
"email": user.email, | ||
"previous_cohort": previous_cohort | ||
} | ||
for (user, previous_cohort) in expected_changed | ||
] | ||
) | ||
self.assertItemsEqual( | ||
result.get("present"), | ||
[username_or_email for (_, username_or_email) in expected_present] | ||
) | ||
self.assertItemsEqual(result.get("unknown"), expected_unknown) | ||
for user in expected_added + [user for (user, _) in expected_changed + expected_present]: | ||
self.assertEqual( | ||
CourseUserGroup.objects.get( | ||
course_id=self.course.id, | ||
group_type=CourseUserGroup.COHORT, | ||
users__id=user.id | ||
), | ||
self.cohort1 | ||
) | ||
|
||
def test_empty(self): | ||
self.check_request("") | ||
|
||
def test_only_added(self): | ||
self.check_request( | ||
",".join([user.username for user in self.cohortless_users]), | ||
expected_added=self.cohortless_users | ||
) | ||
|
||
def test_only_changed(self): | ||
self.check_request( | ||
",".join([user.username for user in self.cohort2_users + self.cohort3_users]), | ||
expected_changed=( | ||
[(user, self.cohort2.name) for user in self.cohort2_users] + | ||
[(user, self.cohort3.name) for user in self.cohort3_users] | ||
) | ||
) | ||
|
||
def test_only_present(self): | ||
usernames = [user.username for user in self.cohort1_users] | ||
self.check_request( | ||
",".join(usernames), | ||
expected_present=[(user, user.username) for user in self.cohort1_users] | ||
) | ||
|
||
def test_only_unknown(self): | ||
usernames = ["unknown_user{}".format(i) for i in range(3)] | ||
self.check_request( | ||
",".join(usernames), | ||
expected_unknown=usernames | ||
) | ||
|
||
def test_all(self): | ||
unknowns = ["unknown_user{}".format(i) for i in range(3)] | ||
self.check_request( | ||
",".join( | ||
unknowns + | ||
[ | ||
user.username | ||
for user in self.cohortless_users + self.cohort1_users + self.cohort2_users + self.cohort3_users | ||
] | ||
), | ||
self.cohortless_users, | ||
( | ||
[(user, self.cohort2.name) for user in self.cohort2_users] + | ||
[(user, self.cohort3.name) for user in self.cohort3_users] | ||
), | ||
[(user, user.username) for user in self.cohort1_users], | ||
unknowns | ||
) | ||
|
||
def test_emails(self): | ||
unknown = "unknown_user@example.com" | ||
self.check_request( | ||
",".join([ | ||
self.cohort1_users[0].email, | ||
self.cohort2_users[0].email, | ||
self.cohortless_users[0].email, | ||
unknown | ||
]), | ||
[self.cohortless_users[0]], | ||
[(self.cohort2_users[0], self.cohort2.name)], | ||
[(self.cohort1_users[0], self.cohort1_users[0].email)], | ||
[unknown] | ||
) | ||
|
||
def test_delimiters(self): | ||
unknown = "unknown_user" | ||
self.check_request( | ||
" {} {}\t{}, \r\n{}".format( | ||
unknown, | ||
self.cohort1_users[0].username, | ||
self.cohort2_users[0].username, | ||
self.cohortless_users[0].username | ||
), | ||
[self.cohortless_users[0]], | ||
[(self.cohort2_users[0], self.cohort2.name)], | ||
[(self.cohort1_users[0], self.cohort1_users[0].username)], | ||
[unknown] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless this is already enforced by a db constraint, I think it would be good to have an assertion, somewhere, that users were actually removed from their previous cohorts when changed to a new cohort.