2018-07-09 23:22:26 +02:00
|
|
|
from importlib import import_module
|
2019-09-02 13:57:12 +02:00
|
|
|
from typing import Optional, Tuple
|
2018-11-04 14:02:30 +01:00
|
|
|
from unittest.mock import patch
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
from asgiref.sync import sync_to_async
|
|
|
|
from django.conf import settings
|
2019-01-06 16:22:33 +01:00
|
|
|
from django.contrib.auth import BACKEND_SESSION_KEY, HASH_SESSION_KEY, SESSION_KEY
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
from openslides.asgi import application
|
|
|
|
from openslides.core.config import config
|
2018-11-04 14:02:30 +01:00
|
|
|
from openslides.utils.autoupdate import (
|
2019-11-04 14:56:01 +01:00
|
|
|
AutoupdateElement,
|
2018-11-04 14:02:30 +01:00
|
|
|
inform_deleted_data,
|
2019-11-04 14:56:01 +01:00
|
|
|
inform_elements,
|
2018-11-04 14:02:30 +01:00
|
|
|
)
|
2018-07-09 23:22:26 +02:00
|
|
|
from openslides.utils.cache import element_cache
|
2019-07-29 15:19:59 +02:00
|
|
|
from openslides.utils.websocket import (
|
|
|
|
WEBSOCKET_CHANGE_ID_TOO_HIGH,
|
|
|
|
WEBSOCKET_WRONG_FORMAT,
|
|
|
|
)
|
2018-07-09 23:22:26 +02:00
|
|
|
|
2019-09-02 13:57:12 +02:00
|
|
|
from ...unit.utils.cache_provider import (
|
|
|
|
Collection1,
|
|
|
|
Collection2,
|
|
|
|
PersonalizedCollection,
|
|
|
|
get_cachable_provider,
|
|
|
|
)
|
2019-03-06 14:53:24 +01:00
|
|
|
from ..helpers import TConfig, TProjector, TUser
|
2019-06-12 08:18:37 +02:00
|
|
|
from ..websocket import WebsocketCommunicator
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2018-09-01 08:00:00 +02:00
|
|
|
async def prepare_element_cache(settings):
|
2018-07-09 23:22:26 +02:00
|
|
|
"""
|
|
|
|
Resets the element cache.
|
|
|
|
|
|
|
|
Uses a cacheable_provider for tests with example data.
|
|
|
|
"""
|
2018-09-01 08:00:00 +02:00
|
|
|
await element_cache.cache_provider.clear_cache()
|
2018-07-09 23:22:26 +02:00
|
|
|
orig_cachable_provider = element_cache.cachable_provider
|
2019-01-06 16:22:33 +01:00
|
|
|
element_cache.cachable_provider = get_cachable_provider(
|
2019-09-02 13:57:12 +02:00
|
|
|
[
|
|
|
|
Collection1(),
|
|
|
|
Collection2(),
|
|
|
|
PersonalizedCollection(),
|
|
|
|
TConfig(),
|
|
|
|
TUser(),
|
|
|
|
TProjector(),
|
|
|
|
]
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2018-07-09 23:22:26 +02:00
|
|
|
element_cache._cachables = None
|
2020-05-28 13:53:01 +02:00
|
|
|
await element_cache.async_ensure_cache(default_change_id=10)
|
2018-07-09 23:22:26 +02:00
|
|
|
yield
|
|
|
|
# Reset the cachable_provider
|
|
|
|
element_cache.cachable_provider = orig_cachable_provider
|
|
|
|
element_cache._cachables = None
|
2018-09-01 08:00:00 +02:00
|
|
|
await element_cache.cache_provider.clear_cache()
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2018-10-19 16:32:48 +02:00
|
|
|
async def get_communicator():
|
2019-06-12 08:18:37 +02:00
|
|
|
communicator: Optional[WebsocketCommunicator] = None
|
2018-10-19 16:32:48 +02:00
|
|
|
|
2019-09-02 13:57:12 +02:00
|
|
|
def get_communicator(query_string="", headers=None):
|
2018-10-19 16:32:48 +02:00
|
|
|
nonlocal communicator # use the outer communicator variable
|
|
|
|
if query_string:
|
2019-01-12 23:01:42 +01:00
|
|
|
query_string = f"?{query_string}"
|
2019-09-02 13:57:12 +02:00
|
|
|
communicator = WebsocketCommunicator(
|
|
|
|
application, f"/ws/{query_string}", headers=headers
|
|
|
|
)
|
2018-10-19 16:32:48 +02:00
|
|
|
return communicator
|
|
|
|
|
|
|
|
yield get_communicator
|
|
|
|
if communicator:
|
|
|
|
await communicator.disconnect()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
async def communicator(get_communicator):
|
|
|
|
yield get_communicator()
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
2018-11-04 14:02:30 +01:00
|
|
|
@pytest.fixture
|
|
|
|
async def set_config():
|
|
|
|
"""
|
|
|
|
Set a config variable in the element_cache without hitting the database.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-11-04 14:02:30 +01:00
|
|
|
async def _set_config(key, value):
|
2019-01-06 16:22:33 +01:00
|
|
|
with patch("openslides.utils.autoupdate.save_history"):
|
2018-11-04 14:02:30 +01:00
|
|
|
collection_string = config.get_collection_string()
|
|
|
|
config_id = config.key_to_id[key] # type: ignore
|
2019-01-06 16:22:33 +01:00
|
|
|
full_data = {"id": config_id, "key": key, "value": value}
|
2019-11-04 14:56:01 +01:00
|
|
|
await sync_to_async(inform_elements)(
|
2019-01-06 16:22:33 +01:00
|
|
|
[
|
2019-11-04 14:56:01 +01:00
|
|
|
AutoupdateElement(
|
2019-01-06 16:22:33 +01:00
|
|
|
id=config_id,
|
|
|
|
collection_string=collection_string,
|
|
|
|
full_data=full_data,
|
|
|
|
disable_history=True,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2018-11-04 14:02:30 +01:00
|
|
|
|
|
|
|
return _set_config
|
|
|
|
|
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
@pytest.mark.asyncio
|
2018-11-04 14:02:30 +01:00
|
|
|
async def test_normal_connection(get_communicator, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2018-10-19 16:32:48 +02:00
|
|
|
connected, __ = await get_communicator().connect()
|
|
|
|
assert connected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2018-11-04 14:02:30 +01:00
|
|
|
async def test_connection_with_change_id(get_communicator, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
|
|
|
communicator = get_communicator("change_id=0")
|
2018-07-09 23:22:26 +02:00
|
|
|
await communicator.connect()
|
|
|
|
|
|
|
|
response = await communicator.receive_json_from()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
type = response.get("type")
|
|
|
|
content = response.get("content")
|
|
|
|
assert type == "autoupdate"
|
|
|
|
assert "changed" in content
|
|
|
|
assert "deleted" in content
|
|
|
|
assert "from_change_id" in content
|
|
|
|
assert "to_change_id" in content
|
|
|
|
assert Collection1().get_collection_string() in content["changed"]
|
|
|
|
assert Collection2().get_collection_string() in content["changed"]
|
2019-09-02 13:57:12 +02:00
|
|
|
assert PersonalizedCollection().get_collection_string() in content["changed"]
|
2019-01-06 16:22:33 +01:00
|
|
|
assert TConfig().get_collection_string() in content["changed"]
|
|
|
|
assert TUser().get_collection_string() in content["changed"]
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
2019-01-24 16:47:53 +01:00
|
|
|
@pytest.mark.xfail # This will fail until a proper solution in #4009
|
2018-07-09 23:22:26 +02:00
|
|
|
@pytest.mark.asyncio
|
2018-11-04 14:02:30 +01:00
|
|
|
async def test_connection_with_invalid_change_id(get_communicator, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
|
|
|
communicator = get_communicator("change_id=invalid")
|
2018-10-19 16:32:48 +02:00
|
|
|
connected, __ = await communicator.connect()
|
|
|
|
|
|
|
|
assert connected is False
|
|
|
|
|
|
|
|
|
2018-11-02 05:10:49 +01:00
|
|
|
@pytest.mark.asyncio
|
2019-07-29 15:19:59 +02:00
|
|
|
async def test_connection_with_too_big_change_id(get_communicator, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
|
|
|
communicator = get_communicator("change_id=100")
|
2018-11-02 05:10:49 +01:00
|
|
|
|
|
|
|
connected, __ = await communicator.connect()
|
|
|
|
|
|
|
|
assert connected is True
|
2019-07-29 15:19:59 +02:00
|
|
|
await communicator.assert_receive_error(code=WEBSOCKET_CHANGE_ID_TOO_HIGH)
|
2018-11-02 05:10:49 +01:00
|
|
|
|
|
|
|
|
2018-10-19 16:32:48 +02:00
|
|
|
@pytest.mark.asyncio
|
2020-05-15 18:24:21 +02:00
|
|
|
async def test_changed_data_autoupdate(get_communicator, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2020-05-28 13:53:01 +02:00
|
|
|
communicator = get_communicator()
|
2018-07-09 23:22:26 +02:00
|
|
|
await communicator.connect()
|
|
|
|
|
2018-10-19 16:32:48 +02:00
|
|
|
# Change a config value
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_event_name", "Test Event")
|
2018-07-09 23:22:26 +02:00
|
|
|
response = await communicator.receive_json_from()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
id = config.get_key_to_id()["general_event_name"]
|
|
|
|
type = response.get("type")
|
|
|
|
content = response.get("content")
|
|
|
|
assert type == "autoupdate"
|
|
|
|
assert content["changed"] == {
|
|
|
|
"core/config": [{"id": id, "key": "general_event_name", "value": "Test Event"}]
|
|
|
|
}
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
2019-01-24 16:47:53 +01:00
|
|
|
@pytest.mark.xfail # This will fail until a proper solution in #4009
|
2018-07-09 23:22:26 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_anonymous_disabled(communicator):
|
|
|
|
connected, __ = await communicator.connect()
|
|
|
|
|
|
|
|
assert not connected
|
|
|
|
|
|
|
|
|
2019-09-02 13:57:12 +02:00
|
|
|
async def create_user_session_cookie(user_id: int) -> Tuple[bytes, bytes]:
|
2018-07-09 23:22:26 +02:00
|
|
|
# login user with id 1
|
|
|
|
engine = import_module(settings.SESSION_ENGINE)
|
|
|
|
session = engine.SessionStore() # type: ignore
|
2019-09-02 13:57:12 +02:00
|
|
|
session[SESSION_KEY] = str(user_id)
|
2019-01-06 16:22:33 +01:00
|
|
|
session[
|
|
|
|
HASH_SESSION_KEY
|
|
|
|
] = "362d4f2de1463293cb3aaba7727c967c35de43ee" # see helpers.TUser
|
|
|
|
session[BACKEND_SESSION_KEY] = "django.contrib.auth.backends.ModelBackend"
|
2018-07-09 23:22:26 +02:00
|
|
|
session.save()
|
|
|
|
scn = settings.SESSION_COOKIE_NAME
|
2019-09-02 13:57:12 +02:00
|
|
|
cookie_header = (b"cookie", f"{scn}={session.session_key}".encode())
|
|
|
|
return cookie_header
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_with_user(get_communicator):
|
|
|
|
cookie_header = await create_user_session_cookie(1)
|
2020-05-28 13:53:01 +02:00
|
|
|
communicator = get_communicator(headers=[cookie_header])
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
connected, __ = await communicator.connect()
|
|
|
|
|
|
|
|
assert connected
|
|
|
|
|
2019-09-02 13:57:12 +02:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_skipping_autoupdate(set_config, get_communicator):
|
|
|
|
cookie_header = await create_user_session_cookie(1)
|
2020-05-28 13:53:01 +02:00
|
|
|
communicator = get_communicator(headers=[cookie_header])
|
2019-09-02 13:57:12 +02:00
|
|
|
|
|
|
|
await communicator.connect()
|
|
|
|
|
|
|
|
with patch("openslides.utils.autoupdate.save_history"):
|
2019-11-04 14:56:01 +01:00
|
|
|
await sync_to_async(inform_elements)(
|
2019-09-02 13:57:12 +02:00
|
|
|
[
|
2019-11-04 14:56:01 +01:00
|
|
|
AutoupdateElement(
|
2019-09-02 13:57:12 +02:00
|
|
|
id=2,
|
|
|
|
collection_string=PersonalizedCollection().get_collection_string(),
|
|
|
|
full_data={"id": 2, "value": "new value 1", "user_id": 2},
|
|
|
|
disable_history=True,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2019-11-04 14:56:01 +01:00
|
|
|
await sync_to_async(inform_elements)(
|
2019-09-02 13:57:12 +02:00
|
|
|
[
|
2019-11-04 14:56:01 +01:00
|
|
|
AutoupdateElement(
|
2019-09-02 13:57:12 +02:00
|
|
|
id=2,
|
|
|
|
collection_string=PersonalizedCollection().get_collection_string(),
|
|
|
|
full_data={"id": 2, "value": "new value 2", "user_id": 2},
|
|
|
|
disable_history=True,
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
assert await communicator.receive_nothing()
|
|
|
|
|
|
|
|
# Trigger autoupdate
|
|
|
|
await set_config("general_event_name", "Test Event")
|
|
|
|
response = await communicator.receive_json_from()
|
|
|
|
content = response["content"]
|
|
|
|
|
|
|
|
assert PersonalizedCollection().get_collection_string() not in content["deleted"]
|
|
|
|
assert PersonalizedCollection().get_collection_string() not in content["changed"]
|
|
|
|
assert config.get_collection_string() in content["changed"]
|
|
|
|
assert (
|
|
|
|
content["to_change_id"] - content["from_change_id"]
|
|
|
|
) == 2 # Skipped two autoupdates
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2018-11-04 14:02:30 +01:00
|
|
|
async def test_receive_deleted_data(get_communicator, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2020-05-28 13:53:01 +02:00
|
|
|
communicator = get_communicator()
|
2018-07-09 23:22:26 +02:00
|
|
|
await communicator.connect()
|
|
|
|
|
|
|
|
# Delete test element
|
2019-01-06 16:22:33 +01:00
|
|
|
with patch("openslides.utils.autoupdate.save_history"):
|
|
|
|
await sync_to_async(inform_deleted_data)(
|
|
|
|
[(Collection1().get_collection_string(), 1)]
|
|
|
|
)
|
2018-07-09 23:22:26 +02:00
|
|
|
response = await communicator.receive_json_from()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
type = response.get("type")
|
|
|
|
content = response.get("content")
|
|
|
|
assert type == "autoupdate"
|
|
|
|
assert content["deleted"] == {Collection1().get_collection_string(): [1]}
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2018-11-04 14:02:30 +01:00
|
|
|
async def test_send_notify(communicator, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2018-07-09 23:22:26 +02:00
|
|
|
await communicator.connect()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
await communicator.send_json_to(
|
|
|
|
{
|
|
|
|
"type": "notify",
|
2018-10-17 18:04:06 +02:00
|
|
|
"content": {"content": "foobar, what else.", "name": "message_name"},
|
2019-01-06 16:22:33 +01:00
|
|
|
"id": "test",
|
|
|
|
}
|
|
|
|
)
|
2018-08-22 16:50:23 +02:00
|
|
|
response = await communicator.receive_json_from()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
content = response["content"]
|
2018-10-17 18:04:06 +02:00
|
|
|
assert isinstance(content, dict)
|
|
|
|
assert content["content"] == "foobar, what else."
|
|
|
|
assert content["name"] == "message_name"
|
|
|
|
assert "senderChannelName" in content
|
|
|
|
assert content["senderUserId"] == 0
|
2018-08-22 16:50:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2018-11-04 14:02:30 +01:00
|
|
|
async def test_invalid_websocket_message_type(communicator, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2018-08-22 16:50:23 +02:00
|
|
|
await communicator.connect()
|
|
|
|
|
|
|
|
await communicator.send_json_to([])
|
|
|
|
|
2019-07-29 15:19:59 +02:00
|
|
|
await communicator.assert_receive_error(code=WEBSOCKET_WRONG_FORMAT)
|
2018-08-22 16:50:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2018-11-04 14:02:30 +01:00
|
|
|
async def test_invalid_websocket_message_no_id(communicator, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2018-08-22 16:50:23 +02:00
|
|
|
await communicator.connect()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
await communicator.send_json_to({"type": "test", "content": "foobar"})
|
2018-08-22 16:50:23 +02:00
|
|
|
|
2019-07-29 15:19:59 +02:00
|
|
|
await communicator.assert_receive_error(code=WEBSOCKET_WRONG_FORMAT)
|
2018-08-22 16:50:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2018-11-04 14:02:30 +01:00
|
|
|
async def test_send_unknown_type(communicator, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2018-08-22 16:50:23 +02:00
|
|
|
await communicator.connect()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
await communicator.send_json_to(
|
|
|
|
{
|
|
|
|
"type": "if_you_add_this_type_to_openslides_I_will_be_sad",
|
|
|
|
"content": True,
|
|
|
|
"id": "test_id",
|
|
|
|
}
|
|
|
|
)
|
2018-08-22 16:50:23 +02:00
|
|
|
|
2019-07-29 15:19:59 +02:00
|
|
|
await communicator.assert_receive_error(
|
|
|
|
code=WEBSOCKET_WRONG_FORMAT, in_response="test_id"
|
|
|
|
)
|
2018-08-30 09:07:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2018-11-04 14:02:30 +01:00
|
|
|
async def test_request_constants(communicator, settings, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2018-08-30 09:07:06 +02:00
|
|
|
await communicator.connect()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
await communicator.send_json_to(
|
|
|
|
{"type": "constants", "content": "", "id": "test_id"}
|
|
|
|
)
|
2018-08-30 09:07:06 +02:00
|
|
|
|
|
|
|
response = await communicator.receive_json_from()
|
2019-01-06 16:22:33 +01:00
|
|
|
assert response["type"] == "constants"
|
2018-08-30 09:07:06 +02:00
|
|
|
# See conftest.py for the content of 'content'
|
2019-01-06 16:22:33 +01:00
|
|
|
assert response["content"] == {"constant1": "value1", "constant2": "value2"}
|
2018-10-19 16:32:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2018-11-04 14:02:30 +01:00
|
|
|
async def test_send_get_elements(communicator, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2018-10-19 16:32:48 +02:00
|
|
|
await communicator.connect()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
await communicator.send_json_to(
|
|
|
|
{"type": "getElements", "content": {}, "id": "test_id"}
|
|
|
|
)
|
2018-10-19 16:32:48 +02:00
|
|
|
response = await communicator.receive_json_from()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
type = response.get("type")
|
|
|
|
content = response.get("content")
|
|
|
|
assert type == "autoupdate"
|
|
|
|
assert "changed" in content
|
|
|
|
assert "deleted" in content
|
|
|
|
assert "from_change_id" in content
|
|
|
|
assert "to_change_id" in content
|
|
|
|
assert Collection1().get_collection_string() in content["changed"]
|
|
|
|
assert Collection2().get_collection_string() in content["changed"]
|
2019-09-02 13:57:12 +02:00
|
|
|
assert PersonalizedCollection().get_collection_string() in content["changed"]
|
2019-01-06 16:22:33 +01:00
|
|
|
assert TConfig().get_collection_string() in content["changed"]
|
|
|
|
assert TUser().get_collection_string() in content["changed"]
|
2018-10-19 16:32:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2019-07-29 15:19:59 +02:00
|
|
|
async def test_send_get_elements_too_big_change_id(communicator, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2018-10-19 16:32:48 +02:00
|
|
|
await communicator.connect()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
await communicator.send_json_to(
|
|
|
|
{"type": "getElements", "content": {"change_id": 100}, "id": "test_id"}
|
|
|
|
)
|
2019-07-29 15:19:59 +02:00
|
|
|
await communicator.assert_receive_error(
|
|
|
|
code=WEBSOCKET_CHANGE_ID_TOO_HIGH, in_response="test_id"
|
|
|
|
)
|
2018-10-19 16:32:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2019-08-14 12:19:20 +02:00
|
|
|
async def test_send_get_elements_too_small_change_id(communicator, set_config):
|
2020-05-28 13:53:01 +02:00
|
|
|
# Note: this test depends on the default_change_id set in prepare_element_cache
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2018-10-19 16:32:48 +02:00
|
|
|
await communicator.connect()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
await communicator.send_json_to(
|
|
|
|
{"type": "getElements", "content": {"change_id": 1}, "id": "test_id"}
|
|
|
|
)
|
2018-10-19 16:32:48 +02:00
|
|
|
response = await communicator.receive_json_from()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
type = response.get("type")
|
|
|
|
assert type == "autoupdate"
|
|
|
|
assert response.get("in_response") == "test_id"
|
|
|
|
assert response.get("content")["all_data"]
|
2018-10-19 16:32:48 +02:00
|
|
|
|
|
|
|
|
2018-10-27 14:41:27 +02:00
|
|
|
@pytest.mark.asyncio
|
2019-07-29 15:19:59 +02:00
|
|
|
async def test_send_connect_up_to_date(communicator, set_config):
|
2018-10-27 14:41:27 +02:00
|
|
|
"""
|
2019-07-29 15:19:59 +02:00
|
|
|
Test, that a second request with change_id+1 from the first request does not
|
|
|
|
send anything, becuase the client is up to date.
|
2018-10-27 14:41:27 +02:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2018-10-27 14:41:27 +02:00
|
|
|
element_cache.cache_provider.change_id_data = {} # type: ignore
|
|
|
|
await communicator.connect()
|
2019-01-06 16:22:33 +01:00
|
|
|
await communicator.send_json_to(
|
|
|
|
{"type": "getElements", "content": {"change_id": 0}, "id": "test_id"}
|
|
|
|
)
|
2018-10-27 14:41:27 +02:00
|
|
|
response1 = await communicator.receive_json_from()
|
2020-05-15 18:24:21 +02:00
|
|
|
max_change_id = response1.get("content")["to_change_id"]
|
2019-01-06 16:22:33 +01:00
|
|
|
|
|
|
|
await communicator.send_json_to(
|
|
|
|
{
|
|
|
|
"type": "getElements",
|
2020-05-15 18:24:21 +02:00
|
|
|
"content": {"change_id": max_change_id},
|
2019-01-06 16:22:33 +01:00
|
|
|
"id": "test_id",
|
|
|
|
}
|
|
|
|
)
|
2019-07-29 15:19:59 +02:00
|
|
|
assert await communicator.receive_nothing()
|
2018-10-27 14:41:27 +02:00
|
|
|
|
|
|
|
|
2019-01-24 16:47:53 +01:00
|
|
|
@pytest.mark.xfail # This test is broken
|
2018-10-27 14:41:27 +02:00
|
|
|
@pytest.mark.asyncio
|
2019-01-06 16:22:33 +01:00
|
|
|
async def test_send_connect_twice_with_clear_change_id_cache_same_change_id_then_first_request(
|
|
|
|
communicator, set_config
|
|
|
|
):
|
2018-10-27 14:41:27 +02:00
|
|
|
"""
|
|
|
|
Test, that a second request with the change_id from the first request, returns
|
|
|
|
all data.
|
|
|
|
|
|
|
|
A client should not do this but request for change_id+1
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2018-11-03 15:59:24 +01:00
|
|
|
await element_cache.cache_provider.clear_cache()
|
2018-10-27 14:41:27 +02:00
|
|
|
await communicator.connect()
|
2019-01-06 16:22:33 +01:00
|
|
|
await communicator.send_json_to(
|
|
|
|
{"type": "getElements", "content": {"change_id": 0}, "id": "test_id"}
|
|
|
|
)
|
2018-10-27 14:41:27 +02:00
|
|
|
response1 = await communicator.receive_json_from()
|
2019-01-06 16:22:33 +01:00
|
|
|
first_change_id = response1.get("content")["to_change_id"]
|
|
|
|
|
|
|
|
await communicator.send_json_to(
|
|
|
|
{
|
|
|
|
"type": "getElements",
|
|
|
|
"content": {"change_id": first_change_id},
|
|
|
|
"id": "test_id",
|
|
|
|
}
|
|
|
|
)
|
2018-10-27 14:41:27 +02:00
|
|
|
response2 = await communicator.receive_json_from()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
assert response2["type"] == "autoupdate"
|
|
|
|
assert response2.get("content")["all_data"]
|
2018-10-27 14:41:27 +02:00
|
|
|
|
|
|
|
|
2018-11-18 07:57:44 +01:00
|
|
|
@pytest.mark.asyncio
|
2018-11-04 14:02:30 +01:00
|
|
|
async def test_request_changed_elements_no_douple_elements(communicator, set_config):
|
2018-11-18 07:57:44 +01:00
|
|
|
"""
|
|
|
|
Test, that when an elements is changed twice, it is only returned
|
|
|
|
onces when ask a range of change ids.
|
|
|
|
|
|
|
|
Test when all_data is false
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2018-11-18 07:57:44 +01:00
|
|
|
await communicator.connect()
|
|
|
|
# Change element twice
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_event_name", "Test Event")
|
|
|
|
await set_config("general_event_name", "Other value")
|
2018-11-18 07:57:44 +01:00
|
|
|
# Ask for all elements
|
2019-01-06 16:22:33 +01:00
|
|
|
await communicator.send_json_to(
|
|
|
|
{"type": "getElements", "content": {"change_id": 2}, "id": "test_id"}
|
|
|
|
)
|
2018-11-18 07:57:44 +01:00
|
|
|
|
|
|
|
response = await communicator.receive_json_from()
|
2019-01-06 16:22:33 +01:00
|
|
|
type = response.get("type")
|
|
|
|
content = response.get("content")
|
|
|
|
assert type == "autoupdate"
|
|
|
|
assert not response.get("content")["all_data"]
|
|
|
|
config_ids = [e["id"] for e in content["changed"]["core/config"]]
|
2018-11-18 07:57:44 +01:00
|
|
|
# test that config_ids are unique
|
|
|
|
assert len(config_ids) == len(set(config_ids))
|
|
|
|
|
|
|
|
|
2018-10-19 16:32:48 +02:00
|
|
|
@pytest.mark.asyncio
|
2018-11-04 14:02:30 +01:00
|
|
|
async def test_send_invalid_get_elements(communicator, set_config):
|
2019-01-06 16:22:33 +01:00
|
|
|
await set_config("general_system_enable_anonymous", True)
|
2018-10-19 16:32:48 +02:00
|
|
|
await communicator.connect()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
await communicator.send_json_to(
|
|
|
|
{"type": "getElements", "content": {"change_id": "some value"}, "id": "test_id"}
|
|
|
|
)
|
2018-10-19 16:32:48 +02:00
|
|
|
response = await communicator.receive_json_from()
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
type = response.get("type")
|
|
|
|
assert type == "error"
|
|
|
|
assert response.get("in_response") == "test_id"
|
2018-10-19 16:32:48 +02:00
|
|
|
|
|
|
|
|
2018-12-23 11:05:38 +01:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_listen_to_projector(communicator, set_config):
|
|
|
|
await set_config("general_system_enable_anonymous", True)
|
|
|
|
await communicator.connect()
|
|
|
|
|
|
|
|
await communicator.send_json_to(
|
|
|
|
{
|
|
|
|
"type": "listenToProjectors",
|
|
|
|
"content": {"projector_ids": [1]},
|
|
|
|
"id": "test_id",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await communicator.receive_json_from()
|
|
|
|
|
|
|
|
type = response.get("type")
|
|
|
|
content = response.get("content")
|
|
|
|
assert type == "projector"
|
2019-01-18 10:53:23 +01:00
|
|
|
assert content == {
|
2020-05-28 13:53:01 +02:00
|
|
|
"change_id": 11,
|
2019-10-01 15:36:59 +02:00
|
|
|
"data": {
|
|
|
|
"1": [
|
|
|
|
{
|
|
|
|
"data": {"name": "slide1", "event_name": "OpenSlides"},
|
|
|
|
"element": {"id": 1, "name": "test/slide1"},
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2019-01-18 10:53:23 +01:00
|
|
|
}
|
2018-12-23 11:05:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_update_projector(communicator, set_config):
|
|
|
|
await set_config("general_system_enable_anonymous", True)
|
|
|
|
await communicator.connect()
|
|
|
|
await communicator.send_json_to(
|
|
|
|
{
|
|
|
|
"type": "listenToProjectors",
|
|
|
|
"content": {"projector_ids": [1]},
|
|
|
|
"id": "test_id",
|
|
|
|
}
|
|
|
|
)
|
2020-05-15 18:24:21 +02:00
|
|
|
await communicator.receive_json_from() # recieve initial projector data
|
2018-12-23 11:05:38 +01:00
|
|
|
|
|
|
|
# Change a config value
|
|
|
|
await set_config("general_event_name", "Test Event")
|
2020-05-15 18:24:21 +02:00
|
|
|
|
|
|
|
# We need two messages: The autoupdate and the projector data in this order
|
|
|
|
response = await communicator.receive_json_from()
|
|
|
|
assert response.get("type") == "autoupdate"
|
|
|
|
|
2018-12-23 11:05:38 +01:00
|
|
|
response = await communicator.receive_json_from()
|
|
|
|
|
|
|
|
type = response.get("type")
|
|
|
|
content = response.get("content")
|
|
|
|
assert type == "projector"
|
2019-01-18 10:53:23 +01:00
|
|
|
assert content == {
|
2020-05-28 13:53:01 +02:00
|
|
|
"change_id": 12,
|
2019-10-01 15:36:59 +02:00
|
|
|
"data": {
|
|
|
|
"1": [
|
|
|
|
{
|
|
|
|
"data": {"name": "slide1", "event_name": "Test Event"},
|
|
|
|
"element": {"id": 1, "name": "test/slide1"},
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2019-01-18 10:53:23 +01:00
|
|
|
}
|
2018-12-23 11:05:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_update_projector_to_current_value(communicator, set_config):
|
|
|
|
"""
|
|
|
|
When a value does not change, the projector should not be updated.
|
|
|
|
"""
|
|
|
|
await set_config("general_system_enable_anonymous", True)
|
|
|
|
await communicator.connect()
|
|
|
|
await communicator.send_json_to(
|
|
|
|
{
|
|
|
|
"type": "listenToProjectors",
|
|
|
|
"content": {"projector_ids": [1]},
|
|
|
|
"id": "test_id",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
await communicator.receive_json_from()
|
|
|
|
|
|
|
|
# Change a config value to current_value
|
|
|
|
await set_config("general_event_name", "OpenSlides")
|
|
|
|
|
2020-05-15 18:24:21 +02:00
|
|
|
# We await an autoupdate, bot no projector data
|
|
|
|
response = await communicator.receive_json_from()
|
|
|
|
assert response.get("type") == "autoupdate"
|
|
|
|
|
2018-12-23 11:05:38 +01:00
|
|
|
assert await communicator.receive_nothing()
|