Skip to content

Commit

Permalink
trying to work out the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnagro committed Nov 1, 2023
1 parent 1a3c0a4 commit 1517e2d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions enterprise_subsidy/apps/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ class Meta:
"state",
"idempotency_key",
"lms_user_id",
"lms_user_email",
"content_key",
"content_title",
"quantity",
"unit", # Manually fetch from parent ledger via get_unit().
"fulfillment_identifier",
Expand Down
23 changes: 21 additions & 2 deletions enterprise_subsidy/apps/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ class APITestBase(APITestMixin):
subsidy_access_policy_1_uuid = str(uuid.uuid4())
subsidy_access_policy_2_uuid = str(uuid.uuid4())
content_key_1 = "course-v1:edX+test+course.1"
content_title_1 = "edx: Test Course 1"
content_key_2 = "course-v1:edX+test+course.2"
content_title_2 = "edx: Test Course 2"
lms_user_email = '[email protected]'

def setUp(self):
super().setUp()
Expand All @@ -69,17 +72,21 @@ def setUp(self):
quantity=-1000,
ledger=self.subsidy_1.ledger,
lms_user_id=STATIC_LMS_USER_ID, # This is the only transaction belonging to the requester.
lms_user_email=self.lms_user_email,
subsidy_access_policy_uuid=self.subsidy_access_policy_1_uuid,
content_key=self.content_key_1,
content_title=self.content_title_1,
)
self.subsidy_1_transaction_2 = TransactionFactory(
uuid=self.subsidy_1_transaction_2_uuid,
state=TransactionStateChoices.COMMITTED,
quantity=-1000,
ledger=self.subsidy_1.ledger,
lms_user_id=STATIC_LMS_USER_ID+1000,
lms_user_email=self.lms_user_email,
subsidy_access_policy_uuid=self.subsidy_access_policy_2_uuid,
content_key=self.content_key_2,
content_title=self.content_title_2,
)

# Create an extra subsidy with the same enterprise_customer_uuid, but the learner does not have any transactions
Expand All @@ -96,13 +103,15 @@ def setUp(self):
quantity=-1000,
ledger=self.subsidy_2.ledger,
lms_user_id=STATIC_LMS_USER_ID+1000,
lms_user_email=self.lms_user_email,
)
TransactionFactory(
uuid=self.subsidy_2_transaction_2_uuid,
state=TransactionStateChoices.COMMITTED,
quantity=-1000,
ledger=self.subsidy_2.ledger,
lms_user_id=STATIC_LMS_USER_ID+1000,
lms_user_email=self.lms_user_email,
)

# Create third subsidy with a different enterprise_customer_uuid. Neither test learner nor the test admin
Expand All @@ -119,13 +128,15 @@ def setUp(self):
quantity=-1000,
ledger=self.subsidy_3.ledger,
lms_user_id=STATIC_LMS_USER_ID+1000,
lms_user_email=self.lms_user_email,
)
TransactionFactory(
uuid=self.subsidy_3_transaction_2_uuid,
state=TransactionStateChoices.COMMITTED,
quantity=-1000,
ledger=self.subsidy_3.ledger,
lms_user_id=STATIC_LMS_USER_ID+1000,
lms_user_email=self.lms_user_email,
)

