tests: Format the pan client test.

This commit is contained in:
Damir Jelić 2019-12-19 16:02:18 +01:00
parent e115c6f690
commit c8b31fce2f

View File

@ -1,21 +1,16 @@
import asyncio
import os import os
import re import re
import janus import janus
import pytest import pytest
from nio import ( from nio import (
DeviceList,
DeviceOneTimeKeyCount,
LoginResponse, LoginResponse,
RoomEncryptionEvent, KeysQueryResponse,
RoomInfo, KeysUploadResponse,
RoomMemberEvent,
Rooms,
RoomSummary,
SyncResponse, SyncResponse,
Timeline,
) )
from nio.crypto import Olm, OlmDevice
from nio.store import SqliteMemoryStore
from nio.store import SqliteStore from nio.store import SqliteStore
from pantalaimon.client import PanClient from pantalaimon.client import PanClient
@ -47,7 +42,7 @@ async def client(tmpdir, loop):
"@example:example.org", "@example:example.org",
"DEVICEID", "DEVICEID",
tmpdir, tmpdir,
store_class=SqliteStore store_class=SqliteStore,
) )
yield pan_client yield pan_client
@ -63,7 +58,7 @@ class TestClass(object):
"access_token": "abc123", "access_token": "abc123",
"device_id": "DEVICEID", "device_id": "DEVICEID",
"home_server": "example.org", "home_server": "example.org",
"user_id": "@example:example.org" "user_id": "@example:example.org",
} }
) )
@ -72,30 +67,20 @@ class TestClass(object):
return { return {
"device_one_time_keys_count": {}, "device_one_time_keys_count": {},
"next_batch": "s526_47314_0_7_1_1_1_11444_1", "next_batch": "s526_47314_0_7_1_1_1_11444_1",
"device_lists": { "device_lists": {"changed": ["@example:example.org"], "left": []},
"changed": [
"@example:example.org"
],
"left": []
},
"rooms": { "rooms": {
"invite": {}, "invite": {},
"join": { "join": {
"!SVkFJHzfwvuaIEawgC:localhost": { "!SVkFJHzfwvuaIEawgC:localhost": {
"account_data": { "account_data": {"events": []},
"events": [] "ephemeral": {"events": []},
},
"ephemeral": {
"events": []
},
"state": { "state": {
"events": [ "events": [
{ {
"content": { "content": {
"avatar_url": None, "avatar_url": None,
"displayname": "example", "displayname": "example",
"membership": "join" "membership": "join",
}, },
"event_id": "$151800140517rfvjc:localhost", "event_id": "$151800140517rfvjc:localhost",
"membership": "join", "membership": "join",
@ -105,54 +90,38 @@ class TestClass(object):
"type": "m.room.member", "type": "m.room.member",
"unsigned": { "unsigned": {
"age": 2970366338, "age": 2970366338,
"replaces_state": "$151800111315tsynI:localhost" "replaces_state": "$151800111315tsynI:localhost",
} },
}, },
{ {
"content": { "content": {"history_visibility": "shared"},
"history_visibility": "shared"
},
"event_id": "$15139375515VaJEY:localhost", "event_id": "$15139375515VaJEY:localhost",
"origin_server_ts": 1513937551613, "origin_server_ts": 1513937551613,
"sender": "@example:localhost", "sender": "@example:localhost",
"state_key": "", "state_key": "",
"type": "m.room.history_visibility", "type": "m.room.history_visibility",
"unsigned": { "unsigned": {"age": 7034220281},
"age": 7034220281
}
}, },
{ {
"content": { "content": {"creator": "@example:localhost"},
"creator": "@example:localhost"
},
"event_id": "$15139375510KUZHi:localhost", "event_id": "$15139375510KUZHi:localhost",
"origin_server_ts": 1513937551203, "origin_server_ts": 1513937551203,
"sender": "@example:localhost", "sender": "@example:localhost",
"state_key": "", "state_key": "",
"type": "m.room.create", "type": "m.room.create",
"unsigned": { "unsigned": {"age": 7034220691},
"age": 7034220691
}
}, },
{ {
"content": { "content": {"aliases": ["#tutorial:localhost"]},
"aliases": [
"#tutorial:localhost"
]
},
"event_id": "$15139375516NUgtD:localhost", "event_id": "$15139375516NUgtD:localhost",
"origin_server_ts": 1513937551720, "origin_server_ts": 1513937551720,
"sender": "@example:localhost", "sender": "@example:localhost",
"state_key": "localhost", "state_key": "localhost",
"type": "m.room.aliases", "type": "m.room.aliases",
"unsigned": { "unsigned": {"age": 7034220174},
"age": 7034220174
}
}, },
{ {
"content": { "content": {"topic": "\ud83d\ude00"},
"topic": "\ud83d\ude00"
},
"event_id": "$151957878228ssqrJ:localhost", "event_id": "$151957878228ssqrJ:localhost",
"origin_server_ts": 1519578782185, "origin_server_ts": 1519578782185,
"sender": "@example:localhost", "sender": "@example:localhost",
@ -160,12 +129,10 @@ class TestClass(object):
"type": "m.room.topic", "type": "m.room.topic",
"unsigned": { "unsigned": {
"age": 1392989709, "age": 1392989709,
"prev_content": { "prev_content": {"topic": "test"},
"topic": "test"
},
"prev_sender": "@example:localhost", "prev_sender": "@example:localhost",
"replaces_state": "$151957069225EVYKm:localhost" "replaces_state": "$151957069225EVYKm:localhost",
} },
}, },
{ {
"content": { "content": {
@ -175,45 +142,37 @@ class TestClass(object):
"m.room.canonical_alias": 50, "m.room.canonical_alias": 50,
"m.room.history_visibility": 100, "m.room.history_visibility": 100,
"m.room.name": 50, "m.room.name": 50,
"m.room.power_levels": 100 "m.room.power_levels": 100,
}, },
"events_default": 0, "events_default": 0,
"invite": 0, "invite": 0,
"kick": 50, "kick": 50,
"redact": 50, "redact": 50,
"state_default": 50, "state_default": 50,
"users": { "users": {"@example:localhost": 100},
"@example:localhost": 100 "users_default": 0,
},
"users_default": 0
}, },
"event_id": "$15139375512JaHAW:localhost", "event_id": "$15139375512JaHAW:localhost",
"origin_server_ts": 1513937551359, "origin_server_ts": 1513937551359,
"sender": "@example:localhost", "sender": "@example:localhost",
"state_key": "", "state_key": "",
"type": "m.room.power_levels", "type": "m.room.power_levels",
"unsigned": { "unsigned": {"age": 7034220535},
"age": 7034220535
}
}, },
{ {
"content": { "content": {"alias": "#tutorial:localhost"},
"alias": "#tutorial:localhost"
},
"event_id": "$15139375513VdeRF:localhost", "event_id": "$15139375513VdeRF:localhost",
"origin_server_ts": 1513937551461, "origin_server_ts": 1513937551461,
"sender": "@example:localhost", "sender": "@example:localhost",
"state_key": "", "state_key": "",
"type": "m.room.canonical_alias", "type": "m.room.canonical_alias",
"unsigned": { "unsigned": {"age": 7034220433},
"age": 7034220433
}
}, },
{ {
"content": { "content": {
"avatar_url": None, "avatar_url": None,
"displayname": "example2", "displayname": "example2",
"membership": "join" "membership": "join",
}, },
"event_id": "$152034824468gOeNB:localhost", "event_id": "$152034824468gOeNB:localhost",
"membership": "join", "membership": "join",
@ -223,18 +182,16 @@ class TestClass(object):
"type": "m.room.member", "type": "m.room.member",
"unsigned": { "unsigned": {
"age": 623527289, "age": 623527289,
"prev_content": { "prev_content": {"membership": "leave"},
"membership": "leave"
},
"prev_sender": "@example:localhost", "prev_sender": "@example:localhost",
"replaces_state": "$152034819067QWJxM:localhost" "replaces_state": "$152034819067QWJxM:localhost",
} },
}, },
{ {
"content": { "content": {
"algorithm": "m.megolm.v1.aes-sha2", "algorithm": "m.megolm.v1.aes-sha2",
"rotation_period_ms": 604800000, "rotation_period_ms": 604800000,
"rotation_period_msgs": 100 "rotation_period_msgs": 100,
}, },
"event_id": "$143273582443PhrSn:example.org", "event_id": "$143273582443PhrSn:example.org",
"origin_server_ts": 1432735824653, "origin_server_ts": 1432735824653,
@ -242,10 +199,8 @@ class TestClass(object):
"sender": "@example:example.org", "sender": "@example:example.org",
"state_key": "", "state_key": "",
"type": "m.room.encryption", "type": "m.room.encryption",
"unsigned": { "unsigned": {"age": 1234},
"age": 1234 },
}
}
] ]
}, },
"timeline": { "timeline": {
@ -255,147 +210,109 @@ class TestClass(object):
"body": "baba", "body": "baba",
"format": "org.matrix.custom.html", "format": "org.matrix.custom.html",
"formatted_body": "<strong>baba</strong>", "formatted_body": "<strong>baba</strong>",
"msgtype": "m.text" "msgtype": "m.text",
}, },
"event_id": "$152037280074GZeOm:localhost", "event_id": "$152037280074GZeOm:localhost",
"origin_server_ts": 1520372800469, "origin_server_ts": 1520372800469,
"sender": "@example:localhost", "sender": "@example:localhost",
"type": "m.room.message", "type": "m.room.message",
"unsigned": { "unsigned": {"age": 598971425},
"age": 598971425
}
} }
], ],
"limited": True, "limited": True,
"prev_batch": "t392-516_47314_0_7_1_1_1_11444_1" "prev_batch": "t392-516_47314_0_7_1_1_1_11444_1",
}, },
"unread_notifications": { "unread_notifications": {
"highlight_count": 0, "highlight_count": 0,
"notification_count": 11 "notification_count": 11,
} },
} }
}, },
"leave": {} "leave": {},
}, },
"to_device": { "to_device": {"events": []},
"events": [] }
}
}
@property @property
def keys_upload_response(self): def keys_upload_response(self):
return { return {"one_time_key_counts": {"curve25519": 10, "signed_curve25519": 20}}
"one_time_key_counts": {
"curve25519": 10,
"signed_curve25519": 20
}
}
@property @property
def keys_query_response(self): def keys_query_response(self):
return { return {
"device_keys": { "device_keys": {
"@alice:example.org": { "@alice:example.org": {
"JLAFKJWSCS": { "JLAFKJWSCS": {
"algorithms": [ "algorithms": [
"m.olm.v1.curve25519-aes-sha2", "m.olm.v1.curve25519-aes-sha2",
"m.megolm.v1.aes-sha2" "m.megolm.v1.aes-sha2",
], ],
"device_id": "JLAFKJWSCS", "device_id": "JLAFKJWSCS",
"user_id": "@alice:example.org", "user_id": "@alice:example.org",
"keys": { "keys": {
"curve25519:JLAFKJWSCS": "wjLpTLRqbqBzLs63aYaEv2Boi6cFEbbM/sSRQ2oAKk4", "curve25519:JLAFKJWSCS": "wjLpTLRqbqBzLs63aYaEv2Boi6cFEbbM/sSRQ2oAKk4",
"ed25519:JLAFKJWSCS": "nE6W2fCblxDcOFmeEtCHNl8/l8bXcu7GKyAswA4r3mM" "ed25519:JLAFKJWSCS": "nE6W2fCblxDcOFmeEtCHNl8/l8bXcu7GKyAswA4r3mM",
}, },
"signatures": { "signatures": {
"@alice:example.org": { "@alice:example.org": {
"ed25519:JLAFKJWSCS": "m53Wkbh2HXkc3vFApZvCrfXcX3AI51GsDHustMhKwlv3TuOJMj4wistcOTM8q2+e/Ro7rWFUb9ZfnNbwptSUBA" "ed25519:JLAFKJWSCS": "m53Wkbh2HXkc3vFApZvCrfXcX3AI51GsDHustMhKwlv3TuOJMj4wistcOTM8q2+e/Ro7rWFUb9ZfnNbwptSUBA"
} }
} },
} }
} }
}, },
"failures": {} "failures": {},
} }
@property @property
def empty_sync(self): def empty_sync(self):
return { return {
"account_data": { "account_data": {"events": []},
"events": [] "device_lists": {"changed": [], "left": []},
}, "device_one_time_keys_count": {"signed_curve25519": 50},
"device_lists": { "groups": {"invite": {}, "join": {}, "leave": {}},
"changed": [],
"left": []
},
"device_one_time_keys_count": {
"signed_curve25519": 50
},
"groups": {
"invite": {},
"join": {},
"leave": {}
},
"next_batch": "s1059_133339_44_763_246_1_586_12411_1", "next_batch": "s1059_133339_44_763_246_1_586_12411_1",
"presence": { "presence": {"events": []},
"events": [] "rooms": {"invite": {}, "join": {}, "leave": {}},
}, "to_device": {"events": []},
"rooms": {
"invite": {},
"join": {},
"leave": {}
},
"to_device": {
"events": []
}
} }
@property @property
def messages_response(self): def messages_response(self):
return { return {
"chunk": [ "chunk": [
{ {
"age": 1042, "age": 1042,
"content": { "content": {"body": "hello world", "msgtype": "m.text"},
"body": "hello world", "event_id": "$1444812213350496Caaaa:example.com",
"msgtype": "m.text" "origin_server_ts": 1444812213737,
"room_id": "!Xq3620DUiqCaoxq:example.com",
"sender": "@alice:example.com",
"type": "m.room.message",
}, },
"event_id": "$1444812213350496Caaaa:example.com", {
"origin_server_ts": 1444812213737, "age": 20123,
"room_id": "!Xq3620DUiqCaoxq:example.com", "content": {"body": "the world is big", "msgtype": "m.text"},
"sender": "@alice:example.com", "event_id": "$1444812213350496Cbbbb:example.com",
"type": "m.room.message" "origin_server_ts": 1444812194656,
}, "room_id": "!Xq3620DUiqCaoxq:example.com",
{ "sender": "@alice:example.com",
"age": 20123, "type": "m.room.message",
"content": {
"body": "the world is big",
"msgtype": "m.text"
}, },
"event_id": "$1444812213350496Cbbbb:example.com", {
"origin_server_ts": 1444812194656, "age": 50789,
"room_id": "!Xq3620DUiqCaoxq:example.com", "content": {"name": "New room name"},
"sender": "@alice:example.com", "event_id": "$1444812213350496Ccccc:example.com",
"type": "m.room.message" "origin_server_ts": 1444812163990,
}, "prev_content": {"name": "Old room name"},
{ "room_id": "!Xq3620DUiqCaoxq:example.com",
"age": 50789, "sender": "@alice:example.com",
"content": { "state_key": "",
"name": "New room name" "type": "m.room.name",
}, },
"event_id": "$1444812213350496Ccccc:example.com",
"origin_server_ts": 1444812163990,
"prev_content": {
"name": "Old room name"
},
"room_id": "!Xq3620DUiqCaoxq:example.com",
"sender": "@alice:example.com",
"state_key": "",
"type": "m.room.name"
}
], ],
"end": "t47409-4357353_219380_26003_2265", "end": "t47409-4357353_219380_26003_2265",
"start": "t47429-4392820_219380_26003_2265" "start": "t47429-4392820_219380_26003_2265",
} }
@property @property
@ -403,7 +320,7 @@ class TestClass(object):
return { return {
"chunk": [], "chunk": [],
"end": "t47429-4392820_219380_26003_2277", "end": "t47429-4392820_219380_26003_2277",
"start": "t47409-4357353_219380_26003_2265" "start": "t47409-4357353_219380_26003_2265",
} }
async def test_login(self, client): async def test_login(self, client):
@ -412,28 +329,25 @@ class TestClass(object):
async def test_start_loop(self, client, aioresponse): async def test_start_loop(self, client, aioresponse):
sync_url = re.compile( sync_url = re.compile(
r'^https://example\.org/_matrix/client/r0/sync\?access_token=.*' r"^https://example\.org/_matrix/client/r0/sync\?access_token=.*"
) )
aioresponse.get( aioresponse.get(
sync_url, sync_url, status=200, payload=self.initial_sync_response, repeat=True
status=200,
payload=self.initial_sync_response,
repeat=True
) )
aioresponse.post( aioresponse.post(
"https://example.org/_matrix/client/r0/keys/upload?access_token=abc123", "https://example.org/_matrix/client/r0/keys/upload?access_token=abc123",
status=200, status=200,
payload=self.keys_upload_response, payload=self.keys_upload_response,
repeat=True repeat=True,
) )
aioresponse.post( aioresponse.post(
"https://example.org/_matrix/client/r0/keys/query?access_token=abc123", "https://example.org/_matrix/client/r0/keys/query?access_token=abc123",
status=200, status=200,
payload=self.keys_query_response, payload=self.keys_query_response,
repeat=True repeat=True,
) )
await client.receive_response(self.login_response) await client.receive_response(self.login_response)
@ -462,51 +376,39 @@ class TestClass(object):
pytest.skip("Indexing needs to be enabled to test this") pytest.skip("Indexing needs to be enabled to test this")
sync_url = re.compile( sync_url = re.compile(
r'^https://example\.org/_matrix/client/r0/sync\?access_token=.*' r"^https://example\.org/_matrix/client/r0/sync\?access_token=.*"
) )
aioresponse.get( aioresponse.get(
sync_url, sync_url, status=200, payload=self.initial_sync_response,
status=200,
payload=self.initial_sync_response,
) )
aioresponse.get( aioresponse.get(sync_url, status=200, payload=self.empty_sync, repeat=True)
sync_url,
status=200,
payload=self.empty_sync,
repeat=True
)
aioresponse.post( aioresponse.post(
"https://example.org/_matrix/client/r0/keys/upload?access_token=abc123", "https://example.org/_matrix/client/r0/keys/upload?access_token=abc123",
status=200, status=200,
payload=self.keys_upload_response, payload=self.keys_upload_response,
repeat=True repeat=True,
) )
aioresponse.post( aioresponse.post(
"https://example.org/_matrix/client/r0/keys/query?access_token=abc123", "https://example.org/_matrix/client/r0/keys/query?access_token=abc123",
status=200, status=200,
payload=self.keys_query_response, payload=self.keys_query_response,
repeat=True repeat=True,
) )
messages_url = re.compile( messages_url = re.compile(
r'^https://example\.org/_matrix/client/r0/rooms/{}/messages\?.*'.format(TEST_ROOM_ID) r"^https://example\.org/_matrix/client/r0/rooms/{}/messages\?.*".format(
TEST_ROOM_ID
)
) )
aioresponse.get( aioresponse.get(messages_url, status=200, payload=self.messages_response)
messages_url,
status=200,
payload=self.messages_response
)
aioresponse.get( aioresponse.get(
messages_url, messages_url, status=200, payload=self.empty_messages, repeat=True
status=200,
payload=self.empty_messages,
repeat=True
) )
await client.receive_response(self.login_response) await client.receive_response(self.login_response)
@ -516,10 +418,7 @@ class TestClass(object):
await client.new_fetch_task.wait() await client.new_fetch_task.wait()
# Load the currently waiting task # Load the currently waiting task
tasks = client.pan_store.load_fetcher_tasks( tasks = client.pan_store.load_fetcher_tasks(client.server_name, client.user_id)
client.server_name,
client.user_id
)
assert len(tasks) == 1 assert len(tasks) == 1
# Check that the task is our prev_batch from the sync resposne # Check that the task is our prev_batch from the sync resposne
@ -529,10 +428,7 @@ class TestClass(object):
# Let's wait for the next fetch task # Let's wait for the next fetch task
await client.new_fetch_task.wait() await client.new_fetch_task.wait()
tasks = client.pan_store.load_fetcher_tasks( tasks = client.pan_store.load_fetcher_tasks(client.server_name, client.user_id)
client.server_name,
client.user_id
)
assert len(tasks) == 1 assert len(tasks) == 1
# Check that the task is our end token from the messages resposne # Check that the task is our end token from the messages resposne
@ -542,10 +438,7 @@ class TestClass(object):
# Wait for the next fetch loop iteration. # Wait for the next fetch loop iteration.
await client.fetch_loop_event.wait() await client.fetch_loop_event.wait()
tasks = client.pan_store.load_fetcher_tasks( tasks = client.pan_store.load_fetcher_tasks(client.server_name, client.user_id)
client.server_name,
client.user_id
)
# Check that there are no more tasks since we reached the start of the # Check that there are no more tasks since we reached the start of the
# room timeline. # room timeline.
assert not tasks assert not tasks
@ -557,51 +450,39 @@ class TestClass(object):
pytest.skip("Indexing needs to be enabled to test this") pytest.skip("Indexing needs to be enabled to test this")
sync_url = re.compile( sync_url = re.compile(
r'^https://example\.org/_matrix/client/r0/sync\?access_token=.*' r"^https://example\.org/_matrix/client/r0/sync\?access_token=.*"
) )
aioresponse.get( aioresponse.get(
sync_url, sync_url, status=200, payload=self.initial_sync_response,
status=200,
payload=self.initial_sync_response,
) )
aioresponse.get( aioresponse.get(sync_url, status=200, payload=self.empty_sync, repeat=True)
sync_url,
status=200,
payload=self.empty_sync,
repeat=True
)
aioresponse.post( aioresponse.post(
"https://example.org/_matrix/client/r0/keys/upload?access_token=abc123", "https://example.org/_matrix/client/r0/keys/upload?access_token=abc123",
status=200, status=200,
payload=self.keys_upload_response, payload=self.keys_upload_response,
repeat=True repeat=True,
) )
aioresponse.post( aioresponse.post(
"https://example.org/_matrix/client/r0/keys/query?access_token=abc123", "https://example.org/_matrix/client/r0/keys/query?access_token=abc123",
status=200, status=200,
payload=self.keys_query_response, payload=self.keys_query_response,
repeat=True repeat=True,
) )
messages_url = re.compile( messages_url = re.compile(
r'^https://example\.org/_matrix/client/r0/rooms/{}/messages\?.*'.format(TEST_ROOM_ID) r"^https://example\.org/_matrix/client/r0/rooms/{}/messages\?.*".format(
TEST_ROOM_ID
)
) )
aioresponse.get( aioresponse.get(messages_url, status=200, payload=self.messages_response)
messages_url,
status=200,
payload=self.messages_response
)
aioresponse.get( aioresponse.get(
messages_url, messages_url, status=200, payload=self.empty_messages, repeat=True
status=200,
payload=self.empty_messages,
repeat=True
) )
await client.receive_response(self.login_response) await client.receive_response(self.login_response)
@ -613,11 +494,7 @@ class TestClass(object):
await client.loop_stop() await client.loop_stop()
index_path = os.path.join( index_path = os.path.join(client.store_path, client.server_name, client.user_id)
client.store_path,
client.server_name,
client.user_id
)
# Remove the lock file since the GC won't do it for us # Remove the lock file since the GC won't do it for us
writer_lock = os.path.join(index_path, ".tantivy-writer.lock") writer_lock = os.path.join(index_path, ".tantivy-writer.lock")
@ -638,8 +515,7 @@ class TestClass(object):
client2.access_token = client.access_token client2.access_token = client.access_token
tasks = client2.pan_store.load_fetcher_tasks( tasks = client2.pan_store.load_fetcher_tasks(
client2.server_name, client2.server_name, client2.user_id
client2.user_id
) )
assert len(tasks) == 1 assert len(tasks) == 1
@ -655,8 +531,7 @@ class TestClass(object):
await client2.fetch_loop_event.wait() await client2.fetch_loop_event.wait()
tasks = client2.pan_store.load_fetcher_tasks( tasks = client2.pan_store.load_fetcher_tasks(
client2.server_name, client2.server_name, client2.user_id
client2.user_id
) )
# Check that there are no more tasks since we reached the start of the # Check that there are no more tasks since we reached the start of the
# room timeline. # room timeline.