| 1 |
import pytest |
| 2 |
from tests.bidi import recursive_compare |
| 3 |
from tests.support.helpers import get_origin_from_url |
| 4 |
from webdriver.bidi.modules.network import NetworkStringValue |
| 5 |
from webdriver.bidi.modules.storage import ( |
| 6 |
BrowsingContextPartitionDescriptor, |
| 7 |
PartialCookie, |
| 8 |
) |
| 9 |
|
| 10 |
pytestmark = pytest.mark.asyncio |
| 11 |
|
| 12 |
|
| 13 |
@pytest.mark.parametrize( |
| 14 |
"with_document_cookie", |
| 15 |
[True, False], |
| 16 |
ids=["with document cookie", "with set cookie"], |
| 17 |
) |
| 18 |
async def test_partition_context( |
| 19 |
bidi_session, |
| 20 |
new_tab, |
| 21 |
test_page, |
| 22 |
domain_value, |
| 23 |
add_cookie, |
| 24 |
set_cookie, |
| 25 |
with_document_cookie, |
| 26 |
): |
| 27 |
await bidi_session.browsing_context.navigate( |
| 28 |
context=new_tab["context"], url=test_page, wait="complete" |
| 29 |
) |
| 30 |
|
| 31 |
cookie_name = "foo" |
| 32 |
cookie_value = "bar" |
| 33 |
source_origin = get_origin_from_url(test_page) |
| 34 |
partition = BrowsingContextPartitionDescriptor(new_tab["context"]) |
| 35 |
if with_document_cookie: |
| 36 |
await add_cookie(new_tab["context"], cookie_name, cookie_value) |
| 37 |
else: |
| 38 |
await set_cookie( |
| 39 |
cookie=PartialCookie( |
| 40 |
domain=domain_value(), |
| 41 |
name=cookie_name, |
| 42 |
value=NetworkStringValue(cookie_value), |
| 43 |
), |
| 44 |
partition=partition, |
| 45 |
) |
| 46 |
|
| 47 |
result = await bidi_session.storage.delete_cookies(partition=partition) |
| 48 |
recursive_compare({"partitionKey": {"sourceOrigin": source_origin}}, result) |
| 49 |
|
| 50 |
result = await bidi_session.storage.get_cookies(partition=partition) |
| 51 |
assert result["cookies"] == [] |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.parametrize("domain", ["", "alt"], ids=["same_origin", "cross_origin"]) |
| 55 |
async def test_partition_context_iframe_with_set_cookie( |
| 56 |
bidi_session, new_tab, inline, domain_value, domain, set_cookie |
| 57 |
): |
| 58 |
iframe_url = inline("<div id='in-iframe'>foo</div>", domain=domain) |
| 59 |
page_url = inline(f"<iframe src='{iframe_url}'></iframe>") |
| 60 |
await bidi_session.browsing_context.navigate( |
| 61 |
context=new_tab["context"], url=page_url, wait="complete" |
| 62 |
) |
| 63 |
source_origin = get_origin_from_url(iframe_url) |
| 64 |
|
| 65 |
contexts = await bidi_session.browsing_context.get_tree(root=new_tab["context"]) |
| 66 |
iframe_context = contexts[0]["children"][0] |
| 67 |
|
| 68 |
cookie_name = "foo" |
| 69 |
cookie_value = "bar" |
| 70 |
frame_partition = BrowsingContextPartitionDescriptor(iframe_context["context"]) |
| 71 |
await set_cookie( |
| 72 |
cookie=PartialCookie( |
| 73 |
domain=domain_value(domain), |
| 74 |
name=cookie_name, |
| 75 |
value=NetworkStringValue(cookie_value), |
| 76 |
), |
| 77 |
partition=frame_partition, |
| 78 |
) |
| 79 |
|
| 80 |
result = await bidi_session.storage.delete_cookies(partition=frame_partition) |
| 81 |
recursive_compare({"partitionKey": {"sourceOrigin": source_origin}}, result) |
| 82 |
|
| 83 |
result = await bidi_session.storage.get_cookies(partition=frame_partition) |
| 84 |
assert result["cookies"] == [] |
| 85 |
|
| 86 |
|
| 87 |
# Because of Dynamic First-Party Isolation, adding the cookie with `document.cookie` |
| 88 |
# works only with same-origin iframes. |
| 89 |
async def test_partition_context_same_origin_iframe_with_document_cookie( |
| 90 |
bidi_session, |
| 91 |
new_tab, |
| 92 |
inline, |
| 93 |
add_cookie, |
| 94 |
): |
| 95 |
iframe_url = inline("<div id='in-iframe'>foo</div>") |
| 96 |
page_url = inline(f"<iframe src='{iframe_url}'></iframe>") |
| 97 |
await bidi_session.browsing_context.navigate( |
| 98 |
context=new_tab["context"], url=page_url, wait="complete" |
| 99 |
) |
| 100 |
source_origin = get_origin_from_url(iframe_url) |
| 101 |
|
| 102 |
contexts = await bidi_session.browsing_context.get_tree(root=new_tab["context"]) |
| 103 |
iframe_context = contexts[0]["children"][0] |
| 104 |
|
| 105 |
cookie_name = "foo" |
| 106 |
cookie_value = "bar" |
| 107 |
frame_partition = BrowsingContextPartitionDescriptor(iframe_context["context"]) |
| 108 |
await add_cookie(iframe_context["context"], cookie_name, cookie_value) |
| 109 |
|
| 110 |
result = await bidi_session.storage.delete_cookies(partition=frame_partition) |
| 111 |
recursive_compare({"partitionKey": {"sourceOrigin": source_origin}}, result) |
| 112 |
|
| 113 |
result = await bidi_session.storage.get_cookies(partition=frame_partition) |
| 114 |
assert result["cookies"] == [] |