Skip to content

Commit

Permalink
fix: unquote query string parameter (#636)
Browse files Browse the repository at this point in the history
* fix: unquote query string parameter

- also includes test to check the checksummed address is passed on in the HTTP request querying models

* fix: test_analysis_eth_unqoute_model_list
  • Loading branch information
nutrina authored Jul 17, 2024
1 parent fb7d185 commit 5181d5c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
3 changes: 2 additions & 1 deletion api/aws_lambdas/passport/analysis_GET.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import asyncio
from urllib.parse import unquote

from django.db import close_old_connections

Expand All @@ -21,7 +22,7 @@ def _handler(event, _context, _request, _user_account, _body):
"""

address = event["path"].split("/")[-1]
model_list = event.get("queryStringParameters", {}).get("model_list", "")
model_list = unquote(event.get("queryStringParameters", {}).get("model_list", ""))

loop = asyncio.get_event_loop()
response = loop.run_until_complete(handle_get_analysis(address, model_list))
Expand Down
25 changes: 23 additions & 2 deletions api/aws_lambdas/passport/tests/test_passport_analysis_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import pytest
from django.conf import settings

import aws_lambdas.passport.analysis_GET
from aws_lambdas.passport.analysis_GET import _handler
from aws_lambdas.scorer_api_passport.tests.helpers import MockContext

from ..analysis_GET import _handler

pytestmark = pytest.mark.django_db

address = "0x06e3c221011767FE816D0B8f5B16253E43e4Af7D"
Expand Down Expand Up @@ -147,3 +147,24 @@ def test_bad_model(
json.loads(response["body"])["error"]
== f"Invalid model name(s): {', '.join([model])}. Must be one of {', '.join(settings.MODEL_ENDPOINTS.keys())}"
)


def test_analysis_eth_unqoute_model_list(
scorer_api_key,
mocker,
):
"""
Tests that the model_list in the aws_lambda function is urlunquoted
"""

event = {
"headers": {"x-api-key": scorer_api_key},
"path": f"/passport/analysis/{address}",
"queryStringParameters": {"model_list": "ethereum_activity%2C%20zksync"},
"isBase64Encoded": False,
}
spy = mocker.spy(aws_lambdas.passport.analysis_GET, "handle_get_analysis")
response = _handler(event, MockContext())

# Check that the model_list is unquoted properly
spy.assert_called_with(address, "ethereum_activity, zksync")
23 changes: 21 additions & 2 deletions api/passport/test/test_analysis.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from unittest.mock import patch

import pytest
from account.models import Account, AccountAPIKey
from aws_lambdas.passport.tests.test_passport_analysis_lambda import mock_post_response
from django.conf import settings
from django.contrib.auth import get_user_model
from django.test import Client, TestCase, override_settings
from web3 import Web3

from account.models import Account, AccountAPIKey
from aws_lambdas.passport.tests.test_passport_analysis_lambda import mock_post_response

pytestmark = pytest.mark.django_db

web3 = Web3()
Expand Down Expand Up @@ -87,3 +88,21 @@ def test_rate_limit_is_applied(self):
**self.headers,
)
assert response.status_code == 429

@patch("passport.api.fetch", side_effect=mock_post_response)
def test_checksummed_address_is_passed_on(self, mock_post):
"""
It is a requirement that the checksummed address is passed on in the requests to the model APIs.
This is not enforced in the models.
Changing this would affect the current cached values
"""
self.client.get(
"/passport/analysis/0x06e3c221011767FE816D0B8f5B16253E43e4Af7d".lower(),
content_type="application/json",
**self.headers,
)

# Verify that the address passed on is the checksummed address
assert mock_post.call_args.args[2] == {
"address": "0x06e3c221011767FE816D0B8f5B16253E43e4Af7D"
}

0 comments on commit 5181d5c

Please sign in to comment.