summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/mozilla/tests/webdriver/bidi/storage/delete_cookies/partition.py
blob: d8e2729fe233018603dfc7d2c1860e13aeec420b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import pytest
from tests.bidi import recursive_compare
from tests.support.helpers import get_origin_from_url
from webdriver.bidi.modules.network import NetworkStringValue
from webdriver.bidi.modules.storage import (
    BrowsingContextPartitionDescriptor,
    PartialCookie,
)

pytestmark = pytest.mark.asyncio


@pytest.mark.parametrize(
    "with_document_cookie",
    [True, False],
    ids=["with document cookie", "with set cookie"],
)
async def test_partition_context(
    bidi_session,
    new_tab,
    test_page,
    domain_value,
    add_cookie,
    set_cookie,
    with_document_cookie,
):
    await bidi_session.browsing_context.navigate(
        context=new_tab["context"], url=test_page, wait="complete"
    )

    cookie_name = "foo"
    cookie_value = "bar"
    source_origin = get_origin_from_url(test_page)
    partition = BrowsingContextPartitionDescriptor(new_tab["context"])
    if with_document_cookie:
        await add_cookie(new_tab["context"], cookie_name, cookie_value)
    else:
        await set_cookie(
            cookie=PartialCookie(
                domain=domain_value(),
                name=cookie_name,
                value=NetworkStringValue(cookie_value),
            ),
            partition=partition,
        )

    result = await bidi_session.storage.delete_cookies(partition=partition)
    recursive_compare({"partitionKey": {"sourceOrigin": source_origin}}, result)

    result = await bidi_session.storage.get_cookies(partition=partition)
    assert result["cookies"] == []


@pytest.mark.parametrize("domain", ["", "alt"], ids=["same_origin", "cross_origin"])
async def test_partition_context_iframe_with_set_cookie(
    bidi_session, new_tab, inline, domain_value, domain, set_cookie
):
    iframe_url = inline("<div id='in-iframe'>foo</div>", domain=domain)
    page_url = inline(f"<iframe src='{iframe_url}'></iframe>")
    await bidi_session.browsing_context.navigate(
        context=new_tab["context"], url=page_url, wait="complete"
    )
    source_origin = get_origin_from_url(iframe_url)

    contexts = await bidi_session.browsing_context.get_tree(root=new_tab["context"])
    iframe_context = contexts[0]["children"][0]

    cookie_name = "foo"
    cookie_value = "bar"
    frame_partition = BrowsingContextPartitionDescriptor(iframe_context["context"])
    await set_cookie(
        cookie=PartialCookie(
            domain=domain_value(domain),
            name=cookie_name,
            value=NetworkStringValue(cookie_value),
        ),
        partition=frame_partition,
    )

    result = await bidi_session.storage.delete_cookies(partition=frame_partition)
    recursive_compare({"partitionKey": {"sourceOrigin": source_origin}}, result)

    result = await bidi_session.storage.get_cookies(partition=frame_partition)
    assert result["cookies"] == []


# Because of Dynamic First-Party Isolation, adding the cookie with `document.cookie`
# works only with same-origin iframes.
async def test_partition_context_same_origin_iframe_with_document_cookie(
    bidi_session,
    new_tab,
    inline,
    add_cookie,
):
    iframe_url = inline("<div id='in-iframe'>foo</div>")
    page_url = inline(f"<iframe src='{iframe_url}'></iframe>")
    await bidi_session.browsing_context.navigate(
        context=new_tab["context"], url=page_url, wait="complete"
    )
    source_origin = get_origin_from_url(iframe_url)

    contexts = await bidi_session.browsing_context.get_tree(root=new_tab["context"])
    iframe_context = contexts[0]["children"][0]

    cookie_name = "foo"
    cookie_value = "bar"
    frame_partition = BrowsingContextPartitionDescriptor(iframe_context["context"])
    await add_cookie(iframe_context["context"], cookie_name, cookie_value)

    result = await bidi_session.storage.delete_cookies(partition=frame_partition)
    recursive_compare({"partitionKey": {"sourceOrigin": source_origin}}, result)

    result = await bidi_session.storage.get_cookies(partition=frame_partition)
    assert result["cookies"] == []