-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathconftest.py
184 lines (137 loc) · 4.4 KB
/
conftest.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
import asyncio
import shutil
import tempfile
from random import choices
from string import ascii_letters, ascii_uppercase, digits
from urllib.parse import urlparse
import janus
import pytest
from aiohttp import web
from aioresponses import aioresponses
from faker import Faker
from faker.providers import BaseProvider
from nio.crypto import OlmAccount, OlmDevice
from nio.store import SqliteStore
from pantalaimon.config import ServerConfig
from pantalaimon.daemon import ProxyDaemon
from pantalaimon.store import ClientInfo, PanStore
faker = Faker()
class Provider(BaseProvider):
def mx_id(self):
return "@{}:{}".format(faker.user_name(), faker.hostname())
def device_id(self):
return "".join(choices(ascii_uppercase, k=10))
def access_token(self):
return "MDA" + "".join(choices(digits + ascii_letters, k=272))
def client(self):
return ClientInfo(faker.mx_id(), faker.access_token())
def avatar_url(self):
return "mxc://{}/{}#auto".format(
faker.hostname(), "".join(choices(ascii_letters) for i in range(24))
)
def olm_key_pair(self):
return OlmAccount().identity_keys
def olm_device(self):
user_id = faker.mx_id()
device_id = faker.device_id()
key_pair = faker.olm_key_pair()
return OlmDevice(
user_id,
device_id,
key_pair,
)
faker.add_provider(Provider)
@pytest.fixture
def access_token():
return faker.access_token()
@pytest.fixture
def client():
return faker.client()
@pytest.fixture
def tempdir():
newpath = tempfile.mkdtemp()
yield newpath
shutil.rmtree(newpath)
@pytest.fixture
def panstore(tempdir):
for _ in range(10):
store = SqliteStore(faker.mx_id(), faker.device_id(), tempdir, "", "pan.db")
account = OlmAccount()
store.save_account(account)
store = PanStore(tempdir, "pan.db")
return store
@pytest.fixture
def panstore_with_users(panstore):
accounts = panstore.load_all_users()
user_id, device_id = accounts[0]
server = "example"
panstore.save_server_user(server, user_id)
server2 = "localhost"
user_id2, device_id2 = accounts[1]
panstore.save_server_user(server2, user_id2)
return panstore
@pytest.fixture
async def pan_proxy_server(tempdir, aiohttp_server):
loop = asyncio.get_event_loop()
app = web.Application()
server_name = faker.hostname()
config = ServerConfig(server_name, urlparse("https://example.org"), keyring=False)
pan_queue = janus.Queue()
ui_queue = janus.Queue()
proxy = ProxyDaemon(
config.name,
config.homeserver,
config,
tempdir,
send_queue=pan_queue.async_q,
recv_queue=ui_queue.async_q,
proxy=None,
ssl=False,
client_store_class=SqliteStore,
)
app.add_routes(
[
web.post("/_matrix/client/r0/login", proxy.login),
web.get("/_matrix/client/r0/sync", proxy.sync),
web.get("/_matrix/client/r0/rooms/{room_id}/messages", proxy.messages),
web.put(
r"/_matrix/client/r0/rooms/{room_id}/send/{event_type}/{txnid}",
proxy.send_message,
),
web.post("/_matrix/client/r0/user/{user_id}/filter", proxy.filter),
web.post("/_matrix/client/r0/search", proxy.search),
web.options("/_matrix/client/r0/search", proxy.search_opts),
]
)
server = await aiohttp_server(app)
yield server, proxy, (pan_queue, ui_queue)
await proxy.shutdown(app)
@pytest.fixture
async def running_proxy(pan_proxy_server, aioresponse, aiohttp_client):
server, proxy, queues = pan_proxy_server
login_response = {
"access_token": "abc123",
"device_id": "GHTYAJCE",
"home_server": "example.org",
"user_id": "@example:example.org",
}
aioclient = await aiohttp_client(server)
aioresponse.post(
"https://example.org/_matrix/client/r0/login",
status=200,
payload=login_response,
repeat=True,
)
await aioclient.post(
"/_matrix/client/r0/login",
json={
"type": "m.login.password",
"user": "example",
"password": "wordpass",
},
)
yield server, aioclient, proxy, queues
@pytest.fixture
def aioresponse():
with aioresponses(passthrough=["http://127.0.0.1"]) as m:
yield m