diff --git a/commerce_coordinator/apps/commercetools/tests/test_clients.py b/commerce_coordinator/apps/commercetools/tests/test_clients.py index fad38ac4..bf2d4082 100644 --- a/commerce_coordinator/apps/commercetools/tests/test_clients.py +++ b/commerce_coordinator/apps/commercetools/tests/test_clients.py @@ -806,94 +806,84 @@ def test_update_customer_with_anonymized_fields_exception(self): def test_is_first_time_discount_eligible_success(self): base_url = self.client_set.get_base_url_from_client() email = 'email@example.com' + code = 'discount-code' - mock_discount_codes = { + mock_orders = { + "total": 1, "results": [ - {"id": "discount-id-1"}, - {"id": "discount-id-2"} + { + "discountCodes": [ + { + "discountCode": { + "obj": { + "code": 'another-code' + } + } + } + ] + } ] } - mock_orders = { - "total": 0 - } - with requests_mock.Mocker(real_http=True, case_sensitive=False) as mocker: - mocker.get( - f"{base_url}discount-codes", - json=mock_discount_codes, - status_code=200 - ) - mocker.get( f"{base_url}orders", json=mock_orders, status_code=200 ) - result = self.client_set.client.is_first_time_discount_eligible(email) + result = self.client_set.client.is_first_time_discount_eligible(email, code) self.assertTrue(result) - def test_is_first_time_discount_eligible_not_eligible(self): + def test_is_first_time_discount_not_eligible(self): base_url = self.client_set.get_base_url_from_client() email = 'email@example.com' + code = 'discount-code' - mock_discount_codes = { + mock_orders = { + "total": 1, "results": [ - {"id": "discount-id-1"}, - {"id": "discount-id-2"} + { + "discountCodes": [ + { + "discountCode": { + "obj": { + "code": code + } + } + } + ] + } ] } - mock_orders = { - "total": 1 - } - with requests_mock.Mocker(real_http=True, case_sensitive=False) as mocker: - mocker.get( - f"{base_url}discount-codes", - json=mock_discount_codes, - status_code=200 - ) - mocker.get( f"{base_url}orders", json=mock_orders, status_code=200 ) - result = self.client_set.client.is_first_time_discount_eligible(email) + result = self.client_set.client.is_first_time_discount_eligible(email, code) self.assertFalse(result) def test_is_first_time_discount_eligible_invalid_email(self): invalid_email = "invalid_email@example.com" + code = 'discount-code' base_url = self.client_set.get_base_url_from_client() - mock_discount_codes = { - "results": [ - {"id": "discount-id-1"}, - {"id": "discount-id-2"} - ] - } - mock_orders = { "total": 0 } with requests_mock.Mocker(real_http=True, case_sensitive=False) as mocker: - mocker.get( - f"{base_url}discount-codes", - json=mock_discount_codes, - status_code=200 - ) - mocker.get( f"{base_url}orders", json=mock_orders, status_code=200 ) - result = self.client_set.client.is_first_time_discount_eligible(invalid_email) + result = self.client_set.client.is_first_time_discount_eligible(invalid_email, code) self.assertTrue(result) diff --git a/commerce_coordinator/apps/commercetools/tests/test_pipeline.py b/commerce_coordinator/apps/commercetools/tests/test_pipeline.py index 8fde2abf..45c20729 100644 --- a/commerce_coordinator/apps/commercetools/tests/test_pipeline.py +++ b/commerce_coordinator/apps/commercetools/tests/test_pipeline.py @@ -271,6 +271,7 @@ class CommercetoolsDiscountEligibilityPipelineTests(TestCase): """Commercetools pipeline testcase for checking discount eligibility in CT""" def setUp(self) -> None: self.mock_email = "mock_email@example.com" + self.mock_code = "mock_code" self.mock_eligible_result = True self.mock_ineligible_result = False @@ -281,7 +282,7 @@ def setUp(self) -> None: def test_pipeline_eligible(self, mock_is_eligible): pipe = CheckCommercetoolsDiscountEligibility("test_pipe", None) mock_is_eligible.return_value = self.mock_eligible_result - ret = pipe.run_filter(self.mock_email) + ret = pipe.run_filter(self.mock_email, self.mock_code) result_data = ret['is_eligible'] self.assertEqual(result_data, self.mock_eligible_result) @@ -292,6 +293,6 @@ def test_pipeline_eligible(self, mock_is_eligible): def test_pipeline_ineligible(self, mock_is_eligible): pipe = CheckCommercetoolsDiscountEligibility("test_pipe", None) mock_is_eligible.return_value = self.mock_ineligible_result - ret = pipe.run_filter(self.mock_email) + ret = pipe.run_filter(self.mock_email, self.mock_code) result_data = ret['is_eligible'] self.assertEqual(result_data, self.mock_ineligible_result) diff --git a/commerce_coordinator/apps/lms/tests/test_views.py b/commerce_coordinator/apps/lms/tests/test_views.py index f0c34967..b4bc0131 100644 --- a/commerce_coordinator/apps/lms/tests/test_views.py +++ b/commerce_coordinator/apps/lms/tests/test_views.py @@ -393,6 +393,7 @@ class FirstTimeDiscountEligibleViewTests(APITestCase): test_user_username = 'test' test_user_email = 'test@example.com' test_user_password = 'secret' + test_discount = 'first_time_discount' url = reverse('lms:first_time_discount_eligible') @@ -421,11 +422,11 @@ def test_get_with_valid_email_eligibility_true(self, mock_filter): self.authenticate_user() mock_filter.return_value = {'is_eligible': True} - response = self.client.get(self.url, {'email': self.test_user_email}) + response = self.client.get(self.url, {'email': self.test_user_email, 'code': self.test_discount}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, {"is_eligible": True}) - mock_filter.assert_called_once_with(email=self.test_user_email) + mock_filter.assert_called_once_with(email=self.test_user_email, code='first_time_discount') @patch('commerce_coordinator.apps.lms.views.CheckFirstTimeDiscountEligibility.run_filter') def test_get_with_valid_email_eligibility_false(self, mock_filter): @@ -435,11 +436,11 @@ def test_get_with_valid_email_eligibility_false(self, mock_filter): self.authenticate_user() mock_filter.return_value = {'is_eligible': False} - response = self.client.get(self.url, {'email': self.test_user_email}) + response = self.client.get(self.url, {'email': self.test_user_email, 'code': self.test_discount}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, {"is_eligible": False}) - mock_filter.assert_called_once_with(email=self.test_user_email) + mock_filter.assert_called_once_with(email=self.test_user_email, code=self.test_discount) def test_get_with_missing_email_fails(self): """ @@ -450,4 +451,4 @@ def test_get_with_missing_email_fails(self): response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) - self.assertEqual(response.data, {'detail': 'Could not detect user email.'}) + self.assertEqual(response.data, {'detail': 'Could not detect user email or discount code.'})