Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wdspec] Add tests for permissions #43802

Merged
merged 5 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tools/webdriver/webdriver/bidi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tools/webdriver/webdriver/bidi/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions tools/webdriver/webdriver/bidi/modules/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import Any, Optional, Mapping, MutableMapping, Union
from webdriver.bidi.undefined import UNDEFINED, Undefined

from ._module import BidiModule, command


class Permissions(BidiModule):
@command
def set_permission(self,
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,
"origin": origin
}
OrKoN marked this conversation as resolved.
Show resolved Hide resolved
return params
Empty file.
23 changes: 23 additions & 0 deletions webdriver/tests/bidi/permissions/set_permission/__init__.py
Original file line number Diff line number Diff line change
@@ -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"]
56 changes: 56 additions & 0 deletions webdriver/tests/bidi/permissions/set_permission/invalid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import pytest
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])
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, UNDEFINED])
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, UNDEFINED])
async def test_params_origin_invalid_type(bidi_session, origin):
OrKoN marked this conversation as resolved.
Show resolved Hide resolved
with pytest.raises(error.InvalidArgumentException):
await bidi_session.permissions.set_permission(
descriptor={"name": "geolocation"},
state="granted",
origin=origin,
)
105 changes: 105 additions & 0 deletions webdriver/tests/bidi/permissions/set_permission/set_permission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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,
)

@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_set_permission_origin_unknown(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"