def test_message_call_method_cache_backend_dummy(client, monkeypatch,
                                                 settings):
    monkeypatch.setattr(unicorn_view, "COMPONENTS_MODULE_CACHE_ENABLED", True)
    settings.CACHES["default"][
        "BACKEND"] = "django.core.cache.backends.dummy.DummyCache"

    component_id = shortuuid.uuid()[:8]
    response = post_and_get_response(
        client,
        url="/message/tests.views.fake_components.FakeComponent",
        data={"method_count": 0},
        action_queue=[{
            "payload": {
                "name": "test_method"
            },
            "type": "callMethod",
        }],
        component_id=component_id,
    )

    method_count = response["data"].get("method_count")

    assert method_count == 1

    # Get the component again and it should be found in local memory cache
    view = UnicornView.create(
        component_name="tests.views.fake_components.FakeComponent",
        component_id=component_id,
        use_cache=True,
    )

    # Component is retrieved from the module cache
    assert view.method_count == method_count
def test_message_multiple_calls(client):
    action_queue = [
        {
            "payload": {
                "name": "test_call"
            },
            "type": "callMethod",
            "target": None,
        },
        {
            "payload": {
                "name": "test_call2"
            },
            "type": "callMethod",
            "target": None,
        },
    ]
    response = post_and_get_response(client,
                                     url=FAKE_CALLS_COMPONENT_URL,
                                     action_queue=action_queue)

    assert response.get("calls") == [
        {
            "args": [],
            "fn": "testCall"
        },
        {
            "args": [],
            "fn": "testCall2"
        },
    ]
Beispiel #3
0
def test_message_hash_no_change_but_return_redirect(client):
    component_id = shortuuid.uuid()[:8]
    component = FakeComponent(
        component_id=component_id,
        component_name="tests.views.fake_components.FakeComponent",
    )
    rendered_content = component.render()
    hash = generate_checksum(rendered_content)

    data = {"method_count": 0}
    response = post_and_get_response(
        client,
        url="/message/tests.views.fake_components.FakeComponent",
        data=data,
        action_queue=[{
            "payload": {
                "name": "test_redirect"
            },
            "type": "callMethod",
        }],
        component_id=component_id,
        hash=hash,
    )

    # check that the response is JSON and not a 304
    assert isinstance(response, dict)
    assert response["return"]["value"]
Beispiel #4
0
def test_message_hash_no_change(client):
    component_id = shortuuid.uuid()[:8]
    component = FakeComponent(
        component_id=component_id,
        component_name="tests.views.fake_components.FakeComponent",
    )
    rendered_content = component.render()
    hash = generate_checksum(rendered_content)

    data = {"method_count": 0}
    response = post_and_get_response(
        client,
        url="/message/tests.views.fake_components.FakeComponent",
        data=data,
        action_queue=[{
            "payload": {
                "name": "test_method_kwargs(count=0)"
            },
            "type": "callMethod",
        }],
        component_id=component_id,
        hash=hash,
    )

    assert response.status_code == 304
Beispiel #5
0
def test_message_hash_no_change_but_calls(client):
    component_id = shortuuid.uuid()[:8]
    component = FakeCallsComponent(
        component_id=component_id,
        component_name="tests.views.message.test_calls.FakeCallsComponent",
    )
    rendered_content = component.render()
    hash = generate_checksum(rendered_content)

    data = {}
    response = post_and_get_response(
        client,
        url="/message/tests.views.message.test_calls.FakeCallsComponent",
        data=data,
        action_queue=[{
            "payload": {
                "name": "test_call"
            },
            "type": "callMethod",
        }],
        component_id=component_id,
        hash=hash,
    )

    # check that the response is JSON and not a 304
    assert isinstance(response, dict)
    assert response.get("calls") == [{"args": [], "fn": "testCall"}]
Beispiel #6
0
def test_message_calls_with_arg(client):
    action_queue = [
        {"payload": {"name": "test_call3"}, "type": "callMethod", "target": None,}
    ]

    response = post_and_get_response(
        client, url=FAKE_CALLS_COMPONENT_URL, action_queue=action_queue
    )

    assert response.get("calls") == [{"args": ["hello"], "fn": "testCall3"}]
def test_message_call_method(client):
    data = {"method_count": 0}
    response = post_and_get_response(
        client,
        url="/message/tests.views.fake_components.FakeComponent",
        data=data,
        action_queue=[{
            "payload": {
                "name": "test_method"
            },
            "type": "callMethod",
        }],
    )

    assert response["data"].get("method_count") == 1
def test_message_nested_sync_input(client):
    data = {"dictionary": {"name": "test"}}
    action_queue = [{
        "payload": {
            "name": "dictionary.name",
            "value": "test1"
        },
        "type": "syncInput",
    }]
    response = post_and_get_response(
        client,
        url="/message/tests.views.fake_components.FakeComponent",
        data=data,
        action_queue=action_queue,
    )

    assert not response["errors"]
    assert response["data"].get("dictionary") == {"name": "test1"}
Beispiel #9
0
def test_message_decimal(client):
    data = {"decimal_example": "1.5"}
    action_queue = [{
        "payload": {
            "name": "assert_decimal"
        },
        "type": "callMethod",
    }]
    response = post_and_get_response(
        client,
        url=FAKE_OBJECTS_COMPONENT_URL,
        data=data,
        action_queue=action_queue,
    )

    assert not response.get(
        "error"
    )  # UnicornViewError/AssertionError returns a a JsonResponse with "error" key
    assert not response["errors"]
def test_html_entities_encoded(client):
    data = {"hello": "test"}
    action_queue = [{
        "payload": {
            "name": "hello",
            "value": "<b>test1</b>"
        },
        "type": "syncInput",
    }]
    response = post_and_get_response(
        client,
        url="/message/tests.views.test_process_component_request.FakeComponent",
        data=data,
        action_queue=action_queue,
    )

    assert not response["errors"]
    assert response["data"].get("hello") == "<b>test1</b>"
    assert "&lt;b&gt;test1&lt;/b&gt;" in response["dom"]