Skip to content

Commit

Permalink
fix: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Noyan Aziz authored and Muhammad Noyan Aziz committed Nov 27, 2024
1 parent e4f2f55 commit 2f4e823
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
93 changes: 93 additions & 0 deletions commerce_coordinator/apps/commercetools/tests/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,99 @@ def test_update_customer_with_anonymized_fields_exception(self):

log_mock.assert_called_once_with(expected_message)

def test_is_first_time_discount_eligible_success(self):
base_url = self.client_set.get_base_url_from_client()
email = '[email protected]'

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(email)
self.assertTrue(result)

def test_is_first_time_discount_eligible_not_eligible(self):
base_url = self.client_set.get_base_url_from_client()
email = '[email protected]'

mock_discount_codes = {
"results": [
{"id": "discount-id-1"},
{"id": "discount-id-2"}
]
}

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)
self.assertFalse(result)

def test_is_first_time_discount_eligible_invalid_email(self):
invalid_email = "[email protected]"
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)
self.assertTrue(result)


class PaginatedResultsTest(TestCase):
"""Tests for the simple logic in our Paginated Results Class"""
Expand Down
31 changes: 31 additions & 0 deletions commerce_coordinator/apps/commercetools/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from commerce_coordinator.apps.commercetools.constants import COMMERCETOOLS_ORDER_MANAGEMENT_SYSTEM
from commerce_coordinator.apps.commercetools.pipeline import (
AnonymizeRetiredUser,
CheckCommercetoolsDiscountEligibility,
CreateReturnForCommercetoolsOrder,
CreateReturnPaymentTransaction,
GetCommercetoolsOrders,
Expand Down Expand Up @@ -264,3 +265,33 @@ def test_pipeline(self, mock_anonymize_fields, mock_customer_by_lms_id, mock_ano
ret = pipe.run_filter(lms_user_id=self.mock_lms_user_id)
result_data = ret['returned_customer']
self.assertEqual(result_data, self.update_customer_response)


class CommercetoolsDiscountEligibilityPipelineTests(TestCase):
"""Commercetools pipeline testcase for checking discount eligibility in CT"""
def setUp(self) -> None:
self.mock_email = "[email protected]"
self.mock_eligible_result = True
self.mock_ineligible_result = False

@patch(
'commerce_coordinator.apps.commercetools.clients.CommercetoolsAPIClient'
'.is_first_time_discount_eligible'
)
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)
result_data = ret['is_eligible']
self.assertEqual(result_data, self.mock_eligible_result)

@patch(
'commerce_coordinator.apps.commercetools.clients.CommercetoolsAPIClient'
'.is_first_time_discount_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)
result_data = ret['is_eligible']
self.assertEqual(result_data, self.mock_ineligible_result)
69 changes: 69 additions & 0 deletions commerce_coordinator/apps/lms/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,72 @@ def test_post_with_unexpected_exception_fails(self, mock_filter):
response = self.client.post(self.url, self.valid_payload, format='json')

self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)


@ddt.ddt
class FirstTimeDiscountEligibleViewTests(APITestCase):
"""
Tests for the FirstTimeDiscountEligibleView to check if a user is eligible for a first-time discount.
"""

test_user_username = 'test'
test_user_email = '[email protected]'
test_user_password = 'secret'

url = reverse('lms:first_time_discount_eligible')

def setUp(self):
super().setUp()
self.user = User.objects.create_user(
self.test_user_username,
self.test_user_email,
self.test_user_password,
is_staff=True,
)

def tearDown(self):
super().tearDown()
self.client.logout()

def authenticate_user(self):
self.client.login(username=self.test_user_username, password=self.test_user_password)
self.client.force_authenticate(user=self.user)

@patch('commerce_coordinator.apps.lms.views.CheckFirstTimeDiscountEligibility.run_filter')
def test_get_with_valid_email_eligibility_true(self, mock_filter):
"""
Test case where the email is eligible for a first-time discount.
"""
self.authenticate_user()
mock_filter.return_value = {'is_eligible': True}

response = self.client.get(self.url, {'email': self.test_user_email})

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)

@patch('commerce_coordinator.apps.lms.views.CheckFirstTimeDiscountEligibility.run_filter')
def test_get_with_valid_email_eligibility_false(self, mock_filter):
"""
Test case where the email is not eligible for a first-time discount.
"""
self.authenticate_user()
mock_filter.return_value = {'is_eligible': False}

response = self.client.get(self.url, {'email': self.test_user_email})

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)

def test_get_with_missing_email_fails(self):
"""
Test case where the email is not provided in the request query params.
"""
self.authenticate_user()

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.'})

0 comments on commit 2f4e823

Please sign in to comment.