|
10 | 10 | from django.test import TestCase |
11 | 11 | from django_countries.fields import Country |
12 | 12 |
|
13 | | -from common.djangoapps.student.models import CourseEnrollmentAllowed |
14 | | -from common.djangoapps.student.tests.factories import CourseEnrollmentAllowedFactory, UserFactory |
| 13 | +from common.djangoapps.student.models import CourseEnrollmentAllowed, CourseEnrollment |
| 14 | +from common.djangoapps.student.tests.factories import CourseEnrollmentAllowedFactory, UserFactory, UserProfileFactory |
15 | 15 | from common.djangoapps.student.tests.tests import UserSettingsEventTestMixin |
16 | 16 |
|
| 17 | +from openedx_events.learning.data import ( |
| 18 | + CourseData, |
| 19 | + CourseEnrollmentData, |
| 20 | + UserData, |
| 21 | + UserPersonalData, |
| 22 | +) |
| 23 | +from openedx_events.learning.signals import COURSE_ENROLLMENT_CREATED |
| 24 | +from openedx_events.tests.utils import OpenEdxEventsTestMixin |
| 25 | +from openedx.core.djangolib.testing.utils import skip_unless_lms |
| 26 | + |
| 27 | +from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase |
| 28 | +from xmodule.modulestore.tests.factories import CourseFactory |
| 29 | + |
17 | 30 |
|
18 | 31 | class TestUserProfileEvents(UserSettingsEventTestMixin, TestCase): |
19 | 32 | """ |
@@ -179,3 +192,87 @@ def test_enrolled_after_email_change(self): |
179 | 192 | # CEAs shouldn't have been affected |
180 | 193 | assert CourseEnrollmentAllowed.objects.count() == 1 |
181 | 194 | assert CourseEnrollmentAllowed.objects.filter(email='test@edx.org').count() == 1 |
| 195 | + |
| 196 | + |
| 197 | +@skip_unless_lms |
| 198 | +class EnrollmentEventsTest(SharedModuleStoreTestCase, OpenEdxEventsTestMixin): |
| 199 | + """ |
| 200 | + Tests for the Open edX Events associated with the enrollment process through the enroll method. |
| 201 | +
|
| 202 | + This class guarantees that the following events are sent during the user's enrollment, with |
| 203 | + the exact Data Attributes as the event definition stated: |
| 204 | +
|
| 205 | + - COURSE_ENROLLMENT_CREATED: sent after the user's enrollment. |
| 206 | + """ |
| 207 | + |
| 208 | + ENABLED_OPENEDX_EVENTS = ["org.openedx.learning.course.enrollment.created.v1"] |
| 209 | + |
| 210 | + @classmethod |
| 211 | + def setUpClass(cls): |
| 212 | + """ |
| 213 | + Set up class method for the Test class. |
| 214 | +
|
| 215 | + This method starts manually events isolation. Explanation here: |
| 216 | + openedx/core/djangoapps/user_authn/views/tests/test_events.py#L44 |
| 217 | + """ |
| 218 | + super().setUpClass() |
| 219 | + cls.start_events_isolation() |
| 220 | + |
| 221 | + def setUp(self): # pylint: disable=arguments-differ |
| 222 | + super().setUp() |
| 223 | + self.course = CourseFactory.create() |
| 224 | + self.user = UserFactory.create( |
| 225 | + username="test", |
| 226 | + email="test@example.com", |
| 227 | + password="password", |
| 228 | + ) |
| 229 | + self.user_profile = UserProfileFactory.create(user=self.user, name="Test Example") |
| 230 | + self.receiver_called = False |
| 231 | + |
| 232 | + def _event_receiver_side_effect(self, **kwargs): # pylint: disable=unused-argument |
| 233 | + """ |
| 234 | + Used show that the Open edX Event was called by the Django signal handler. |
| 235 | + """ |
| 236 | + self.receiver_called = True |
| 237 | + |
| 238 | + def test_enrollment_created_event_emitted(self): |
| 239 | + """ |
| 240 | + Test whether the student enrollment event is sent after the user's |
| 241 | + enrollment process. |
| 242 | +
|
| 243 | + Expected result: |
| 244 | + - COURSE_ENROLLMENT_CREATED is sent and received by the mocked receiver. |
| 245 | + - The arguments that the receiver gets are the arguments sent by the event |
| 246 | + except the metadata generated on the fly. |
| 247 | + """ |
| 248 | + event_receiver = mock.Mock(side_effect=self._event_receiver_side_effect) |
| 249 | + COURSE_ENROLLMENT_CREATED.connect(event_receiver) |
| 250 | + |
| 251 | + enrollment = CourseEnrollment.enroll(self.user, self.course.id) |
| 252 | + |
| 253 | + self.assertTrue(self.receiver_called) |
| 254 | + self.assertDictContainsSubset( |
| 255 | + { |
| 256 | + "signal": COURSE_ENROLLMENT_CREATED, |
| 257 | + "sender": None, |
| 258 | + "enrollment": CourseEnrollmentData( |
| 259 | + user=UserData( |
| 260 | + pii=UserPersonalData( |
| 261 | + username=self.user.username, |
| 262 | + email=self.user.email, |
| 263 | + name=self.user.profile.name, |
| 264 | + ), |
| 265 | + id=self.user.id, |
| 266 | + is_active=self.user.is_active, |
| 267 | + ), |
| 268 | + course=CourseData( |
| 269 | + course_key=self.course.id, |
| 270 | + display_name=self.course.display_name, |
| 271 | + ), |
| 272 | + mode=enrollment.mode, |
| 273 | + is_active=enrollment.is_active, |
| 274 | + creation_date=enrollment.created, |
| 275 | + ), |
| 276 | + }, |
| 277 | + event_receiver.call_args.kwargs |
| 278 | + ) |
0 commit comments