2019-07-22 05:31:52 -04:00
|
|
|
import os
|
2019-07-03 11:49:45 -04:00
|
|
|
import re
|
|
|
|
|
|
|
|
import janus
|
|
|
|
import pytest
|
2019-07-04 08:58:15 -04:00
|
|
|
from nio import (
|
|
|
|
LoginResponse,
|
2019-12-19 10:02:18 -05:00
|
|
|
KeysQueryResponse,
|
|
|
|
KeysUploadResponse,
|
2019-07-04 08:58:15 -04:00
|
|
|
SyncResponse,
|
|
|
|
)
|
2019-12-19 10:02:18 -05:00
|
|
|
from nio.crypto import Olm, OlmDevice
|
|
|
|
from nio.store import SqliteMemoryStore
|
2019-07-22 10:21:54 -04:00
|
|
|
from nio.store import SqliteStore
|
2019-07-03 11:49:45 -04:00
|
|
|
|
|
|
|
from pantalaimon.client import PanClient
|
|
|
|
from pantalaimon.config import ServerConfig
|
|
|
|
from pantalaimon.store import PanStore
|
2019-07-12 09:38:04 -04:00
|
|
|
from pantalaimon.index import INDEXING_ENABLED
|
2019-07-03 11:49:45 -04:00
|
|
|
|
|
|
|
TEST_ROOM_ID = "!SVkFJHzfwvuaIEawgC:localhost"
|
|
|
|
TEST_ROOM2 = "!testroom:localhost"
|
|
|
|
|
|
|
|
ALICE_ID = "@alice:example.org"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
async def client(tmpdir, loop):
|
|
|
|
store = PanStore(tmpdir)
|
2020-04-29 10:48:43 -04:00
|
|
|
queue = janus.Queue()
|
2019-07-03 11:49:45 -04:00
|
|
|
conf = ServerConfig("example", "https://exapmle.org")
|
2019-07-04 08:58:15 -04:00
|
|
|
conf.history_fetch_delay = 0.1
|
2019-07-03 11:49:45 -04:00
|
|
|
|
|
|
|
store.save_server_user("example", "@example:example.org")
|
|
|
|
|
|
|
|
pan_client = PanClient(
|
|
|
|
"example",
|
|
|
|
store,
|
|
|
|
conf,
|
|
|
|
"https://example.org",
|
|
|
|
queue.async_q,
|
|
|
|
"@example:example.org",
|
|
|
|
"DEVICEID",
|
2019-07-22 10:21:54 -04:00
|
|
|
tmpdir,
|
2019-12-19 10:02:18 -05:00
|
|
|
store_class=SqliteStore,
|
2019-07-03 11:49:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
yield pan_client
|
|
|
|
|
|
|
|
await pan_client.close()
|
|
|
|
|
|
|
|
|
|
|
|
class TestClass(object):
|
|
|
|
@property
|
|
|
|
def login_response(self):
|
|
|
|
return LoginResponse.from_dict(
|
|
|
|
{
|
|
|
|
"access_token": "abc123",
|
|
|
|
"device_id": "DEVICEID",
|
|
|
|
"home_server": "example.org",
|
2019-12-19 10:02:18 -05:00
|
|
|
"user_id": "@example:example.org",
|
2019-07-03 11:49:45 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def initial_sync_response(self):
|
|
|
|
return {
|
|
|
|
"device_one_time_keys_count": {},
|
|
|
|
"next_batch": "s526_47314_0_7_1_1_1_11444_1",
|
2019-12-19 10:02:18 -05:00
|
|
|
"device_lists": {"changed": ["@example:example.org"], "left": []},
|
2019-07-03 11:49:45 -04:00
|
|
|
"rooms": {
|
|
|
|
"invite": {},
|
|
|
|
"join": {
|
|
|
|
"!SVkFJHzfwvuaIEawgC:localhost": {
|
2019-12-19 10:02:18 -05:00
|
|
|
"account_data": {"events": []},
|
|
|
|
"ephemeral": {"events": []},
|
2019-07-03 11:49:45 -04:00
|
|
|
"state": {
|
|
|
|
"events": [
|
|
|
|
{
|
|
|
|
"content": {
|
|
|
|
"avatar_url": None,
|
|
|
|
"displayname": "example",
|
2019-12-19 10:02:18 -05:00
|
|
|
"membership": "join",
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
"event_id": "$151800140517rfvjc:localhost",
|
|
|
|
"membership": "join",
|
|
|
|
"origin_server_ts": 1518001405556,
|
|
|
|
"sender": "@example:localhost",
|
|
|
|
"state_key": "@example:localhost",
|
|
|
|
"type": "m.room.member",
|
|
|
|
"unsigned": {
|
|
|
|
"age": 2970366338,
|
2019-12-19 10:02:18 -05:00
|
|
|
"replaces_state": "$151800111315tsynI:localhost",
|
|
|
|
},
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
{
|
2019-12-19 10:02:18 -05:00
|
|
|
"content": {"history_visibility": "shared"},
|
2019-07-03 11:49:45 -04:00
|
|
|
"event_id": "$15139375515VaJEY:localhost",
|
|
|
|
"origin_server_ts": 1513937551613,
|
|
|
|
"sender": "@example:localhost",
|
|
|
|
"state_key": "",
|
|
|
|
"type": "m.room.history_visibility",
|
2019-12-19 10:02:18 -05:00
|
|
|
"unsigned": {"age": 7034220281},
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
{
|
2019-12-19 10:02:18 -05:00
|
|
|
"content": {"creator": "@example:localhost"},
|
2019-07-03 11:49:45 -04:00
|
|
|
"event_id": "$15139375510KUZHi:localhost",
|
|
|
|
"origin_server_ts": 1513937551203,
|
|
|
|
"sender": "@example:localhost",
|
|
|
|
"state_key": "",
|
|
|
|
"type": "m.room.create",
|
2019-12-19 10:02:18 -05:00
|
|
|
"unsigned": {"age": 7034220691},
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
{
|
2019-12-19 10:02:18 -05:00
|
|
|
"content": {"aliases": ["#tutorial:localhost"]},
|
2019-07-03 11:49:45 -04:00
|
|
|
"event_id": "$15139375516NUgtD:localhost",
|
|
|
|
"origin_server_ts": 1513937551720,
|
|
|
|
"sender": "@example:localhost",
|
|
|
|
"state_key": "localhost",
|
|
|
|
"type": "m.room.aliases",
|
2019-12-19 10:02:18 -05:00
|
|
|
"unsigned": {"age": 7034220174},
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
{
|
2019-12-19 10:02:18 -05:00
|
|
|
"content": {"topic": "\ud83d\ude00"},
|
2019-07-03 11:49:45 -04:00
|
|
|
"event_id": "$151957878228ssqrJ:localhost",
|
|
|
|
"origin_server_ts": 1519578782185,
|
|
|
|
"sender": "@example:localhost",
|
|
|
|
"state_key": "",
|
|
|
|
"type": "m.room.topic",
|
|
|
|
"unsigned": {
|
|
|
|
"age": 1392989709,
|
2019-12-19 10:02:18 -05:00
|
|
|
"prev_content": {"topic": "test"},
|
2019-07-03 11:49:45 -04:00
|
|
|
"prev_sender": "@example:localhost",
|
2019-12-19 10:02:18 -05:00
|
|
|
"replaces_state": "$151957069225EVYKm:localhost",
|
|
|
|
},
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"content": {
|
|
|
|
"ban": 50,
|
|
|
|
"events": {
|
|
|
|
"m.room.avatar": 50,
|
|
|
|
"m.room.canonical_alias": 50,
|
|
|
|
"m.room.history_visibility": 100,
|
|
|
|
"m.room.name": 50,
|
2019-12-19 10:02:18 -05:00
|
|
|
"m.room.power_levels": 100,
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
"events_default": 0,
|
|
|
|
"invite": 0,
|
|
|
|
"kick": 50,
|
|
|
|
"redact": 50,
|
|
|
|
"state_default": 50,
|
2019-12-19 10:02:18 -05:00
|
|
|
"users": {"@example:localhost": 100},
|
|
|
|
"users_default": 0,
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
"event_id": "$15139375512JaHAW:localhost",
|
|
|
|
"origin_server_ts": 1513937551359,
|
|
|
|
"sender": "@example:localhost",
|
|
|
|
"state_key": "",
|
|
|
|
"type": "m.room.power_levels",
|
2019-12-19 10:02:18 -05:00
|
|
|
"unsigned": {"age": 7034220535},
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
{
|
2019-12-19 10:02:18 -05:00
|
|
|
"content": {"alias": "#tutorial:localhost"},
|
2019-07-03 11:49:45 -04:00
|
|
|
"event_id": "$15139375513VdeRF:localhost",
|
|
|
|
"origin_server_ts": 1513937551461,
|
|
|
|
"sender": "@example:localhost",
|
|
|
|
"state_key": "",
|
|
|
|
"type": "m.room.canonical_alias",
|
2019-12-19 10:02:18 -05:00
|
|
|
"unsigned": {"age": 7034220433},
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"content": {
|
|
|
|
"avatar_url": None,
|
|
|
|
"displayname": "example2",
|
2019-12-19 10:02:18 -05:00
|
|
|
"membership": "join",
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
"event_id": "$152034824468gOeNB:localhost",
|
|
|
|
"membership": "join",
|
|
|
|
"origin_server_ts": 1520348244605,
|
|
|
|
"sender": "@example2:localhost",
|
|
|
|
"state_key": "@example2:localhost",
|
|
|
|
"type": "m.room.member",
|
|
|
|
"unsigned": {
|
|
|
|
"age": 623527289,
|
2019-12-19 10:02:18 -05:00
|
|
|
"prev_content": {"membership": "leave"},
|
2019-07-03 11:49:45 -04:00
|
|
|
"prev_sender": "@example:localhost",
|
2019-12-19 10:02:18 -05:00
|
|
|
"replaces_state": "$152034819067QWJxM:localhost",
|
|
|
|
},
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"content": {
|
|
|
|
"algorithm": "m.megolm.v1.aes-sha2",
|
|
|
|
"rotation_period_ms": 604800000,
|
2019-12-19 10:02:18 -05:00
|
|
|
"rotation_period_msgs": 100,
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
"event_id": "$143273582443PhrSn:example.org",
|
|
|
|
"origin_server_ts": 1432735824653,
|
|
|
|
"room_id": "!jEsUZKDJdhlrceRyVU:example.org",
|
|
|
|
"sender": "@example:example.org",
|
|
|
|
"state_key": "",
|
|
|
|
"type": "m.room.encryption",
|
2019-12-19 10:02:18 -05:00
|
|
|
"unsigned": {"age": 1234},
|
|
|
|
},
|
2019-07-03 11:49:45 -04:00
|
|
|
]
|
|
|
|
},
|
|
|
|
"timeline": {
|
|
|
|
"events": [
|
|
|
|
{
|
|
|
|
"content": {
|
|
|
|
"body": "baba",
|
|
|
|
"format": "org.matrix.custom.html",
|
|
|
|
"formatted_body": "<strong>baba</strong>",
|
2019-12-19 10:02:18 -05:00
|
|
|
"msgtype": "m.text",
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
"event_id": "$152037280074GZeOm:localhost",
|
|
|
|
"origin_server_ts": 1520372800469,
|
|
|
|
"sender": "@example:localhost",
|
|
|
|
"type": "m.room.message",
|
2019-12-19 10:02:18 -05:00
|
|
|
"unsigned": {"age": 598971425},
|
2019-07-03 11:49:45 -04:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"limited": True,
|
2019-12-19 10:02:18 -05:00
|
|
|
"prev_batch": "t392-516_47314_0_7_1_1_1_11444_1",
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
|
|
|
"unread_notifications": {
|
|
|
|
"highlight_count": 0,
|
2019-12-19 10:02:18 -05:00
|
|
|
"notification_count": 11,
|
|
|
|
},
|
2019-07-03 11:49:45 -04:00
|
|
|
}
|
|
|
|
},
|
2019-12-19 10:02:18 -05:00
|
|
|
"leave": {},
|
2019-07-03 11:49:45 -04:00
|
|
|
},
|
2019-12-19 10:02:18 -05:00
|
|
|
"to_device": {"events": []},
|
|
|
|
}
|
2019-07-03 11:49:45 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def keys_upload_response(self):
|
2019-12-19 10:02:18 -05:00
|
|
|
return {"one_time_key_counts": {"curve25519": 10, "signed_curve25519": 20}}
|
2019-07-03 11:49:45 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def keys_query_response(self):
|
|
|
|
return {
|
2019-12-19 10:02:18 -05:00
|
|
|
"device_keys": {
|
|
|
|
"@alice:example.org": {
|
|
|
|
"JLAFKJWSCS": {
|
|
|
|
"algorithms": [
|
|
|
|
"m.olm.v1.curve25519-aes-sha2",
|
|
|
|
"m.megolm.v1.aes-sha2",
|
|
|
|
],
|
|
|
|
"device_id": "JLAFKJWSCS",
|
|
|
|
"user_id": "@alice:example.org",
|
|
|
|
"keys": {
|
|
|
|
"curve25519:JLAFKJWSCS": "wjLpTLRqbqBzLs63aYaEv2Boi6cFEbbM/sSRQ2oAKk4",
|
|
|
|
"ed25519:JLAFKJWSCS": "nE6W2fCblxDcOFmeEtCHNl8/l8bXcu7GKyAswA4r3mM",
|
|
|
|
},
|
|
|
|
"signatures": {
|
|
|
|
"@alice:example.org": {
|
|
|
|
"ed25519:JLAFKJWSCS": "m53Wkbh2HXkc3vFApZvCrfXcX3AI51GsDHustMhKwlv3TuOJMj4wistcOTM8q2+e/Ro7rWFUb9ZfnNbwptSUBA"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"failures": {},
|
2019-07-03 11:49:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def empty_sync(self):
|
2019-07-04 08:58:15 -04:00
|
|
|
return {
|
2019-12-19 10:02:18 -05:00
|
|
|
"account_data": {"events": []},
|
|
|
|
"device_lists": {"changed": [], "left": []},
|
|
|
|
"device_one_time_keys_count": {"signed_curve25519": 50},
|
|
|
|
"groups": {"invite": {}, "join": {}, "leave": {}},
|
2019-07-04 08:58:15 -04:00
|
|
|
"next_batch": "s1059_133339_44_763_246_1_586_12411_1",
|
2019-12-19 10:02:18 -05:00
|
|
|
"presence": {"events": []},
|
|
|
|
"rooms": {"invite": {}, "join": {}, "leave": {}},
|
|
|
|
"to_device": {"events": []},
|
2019-07-04 08:58:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def messages_response(self):
|
|
|
|
return {
|
|
|
|
"chunk": [
|
2019-12-19 10:02:18 -05:00
|
|
|
{
|
|
|
|
"age": 1042,
|
|
|
|
"content": {"body": "hello world", "msgtype": "m.text"},
|
|
|
|
"event_id": "$1444812213350496Caaaa:example.com",
|
|
|
|
"origin_server_ts": 1444812213737,
|
|
|
|
"room_id": "!Xq3620DUiqCaoxq:example.com",
|
|
|
|
"sender": "@alice:example.com",
|
|
|
|
"type": "m.room.message",
|
2019-07-04 08:58:15 -04:00
|
|
|
},
|
2019-12-19 10:02:18 -05:00
|
|
|
{
|
|
|
|
"age": 20123,
|
|
|
|
"content": {"body": "the world is big", "msgtype": "m.text"},
|
|
|
|
"event_id": "$1444812213350496Cbbbb:example.com",
|
|
|
|
"origin_server_ts": 1444812194656,
|
|
|
|
"room_id": "!Xq3620DUiqCaoxq:example.com",
|
|
|
|
"sender": "@alice:example.com",
|
|
|
|
"type": "m.room.message",
|
2019-07-04 08:58:15 -04:00
|
|
|
},
|
2019-12-19 10:02:18 -05:00
|
|
|
{
|
|
|
|
"age": 50789,
|
|
|
|
"content": {"name": "New 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",
|
2019-07-04 08:58:15 -04:00
|
|
|
},
|
|
|
|
],
|
|
|
|
"end": "t47409-4357353_219380_26003_2265",
|
2019-12-19 10:02:18 -05:00
|
|
|
"start": "t47429-4392820_219380_26003_2265",
|
2019-07-04 08:58:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def empty_messages(self):
|
|
|
|
return {
|
|
|
|
"chunk": [],
|
|
|
|
"end": "t47429-4392820_219380_26003_2277",
|
2019-12-19 10:02:18 -05:00
|
|
|
"start": "t47409-4357353_219380_26003_2265",
|
2019-07-04 08:58:15 -04:00
|
|
|
}
|
2019-07-03 11:49:45 -04:00
|
|
|
|
|
|
|
async def test_login(self, client):
|
|
|
|
await client.receive_response(self.login_response)
|
|
|
|
assert client.logged_in
|
|
|
|
|
|
|
|
async def test_start_loop(self, client, aioresponse):
|
|
|
|
sync_url = re.compile(
|
2019-12-19 10:02:18 -05:00
|
|
|
r"^https://example\.org/_matrix/client/r0/sync\?access_token=.*"
|
2019-07-03 11:49:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
aioresponse.get(
|
2019-12-19 10:02:18 -05:00
|
|
|
sync_url, status=200, payload=self.initial_sync_response, repeat=True
|
2019-07-03 11:49:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
aioresponse.post(
|
|
|
|
"https://example.org/_matrix/client/r0/keys/upload?access_token=abc123",
|
|
|
|
status=200,
|
|
|
|
payload=self.keys_upload_response,
|
2019-12-19 10:02:18 -05:00
|
|
|
repeat=True,
|
2019-07-03 11:49:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
aioresponse.post(
|
|
|
|
"https://example.org/_matrix/client/r0/keys/query?access_token=abc123",
|
|
|
|
status=200,
|
|
|
|
payload=self.keys_query_response,
|
2019-12-19 10:02:18 -05:00
|
|
|
repeat=True,
|
2019-07-03 11:49:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
await client.receive_response(self.login_response)
|
|
|
|
|
2019-07-04 08:58:15 -04:00
|
|
|
# Set a big history fetch delay so it doesn't consume the fetch tasks.
|
|
|
|
client.pan_conf.history_fetch_delay = 10
|
2019-07-03 11:49:45 -04:00
|
|
|
client.start_loop(100)
|
|
|
|
|
2019-07-04 08:58:15 -04:00
|
|
|
# Sync tasks are done after we get a sync event so wait for two of them
|
2019-07-03 11:49:45 -04:00
|
|
|
await client.synced.wait()
|
|
|
|
await client.synced.wait()
|
|
|
|
|
2019-07-04 08:58:15 -04:00
|
|
|
# Make sure that we have only a single history fetch task for the
|
|
|
|
# single room we have
|
2019-07-03 11:49:45 -04:00
|
|
|
assert not client.history_fetch_queue.empty()
|
2019-07-04 08:58:15 -04:00
|
|
|
assert client.history_fetch_queue.qsize() == 1
|
|
|
|
|
|
|
|
# Do another round to be sure we don't get more tasks than necessary.
|
|
|
|
await client.synced.wait()
|
|
|
|
assert client.history_fetch_queue.qsize() == 1
|
2019-07-03 11:49:45 -04:00
|
|
|
|
|
|
|
await client.loop_stop()
|
2019-07-04 08:58:30 -04:00
|
|
|
|
2019-07-12 09:38:04 -04:00
|
|
|
async def test_history_fetching_tasks(self, client, aioresponse, loop):
|
|
|
|
if not INDEXING_ENABLED:
|
|
|
|
pytest.skip("Indexing needs to be enabled to test this")
|
|
|
|
|
2019-07-04 08:58:30 -04:00
|
|
|
sync_url = re.compile(
|
2019-12-19 10:02:18 -05:00
|
|
|
r"^https://example\.org/_matrix/client/r0/sync\?access_token=.*"
|
2019-07-04 08:58:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
aioresponse.get(
|
2019-12-19 10:02:18 -05:00
|
|
|
sync_url, status=200, payload=self.initial_sync_response,
|
2019-07-04 08:58:30 -04:00
|
|
|
)
|
|
|
|
|
2019-12-19 10:02:18 -05:00
|
|
|
aioresponse.get(sync_url, status=200, payload=self.empty_sync, repeat=True)
|
2019-07-04 08:58:30 -04:00
|
|
|
|
|
|
|
aioresponse.post(
|
|
|
|
"https://example.org/_matrix/client/r0/keys/upload?access_token=abc123",
|
|
|
|
status=200,
|
|
|
|
payload=self.keys_upload_response,
|
2019-12-19 10:02:18 -05:00
|
|
|
repeat=True,
|
2019-07-04 08:58:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
aioresponse.post(
|
|
|
|
"https://example.org/_matrix/client/r0/keys/query?access_token=abc123",
|
|
|
|
status=200,
|
|
|
|
payload=self.keys_query_response,
|
2019-12-19 10:02:18 -05:00
|
|
|
repeat=True,
|
2019-07-04 08:58:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
messages_url = re.compile(
|
2019-12-19 10:02:18 -05:00
|
|
|
r"^https://example\.org/_matrix/client/r0/rooms/{}/messages\?.*".format(
|
|
|
|
TEST_ROOM_ID
|
|
|
|
)
|
2019-07-04 08:58:30 -04:00
|
|
|
)
|
|
|
|
|
2019-12-19 10:02:18 -05:00
|
|
|
aioresponse.get(messages_url, status=200, payload=self.messages_response)
|
2019-07-04 08:58:30 -04:00
|
|
|
|
|
|
|
aioresponse.get(
|
2019-12-19 10:02:18 -05:00
|
|
|
messages_url, status=200, payload=self.empty_messages, repeat=True
|
2019-07-04 08:58:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
await client.receive_response(self.login_response)
|
|
|
|
|
|
|
|
client.start_loop(100)
|
|
|
|
|
|
|
|
await client.new_fetch_task.wait()
|
|
|
|
|
|
|
|
# Load the currently waiting task
|
2019-12-19 10:02:18 -05:00
|
|
|
tasks = client.pan_store.load_fetcher_tasks(client.server_name, client.user_id)
|
2019-07-04 08:58:30 -04:00
|
|
|
assert len(tasks) == 1
|
|
|
|
|
|
|
|
# Check that the task is our prev_batch from the sync resposne
|
|
|
|
assert tasks[0].room_id == TEST_ROOM_ID
|
|
|
|
assert tasks[0].token == "t392-516_47314_0_7_1_1_1_11444_1"
|
|
|
|
|
|
|
|
# Let's wait for the next fetch task
|
|
|
|
await client.new_fetch_task.wait()
|
|
|
|
|
2019-12-19 10:02:18 -05:00
|
|
|
tasks = client.pan_store.load_fetcher_tasks(client.server_name, client.user_id)
|
2019-07-04 08:58:30 -04:00
|
|
|
assert len(tasks) == 1
|
|
|
|
|
|
|
|
# Check that the task is our end token from the messages resposne
|
|
|
|
assert tasks[0].room_id == TEST_ROOM_ID
|
|
|
|
assert tasks[0].token == "t47409-4357353_219380_26003_2265"
|
|
|
|
|
|
|
|
# Wait for the next fetch loop iteration.
|
|
|
|
await client.fetch_loop_event.wait()
|
|
|
|
|
2019-12-19 10:02:18 -05:00
|
|
|
tasks = client.pan_store.load_fetcher_tasks(client.server_name, client.user_id)
|
2019-07-04 08:58:30 -04:00
|
|
|
# Check that there are no more tasks since we reached the start of the
|
|
|
|
# room timeline.
|
|
|
|
assert not tasks
|
|
|
|
|
|
|
|
await client.loop_stop()
|
2019-07-22 05:31:52 -04:00
|
|
|
|
|
|
|
async def test_history_fetching_resume(self, client, aioresponse, loop):
|
|
|
|
if not INDEXING_ENABLED:
|
|
|
|
pytest.skip("Indexing needs to be enabled to test this")
|
|
|
|
|
|
|
|
sync_url = re.compile(
|
2019-12-19 10:02:18 -05:00
|
|
|
r"^https://example\.org/_matrix/client/r0/sync\?access_token=.*"
|
2019-07-22 05:31:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
aioresponse.get(
|
2019-12-19 10:02:18 -05:00
|
|
|
sync_url, status=200, payload=self.initial_sync_response,
|
2019-07-22 05:31:52 -04:00
|
|
|
)
|
|
|
|
|
2019-12-19 10:02:18 -05:00
|
|
|
aioresponse.get(sync_url, status=200, payload=self.empty_sync, repeat=True)
|
2019-07-22 05:31:52 -04:00
|
|
|
|
|
|
|
aioresponse.post(
|
|
|
|
"https://example.org/_matrix/client/r0/keys/upload?access_token=abc123",
|
|
|
|
status=200,
|
|
|
|
payload=self.keys_upload_response,
|
2019-12-19 10:02:18 -05:00
|
|
|
repeat=True,
|
2019-07-22 05:31:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
aioresponse.post(
|
|
|
|
"https://example.org/_matrix/client/r0/keys/query?access_token=abc123",
|
|
|
|
status=200,
|
|
|
|
payload=self.keys_query_response,
|
2019-12-19 10:02:18 -05:00
|
|
|
repeat=True,
|
2019-07-22 05:31:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
messages_url = re.compile(
|
2019-12-19 10:02:18 -05:00
|
|
|
r"^https://example\.org/_matrix/client/r0/rooms/{}/messages\?.*".format(
|
|
|
|
TEST_ROOM_ID
|
|
|
|
)
|
2019-07-22 05:31:52 -04:00
|
|
|
)
|
|
|
|
|
2019-12-19 10:02:18 -05:00
|
|
|
aioresponse.get(messages_url, status=200, payload=self.messages_response)
|
2019-07-22 05:31:52 -04:00
|
|
|
|
|
|
|
aioresponse.get(
|
2019-12-19 10:02:18 -05:00
|
|
|
messages_url, status=200, payload=self.empty_messages, repeat=True
|
2019-07-22 05:31:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
await client.receive_response(self.login_response)
|
|
|
|
|
|
|
|
client.start_loop(100)
|
|
|
|
|
|
|
|
await client.new_fetch_task.wait()
|
|
|
|
await client.new_fetch_task.wait()
|
|
|
|
|
|
|
|
await client.loop_stop()
|
|
|
|
|
2019-12-19 10:02:18 -05:00
|
|
|
index_path = os.path.join(client.store_path, client.server_name, client.user_id)
|
2019-07-22 05:31:52 -04:00
|
|
|
|
|
|
|
# Remove the lock file since the GC won't do it for us
|
|
|
|
writer_lock = os.path.join(index_path, ".tantivy-writer.lock")
|
|
|
|
os.remove(writer_lock)
|
|
|
|
|
|
|
|
# Create a new client
|
|
|
|
client2 = PanClient(
|
|
|
|
client.server_name,
|
|
|
|
client.pan_store,
|
|
|
|
client.pan_conf,
|
|
|
|
client.homeserver,
|
|
|
|
client.queue,
|
|
|
|
client.user_id,
|
|
|
|
client.device_id,
|
|
|
|
client.store_path,
|
|
|
|
)
|
|
|
|
client2.user_id = client.user_id
|
|
|
|
client2.access_token = client.access_token
|
|
|
|
|
|
|
|
tasks = client2.pan_store.load_fetcher_tasks(
|
2019-12-19 10:02:18 -05:00
|
|
|
client2.server_name, client2.user_id
|
2019-07-22 05:31:52 -04:00
|
|
|
)
|
|
|
|
assert len(tasks) == 1
|
|
|
|
|
|
|
|
# Check that the task is our end token from the messages resposne
|
|
|
|
assert tasks[0].room_id == TEST_ROOM_ID
|
|
|
|
assert tasks[0].token == "t47409-4357353_219380_26003_2265"
|
|
|
|
|
|
|
|
client2.start_loop(100)
|
|
|
|
|
|
|
|
# We wait for two events here because the event gets fired at the start
|
|
|
|
# of the loop
|
|
|
|
await client2.fetch_loop_event.wait()
|
|
|
|
await client2.fetch_loop_event.wait()
|
|
|
|
|
|
|
|
tasks = client2.pan_store.load_fetcher_tasks(
|
2019-12-19 10:02:18 -05:00
|
|
|
client2.server_name, client2.user_id
|
2019-07-22 05:31:52 -04:00
|
|
|
)
|
|
|
|
# Check that there are no more tasks since we reached the start of the
|
|
|
|
# room timeline.
|
|
|
|
assert not tasks
|
|
|
|
|
|
|
|
await client2.loop_stop()
|
2019-12-19 10:02:31 -05:00
|
|
|
|
|
|
|
async def test_room_key_on_client_sync_stream(self, client):
|
|
|
|
await client.receive_response(self.login_response)
|
|
|
|
await client.receive_response(
|
|
|
|
SyncResponse.from_dict(self.initial_sync_response)
|
|
|
|
)
|
|
|
|
await client.receive_response(
|
|
|
|
KeysUploadResponse.from_dict(self.keys_upload_response)
|
|
|
|
)
|
|
|
|
await client.receive_response(
|
|
|
|
KeysQueryResponse.from_dict(self.keys_query_response)
|
|
|
|
)
|
|
|
|
|
|
|
|
BobId = "@bob:example.org"
|
|
|
|
Bob_device = "BOBDEVICE"
|
|
|
|
|
|
|
|
bob_olm = Olm(BobId, Bob_device, SqliteMemoryStore("ephemeral", "DEVICEID"))
|
|
|
|
|
|
|
|
alice_device = OlmDevice(
|
|
|
|
client.user_id, client.device_id, client.olm.account.identity_keys
|
|
|
|
)
|
|
|
|
|
|
|
|
bob_device = OlmDevice(
|
|
|
|
bob_olm.user_id, bob_olm.device_id, bob_olm.account.identity_keys
|
|
|
|
)
|
|
|
|
|
|
|
|
client.olm.device_store.add(bob_device)
|
|
|
|
bob_olm.device_store.add(alice_device)
|
|
|
|
bob_olm.store.save_device_keys(
|
|
|
|
{client.user_id: {client.device_id: alice_device}}
|
|
|
|
)
|
|
|
|
|
|
|
|
client.olm.account.generate_one_time_keys(1)
|
|
|
|
one_time = list(client.olm.account.one_time_keys["curve25519"].values())[0]
|
|
|
|
client.olm.account.mark_keys_as_published()
|
|
|
|
|
|
|
|
bob_olm.create_session(one_time, alice_device.curve25519)
|
|
|
|
|
|
|
|
_, to_device = bob_olm.share_group_session(
|
|
|
|
TEST_ROOM_ID, [client.user_id], ignore_unverified_devices=True
|
|
|
|
)
|
|
|
|
outbound_session = bob_olm.outbound_group_sessions[TEST_ROOM_ID]
|
|
|
|
olm_content = to_device["messages"][client.user_id][client.device_id]
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
"sender": bob_olm.user_id,
|
|
|
|
"type": "m.room.encrypted",
|
|
|
|
"content": olm_content,
|
|
|
|
}
|
|
|
|
|
|
|
|
sync_response = self.empty_sync
|
|
|
|
sync_response["to_device"]["events"].append(payload)
|
|
|
|
|
|
|
|
session = client.olm.inbound_group_store.get(
|
|
|
|
TEST_ROOM_ID, bob_device.curve25519, outbound_session.id
|
|
|
|
)
|
|
|
|
assert not session
|
|
|
|
|
|
|
|
client.handle_to_device_from_sync_body(sync_response)
|
|
|
|
|
|
|
|
session = client.olm.inbound_group_store.get(
|
|
|
|
TEST_ROOM_ID, bob_device.curve25519, outbound_session.id
|
|
|
|
)
|
|
|
|
assert session
|