Skip to content

Commit

Permalink
test: verify that allow_enrollment is called_correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
0x29a committed Apr 22, 2024
1 parent 7a03833 commit 0b19535
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
8 changes: 4 additions & 4 deletions tests/test_enterprise/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,9 @@ def test_ensure_course_enrollment_is_allowed(self, invite_only):
ensure_course_enrollment_is_allowed("test-course-id", self.user.email, mock_enrollment_api)

if invite_only:
mock_enrollment_api.return_value.allow_enrollment.assert_called_with(
email=self.user.email,
course_id="test-course-id",
mock_enrollment_api.allow_enrollment.assert_called_with(
self.user.email,
"test-course-id",
)
else:
mock_enrollment_api.return_value.allow_enrollment.assert_not_called()
mock_enrollment_api.allow_enrollment.assert_not_called()
4 changes: 2 additions & 2 deletions tests/test_enterprise/views/test_course_enrollment_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -1663,8 +1663,8 @@ def test_post_course_specific_enrollment_view_invite_only_courses(
)

enrollment_api_client_mock.return_value.allow_enrollment.assert_called_with(
email=self.user.email,
course_id=course_id,
self.user.email,
course_id,
)
assert response.status_code == 302

Expand Down
47 changes: 45 additions & 2 deletions tests/test_enterprise/views/test_grant_data_sharing_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ def _assert_enterprise_linking_messages(self, response, user_is_active=True):
'You will not be able to log back into your account until you have activated it.'
)

def _assert_allow_enrollment_is_called_correctly(
self,
mock_enrollment_api_client,
license_is_present,
course_invite_only,
enrollment_in_invite_only_courses_allowed,
):
"""
Verify that the allow_enrollment endpoint is called only when:
- License is present
- Course is invite only
- Enrollment in invite only courses is allowed
"""
if license_is_present:
if course_invite_only:
if enrollment_in_invite_only_courses_allowed:
mock_enrollment_api_client.return_value.allow_enrollment.assert_called_once()
else:
mock_enrollment_api_client.return_value.allow_enrollment.assert_not_called()
else:
mock_enrollment_api_client.return_value.allow_enrollment.assert_not_called()
else:
mock_enrollment_api_client.return_value.allow_enrollment.assert_not_called()

@mock.patch('enterprise.views.render', side_effect=fake_render)
@mock.patch('enterprise.models.EnterpriseCatalogApiClient')
@mock.patch('enterprise.api_client.discovery.CourseCatalogApiServiceClient')
Expand Down Expand Up @@ -398,12 +422,21 @@ def test_get_course_specific_consent_not_needed(
@mock.patch('enterprise.views.get_best_mode_from_course_key')
@mock.patch('enterprise.api_client.discovery.CourseCatalogApiServiceClient')
@ddt.data(
str(uuid.uuid4()),
'',
(str(uuid.uuid4()), True, True),
(str(uuid.uuid4()), True, False),
(str(uuid.uuid4()), False, True),
(str(uuid.uuid4()), False, False),
('', True, True),
('', True, False),
('', False, True),
('', False, False),
)
@ddt.unpack
def test_get_course_specific_data_sharing_consent_not_enabled(
self,
license_uuid,
course_invite_only,
allow_enrollment_in_invite_only_courses,
course_catalog_api_client_mock,
mock_get_course_mode,
mock_enrollment_api_client,
Expand All @@ -414,6 +447,7 @@ def test_get_course_specific_data_sharing_consent_not_enabled(
enterprise_customer = EnterpriseCustomerFactory(
name='Starfleet Academy',
enable_data_sharing_consent=False,
allow_enrollment_in_invite_only_courses=allow_enrollment_in_invite_only_courses,
)
content_filter = {
'key': [
Expand All @@ -432,6 +466,8 @@ def test_get_course_specific_data_sharing_consent_not_enabled(
course_catalog_api_client_mock.return_value.program_exists.return_value = True
course_catalog_api_client_mock.return_value.get_course_id.return_value = course_id

mock_enrollment_api_client.return_value.get_course_details.return_value = {"invite_only": course_invite_only}

course_mode = 'verified'
mock_get_course_mode.return_value = course_mode
mock_enrollment_api_client.return_value.get_course_enrollment.return_value = {
Expand Down Expand Up @@ -467,6 +503,13 @@ def test_get_course_specific_data_sharing_consent_not_enabled(
else:
assert not mock_enrollment_api_client.return_value.enroll_user_in_course.called

self._assert_allow_enrollment_is_called_correctly(
mock_enrollment_api_client,
bool(license_uuid),
course_invite_only,
allow_enrollment_in_invite_only_courses
)

@mock.patch('enterprise.views.render', side_effect=fake_render)
@mock.patch('enterprise.views.get_best_mode_from_course_key')
@mock.patch('enterprise.models.EnterpriseCatalogApiClient')
Expand Down

0 comments on commit 0b19535

Please sign in to comment.