|
| 1 | +""" |
| 2 | +Tests for enterprise.filters.enrollment pipeline step. |
| 3 | +""" |
| 4 | +import uuid |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +from django.test import TestCase |
| 8 | + |
| 9 | +from enterprise.filters.enrollment import EnterpriseEnrollmentPostProcessor |
| 10 | +from test_utils.factories import UserFactory |
| 11 | + |
| 12 | + |
| 13 | +class TestEnterpriseEnrollmentPostProcessor(TestCase): |
| 14 | + """ |
| 15 | + Tests for EnterpriseEnrollmentPostProcessor pipeline step. |
| 16 | + """ |
| 17 | + |
| 18 | + def _make_step(self): |
| 19 | + return EnterpriseEnrollmentPostProcessor( |
| 20 | + "org.openedx.learning.course.enrollment.started.v1", |
| 21 | + [], |
| 22 | + ) |
| 23 | + |
| 24 | + @patch("enterprise.filters.enrollment.ConsentApiServiceClient") |
| 25 | + @patch("enterprise.filters.enrollment.EnterpriseApiServiceClient") |
| 26 | + def test_returns_unchanged_args_for_non_enterprise_user( |
| 27 | + self, |
| 28 | + mock_enterprise_client, |
| 29 | + mock_consent_client, |
| 30 | + ): |
| 31 | + """ |
| 32 | + When the user is not linked to an enterprise customer, return the |
| 33 | + arguments unchanged without calling any API clients. |
| 34 | + """ |
| 35 | + user = UserFactory.create(username="regular-user") |
| 36 | + course_key = MagicMock() |
| 37 | + course_key.__str__.return_value = "course-v1:org+course+run" |
| 38 | + |
| 39 | + step = self._make_step() |
| 40 | + result = step.run_filter( |
| 41 | + user=user, |
| 42 | + course_key=course_key, |
| 43 | + linked_enterprise=None, |
| 44 | + has_api_key_permissions=True, |
| 45 | + ) |
| 46 | + self.assertEqual( |
| 47 | + result, |
| 48 | + { |
| 49 | + "user": user, |
| 50 | + "course_key": course_key, |
| 51 | + "linked_enterprise": None, |
| 52 | + "has_api_key_permissions": True, |
| 53 | + }, |
| 54 | + ) |
| 55 | + |
| 56 | + mock_enterprise_client.return_value.post_enterprise_course_enrollment.assert_not_called() |
| 57 | + mock_consent_client.return_value.provide_consent.assert_not_called() |
| 58 | + |
| 59 | + @patch("enterprise.filters.enrollment.ConsentApiServiceClient") |
| 60 | + @patch("enterprise.filters.enrollment.EnterpriseApiServiceClient") |
| 61 | + def test_returns_unchanged_args_for_no_api_permissions( |
| 62 | + self, |
| 63 | + mock_enterprise_client, |
| 64 | + mock_consent_client, |
| 65 | + ): |
| 66 | + """ |
| 67 | + When the user request is sent without proper api key permissions, return the |
| 68 | + arguments unchanged without calling any API clients. |
| 69 | + """ |
| 70 | + user = UserFactory.create(username="regular-user") |
| 71 | + enterprise_uuid = uuid.uuid4() |
| 72 | + |
| 73 | + course_key = MagicMock() |
| 74 | + course_key.__str__.return_value = "course-v1:org+course+run" |
| 75 | + |
| 76 | + step = self._make_step() |
| 77 | + result = step.run_filter( |
| 78 | + user=user, |
| 79 | + course_key=course_key, |
| 80 | + linked_enterprise=enterprise_uuid, |
| 81 | + has_api_key_permissions=False, |
| 82 | + ) |
| 83 | + self.assertEqual( |
| 84 | + result, |
| 85 | + { |
| 86 | + "user": user, |
| 87 | + "course_key": course_key, |
| 88 | + "linked_enterprise": enterprise_uuid, |
| 89 | + "has_api_key_permissions": False, |
| 90 | + }, |
| 91 | + ) |
| 92 | + |
| 93 | + mock_enterprise_client.return_value.post_enterprise_course_enrollment.assert_not_called() |
| 94 | + mock_consent_client.return_value.provide_consent.assert_not_called() |
| 95 | + |
| 96 | + @patch("enterprise.filters.enrollment.ConsentApiServiceClient") |
| 97 | + @patch("enterprise.filters.enrollment.EnterpriseApiServiceClient") |
| 98 | + def test_calls_api_clients_for_enterprise_user( |
| 99 | + self, |
| 100 | + mock_enterprise_client, |
| 101 | + mock_consent_client, |
| 102 | + ): |
| 103 | + """ |
| 104 | + When the user is linked to an enterprise customer, call both |
| 105 | + EnterpriseApiServiceClient and ConsentApiServiceClient. |
| 106 | + """ |
| 107 | + user = UserFactory.create(username="enterprise-learner") |
| 108 | + enterprise_uuid = uuid.uuid4() |
| 109 | + |
| 110 | + course_key = MagicMock() |
| 111 | + course_key.__str__.return_value = "course-v1:org+course+run" |
| 112 | + |
| 113 | + step = self._make_step() |
| 114 | + result = step.run_filter( |
| 115 | + user=user, |
| 116 | + course_key=course_key, |
| 117 | + linked_enterprise=enterprise_uuid, |
| 118 | + has_api_key_permissions=True, |
| 119 | + ) |
| 120 | + self.assertEqual( |
| 121 | + result, |
| 122 | + { |
| 123 | + "user": user, |
| 124 | + "course_key": course_key, |
| 125 | + "linked_enterprise": enterprise_uuid, |
| 126 | + "has_api_key_permissions": True, |
| 127 | + }, |
| 128 | + ) |
| 129 | + |
| 130 | + mock_enterprise_client.return_value.post_enterprise_course_enrollment.assert_called_once_with( |
| 131 | + "enterprise-learner", |
| 132 | + "course-v1:org+course+run", |
| 133 | + ) |
| 134 | + mock_consent_client.return_value.provide_consent.assert_called_once_with( |
| 135 | + username="enterprise-learner", |
| 136 | + course_id="course-v1:org+course+run", |
| 137 | + enterprise_customer_uuid=str(enterprise_uuid), |
| 138 | + ) |
| 139 | + |
| 140 | + @patch("enterprise.filters.enrollment.ConsentApiServiceClient") |
| 141 | + @patch("enterprise.filters.enrollment.EnterpriseApiServiceClient") |
| 142 | + def test_logs_exception_when_enterprise_api_call_fails( |
| 143 | + self, |
| 144 | + mock_enterprise_client, |
| 145 | + mock_consent_client, |
| 146 | + ): |
| 147 | + """ |
| 148 | + When the enterprise API client raises an exception, it is logged and |
| 149 | + execution continues to the consent API call. |
| 150 | + """ |
| 151 | + user = UserFactory.create(username="enterprise-learner") |
| 152 | + enterprise_uuid = uuid.uuid4() |
| 153 | + |
| 154 | + course_key = MagicMock() |
| 155 | + course_key.__str__.return_value = "course-v1:org+course+run" |
| 156 | + |
| 157 | + # Something goes wrong in the enterprise client |
| 158 | + mock_enterprise_client.return_value.post_enterprise_course_enrollment.side_effect = Exception( |
| 159 | + "boom" |
| 160 | + ) |
| 161 | + |
| 162 | + step = self._make_step() |
| 163 | + result = step.run_filter( |
| 164 | + user=user, |
| 165 | + course_key=course_key, |
| 166 | + linked_enterprise=enterprise_uuid, |
| 167 | + has_api_key_permissions=True, |
| 168 | + ) |
| 169 | + self.assertEqual( |
| 170 | + result, |
| 171 | + { |
| 172 | + "user": user, |
| 173 | + "course_key": course_key, |
| 174 | + "linked_enterprise": enterprise_uuid, |
| 175 | + "has_api_key_permissions": True, |
| 176 | + }, |
| 177 | + ) |
| 178 | + |
| 179 | + # Consent API should still be called despite enrollment API failure |
| 180 | + mock_consent_client.return_value.provide_consent.assert_called_once() |
| 181 | + |
| 182 | + @patch("enterprise.filters.enrollment.ConsentApiServiceClient") |
| 183 | + @patch("enterprise.filters.enrollment.EnterpriseApiServiceClient") |
| 184 | + def test_logs_exception_when_consent_api_call_fails( |
| 185 | + self, |
| 186 | + mock_enterprise_client, |
| 187 | + mock_consent_client, |
| 188 | + ): |
| 189 | + """ |
| 190 | + When the consent API client raises an exception, it is logged and |
| 191 | + the filter still returns the original arguments. |
| 192 | + """ |
| 193 | + user = UserFactory.create(username="enterprise-learner") |
| 194 | + enterprise_uuid = uuid.uuid4() |
| 195 | + |
| 196 | + course_key = MagicMock() |
| 197 | + course_key.__str__.return_value = "course-v1:org+course+run" |
| 198 | + |
| 199 | + # Something goes wrong in the consent client |
| 200 | + mock_consent_client.return_value.provide_consent.side_effect = Exception( |
| 201 | + "consent-boom" |
| 202 | + ) |
| 203 | + |
| 204 | + step = self._make_step() |
| 205 | + result = step.run_filter( |
| 206 | + user=user, |
| 207 | + course_key=course_key, |
| 208 | + linked_enterprise=enterprise_uuid, |
| 209 | + has_api_key_permissions=True, |
| 210 | + ) |
| 211 | + self.assertEqual( |
| 212 | + result, |
| 213 | + { |
| 214 | + "user": user, |
| 215 | + "course_key": course_key, |
| 216 | + "linked_enterprise": enterprise_uuid, |
| 217 | + "has_api_key_permissions": True, |
| 218 | + }, |
| 219 | + ) |
| 220 | + |
| 221 | + # Enterprise API should still be called despite consent API failure |
| 222 | + mock_enterprise_client.return_value.post_enterprise_course_enrollment.assert_called_once() |
0 commit comments