-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added CT discount availed check for outline tab (#303)
- Loading branch information
Showing
6 changed files
with
248 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -883,6 +883,89 @@ 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]' | ||
code = 'discount-code' | ||
|
||
mock_orders = { | ||
"total": 1, | ||
"results": [ | ||
{ | ||
"discountCodes": [ | ||
{ | ||
"discountCode": { | ||
"obj": { | ||
"code": 'another-code' | ||
} | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
|
||
with requests_mock.Mocker(real_http=True, case_sensitive=False) as mocker: | ||
mocker.get( | ||
f"{base_url}orders", | ||
json=mock_orders, | ||
status_code=200 | ||
) | ||
|
||
result = self.client_set.client.is_first_time_discount_eligible(email, code) | ||
self.assertTrue(result) | ||
|
||
def test_is_first_time_discount_not_eligible(self): | ||
base_url = self.client_set.get_base_url_from_client() | ||
email = '[email protected]' | ||
code = 'discount-code' | ||
|
||
mock_orders = { | ||
"total": 1, | ||
"results": [ | ||
{ | ||
"discountCodes": [ | ||
{ | ||
"discountCode": { | ||
"obj": { | ||
"code": code | ||
} | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
|
||
with requests_mock.Mocker(real_http=True, case_sensitive=False) as mocker: | ||
mocker.get( | ||
f"{base_url}orders", | ||
json=mock_orders, | ||
status_code=200 | ||
) | ||
|
||
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 = "[email protected]" | ||
code = 'discount-code' | ||
base_url = self.client_set.get_base_url_from_client() | ||
|
||
mock_orders = { | ||
"total": 0 | ||
} | ||
|
||
with requests_mock.Mocker(real_http=True, case_sensitive=False) as mocker: | ||
mocker.get( | ||
f"{base_url}orders", | ||
json=mock_orders, | ||
status_code=200 | ||
) | ||
|
||
result = self.client_set.client.is_first_time_discount_eligible(invalid_email, code) | ||
self.assertTrue(result) | ||
|
||
|
||
class PaginatedResultsTest(TestCase): | ||
"""Tests for the simple logic in our Paginated Results Class""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -382,3 +382,88 @@ 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' | ||
test_discount = 'first_time_discount' | ||
|
||
valid_payload = { | ||
'email': test_user_email, | ||
'code': test_discount, | ||
} | ||
|
||
invalid_payload = { | ||
'email': None, | ||
'code': 'any_discount', | ||
} | ||
|
||
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.commercetools.clients.CommercetoolsAPIClient' | ||
'.is_first_time_discount_eligible' | ||
) | ||
def test_get_with_valid_email_eligibility_true(self, mock_is_first_time_discount_eligible): | ||
""" | ||
Test case where the user is eligible for a first-time discount. | ||
""" | ||
self.authenticate_user() | ||
mock_is_first_time_discount_eligible.return_value = True | ||
|
||
response = self.client.post(self.url, self.valid_payload, format='json') | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {"is_eligible": True}) | ||
mock_is_first_time_discount_eligible.assert_called_once_with(self.test_user_email, self.test_discount) | ||
|
||
@patch( | ||
'commerce_coordinator.apps.commercetools.clients.CommercetoolsAPIClient' | ||
'.is_first_time_discount_eligible' | ||
) | ||
def test_get_with_valid_email_eligibility_false(self, mock_is_first_time_discount_eligible): | ||
""" | ||
Test case where the user is not eligible for a first-time discount. | ||
""" | ||
self.authenticate_user() | ||
mock_is_first_time_discount_eligible.return_value = False | ||
|
||
response = self.client.post(self.url, self.valid_payload, format='json') | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data, {"is_eligible": False}) | ||
mock_is_first_time_discount_eligible.assert_called_once_with(self.test_user_email, self.test_discount) | ||
|
||
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.post(self.url, self.invalid_payload, format='json') | ||
|
||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters