Esempio n. 1
0
async def test_put_custom_state_key(patched_send, opsdroid_matrix):
    patched_send.return_value = nio.RoomGetStateEventResponse({}, "", "", "")
    patched_send.return_value.transport_response = AsyncMock()
    patched_send.return_value.transport_response.status = 404

    db = DatabaseMatrix(
        {
            "should_encrypt": False,
            "single_state_key": "wibble"
        },
        opsdroid=opsdroid_matrix,
    )
    db.should_migrate = False
    await db.put("twim", {"hello": "world"})

    patched_send.assert_has_calls([
        matrix_call(
            "GET",
            "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/wibble",
        ),
        matrix_call(
            "PUT",
            "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/wibble",
            {"twim": {
                "hello": "world"
            }},
        ),
    ])
Esempio n. 2
0
async def test_single_state_not_a_dict(patched_send, opsdroid_matrix):
    patched_send.return_value = nio.RoomGetStateEventResponse({}, "", "", "")
    patched_send.return_value.transport_response = AsyncMock()
    patched_send.return_value.transport_response.status = 404

    value = "world"
    db = DatabaseMatrix({
        "should_encrypt": False,
        "single_state_key": True
    },
                        opsdroid=opsdroid_matrix)
    db.should_migrate = False
    await db.put("twim", value)

    patched_send.assert_has_calls([
        matrix_call(
            "GET",
            "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/",
        ),
        matrix_call(
            "PUT",
            "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/",
            {"twim": value},
        ),
    ])
Esempio n. 3
0
 def side_effect(resp, *args, **kwargs):
     if resp is nio.RoomGetStateEventResponse:
         resp = nio.RoomGetStateEventResponse(
             {"twim": {
                 "encrypted_val": "enceventid"
             }}, "", "", "")
         resp.transport_response = AsyncMock()
         resp.transport_response.status = 200
         return resp
     elif resp is nio.RoomGetEventResponse:
         event = nio.Event({
             "type": "dev.opsdroid.database",
             "event_id": "enceventid",
             "sender": "@someone:localhost",
             "origin_server_ts": "2005",
             "content": {
                 "twim": {
                     "hello": "world"
                 }
             },
         })
         resp = nio.RoomGetEventResponse()
         resp.event = event
         return resp
     else:
         return nio.RoomSendResponse("enceventid", "!notaroomid")
Esempio n. 4
0
async def test_default_update_same_key_value_single_state_key(
        patched_send, opsdroid_matrix, caplog):
    patched_send.return_value = nio.RoomGetStateEventResponse(
        {"twim": {
            "hello": "world"
        }}, "", "", "")

    db = DatabaseMatrix({
        "should_encrypt": False,
        "single_state_key": True
    },
                        opsdroid=opsdroid_matrix)
    db.should_migrate = False
    caplog.clear()
    await db.put("twim", {"hello": "world"})

    patched_send.assert_has_calls([
        matrix_call(
            "GET",
            "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/",
        )
    ])

    assert ["Not updating matrix state, as content hasn't changed."
            ] == [rec.message for rec in caplog.records]
Esempio n. 5
0
async def test_get_empty(patched_send, opsdroid_matrix):
    patched_send.return_value = nio.RoomGetStateEventResponse({}, "", "", "")

    db = DatabaseMatrix({"single_state_key": False}, opsdroid=opsdroid_matrix)
    db.should_migrate = False

    assert await db.get("test") is None
Esempio n. 6
0
async def test_default_update_single_state_key(patched_send, opsdroid_matrix):
    patched_send.return_value = nio.RoomGetStateEventResponse(
        {"twim": "hello"}, "", "", "")

    db = DatabaseMatrix({
        "should_encrypt": False,
        "single_state_key": True
    },
                        opsdroid=opsdroid_matrix)
    db.should_migrate = False

    await db.put("pill", "red")

    patched_send.assert_has_calls([
        matrix_call(
            "GET",
            "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/",
        ),
        matrix_call(
            "PUT",
            "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/",
            {
                "twim": "hello",
                "pill": "red"
            },
        ),
    ])
