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" }}, ), ])
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}, ), ])
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")
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]
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
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" }, ), ])
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"]
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
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")
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")
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
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")
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
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]
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"
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"
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"
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"
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"
def side_effect(resp, *args, **kwargs): if resp is nio.RoomGetStateEventResponse: resp = nio.RoomGetStateEventResponse({"twim": "hello"}, "", "", "") return resp else: return nio.RoomSendResponse("enceventid", "!notaroomid")