| 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.storage import BrowsingContextPartitionDescriptor |
| 5 |
|
| 6 |
pytestmark = pytest.mark.asyncio |
| 7 |
|
| 8 |
|
| 9 |
async def test_partition_context( |
| 10 |
bidi_session, |
| 11 |
new_tab, |
| 12 |
test_page, |
| 13 |
domain_value, |
| 14 |
add_cookie, |
| 15 |
top_context, |
| 16 |
test_page_cross_origin, |
| 17 |
): |
| 18 |
await bidi_session.browsing_context.navigate( |
| 19 |
context=new_tab["context"], url=test_page, wait="complete" |
| 20 |
) |
| 21 |
source_origin_1 = get_origin_from_url(test_page) |
| 22 |
|
| 23 |
await bidi_session.browsing_context.navigate( |
| 24 |
context=top_context["context"], url=test_page_cross_origin, wait="complete" |
| 25 |
) |
| 26 |
source_origin_2 = get_origin_from_url(test_page_cross_origin) |
| 27 |
|
| 28 |
cookie_name = "foo" |
| 29 |
cookie_value = "bar" |
| 30 |
await add_cookie(new_tab["context"], cookie_name, cookie_value) |
| 31 |
|
| 32 |
# Check that added cookies are present on the right context. |
| 33 |
cookies = await bidi_session.storage.get_cookies( |
| 34 |
partition=BrowsingContextPartitionDescriptor(new_tab["context"]) |
| 35 |
) |
| 36 |
|
| 37 |
recursive_compare( |
| 38 |
{ |
| 39 |
"cookies": [ |
| 40 |
{ |
| 41 |
"domain": domain_value(), |
| 42 |
"httpOnly": False, |
| 43 |
"name": cookie_name, |
| 44 |
"path": "/webdriver/tests/support", |
| 45 |
"sameSite": "none", |
| 46 |
"secure": False, |
| 47 |
"size": 6, |
| 48 |
"value": {"type": "string", "value": cookie_value}, |
| 49 |
} |
| 50 |
], |
| 51 |
"partitionKey": {"sourceOrigin": source_origin_1}, |
| 52 |
}, |
| 53 |
cookies, |
| 54 |
) |
| 55 |
|
| 56 |
# Check that added cookies are not present on the other context. |
| 57 |
cookies = await bidi_session.storage.get_cookies( |
| 58 |
partition=BrowsingContextPartitionDescriptor(top_context["context"]) |
| 59 |
) |
| 60 |
|
| 61 |
recursive_compare( |
| 62 |
{ |
| 63 |
"cookies": [], |
| 64 |
"partitionKey": {"sourceOrigin": source_origin_2, "userContext": "default"}, |
| 65 |
}, |
| 66 |
cookies, |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
# Because of Dynamic First-Party Isolation, adding the cookie with `document.cookie` |
| 71 |
# works only with same-origin iframes. |
| 72 |
async def test_partition_context_same_origin_iframe( |
| 73 |
bidi_session, new_tab, inline, domain_value, add_cookie |
| 74 |
): |
| 75 |
iframe_url = inline("<div id='in-iframe'>foo</div>") |
| 76 |
source_origin = get_origin_from_url(iframe_url) |
| 77 |
page_url = inline(f"<iframe src='{iframe_url}'></iframe>") |
| 78 |
await bidi_session.browsing_context.navigate( |
| 79 |
context=new_tab["context"], url=page_url, wait="complete" |
| 80 |
) |
| 81 |
|
| 82 |
contexts = await bidi_session.browsing_context.get_tree(root=new_tab["context"]) |
| 83 |
iframe_context = contexts[0]["children"][0] |
| 84 |
|
| 85 |
cookie_name = "foo" |
| 86 |
cookie_value = "bar" |
| 87 |
await add_cookie(iframe_context["context"], cookie_name, cookie_value) |
| 88 |
|
| 89 |
# Check that added cookies are present on the right context |
| 90 |
cookies = await bidi_session.storage.get_cookies( |
| 91 |
partition=BrowsingContextPartitionDescriptor(iframe_context["context"]) |
| 92 |
) |
| 93 |
|
| 94 |
expected_cookies = [ |
| 95 |
{ |
| 96 |
"domain": domain_value(), |
| 97 |
"httpOnly": False, |
| 98 |
"name": cookie_name, |
| 99 |
"path": "/webdriver/tests/support", |
| 100 |
"sameSite": "none", |
| 101 |
"secure": False, |
| 102 |
"size": 6, |
| 103 |
"value": {"type": "string", "value": cookie_value}, |
| 104 |
} |
| 105 |
] |
| 106 |
|
| 107 |
recursive_compare( |
| 108 |
{ |
| 109 |
"cookies": expected_cookies, |
| 110 |
"partitionKey": {"sourceOrigin": source_origin}, |
| 111 |
}, |
| 112 |
cookies, |
| 113 |
) |
| 114 |
|
| 115 |
cookies = await bidi_session.storage.get_cookies( |
| 116 |
partition=BrowsingContextPartitionDescriptor(new_tab["context"]) |
| 117 |
) |
| 118 |
|
| 119 |
# When the iframe is on the same domain, since the browsing context partition is defined by user context and origin, |
| 120 |
# which will be the same for the page, we get the same cookies as for the iframe. |
| 121 |
recursive_compare( |
| 122 |
{ |
| 123 |
"cookies": expected_cookies, |
| 124 |
"partitionKey": { |
| 125 |
"sourceOrigin": source_origin, |
| 126 |
"userContext": "default", |
| 127 |
}, |
| 128 |
}, |
| 129 |
cookies, |
| 130 |
) |