Esempio n. 7
0
async def test_delete_multiple_keys(patched_send, opsdroid_matrix):
    patched_send.return_value = nio.RoomGetStateEventResponse(
        {
            "hello": "world",
            "twim": "hello",
            "pill": "red"
        }, "", "", "")

    db = DatabaseMatrix({}, opsdroid=opsdroid_matrix)
    db.should_migrate = False
    data = await db.delete(["hello", "twim"])

    patched_send.assert_has_calls([
        matrix_call(
            "GET",
            "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/",
        ),
        matrix_call(
            "PUT",
            "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/",
            {"pill": "red"},
        ),
    ])

    assert data == ["world", "hello"]
Esempio n. 8
0
 def side_effect(resp, *args, **kwargs):
     if resp is nio.RoomGetStateEventResponse:
         resp = nio.RoomGetStateEventResponse(
             {
                 "twim": {
                     "encrypted_val": "enceventid"
                 },
                 "wibble": "wobble"
             },
             "",
             "",
             "",
         )
         return resp
     else:
         event = nio.Event({
             "type": "dev.opsdroid.database",
             "event_id": "enceventid",
             "sender": "@someone:localhost",
             "origin_server_ts": "2005",
             "content": {
                 "twim": "hello"
             },
         })
         resp = nio.RoomGetEventResponse()
         resp.event = event
         return resp
Esempio n. 9
0
 def side_effect(resp, *args, **kwargs):
     if resp is nio.RoomGetStateEventResponse:
         resp = nio.RoomGetStateEventResponse(
             {"hello": {"encrypted_val": "enceventid"}}, "", "", ""
         )
         return resp
     else:
         return nio.RoomGetEventError(message="testing")
Esempio n. 10
0
 def side_effect(resp, *args, **kwargs):
     if resp is nio.RoomGetStateEventResponse:
         resp = nio.RoomGetStateEventResponse({}, "", "", "")
         resp.transport_response = AsyncMock()
         resp.transport_response.status = 404
         return resp
     else:
         return nio.RoomSendResponse("enceventid", "!notaroomid")
Esempio n. 11
0
async def test_delete_no_key(patched_send, opsdroid_matrix):
    patched_send.return_value = nio.RoomGetStateEventResponse(
        {"twim": "hello"}, "", "", "")

    db = DatabaseMatrix({}, opsdroid=opsdroid_matrix)
    db.should_migrate = False
    data = await db.delete("pill")

    assert data is None
Esempio n. 12
0
 def side_effect(resp, *args, **kwargs):
     if resp is nio.RoomGetStateEventResponse:
         resp = nio.RoomGetStateEventResponse(
             {"twim": {
                 "encrypted_val": "enceventid"
             }}, "", "", "")
         resp.transport_response = AsyncMock()
         resp.transport_response.status = 200
         return resp
     else:
         return nio.RoomGetEventError(message="testing")
Esempio n. 13
0
async def test_get_no_key_single_state_key(patched_send, opsdroid_matrix):
    patched_send.return_value = nio.RoomGetStateEventResponse(
        {"wibble": "wobble"}, "", "", ""
    )

    db = DatabaseMatrix(
        {"should_encrypt": False, "single_state_key": True}, opsdroid=opsdroid_matrix
    )
    db.should_migrate = False

    data = await db.get("twim")

    assert data is None
Esempio n. 14
0
async def test_delete_no_key_single_state_key_false(patched_send,
                                                    opsdroid_matrix, caplog):
    patched_send.return_value = nio.RoomGetStateEventResponse({}, "", "", "")
    patched_send.return_value.transport_response = AsyncMock()
    patched_send.return_value.transport_response.status = 404

    db = DatabaseMatrix({"single_state_key": False}, opsdroid=opsdroid_matrix)
    db.should_migrate = False
    caplog.clear()
    data = await db.delete("twim")

    assert data is None

    assert [
        "State event dev.opsdroid.database with state key 'twim' doesn't exist."
    ] == [rec.message for rec in caplog.records]
