-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: catch a possible exception in beta course configuration #36172
Merged
deborahgu
merged 2 commits into
master
from
dkaplan1/APER-3848_improve-exception-handling-in-learner-home-init
Jan 27, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
""" | ||
Unit test for various Utility functions | ||
""" | ||
|
||
import json | ||
from datetime import date, timedelta | ||
from unittest.mock import patch | ||
|
||
import ddt | ||
|
@@ -14,13 +16,44 @@ | |
from common.djangoapps.student.tests.factories import GlobalStaffFactory, UserFactory | ||
from lms.djangoapps.courseware.constants import UNEXPECTED_ERROR_IS_ELIGIBLE | ||
from lms.djangoapps.courseware.tests.factories import FinancialAssistanceConfigurationFactory | ||
from lms.djangoapps.courseware.access_utils import adjust_start_date | ||
from lms.djangoapps.courseware.utils import ( | ||
create_financial_assistance_application, | ||
get_financial_assistance_application_status, | ||
is_eligible_for_financial_aid | ||
is_eligible_for_financial_aid, | ||
) | ||
|
||
|
||
@ddt.ddt | ||
class TestAccessUtils(TestCase): | ||
"""Tests for the access_utils functions.""" | ||
|
||
@ddt.data( | ||
# days_early_for_beta, is_beta_user, expected_date | ||
(None, True, "2025-01-03"), | ||
(2, True, "2025-01-01"), | ||
(timedelta.max.days + 10, True, "2025-01-03"), | ||
(None, False, "2025-01-03"), | ||
(2, False, "2025-01-03"), | ||
(timedelta.max.days + 10, False, "2025-01-03"), | ||
) | ||
@ddt.unpack | ||
def test_adjust_start_date(self, days_early_for_beta, is_beta_user, expected_date): | ||
"""Tests adjust_start_date | ||
|
||
Should only modify the date if the user is beta for the course, | ||
and `days_early_for_beta` is sensible number.""" | ||
start = date(2025, 1, 3) | ||
expected = date.fromisoformat(expected_date) | ||
user = "princessofpower" | ||
course_key = "edx1+8675" | ||
with patch("lms.djangoapps.courseware.access_utils.CourseBetaTesterRole") as role_mock: | ||
instance = role_mock.return_value | ||
instance.has_user.return_value = is_beta_user | ||
adjusted_date = adjust_start_date(user, days_early_for_beta, start, course_key) | ||
self.assertEqual(expected, adjusted_date) | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above – from here on down it is just autoformatting. Again, I can undo it and just put in the individual changes the reviewers think it is too excessive. |
||
@ddt.ddt | ||
class TestFinancialAssistanceViews(TestCase): | ||
""" | ||
|
@@ -29,17 +62,17 @@ class TestFinancialAssistanceViews(TestCase): | |
|
||
def setUp(self) -> None: | ||
super().setUp() | ||
self.test_course_id = 'course-v1:edX+Test+1' | ||
self.test_course_id = "course-v1:edX+Test+1" | ||
self.user = UserFactory() | ||
self.global_staff = GlobalStaffFactory.create() | ||
_ = FinancialAssistanceConfigurationFactory( | ||
api_base_url='http://financial.assistance.test:1234', | ||
api_base_url="http://financial.assistance.test:1234", | ||
service_username=self.global_staff.username, | ||
fa_backend_enabled_courses_percentage=100, | ||
enabled=True | ||
enabled=True, | ||
) | ||
_ = Application.objects.create( | ||
name='Test Application', | ||
name="Test Application", | ||
user=self.global_staff, | ||
client_type=Application.CLIENT_PUBLIC, | ||
authorization_grant_type=Application.GRANT_CLIENT_CREDENTIALS, | ||
|
@@ -51,33 +84,31 @@ def _mock_response(self, status_code, content=None): | |
""" | ||
mock_response = Response() | ||
mock_response.status_code = status_code | ||
mock_response._content = json.dumps(content).encode('utf-8') # pylint: disable=protected-access | ||
mock_response._content = json.dumps(content).encode("utf-8") # pylint: disable=protected-access | ||
return mock_response | ||
|
||
@ddt.data( | ||
{'is_eligible': True, 'reason': None}, | ||
{'is_eligible': False, 'reason': 'This course is not eligible for financial aid'} | ||
{"is_eligible": True, "reason": None}, | ||
{"is_eligible": False, "reason": "This course is not eligible for financial aid"}, | ||
) | ||
def test_is_eligible_for_financial_aid(self, response_data): | ||
""" | ||
Tests the functionality of is_eligible_for_financial_aid which calls edx-financial-assistance backend | ||
to return eligibility status for financial assistance for a given course. | ||
""" | ||
with patch.object(OAuthAPIClient, 'request') as oauth_mock: | ||
with patch.object(OAuthAPIClient, "request") as oauth_mock: | ||
oauth_mock.return_value = self._mock_response(status.HTTP_200_OK, response_data) | ||
is_eligible, reason = is_eligible_for_financial_aid(self.test_course_id) | ||
assert is_eligible is response_data.get('is_eligible') | ||
assert reason == response_data.get('reason') | ||
assert is_eligible is response_data.get("is_eligible") | ||
assert reason == response_data.get("reason") | ||
|
||
def test_is_eligible_for_financial_aid_invalid_course_id(self): | ||
""" | ||
Tests the functionality of is_eligible_for_financial_aid for an invalid course id. | ||
""" | ||
error_message = f"Invalid course id {self.test_course_id} provided" | ||
with patch.object(OAuthAPIClient, 'request') as oauth_mock: | ||
oauth_mock.return_value = self._mock_response( | ||
status.HTTP_400_BAD_REQUEST, {"message": error_message} | ||
) | ||
with patch.object(OAuthAPIClient, "request") as oauth_mock: | ||
oauth_mock.return_value = self._mock_response(status.HTTP_400_BAD_REQUEST, {"message": error_message}) | ||
is_eligible, reason = is_eligible_for_financial_aid(self.test_course_id) | ||
assert is_eligible is False | ||
assert reason == error_message | ||
|
@@ -86,9 +117,9 @@ def test_is_eligible_for_financial_aid_invalid_unexpected_error(self): | |
""" | ||
Tests the functionality of is_eligible_for_financial_aid for an unexpected error | ||
""" | ||
with patch.object(OAuthAPIClient, 'request') as oauth_mock: | ||
with patch.object(OAuthAPIClient, "request") as oauth_mock: | ||
oauth_mock.return_value = self._mock_response( | ||
status.HTTP_500_INTERNAL_SERVER_ERROR, {'error': 'unexpected error occurred'} | ||
status.HTTP_500_INTERNAL_SERVER_ERROR, {"error": "unexpected error occurred"} | ||
) | ||
is_eligible, reason = is_eligible_for_financial_aid(self.test_course_id) | ||
assert is_eligible is False | ||
|
@@ -99,45 +130,39 @@ def test_get_financial_assistance_application_status(self): | |
Tests the functionality of get_financial_assistance_application_status against a user id and a course id | ||
edx-financial-assistance backend to return status of a financial assistance application. | ||
""" | ||
test_response = {'id': 123, 'status': 'ACCEPTED', 'coupon_code': 'ABCD..'} | ||
with patch.object(OAuthAPIClient, 'request') as oauth_mock: | ||
test_response = {"id": 123, "status": "ACCEPTED", "coupon_code": "ABCD.."} | ||
with patch.object(OAuthAPIClient, "request") as oauth_mock: | ||
oauth_mock.return_value = self._mock_response(status.HTTP_200_OK, test_response) | ||
has_application, reason = get_financial_assistance_application_status(self.user.id, self.test_course_id) | ||
assert has_application is True | ||
assert reason == test_response | ||
|
||
@ddt.data( | ||
{ | ||
'status': status.HTTP_400_BAD_REQUEST, | ||
'content': {'message': 'Invalid course id provided'} | ||
}, | ||
{ | ||
'status': status.HTTP_404_NOT_FOUND, | ||
'content': {'message': 'Application details not found'} | ||
} | ||
{"status": status.HTTP_400_BAD_REQUEST, "content": {"message": "Invalid course id provided"}}, | ||
{"status": status.HTTP_404_NOT_FOUND, "content": {"message": "Application details not found"}}, | ||
) | ||
def test_get_financial_assistance_application_status_unsuccessful(self, response_data): | ||
""" | ||
Tests unsuccessful scenarios of get_financial_assistance_application_status | ||
against a user id and a course id edx-financial-assistance backend. | ||
""" | ||
with patch.object(OAuthAPIClient, 'request') as oauth_mock: | ||
oauth_mock.return_value = self._mock_response(response_data.get('status'), response_data.get('content')) | ||
with patch.object(OAuthAPIClient, "request") as oauth_mock: | ||
oauth_mock.return_value = self._mock_response(response_data.get("status"), response_data.get("content")) | ||
has_application, reason = get_financial_assistance_application_status(self.user.id, self.test_course_id) | ||
assert has_application is False | ||
assert reason == response_data.get('content').get('message') | ||
assert reason == response_data.get("content").get("message") | ||
|
||
def test_create_financial_assistance_application(self): | ||
""" | ||
Tests the functionality of create_financial_assistance_application which calls edx-financial-assistance backend | ||
to create a new financial assistance application given a form data. | ||
""" | ||
test_form_data = { | ||
'lms_user_id': self.user.id, | ||
'course_id': self.test_course_id, | ||
"lms_user_id": self.user.id, | ||
"course_id": self.test_course_id, | ||
} | ||
with patch.object(OAuthAPIClient, 'request') as oauth_mock: | ||
oauth_mock.return_value = self._mock_response(status.HTTP_200_OK, {'success': True}) | ||
with patch.object(OAuthAPIClient, "request") as oauth_mock: | ||
oauth_mock.return_value = self._mock_response(status.HTTP_200_OK, {"success": True}) | ||
response = create_financial_assistance_application(form_data=test_form_data) | ||
assert response.status_code == status.HTTP_204_NO_CONTENT | ||
|
||
|
@@ -147,12 +172,12 @@ def test_create_financial_assistance_application_bad_request(self): | |
to create a new financial assistance application given a form data. | ||
""" | ||
test_form_data = { | ||
'lms_user_id': self.user.id, | ||
'course_id': 'invalid_course_id', | ||
"lms_user_id": self.user.id, | ||
"course_id": "invalid_course_id", | ||
} | ||
error_response = {'message': 'Invalid course id provided'} | ||
with patch.object(OAuthAPIClient, 'request') as oauth_mock: | ||
error_response = {"message": "Invalid course id provided"} | ||
with patch.object(OAuthAPIClient, "request") as oauth_mock: | ||
oauth_mock.return_value = self._mock_response(status.HTTP_400_BAD_REQUEST, error_response) | ||
response = create_financial_assistance_application(form_data=test_form_data) | ||
assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
assert json.loads(response.content.decode('utf-8')) == error_response | ||
assert json.loads(response.content.decode("utf-8")) == error_response |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all of the changes in this file from this line down are just because I let my IDE autolint the file to our current standards. Usually I wouldn't commit an autoformat at the same time as an actual code change, but because the code change is just that one tiny block above, and the autoformat isn't extreme, it seemed like a legitimate choice. I can undo if reviewers prefer.