Skip to content

Commit

Permalink
[wdspec] Add tests for permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Jan 16, 2024
1 parent 53d5a05 commit 68376e3
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 0 deletions.
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
28 changes: 28 additions & 0 deletions tools/webdriver/webdriver/bidi/modules/permissions.py
Original file line number Diff line number Diff line change
@@ -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
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"]
53 changes: 53 additions & 0 deletions webdriver/tests/bidi/permissions/set_permission/invalid.py
Original file line number Diff line number Diff line change
@@ -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,
)
42 changes: 42 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,42 @@
import pytest

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"

0 comments on commit 68376e3

Please sign in to comment.