-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathproxy_test.py
202 lines (152 loc) · 5.47 KB
/
proxy_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import json
import re
from collections import defaultdict
from nio.crypto import OlmDevice
from conftest import faker
from pantalaimon.thread_messages import UpdateDevicesMessage, UpdateUsersMessage
BOB_ID = "@bob:example.org"
BOB_DEVICE = "AGMTSWVYML"
BOB_CURVE = "T9tOKF+TShsn6mk1zisW2IBsBbTtzDNvw99RBFMJOgI"
BOB_ONETIME = "6QlQw3mGUveS735k/JDaviuoaih5eEi6S1J65iHjfgU"
class TestClass(object):
@staticmethod
def _load_response(filename):
with open(filename) as f:
return json.loads(f.read())
@property
def login_response(self):
return {
"access_token": "abc123",
"device_id": "GHTYAJCE",
"home_server": "example.org",
"user_id": "@example:example.org",
}
@property
def sync_response(self):
return self._load_response("tests/data/sync.json")
@property
def keys_upload_response(self):
return {"one_time_key_counts": {"curve25519": 10, "signed_curve25519": 20}}
@property
def example_devices(self):
devices = defaultdict(dict)
for _ in range(10):
device = faker.olm_device()
devices[device.user_id][device.id] = device
bob_device = OlmDevice(
BOB_ID, BOB_DEVICE, {"ed25519": BOB_ONETIME, "curve25519": BOB_CURVE}
)
devices[BOB_ID][BOB_DEVICE] = bob_device
return devices
async def test_daemon_start(self, pan_proxy_server, aiohttp_client, aioresponse):
server, daemon, _ = pan_proxy_server
client = await aiohttp_client(server)
aioresponse.post(
"https://example.org/_matrix/client/r0/login",
status=200,
payload=self.login_response,
repeat=True,
)
assert not daemon.pan_clients
resp = await client.post(
"/_matrix/client/r0/login",
json={
"type": "m.login.password",
"user": "example",
"password": "wordpass",
},
)
assert resp.status == 200
assert len(daemon.pan_clients) == 1
pan_client = list(daemon.pan_clients.values())[0]
# Check if our pan client is logged in
assert pan_client.logged_in
# Check if our pan client has a sync loop started
assert pan_client.task
async def test_pan_client_sync(self, pan_proxy_server, aiohttp_client, aioresponse):
server, daemon, _ = pan_proxy_server
client = await aiohttp_client(server)
aioresponse.post(
"https://example.org/_matrix/client/r0/login",
status=200,
payload=self.login_response,
repeat=True,
)
sync_url = re.compile(
r"^https://example\.org/_matrix/client/r0/sync\?access_token=.*"
)
aioresponse.get(
sync_url,
status=200,
payload=self.sync_response,
)
await client.post(
"/_matrix/client/r0/login",
json={
"type": "m.login.password",
"user": "example",
"password": "wordpass",
},
)
# Check that the pan client started to sync after logging in.
pan_client = list(daemon.pan_clients.values())[0]
assert len(pan_client.rooms) == 1
async def test_pan_client_keys_upload(
self, pan_proxy_server, aiohttp_client, aioresponse
):
server, daemon, _ = pan_proxy_server
client = await aiohttp_client(server)
aioresponse.post(
"https://example.org/_matrix/client/r0/login",
status=200,
payload=self.login_response,
repeat=True,
)
sync_url = re.compile(
r"^https://example\.org/_matrix/client/r0/sync\?access_token=.*"
)
aioresponse.get(
sync_url,
status=200,
payload=self.sync_response,
)
keys_upload_url = re.compile(
r"^https://example\.org/_matrix/client/r0/keys/upload\?.*"
)
aioresponse.post(
keys_upload_url,
status=200,
payload=self.keys_upload_response,
)
await client.post(
"/_matrix/client/r0/login",
json={
"type": "m.login.password",
"user": "example",
"password": "wordpass",
},
)
pan_client = list(daemon.pan_clients.values())[0]
assert pan_client.olm.account.shared
async def test_server_users_update(self, running_proxy):
_, _, _, queues = running_proxy
queue, _ = queues
queue = queue.sync_q
message = queue.get_nowait()
assert isinstance(message, UpdateUsersMessage)
assert message.user_id == "@example:example.org"
assert message.device_id == "GHTYAJCE"
async def tests_server_devices_update(self, running_proxy):
_, _, proxy, queues = running_proxy
queue, _ = queues
queue = queue.sync_q
devices = self.example_devices
bob_device = devices[BOB_ID][BOB_DEVICE]
message = queue.get_nowait()
assert isinstance(message, UpdateUsersMessage)
client = list(proxy.pan_clients.values())[0]
client.store.save_device_keys(devices)
await client.send_update_device(bob_device)
message = queue.get_nowait()
assert isinstance(message, UpdateDevicesMessage)
assert BOB_DEVICE in message.devices[BOB_ID]