self.all_initial_transactions = set([
Expand Down Expand Up @@ -254,14 +265,16 @@ def test_can_redeem_bad_request(self, query_params):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

@mock.patch('enterprise_subsidy.apps.api.v1.views.subsidy.can_redeem')
@mock.patch("enterprise_subsidy.apps.subsidy.models.Subsidy.lms_user_client")
@ddt.data(False, True)
def test_can_redeem_happy_path(self, has_existing_transaction, mock_can_redeem):
def test_can_redeem_happy_path(self, has_existing_transaction, mock_lms_user_client, mock_can_redeem):
"""
Tests that the result of ``api.can_redeem()`` is returned as the response
payload for a POST to the can_redeem action, including any relevant
existing transaction.
"""
self.set_up_admin(enterprise_uuids=[self.subsidy_1.enterprise_customer_uuid])
mock_lms_user_client.return_value.best_effort_user_data.return_value = {'email': '[email protected]'}
expected_redeemable = True
expected_active = True
expected_price = 350
Expand Down Expand Up @@ -959,17 +972,20 @@ def test_retrieve_invalid_uuid(self):
@mock.patch("enterprise_subsidy.apps.subsidy.models.Subsidy.enterprise_client")
@mock.patch("enterprise_subsidy.apps.subsidy.models.Subsidy.price_for_content")
@mock.patch("enterprise_subsidy.apps.content_metadata.api.ContentMetadataApi.get_content_summary")
def test_create(self, mock_get_content_summary, mock_price_for_content, mock_enterprise_client):
@mock.patch("enterprise_subsidy.apps.subsidy.models.Subsidy.lms_user_client")
def test_create(self, mock_lms_user_client, mock_get_content_summary, mock_price_for_content, mock_enterprise_client):
"""
Test create Transaction, happy case.
"""
mock_lms_user_client.return_value.best_effort_user_data.return_value = {'email': '[email protected]'}
url = reverse("api:v1:transaction-list")
test_enroll_enterprise_fulfillment_uuid = "test-enroll-reference-id"
mock_enterprise_client.enroll.return_value = test_enroll_enterprise_fulfillment_uuid
mock_price_for_content.return_value = 10000
mock_get_content_summary.return_value = {
'content_uuid': 'course-v1:edX-test-course',
'content_key': 'course-v1:edX-test-course',
'content_title': 'edX: Test Course',
'source': 'edX',
'mode': 'verified',
'content_price': 10000,
Expand All @@ -994,7 +1010,9 @@ def test_create(self, mock_get_content_summary, mock_price_for_content, mock_ent
f"{self.transaction_status_api_url}/{create_response_data['uuid']}/"
)
assert create_response_data["content_key"] == post_data["content_key"]
assert create_response_data["content_title"] == 'edX: Test Course'
assert create_response_data["lms_user_id"] == post_data["lms_user_id"]
assert create_response_data["lms_user_email"] == '[email protected]'
assert create_response_data["subsidy_access_policy_uuid"] == post_data["subsidy_access_policy_uuid"]
assert create_response_data["courseware_url"] == f"http://localhost:2000/course/{post_data['content_key']}/home"
self.assertDictEqual(create_response_data["metadata"], {})
Expand All @@ -1011,6 +1029,7 @@ def test_create(self, mock_get_content_summary, mock_price_for_content, mock_ent
retrieve_response_data = retrieve_response.json()
assert retrieve_response_data["uuid"] == create_response_data["uuid"]
assert retrieve_response_data["idempotency_key"] == create_response_data["idempotency_key"]
# assert retrieve_response_data["content_title"] == 'edX: Test Course'

# Uncomment after Segment events are setup:
#
Expand Down
2 changes: 1 addition & 1 deletion enterprise_subsidy/apps/subsidy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def title_for_content(self, content_key):
try:
content_summary = self.content_metadata_api().get_content_summary(self.enterprise_customer_uuid, content_key)
if content_summary:
content_title = content_summary.get('title')
content_title = content_summary.get('content_title')
except HTTPError as exc:
if exc.response.status_code == status.HTTP_404_NOT_FOUND:
raise ContentNotFoundForCustomerException(
Expand Down
5 changes: 5 additions & 0 deletions enterprise_subsidy/apps/subsidy/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def setUpTestData(cls):
enterprise_customer_uuid=cls.enterprise_customer_uuid,
)
cls.subsidy.content_metadata_api = mock.MagicMock()
#cls.subsidy.lms_user_client = mock.MagicMock()
#cls.subsidy.lms_user_client.return_value.best_effort_user_data.return_value = {'email': '[email protected]'}
super().setUpTestData()

def test_price_for_content(self):
Expand Down Expand Up @@ -198,6 +200,8 @@ def setUp(self):
self.subsidy = SubsidyFactory.create(
enterprise_customer_uuid=self.enterprise_customer_uuid,
)
self.subsidy.lms_user_client = mock.MagicMock()
self.subsidy.lms_user_client.return_value.best_effort_user_data.return_value = {'email': '[email protected]'}
super().setUp()

def test_get_committed_transaction_no_reversal(self):
Expand Down Expand Up @@ -300,6 +304,7 @@ def test_redeem_with_metadata(self, mock_get_content_summary, mock_enterprise_cl
mock_get_content_summary.return_value = {
'content_uuid': 'course-v1:edX+test+course',
'content_key': 'course-v1:edX+test+course',
'content_title': 'edx: Test Course',
'source': 'edX',
'mode': 'verified',
'content_price': 10000,
Expand Down

0 comments on commit 1517e2d

Please sign in to comment.