Skip to content

Commit cf47bad

Browse files
tests(aiohttp): Drop send_default_pii support in tests (#7711)
1 parent 0fbf5bf commit cf47bad

1 file changed

Lines changed: 83 additions & 123 deletions

File tree

‎tests/integrations/aiohttp/test_aiohttp.py‎

Lines changed: 83 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -545,38 +545,29 @@ async def hello(request):
545545
assert error_event["contexts"]["trace"]["trace_id"] == trace_id
546546

547547

548+
_BREADCRUMB_QUERY_CASES = [
549+
pytest.param({}, "query=value", id="data_collection_default"),
550+
pytest.param(
551+
{"url_query_params": {"mode": "allowlist", "terms": []}},
552+
"query=%5BFiltered%5D",
553+
id="data_collection_allowlist_empty",
554+
),
555+
pytest.param(
556+
{"url_query_params": {"mode": "off"}},
557+
None,
558+
id="data_collection_off",
559+
),
560+
]
561+
562+
548563
@pytest.mark.asyncio
549-
@pytest.mark.parametrize(
550-
"pii_options,url_expected,query_expected",
551-
[
552-
({}, False, False),
553-
(
554-
{
555-
"data_collection": {
556-
"url_query_params": {"mode": "denylist", "terms": []}
557-
}
558-
},
559-
True,
560-
True,
561-
),
562-
(
563-
{
564-
"data_collection": {
565-
"url_query_params": {"mode": "allowlist", "terms": []}
566-
}
567-
},
568-
True,
569-
False,
570-
),
571-
],
572-
)
564+
@pytest.mark.parametrize("data_collection, query_expected", _BREADCRUMB_QUERY_CASES)
573565
async def test_crumb_capture(
574566
sentry_init,
575567
aiohttp_raw_server,
576568
aiohttp_client,
577569
capture_events,
578-
pii_options,
579-
url_expected,
570+
data_collection,
580571
query_expected,
581572
):
582573
def before_breadcrumb(crumb, hint):
@@ -586,7 +577,7 @@ def before_breadcrumb(crumb, hint):
586577
sentry_init(
587578
integrations=[AioHttpIntegration()],
588579
before_breadcrumb=before_breadcrumb,
589-
**pii_options,
580+
data_collection=data_collection,
590581
)
591582

592583
async def handler(request):
@@ -613,15 +604,12 @@ async def handler(request):
613604
"reason": "OK",
614605
}
615606

616-
if url_expected:
617-
if query_expected:
618-
expected["url"] = f"http://127.0.0.1:{raw_server.port}/?query=value"
619-
expected["http.query"] = "query=value"
620-
else:
621-
expected["url"] = (
622-
f"http://127.0.0.1:{raw_server.port}/?query=%5BFiltered%5D"
623-
)
624-
expected["http.query"] = "query=%5BFiltered%5D"
607+
expected["url"] = f"http://127.0.0.1:{raw_server.port}/"
608+
if query_expected is None:
609+
assert "http.query" not in crumb["data"]
610+
else:
611+
expected["url"] += "?" + query_expected
612+
expected["http.query"] = query_expected
625613

626614
assert crumb["data"] == ApproxDict(expected)
627615

@@ -636,30 +624,7 @@ async def handler(request):
636624
(500, "error", "Internal Server Error"),
637625
],
638626
)
639-
@pytest.mark.parametrize(
640-
"pii_options,url_expected,query_expected",
641-
[
642-
({}, False, False),
643-
(
644-
{
645-
"data_collection": {
646-
"url_query_params": {"mode": "denylist", "terms": []}
647-
}
648-
},
649-
True,
650-
True,
651-
),
652-
(
653-
{
654-
"data_collection": {
655-
"url_query_params": {"mode": "allowlist", "terms": []}
656-
}
657-
},
658-
True,
659-
False,
660-
),
661-
],
662-
)
627+
@pytest.mark.parametrize("data_collection, query_expected", _BREADCRUMB_QUERY_CASES)
663628
@pytest.mark.asyncio
664629
async def test_crumb_capture_client_error(
665630
sentry_init,
@@ -669,11 +634,10 @@ async def test_crumb_capture_client_error(
669634
status_code,
670635
level,
671636
reason,
672-
pii_options,
673-
url_expected,
637+
data_collection,
674638
query_expected,
675639
):
676-
sentry_init(integrations=[AioHttpIntegration()], **pii_options)
640+
sentry_init(integrations=[AioHttpIntegration()], data_collection=data_collection)
677641

678642
async def handler(request):
679643
return web.Response(status=status_code)
@@ -703,15 +667,12 @@ async def handler(request):
703667
"reason": reason,
704668
}
705669

