Repository: stellar/django-polaris
Pull request #656 (2022-08-17 20:34:37)

SEP-24: make `account` param optional in `POST /deposit`
We made the `account` parameter optional in SEP-24's `POST /deposit` endpoint since it can default to the authenticated account. This PR updates Polaris with that change.


Patch:
diff --git a/polaris/sep24/deposit.py b/polaris/sep24/deposit.py
index b4f83f2..9fa32f2 100644
--- a/polaris/sep24/deposit.py
+++ b/polaris/sep24/deposit.py
@@ -414,7 +414,9 @@ def deposit(token: SEP10Token, request: Request) -> Response:
     returns the URL entry-point for the interactive flow.
     """
     asset_code = request.data.get("asset_code")
-    destination_account = request.data.get("account")
+    destination_account = (
+        request.data.get("account") or token.muxed_account or token.account
+    )
     lang = request.data.get("lang")
     sep9_fields = extract_sep9_fields(request.data)
     claimable_balance_supported = request.data.get("claimable_balance_supported")
@@ -440,10 +442,8 @@ def deposit(token: SEP10Token, request: Request) -> Response:
         activate_lang_for_request(lang)
 
     # Verify that the request is valid.
-    if not all([asset_code, destination_account]):
-        return render_error_response(
-            _("`asset_code` and `account` are required parameters")
-        )
+    if not asset_code:
+        return render_error_response(_("`asset_code` is required"))
 
     # Ensure memo won't cause stellar transaction to fail when submitted
     try:


Test patch:
diff --git a/polaris/tests/sep24/test_deposit.py b/polaris/tests/sep24/test_deposit.py
index 8941c59..7002fc0 100644
--- a/polaris/tests/sep24/test_deposit.py
+++ b/polaris/tests/sep24/test_deposit.py
@@ -3,7 +3,7 @@ This module tests the `/deposit` endpoint.
 Celery tasks are called synchronously. Horizon calls are mocked for speed and correctness.
 """
 import json
-from unittest.mock import patch
+from unittest.mock import patch, Mock
 from urllib.parse import quote_plus
 
 import jwt
@@ -158,17 +158,24 @@ def test_deposit_no_params(client):
     content = json.loads(response.content)
 
     assert response.status_code == 400
-    assert content == {"error": "`asset_code` and `account` are required parameters"}
+    assert content == {"error": "`asset_code` is required"}
 
 
+@pytest.mark.django_db
 @patch("polaris.sep10.utils.check_auth", mock_check_auth_success)
+@patch("polaris.sep24.deposit.Keypair", Mock(from_public_key=Mock()))
 def test_deposit_no_account(client):
     """`POST /transactions/deposit/interactive` fails with no `account` parameter."""
-    response = client.post(DEPOSIT_PATH, {"asset_code": "NADA"}, follow=True)
-    content = json.loads(response.content)
+    asset = Asset.objects.create(
+        code="USD",
+        issuer=Keypair.random().public_key,
+        sep24_enabled=True,
+        deposit_enabled=True,
+    )
 
-    assert response.status_code == 400
-    assert content == {"error": "`asset_code` and `account` are required parameters"}
+    response = client.post(DEPOSIT_PATH, {"asset_code": asset.code}, follow=True)
+
+    assert response.status_code == 200
 
 
 @pytest.mark.django_db
@@ -182,7 +189,7 @@ def test_deposit_no_asset(client, acc1_usd_deposit_transaction_factory):
     content = json.loads(response.content)
 
     assert response.status_code == 400
-    assert content == {"error": "`asset_code` and `account` are required parameters"}
+    assert content == {"error": "`asset_code` is required"}
 
 
 @pytest.mark.django_db