Esempio n. 15
0
async def test_get_single_state_key(patched_send, opsdroid_matrix):
    patched_send.return_value = nio.RoomGetStateEventResponse(
        {"twim": "hello", "wibble": "wobble"}, "", "", ""
    )

    db = DatabaseMatrix({}, opsdroid=opsdroid_matrix)
    db.should_migrate = False

    data = await db.get("twim")

    patched_send.assert_called_once_with(
        nio.RoomGetStateEventResponse,
        "GET",
        "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/?access_token=arbitrarytoken",
        response_data=("dev.opsdroid.database", "", "!notaroomid"),
    )

    assert data == "hello"
Esempio n. 16
0
async def test_room_switch(patched_send, opsdroid_matrix):
    patched_send.return_value = nio.RoomGetStateEventResponse(
        {"hello": "world"}, "", "", "")

    db = DatabaseMatrix({"should_encrypt": False}, opsdroid=opsdroid_matrix)
    db.should_migrate = False
    with db.memory_in_room("!notanotherroom"):
        assert db.room == "!notanotherroom"
        data = await db.get("hello")

    patched_send.assert_called_once_with(
        nio.RoomGetStateEventResponse,
        "GET",
        "/_matrix/client/r0/rooms/%21notanotherroom/state/dev.opsdroid.database?access_token=arbitrarytoken",
        response_data=("dev.opsdroid.database", "", "!notanotherroom"),
    )

    assert db.room == "main"
    assert data == "world"
Esempio n. 17
0
async def test_delete_single_state_key_false(patched_send, opsdroid_matrix):
    patched_send.return_value = nio.RoomGetStateEventResponse(
        {"twim": "hello"}, "", "", "")

    db = DatabaseMatrix({"single_state_key": False}, opsdroid=opsdroid_matrix)
    db.should_migrate = False
    data = await db.delete("twim")

    patched_send.assert_has_calls([
        matrix_call(
            "GET",
            "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/twim",
        ),
        matrix_call(
            "PUT",
            "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/twim",
            {},
        ),
    ])

    assert data == "hello"
Esempio n. 18
0
async def test_delete(patched_send, opsdroid_matrix):
    patched_send.return_value = nio.RoomGetStateEventResponse(
        {"twim": "hello"}, "", "", "")
    patched_send.return_value.transport_response = AsyncMock()
    patched_send.return_value.transport_response.status = 200

    db = DatabaseMatrix({}, opsdroid=opsdroid_matrix)
    db.should_migrate = False
    data = await db.delete("twim")

    patched_send.assert_has_calls([
        matrix_call(
            "GET",
            "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/",
        ),
        matrix_call(
            "PUT",
            "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/",
            {},
        ),
    ], )

    assert data == "hello"
Esempio n. 19
0
async def test_get(patched_send, opsdroid_matrix):
    patched_send.return_value = nio.RoomGetStateEventResponse(
        {"twim": "world"}, "", "", "")
    patched_send.return_value.transport_response = AsyncMock()
    patched_send.return_value.transport_response.status = 200

    db = DatabaseMatrix({
        "should_encrypt": False,
        "single_state_key": False
    },
                        opsdroid=opsdroid_matrix)
    db.should_migrate = False

    data = await db.get("twim")

    patched_send.assert_called_once_with(
        nio.RoomGetStateEventResponse,
        "GET",
        "/_matrix/client/r0/rooms/%21notaroomid/state/dev.opsdroid.database/twim?access_token=arbitrarytoken",
        response_data=("dev.opsdroid.database", "twim", "!notaroomid"),
    )

    assert data == "world"
Esempio n. 20
0
 def side_effect(resp, *args, **kwargs):
     if resp is nio.RoomGetStateEventResponse:
         resp = nio.RoomGetStateEventResponse({"twim": "hello"}, "", "", "")
         return resp
     else:
         return nio.RoomSendResponse("enceventid", "!notaroomid")