From 8cf6d692bb0d95937af622cbbf821a73f4416f87 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Wed, 27 Dec 2023 10:32:26 +0100 Subject: [PATCH 1/5] [wdspec] Add tests for permissions --- tools/webdriver/webdriver/bidi/client.py | 1 + .../webdriver/bidi/modules/__init__.py | 1 + .../webdriver/bidi/modules/permissions.py | 28 +++++++++ webdriver/tests/bidi/permissions/__init__.py | 0 .../permissions/set_permission/__init__.py | 23 +++++++ .../permissions/set_permission/invalid.py | 53 ++++++++++++++++ .../set_permission/set_permission.py | 62 +++++++++++++++++++ 7 files changed, 168 insertions(+) create mode 100644 tools/webdriver/webdriver/bidi/modules/permissions.py create mode 100644 webdriver/tests/bidi/permissions/__init__.py create mode 100644 webdriver/tests/bidi/permissions/set_permission/__init__.py create mode 100644 webdriver/tests/bidi/permissions/set_permission/invalid.py create mode 100644 webdriver/tests/bidi/permissions/set_permission/set_permission.py diff --git a/tools/webdriver/webdriver/bidi/client.py b/tools/webdriver/webdriver/bidi/client.py index 8f891ffafd60d2..73bba55791b3d4 100644 --- a/tools/webdriver/webdriver/bidi/client.py +++ b/tools/webdriver/webdriver/bidi/client.py @@ -94,6 +94,7 @@ def __init__(self, self.browsing_context = modules.BrowsingContext(self) self.input = modules.Input(self) self.network = modules.Network(self) + self.permissions = modules.Permissions(self) self.script = modules.Script(self) self.session = modules.Session(self) self.storage = modules.Storage(self) diff --git a/tools/webdriver/webdriver/bidi/modules/__init__.py b/tools/webdriver/webdriver/bidi/modules/__init__.py index 6f63e85bcd06b8..0a2ef500c42ce0 100644 --- a/tools/webdriver/webdriver/bidi/modules/__init__.py +++ b/tools/webdriver/webdriver/bidi/modules/__init__.py @@ -4,6 +4,7 @@ from .browsing_context import BrowsingContext from .input import Input from .network import Network +from .permissions import Permissions from .script import Script from .session import Session from .storage import Storage diff --git a/tools/webdriver/webdriver/bidi/modules/permissions.py b/tools/webdriver/webdriver/bidi/modules/permissions.py new file mode 100644 index 00000000000000..a914b3b7b28ace --- /dev/null +++ b/tools/webdriver/webdriver/bidi/modules/permissions.py @@ -0,0 +1,28 @@ +from typing import Any, Optional, Mapping, MutableMapping + +from ._module import BidiModule, command + + +class Permissions(BidiModule): + @command + def end(self) -> Mapping[str, Any]: + return {} + + @end.result + async def _end(self, result: Mapping[str, Any]) -> Any: + if self.session.transport: + await self.session.transport.wait_closed() + + return result + + @command + def set_permission(self, + descriptor: Optional[Mapping[str, Any]] = None, + state: Optional[str] = None, + origin: Optional[str] = None) -> Mapping[str, Any]: + params: MutableMapping[str, Any] = { + "descriptor": descriptor, + "state": state, + "origin": origin + } + return params diff --git a/webdriver/tests/bidi/permissions/__init__.py b/webdriver/tests/bidi/permissions/__init__.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/webdriver/tests/bidi/permissions/set_permission/__init__.py b/webdriver/tests/bidi/permissions/set_permission/__init__.py new file mode 100644 index 00000000000000..b8f6358d61746a --- /dev/null +++ b/webdriver/tests/bidi/permissions/set_permission/__init__.py @@ -0,0 +1,23 @@ +from typing import Any, Mapping + +from webdriver.bidi.modules.script import ContextTarget + +async def get_permission_state(bidi_session, context: Mapping[str, Any], name: str) -> str: + result = await bidi_session.script.call_function( + function_declaration="""() => { + return navigator.permissions.query({ name: '%s' }) + .then(val => val.state, err => err.message) + }""" % name, + target=ContextTarget(context["context"]), + await_promise=True) + return result["value"] + + +async def get_context_origin(bidi_session, context: Mapping[str, Any]) -> str: + result = await bidi_session.script.call_function( + function_declaration="""() => { + return window.location.origin; + }""", + target=ContextTarget(context["context"]), + await_promise=False) + return result["value"] diff --git a/webdriver/tests/bidi/permissions/set_permission/invalid.py b/webdriver/tests/bidi/permissions/set_permission/invalid.py new file mode 100644 index 00000000000000..e2263ae62814a3 --- /dev/null +++ b/webdriver/tests/bidi/permissions/set_permission/invalid.py @@ -0,0 +1,53 @@ +import pytest +import webdriver.bidi.error as error + +pytestmark = pytest.mark.asyncio + +@pytest.mark.parametrize("descriptor", [False, "SOME_STRING", 42, {}, [], {"name": 23}, None]) +async def test_params_descriptor_invalid_type(bidi_session, descriptor): + with pytest.raises(error.InvalidArgumentException): + await bidi_session.permissions.set_permission( + descriptor=descriptor, + state="granted", + origin="https://example.com", + ) + + +@pytest.mark.parametrize("descriptor", [{"name": "unknown"}]) +async def test_params_descriptor_invalid_value(bidi_session, descriptor): + with pytest.raises(error.InvalidArgumentException): + await bidi_session.permissions.set_permission( + descriptor=descriptor, + state="granted", + origin="https://example.com", + ) + + +@pytest.mark.parametrize("state", [False, 42, {}, [], None]) +async def test_params_state_invalid_type(bidi_session, state): + with pytest.raises(error.InvalidArgumentException): + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state=state, + origin="https://example.com", + ) + + +@pytest.mark.parametrize("state", ["UNKOWN", "Granted"]) +async def test_params_state_invalid_value(bidi_session, state): + with pytest.raises(error.InvalidArgumentException): + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state=state, + origin="https://example.com", + ) + + +@pytest.mark.parametrize("origin", [False, 42, {}, [], None]) +async def test_params_origin_invalid_type(bidi_session, origin): + with pytest.raises(error.InvalidArgumentException): + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state="granted", + origin=origin, + ) diff --git a/webdriver/tests/bidi/permissions/set_permission/set_permission.py b/webdriver/tests/bidi/permissions/set_permission/set_permission.py new file mode 100644 index 00000000000000..7f38adad24b129 --- /dev/null +++ b/webdriver/tests/bidi/permissions/set_permission/set_permission.py @@ -0,0 +1,62 @@ +import pytest +import webdriver.bidi.error as error + +from . import get_context_origin, get_permission_state + +pytestmark = pytest.mark.asyncio + +@pytest.mark.asyncio +async def test_set_permission(bidi_session, new_tab, url): + test_url = url("/common/blank.html", protocol="https") + await bidi_session.browsing_context.navigate( + context=new_tab["context"], + url=test_url, + wait="complete", + ) + + origin = await get_context_origin(bidi_session, new_tab) + + assert await get_permission_state(bidi_session, new_tab, "geolocation") == "prompt" + + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state="granted", + origin=origin, + ) + + assert await get_permission_state(bidi_session, new_tab, "geolocation") == "granted" + + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state="denied", + origin=origin, + ) + + assert await get_permission_state(bidi_session, new_tab, "geolocation") == "denied" + + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state="prompt", + origin=origin, + ) + + assert await get_permission_state(bidi_session, new_tab, "geolocation") == "prompt" + + +@pytest.mark.asyncio +async def test_set_permission_insecure_context(bidi_session, new_tab, url): + test_url = url("/common/blank.html", protocol="http") + await bidi_session.browsing_context.navigate( + context=new_tab["context"], + url=test_url, + wait="complete", + ) + + origin = await get_context_origin(bidi_session, new_tab) + + with pytest.raises(error.InvalidArgumentException): + await bidi_session.permissions.set_permission( + descriptor={"name": "push"}, + state="granted", + origin=origin, + ) From a4a891b283ee3758d85667aa1c64a5a54c08d110 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Tue, 16 Jan 2024 11:08:45 +0100 Subject: [PATCH 2/5] Add undefined tests --- .../webdriver/bidi/modules/permissions.py | 18 ++++-------------- .../bidi/permissions/set_permission/invalid.py | 7 ++++--- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/tools/webdriver/webdriver/bidi/modules/permissions.py b/tools/webdriver/webdriver/bidi/modules/permissions.py index a914b3b7b28ace..c06a2a3e5f67d6 100644 --- a/tools/webdriver/webdriver/bidi/modules/permissions.py +++ b/tools/webdriver/webdriver/bidi/modules/permissions.py @@ -1,25 +1,15 @@ from typing import Any, Optional, Mapping, MutableMapping +from webdriver.bidi.undefined import UNDEFINED from ._module import BidiModule, command class Permissions(BidiModule): - @command - def end(self) -> Mapping[str, Any]: - return {} - - @end.result - async def _end(self, result: Mapping[str, Any]) -> Any: - if self.session.transport: - await self.session.transport.wait_closed() - - return result - @command def set_permission(self, - descriptor: Optional[Mapping[str, Any]] = None, - state: Optional[str] = None, - origin: Optional[str] = None) -> Mapping[str, Any]: + descriptor: Optional[Mapping[str, Any]] = UNDEFINED, + state: Optional[str] = UNDEFINED, + origin: Optional[str] = UNDEFINED) -> Mapping[str, Any]: params: MutableMapping[str, Any] = { "descriptor": descriptor, "state": state, diff --git a/webdriver/tests/bidi/permissions/set_permission/invalid.py b/webdriver/tests/bidi/permissions/set_permission/invalid.py index e2263ae62814a3..0ef8c57f4154e5 100644 --- a/webdriver/tests/bidi/permissions/set_permission/invalid.py +++ b/webdriver/tests/bidi/permissions/set_permission/invalid.py @@ -1,9 +1,10 @@ import pytest import webdriver.bidi.error as error +from webdriver.bidi.undefined import UNDEFINED pytestmark = pytest.mark.asyncio -@pytest.mark.parametrize("descriptor", [False, "SOME_STRING", 42, {}, [], {"name": 23}, None]) +@pytest.mark.parametrize("descriptor", [False, "SOME_STRING", 42, {}, [], {"name": 23}, None, UNDEFINED]) async def test_params_descriptor_invalid_type(bidi_session, descriptor): with pytest.raises(error.InvalidArgumentException): await bidi_session.permissions.set_permission( @@ -23,7 +24,7 @@ async def test_params_descriptor_invalid_value(bidi_session, descriptor): ) -@pytest.mark.parametrize("state", [False, 42, {}, [], None]) +@pytest.mark.parametrize("state", [False, 42, {}, [], None, UNDEFINED]) async def test_params_state_invalid_type(bidi_session, state): with pytest.raises(error.InvalidArgumentException): await bidi_session.permissions.set_permission( @@ -43,7 +44,7 @@ async def test_params_state_invalid_value(bidi_session, state): ) -@pytest.mark.parametrize("origin", [False, 42, {}, [], None]) +@pytest.mark.parametrize("origin", [False, 42, {}, [], None, UNDEFINED]) async def test_params_origin_invalid_type(bidi_session, origin): with pytest.raises(error.InvalidArgumentException): await bidi_session.permissions.set_permission( From 0b8518f752b50ff1b8aa7a90dd839a1599a625d0 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Tue, 16 Jan 2024 12:54:56 +0100 Subject: [PATCH 3/5] add tests --- .../webdriver/bidi/modules/permissions.py | 10 ++--- .../permissions/set_permission/invalid.py | 2 + .../set_permission/set_permission.py | 43 +++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/tools/webdriver/webdriver/bidi/modules/permissions.py b/tools/webdriver/webdriver/bidi/modules/permissions.py index c06a2a3e5f67d6..7d9e2fefa74906 100644 --- a/tools/webdriver/webdriver/bidi/modules/permissions.py +++ b/tools/webdriver/webdriver/bidi/modules/permissions.py @@ -1,5 +1,5 @@ -from typing import Any, Optional, Mapping, MutableMapping -from webdriver.bidi.undefined import UNDEFINED +from typing import Any, Optional, Mapping, MutableMapping, Union +from webdriver.bidi.undefined import UNDEFINED, Undefined from ._module import BidiModule, command @@ -7,9 +7,9 @@ class Permissions(BidiModule): @command def set_permission(self, - descriptor: Optional[Mapping[str, Any]] = UNDEFINED, - state: Optional[str] = UNDEFINED, - origin: Optional[str] = UNDEFINED) -> Mapping[str, Any]: + descriptor: Union[Optional[Mapping[str, Any]], Undefined] = UNDEFINED, + state: Union[Optional[str], Undefined] = UNDEFINED, + origin: Union[Optional[str], Undefined] = UNDEFINED) -> Mapping[str, Any]: params: MutableMapping[str, Any] = { "descriptor": descriptor, "state": state, diff --git a/webdriver/tests/bidi/permissions/set_permission/invalid.py b/webdriver/tests/bidi/permissions/set_permission/invalid.py index 0ef8c57f4154e5..8f67425aaaeae1 100644 --- a/webdriver/tests/bidi/permissions/set_permission/invalid.py +++ b/webdriver/tests/bidi/permissions/set_permission/invalid.py @@ -2,6 +2,8 @@ import webdriver.bidi.error as error from webdriver.bidi.undefined import UNDEFINED +from . import get_permission_state + pytestmark = pytest.mark.asyncio @pytest.mark.parametrize("descriptor", [False, "SOME_STRING", 42, {}, [], {"name": 23}, None, UNDEFINED]) diff --git a/webdriver/tests/bidi/permissions/set_permission/set_permission.py b/webdriver/tests/bidi/permissions/set_permission/set_permission.py index 7f38adad24b129..e72d56e4742fe4 100644 --- a/webdriver/tests/bidi/permissions/set_permission/set_permission.py +++ b/webdriver/tests/bidi/permissions/set_permission/set_permission.py @@ -60,3 +60,46 @@ async def test_set_permission_insecure_context(bidi_session, new_tab, url): state="granted", origin=origin, ) + +@pytest.mark.asyncio +async def test_set_permission_new_context(bidi_session, new_tab, url): + test_url = url("/common/blank.html", protocol="https") + + await bidi_session.browsing_context.navigate( + context=new_tab["context"], + url=test_url, + wait="complete", + ) + + origin = await get_context_origin(bidi_session, new_tab) + + assert await get_permission_state(bidi_session, new_tab, "geolocation") == "prompt" + + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state="granted", + origin=origin, + ) + + assert await get_permission_state(bidi_session, new_tab, "geolocation") == "granted" + + new_context = await bidi_session.browsing_context.create(type_hint="tab") + assert new_tab["context"] != new_context["context"] + await bidi_session.browsing_context.navigate( + context=new_context["context"], + url=test_url, + wait="complete", + ) + + # See https://github.com/w3c/permissions/issues/437. + assert await get_permission_state(bidi_session, new_context, "geolocation") == "granted" + + +@pytest.mark.parametrize("origin", ['UNKNOWN', '']) +async def test_params_origin_invalid_value(bidi_session, new_tab, origin): + await bidi_session.permissions.set_permission( + descriptor={"name": "geolocation"}, + state="granted", + origin=origin, + ) + assert await get_permission_state(bidi_session, new_tab, "geolocation") == "prompt" From dd58d8d1cc6399ceb69f05ba02a14ef80932a36c Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Tue, 16 Jan 2024 15:01:12 +0100 Subject: [PATCH 4/5] Update webdriver/tests/bidi/permissions/set_permission/set_permission.py Co-authored-by: Maksim Sadym <69349599+sadym-chromium@users.noreply.github.com> --- .../tests/bidi/permissions/set_permission/set_permission.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webdriver/tests/bidi/permissions/set_permission/set_permission.py b/webdriver/tests/bidi/permissions/set_permission/set_permission.py index e72d56e4742fe4..dc6ca14a8d5aca 100644 --- a/webdriver/tests/bidi/permissions/set_permission/set_permission.py +++ b/webdriver/tests/bidi/permissions/set_permission/set_permission.py @@ -96,7 +96,7 @@ async def test_set_permission_new_context(bidi_session, new_tab, url): @pytest.mark.parametrize("origin", ['UNKNOWN', '']) -async def test_params_origin_invalid_value(bidi_session, new_tab, origin): +async def test_set_permission_origin_unknown(bidi_session, new_tab, origin): await bidi_session.permissions.set_permission( descriptor={"name": "geolocation"}, state="granted", From 77ff37aa5a8765a60f326b34facdbae99c20d01e Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Tue, 16 Jan 2024 15:01:12 +0100 Subject: [PATCH 5/5] Update webdriver/tests/bidi/permissions/set_permission/set_permission.py Co-authored-by: Maksim Sadym <69349599+sadym-chromium@users.noreply.github.com> --- tools/webdriver/webdriver/bidi/modules/permissions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/webdriver/webdriver/bidi/modules/permissions.py b/tools/webdriver/webdriver/bidi/modules/permissions.py index 7d9e2fefa74906..3062260b34849f 100644 --- a/tools/webdriver/webdriver/bidi/modules/permissions.py +++ b/tools/webdriver/webdriver/bidi/modules/permissions.py @@ -7,8 +7,8 @@ class Permissions(BidiModule): @command def set_permission(self, - descriptor: Union[Optional[Mapping[str, Any]], Undefined] = UNDEFINED, - state: Union[Optional[str], Undefined] = UNDEFINED, + descriptor: Union[Optional[Mapping[str, Any]], Undefined] = UNDEFINED, + state: Union[Optional[str], Undefined] = UNDEFINED, origin: Union[Optional[str], Undefined] = UNDEFINED) -> Mapping[str, Any]: params: MutableMapping[str, Any] = { "descriptor": descriptor,