706-
if url_expected:
707-
if query_expected:
708-
expected["url"] = f"http://127.0.0.1:{raw_server.port}/?query=value"
709-
expected["http.query"] = "query=value"
710-
else:
711-
expected["url"] = (
712-
f"http://127.0.0.1:{raw_server.port}/?query=%5BFiltered%5D"
713-
)
714-
expected["http.query"] = "query=%5BFiltered%5D"
670+
expected["url"] = f"http://127.0.0.1:{raw_server.port}/"
671+
if query_expected is None:
672+
assert "http.query" not in crumb["data"]
673+
else:
674+
expected["url"] += "?" + query_expected
675+
expected["http.query"] = query_expected
715676

716677
assert crumb["data"] == ApproxDict(expected)
717678

@@ -1475,9 +1436,8 @@ async def hello(request):
14751436

14761437
(server_span,) = [item.payload for item in items]
14771438

1478-
# send_default_pii defaults to False, so _filter_headers substitutes
1479-
# sensitive headers with SENSITIVE_DATA_SUBSTITUTE ("[Filtered]"). The
1480-
# original token must not leak.
1439+
# Data collection always substitutes sensitive headers with
1440+
# SENSITIVE_DATA_SUBSTITUTE ("[Filtered]"). The original token must not leak.
14811441
assert (
14821442
server_span["attributes"]["http.request.header.authorization"]
14831443
== SENSITIVE_DATA_SUBSTITUTE
@@ -1796,14 +1756,27 @@ async def hello(request):
17961756

17971757

17981758
@pytest.mark.asyncio
1799-
@pytest.mark.parametrize("send_pii", [True, False])
1759+
@pytest.mark.parametrize(
1760+
"data_collection, expect_query",
1761+
[
1762+
pytest.param({}, True, id="data_collection_default"),
1763+
pytest.param(
1764+
{"url_query_params": {"mode": "off"}}, False, id="data_collection_off"
1765+
),
1766+
],
1767+
)
18001768
async def test_outgoing_client_span(
1801-
sentry_init, aiohttp_raw_server, aiohttp_client, capture_items, send_pii
1769+
sentry_init,
1770+
aiohttp_raw_server,
1771+
aiohttp_client,
1772+
capture_items,
1773+
data_collection,
1774+
expect_query,
18021775
):
18031776
sentry_init(
18041777
integrations=[AioHttpIntegration()],
18051778
traces_sample_rate=1.0,
1806-
send_default_pii=send_pii,
1779+
data_collection=data_collection,
18071780
)
18081781

18091782
async def handler(request):
@@ -1844,15 +1817,15 @@ async def hello(request):
18441817
assert inner_client_span["attributes"]["http.response.status_code"] == 200
18451818
assert inner_client_span["status"] == "ok"
18461819

1847-
if send_pii:
1820+
if expect_query:
18481821
assert inner_client_span["attributes"]["url.query"] == "foo=bar"
1822+
else:
1823+
assert "url.query" not in inner_client_span["attributes"]
18491824

1850-
url_full = inner_client_span["attributes"]["url.full"]
1851-
1852-
assert url_full.startswith("http://127.0.0.1:")
1853-
assert "?foo=bar" in url_full
1854-
1855-
assert inner_client_span["attributes"]["url.path"] == "/"
1825+
url_full = inner_client_span["attributes"]["url.full"]
1826+
assert url_full.startswith("http://127.0.0.1:")
1827+
assert ("?foo=bar" in url_full) == expect_query
1828+
assert inner_client_span["attributes"]["url.path"] == "/"
18561829

18571830

18581831
@pytest.mark.asyncio
@@ -1888,14 +1861,20 @@ async def handler(request):
18881861

18891862

18901863
@pytest.mark.asyncio
1891-
@pytest.mark.parametrize("send_default_pii", [True, False])
1864+
@pytest.mark.parametrize(
1865+
"data_collection, expect_user_info",
1866+
[
1867+
pytest.param({}, True, id="data_collection_default"),
1868+
pytest.param({"user_info": False}, False, id="data_collection_user_info_off"),
1869+
],
1870+
)
18921871
async def test_user_ip_address_on_all_spans(
1893-
sentry_init, aiohttp_client, capture_items, send_default_pii
1872+
sentry_init, aiohttp_client, capture_items, data_collection, expect_user_info
18941873
):
18951874
sentry_init(
18961875
integrations=[AioHttpIntegration()],
18971876
traces_sample_rate=1.0,
1898-
send_default_pii=send_default_pii,
1877+
data_collection=data_collection,
18991878
)
19001879

19011880
async def hello(request):
@@ -1918,7 +1897,7 @@ async def hello(request):
19181897
assert server_span["attributes"]["sentry.segment.name.source"] == "component"
19191898
assert "sentry.segment.name.source" not in child_span["attributes"]
19201899

1921-
if send_default_pii:
1900+
if expect_user_info:
19221901
assert server_span["attributes"]["user.ip_address"] == "127.0.0.1"
19231902
assert child_span["attributes"]["user.ip_address"] == "127.0.0.1"
19241903
else:
@@ -1928,39 +1907,27 @@ async def hello(request):
19281907

19291908
_QUERY_PARAM_DATA_COLLECTION_CASES = [
19301909
pytest.param(
1931-
{"data_collection": {}},
1910+
{},
19321911
"toy=tennisball&color=red&auth=%5BFiltered%5D",
19331912
id="data_collection_denylist_default",
19341913
),
19351914
pytest.param(
1936-
{
1937-
"data_collection": {
1938-
"url_query_params": {"mode": "denylist", "terms": ["toy"]}
1939-
}
1940-
},
1915+
{"url_query_params": {"mode": "denylist", "terms": ["toy"]}},
19411916
"toy=%5BFiltered%5D&color=red&auth=%5BFiltered%5D",
19421917
id="data_collection_denylist_custom_terms",
19431918
),
19441919
pytest.param(
1945-
{
1946-
"data_collection": {
1947-
"url_query_params": {"mode": "allowlist", "terms": ["toy"]}
1948-
}
1949-
},
1920+
{"url_query_params": {"mode": "allowlist", "terms": ["toy"]}},
19501921
"toy=tennisball&color=%5BFiltered%5D&auth=%5BFiltered%5D",
19511922
id="data_collection_allowlist",
19521923
),
19531924
pytest.param(
1954-
{
1955-
"data_collection": {
1956-
"url_query_params": {"mode": "allowlist", "terms": ["auth"]}
1957-
}
1958-
},
1925+
{"url_query_params": {"mode": "allowlist", "terms": ["auth"]}},
19591926
"toy=%5BFiltered%5D&color=%5BFiltered%5D&auth=%5BFiltered%5D",
19601927
id="data_collection_allowlist_sensitive_term",
19611928
),
19621929
pytest.param(
1963-
{"data_collection": {"url_query_params": {"mode": "off"}}},
1930+
{"url_query_params": {"mode": "off"}},
19641931
None,
19651932
id="data_collection_off",
19661933
),
@@ -1969,16 +1936,15 @@ async def hello(request):
19691936

19701937
@pytest.mark.asyncio
19711938
@pytest.mark.parametrize(
1972-
"init_kwargs, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
1939+
"data_collection, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
19731940
)
19741941
async def test_server_url_query_data_collection(
1975-
sentry_init, aiohttp_client, capture_items, init_kwargs, expected_query
1942+
sentry_init, aiohttp_client, capture_items, data_collection, expected_query
19761943
):
1977-
init_kwargs = dict(init_kwargs)
19781944
sentry_init(
19791945
integrations=[AioHttpIntegration()],
19801946
traces_sample_rate=1.0,
1981-
**init_kwargs,
1947+
data_collection=data_collection,
19821948
)
19831949

19841950
async def hello(request):
@@ -2005,21 +1971,20 @@ async def hello(request):
20051971

20061972
@pytest.mark.asyncio
20071973
@pytest.mark.parametrize(
2008-
"init_kwargs, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
1974+
"data_collection, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
20091975
)
20101976
async def test_client_url_query_data_collection(
20111977
sentry_init,
20121978
aiohttp_raw_server,
20131979
aiohttp_client,
20141980
capture_items,
2015-
init_kwargs,
1981+
data_collection,
20161982
expected_query,
20171983
):
2018-
init_kwargs = dict(init_kwargs)
20191984
sentry_init(
20201985
integrations=[AioHttpIntegration()],
20211986
traces_sample_rate=1.0,
2022-
**init_kwargs,
1987+
data_collection=data_collection,
20231988
)
20241989

20251990
async def handler(request):
@@ -2052,13 +2017,12 @@ async def hello(request):
20522017

20532018
@pytest.mark.asyncio
20542019
@pytest.mark.parametrize(
2055-
"init_kwargs, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
2020+
"data_collection, expected_query", _QUERY_PARAM_DATA_COLLECTION_CASES
20562021
)
20572022
async def test_server_url_query_data_collection_event_processor(
2058-
sentry_init, aiohttp_client, capture_events, init_kwargs, expected_query
2023+
sentry_init, aiohttp_client, capture_events, data_collection, expected_query
20592024
):
2060-
init_kwargs = dict(init_kwargs)
2061-
sentry_init(integrations=[AioHttpIntegration()], **init_kwargs)
2025+
sentry_init(integrations=[AioHttpIntegration()], data_collection=data_collection)
20622026

20632027
async def hello(request):
20642028
1 / 0
@@ -2078,11 +2042,7 @@ async def hello(request):
20782042
assert event["request"]["url"] == "http://{host}/".format(host=host)
20792043
assert event["request"]["method"] == "GET"
20802044

2081-
if "data_collection" not in init_kwargs:
2082-
assert (
2083-
event["request"]["query_string"] == "toy=tennisball&color=red&auth=secret"
2084-
)
2085-
elif expected_query is None:
2045+
if expected_query is None:
20862046
assert "query_string" not in event["request"]
20872047
else:
20882048
assert event["request"]["query_string"] == expected_query

0 commit comments

Comments
 (0)