Skip to content

Commit

Permalink
feat: search transactions by learner email and content title
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnagro committed Nov 6, 2023
1 parent 44df74b commit 5fd88c3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
51 changes: 51 additions & 0 deletions enterprise_subsidy/apps/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def setUp(self):
ledger=self.subsidy_2.ledger,
lms_user_id=STATIC_LMS_USER_ID+1000,
lms_user_email=self.lms_user_email,
content_key=self.content_key_2,
)

# Create third subsidy with a different enterprise_customer_uuid. Neither test learner nor the test admin
Expand Down Expand Up @@ -1353,6 +1354,56 @@ def test_fetch_transaction_with_external_reference(self):
response = self.client.get(os.path.join(url, str(transaction_with_external_reference.uuid) + "/"))
assert response.json().get('external_reference') == [external_reference_id]

@ddt.data(
{
"role": "admin",
"request_query_params": {
"subsidy_uuid": APITestBase.subsidy_2_uuid,
"search": "[email protected]"
},
"expected_response_status": 200,
"expected_response_uuids": [
APITestBase.subsidy_2_transaction_1_uuid,
APITestBase.subsidy_2_transaction_2_uuid,
],
},
{
"role": "admin",
"request_query_params": {
"subsidy_uuid": APITestBase.subsidy_2_uuid,
"search": "Test Course 2"
},
"expected_response_status": 200,
"expected_response_uuids": [
APITestBase.subsidy_2_transaction_2_uuid,
],
},
)
@ddt.unpack
def test_list_search(self, role, request_query_params, expected_response_status, expected_response_uuids):
"""
Test list Transactions permissions.
"""
if role == "admin":
self.set_up_admin()
elif role == "learner":
self.set_up_learner()
elif role == "operator":
self.set_up_operator()
url = reverse("api:v1:transaction-list")
query_string = urllib.parse.urlencode(request_query_params)
if query_string:
query_string = "?" + query_string
response = self.client.get(url + query_string)
assert response.status_code == expected_response_status
if response.status_code < 300:
list_response_data = response.json()["results"]
response_uuids = [tx["uuid"] for tx in list_response_data]
assert (
set(response_uuids) - self.all_initial_transactions ==
set(expected_response_uuids) - self.all_initial_transactions
)


@ddt.ddt
class ContentMetadataViewSetTests(APITestBase):
Expand Down
6 changes: 5 additions & 1 deletion enterprise_subsidy/apps/api/v1/views/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from edx_rbac.utils import ALL_ACCESS_CONTEXT, contexts_accessible_from_jwt
from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication
from openedx_ledger.models import LedgerLockAttemptFailed, Transaction
from rest_framework import mixins, permissions, status, viewsets
from rest_framework import filters, mixins, permissions, status, viewsets
from rest_framework.authentication import SessionAuthentication
from rest_framework.exceptions import ParseError
from rest_framework.response import Response
Expand Down Expand Up @@ -65,6 +65,10 @@ class TransactionViewSet(
lookup_field = "uuid"
serializer_class = TransactionSerializer
pagination_class = TransactionListPaginator
filter_backends = [filters.SearchFilter]

# fields that are queried for search
search_fields = ['lms_user_email', 'content_title']

# Fields that control permissions for 'list' actions, required by PermissionRequiredForListingMixin.
list_lookup_field = "ledger__subsidy__enterprise_customer_uuid"
Expand Down

0 comments on commit 5fd88c3

Please sign in to comment.