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 
 
 
 
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import responses
import pytest
import json
from copy import deepcopy
from matrix_client import client, api
from matrix_client.errors import MatrixRequestError, MatrixError, MatrixHttpLibError
from matrix_client import __version__ as lib_version
from . import response_examples
MATRIX_V2_API_PATH = "/_matrix/client/r0"


class TestTagsApi:
    cli = client.MatrixClient("http://example.com")
    user_id = "@user:matrix.org"
    room_id = "#foo:matrix.org"

    @responses.activate
    def test_get_user_tags(self):
        tags_url = "http://example.com" \
            "/_matrix/client/r0/user/@user:matrix.org/rooms/#foo:matrix.org/tags"
        responses.add(responses.GET, tags_url, body='{}')
        self.cli.api.get_user_tags(self.user_id, self.room_id)
        req = responses.calls[0].request
        assert req.url == tags_url
        assert req.method == 'GET'

    @responses.activate
    def test_add_user_tags(self):
        tags_url = "http://example.com" \
            "/_matrix/client/r0/user/@user:matrix.org/rooms/#foo:matrix.org/tags/foo"
        responses.add(responses.PUT, tags_url, body='{}')
        self.cli.api.add_user_tag(self.user_id, self.room_id, "foo", body={"order": "5"})
        req = responses.calls[0].request
        assert req.url == tags_url
        assert req.method == 'PUT'

    @responses.activate
    def test_remove_user_tags(self):
        tags_url = "http://example.com" \
            "/_matrix/client/r0/user/@user:matrix.org/rooms/#foo:matrix.org/tags/foo"
        responses.add(responses.DELETE, tags_url, body='{}')
        self.cli.api.remove_user_tag(self.user_id, self.room_id, "foo")
        req = responses.calls[0].request
        assert req.url == tags_url
        assert req.method == 'DELETE'


class TestAccountDataApi:
    cli = client.MatrixClient("http://example.com")
    user_id = "@user:matrix.org"
    room_id = "#foo:matrix.org"

    @responses.activate
    def test_set_account_data(self):
        account_data_url = "http://example.com" \
            "/_matrix/client/r0/user/@user:matrix.org/account_data/foo"
        responses.add(responses.PUT, account_data_url, body='{}')
        self.cli.api.set_account_data(self.user_id, 'foo', {'bar': 1})
        req = responses.calls[0].request
        assert req.url == account_data_url
        assert req.method == 'PUT'

    @responses.activate
    def test_set_room_account_data(self):
        account_data_url = "http://example.com/_matrix/client/r0/user" \
            "/@user:matrix.org/rooms/#foo:matrix.org/account_data/foo"
        responses.add(responses.PUT, account_data_url, body='{}')
        self.cli.api.set_room_account_data(self.user_id, self.room_id, 'foo', {'bar': 1})
        req = responses.calls[0].request
        assert req.url == account_data_url
        assert req.method == 'PUT'


class TestUnbanApi:
    cli = client.MatrixClient("http://example.com")
    user_id = "@user:matrix.org"
    room_id = "#foo:matrix.org"

    @responses.activate
    def test_unban(self):
        unban_url = "http://example.com" \
                    "/_matrix/client/r0/rooms/#foo:matrix.org/unban"
        responses.add(responses.POST, unban_url, body='{}')
        self.cli.api.unban_user(self.room_id, self.user_id)
        req = responses.calls[0].request
        assert req.url == unban_url
        assert req.method == 'POST'


class TestDeviceApi:
    cli = client.MatrixClient("http://example.com")
    device_id = "QBUAZIFURK"
    display_name = "test_name"
    auth_body = {
        "auth": {
            "type": "example.type.foo",
            "session": "xxxxx",
            "example_credential": "verypoorsharedsecret"
        }
    }

    @responses.activate
    def test_get_devices(self):
        get_devices_url = "http://example.com/_matrix/client/r0/devices"
        responses.add(responses.GET, get_devices_url, body='{}')
        self.cli.api.get_devices()
        req = responses.calls[0].request
        assert req.url == get_devices_url
        assert req.method == 'GET'

    @responses.activate
    def test_get_device(self):
        get_device_url = "http://example.com/_matrix/client/r0/devices/QBUAZIFURK"
        responses.add(responses.GET, get_device_url, body='{}')
        self.cli.api.get_device(self.device_id)
        req = responses.calls[0].request
        assert req.url == get_device_url
        assert req.method == 'GET'

    @responses.activate
    def test_update_device_info(self):
        update_url = "http://example.com/_matrix/client/r0/devices/QBUAZIFURK"
        responses.add(responses.PUT, update_url, body='{}')
        self.cli.api.update_device_info(self.device_id, self.display_name)
        req = responses.calls[0].request
        assert req.url == update_url
        assert req.method == 'PUT'

    @responses.activate
    def test_delete_device(self):
        delete_device_url = "http://example.com/_matrix/client/r0/devices/QBUAZIFURK"
        responses.add(responses.DELETE, delete_device_url, body='{}')
        # Test for 401 status code of User-Interactive Auth API
        responses.add(responses.DELETE, delete_device_url, body='{}', status=401)
        self.cli.api.delete_device(self.auth_body, self.device_id)
        req = responses.calls[0].request
        assert req.url == delete_device_url
        assert req.method == 'DELETE'

        with pytest.raises(MatrixRequestError):
            self.cli.api.delete_device(self.auth_body, self.device_id)

    @responses.activate
    def test_delete_devices(self):
        delete_devices_url = "http://example.com/_matrix/client/r0/delete_devices"
        responses.add(responses.POST, delete_devices_url, body='{}')
        # Test for 401 status code of User-Interactive Auth API
        responses.add(responses.POST, delete_devices_url, body='{}', status=401)
        self.cli.api.delete_devices(self.auth_body, [self.device_id])
        req = responses.calls[0].request
        assert req.url == delete_devices_url
        assert req.method == 'POST'

        with pytest.raises(MatrixRequestError):
            self.cli.api.delete_devices(self.auth_body, [self.device_id])


class TestKeysApi:
    cli = client.MatrixClient("http://example.com")
    user_id = "@alice:matrix.org"
    device_id = "JLAFKJWSCS"
    one_time_keys = {"curve25519:AAAAAQ": "/qyvZvwjiTxGdGU0RCguDCLeR+nmsb3FfNG3/Ve4vU8"}
    device_keys = {
        "user_id": "@alice:example.com",
        "device_id": "JLAFKJWSCS",
        "algorithms": [
            "m.olm.curve25519-aes-sha256",
            "m.megolm.v1.aes-sha"
        ],
        "keys": {
            "curve25519:JLAFKJWSCS": "3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI",
            "ed25519:JLAFKJWSCS": "lEuiRJBit0IG6nUf5pUzWTUEsRVVe/HJkoKuEww9ULI"
        },
        "signatures": {
            "@alice:example.com": {
                "ed25519:JLAFKJWSCS": ("dSO80A01XiigH3uBiDVx/EjzaoycHcjq9lfQX0uWsqxl2gi"
                                       "MIiSPR8a4d291W1ihKJL/a+myXS367WT6NAIcBA")
            }
        }
    }

    @responses.activate
    @pytest.mark.parametrize("args", [
        {},
        {'device_keys': device_keys},
        {'one_time_keys': one_time_keys}
    ])
    def test_upload_keys(self, args):
        upload_keys_url = "http://example.com/_matrix/client/r0/keys/upload"
        responses.add(responses.POST, upload_keys_url, body='{}')
        self.cli.api.upload_keys(**args)
        req = responses.calls[0].request
        assert req.url == upload_keys_url
        assert req.method == 'POST'

    @responses.activate
    def test_query_keys(self):
        query_user_keys_url = "http://example.com/_matrix/client/r0/keys/query"
        responses.add(responses.POST, query_user_keys_url, body='{}')
        self.cli.api.query_keys({self.user_id: self.device_id}, timeout=10)
        req = responses.calls[0].request
        assert req.url == query_user_keys_url
        assert req.method == 'POST'

    @responses.activate
    def test_claim_keys(self):
        claim_keys_url = "http://example.com/_matrix/client/r0/keys/claim"
        responses.add(responses.POST, claim_keys_url, body='{}')
        self.cli.api.claim_keys({self.user_id: {self.device_id: "algo"}}, timeout=1000)
        req = responses.calls[0].request
        assert req.url == claim_keys_url
        assert req.method == 'POST'

    @responses.activate
    def test_key_changes(self):
        key_changes_url = "http://example.com/_matrix/client/r0/keys/changes"
        responses.add(responses.GET, key_changes_url, body='{}')
        self.cli.api.key_changes('s72594_4483_1934', 's75689_5632_2435')
        req = responses.calls[0].request
        assert req.url.split('?')[0] == key_changes_url
        assert req.method == 'GET'


class TestSendToDeviceApi:
    cli = client.MatrixClient("http://example.com")
    user_id = "@alice:matrix.org"
    device_id = "JLAFKJWSCS"

    @responses.activate
    def test_send_to_device(self):
        txn_id = self.cli.api._make_txn_id()
        send_to_device_url = \
            "http://example.com/_matrix/client/r0/sendToDevice/m.new_device/" + txn_id
        responses.add(responses.PUT, send_to_device_url, body='{}')
        payload = {self.user_id: {self.device_id: {"test": 1}}}
        self.cli.api.send_to_device("m.new_device", payload, txn_id)
        req = responses.calls[0].request
        assert req.url == send_to_device_url
        assert req.method == 'PUT'


class TestMainApi:
    user_id = "@alice:matrix.org"
    token = "Dp0YKRXwx0iWDhFj7lg3DVjwsWzGcUIgARljgyAip2JD8qd5dSaW" \
        "cxowTKEFetPulfLijAhv8eOmUSScyGcWgZyNMRTBmoJ0RFc0HotPvTBZ" \
        "U98yKRLtat7V43aCpFmK"
    test_path = "/account/whoami"

    @responses.activate
    def test_send_token_header(self):
        mapi = api.MatrixHttpApi("http://example.com", token=self.token)
        responses.add(
            responses.GET,
            mapi._base_url+MATRIX_V2_API_PATH+self.test_path,
            body='{"application/json": {"user_id": "%s"}}' % self.user_id
        )
        mapi._send("GET", self.test_path)
        req = responses.calls[0].request
        assert req.method == 'GET'
        assert req.headers['Authorization'] == 'Bearer %s' % self.token

    @responses.activate
    def test_send_user_agent_header(self):
        mapi = api.MatrixHttpApi("http://example.com")
        responses.add(
            responses.GET,
            mapi._base_url+MATRIX_V2_API_PATH+self.test_path,
            body='{"application/json": {"user_id": "%s"}}' % self.user_id
        )
        mapi._send("GET", self.test_path)
        req = responses.calls[0].request
        assert req.method == 'GET'
        assert req.headers['User-Agent'] == 'matrix-python-sdk/%s' % lib_version

    @responses.activate
    def test_send_token_query(self):
        mapi = api.MatrixHttpApi(
            "http://example.com",
            token=self.token,
            use_authorization_header=False
        )
        responses.add(
            responses.GET,
            mapi._base_url+MATRIX_V2_API_PATH+self.test_path,
            body='{"application/json": {"user_id": "%s"}}' % self.user_id
        )
        mapi._send("GET", self.test_path)
        req = responses.calls[0].request
        assert req.method == 'GET'
        assert self.token in req.url

    @responses.activate
    def test_send_user_id(self):
        mapi = api.MatrixHttpApi(
            "http://example.com",
            token=self.token,
            identity=self.user_id
        )
        responses.add(
            responses.GET,
            mapi._base_url+MATRIX_V2_API_PATH+self.test_path,
            body='{"application/json": {"user_id": "%s"}}' % self.user_id
        )
        mapi._send("GET", self.test_path)
        req = responses.calls[0].request
        assert "user_id" in req.url

    @responses.activate
    def test_send_unsup_method(self):
        mapi = api.MatrixHttpApi("http://example.com")
        with pytest.raises(MatrixError):
            mapi._send("GOT", self.test_path)

    @responses.activate
    def test_send_request_error(self):
        mapi = api.MatrixHttpApi("http://example.com")
        with pytest.raises(MatrixHttpLibError):
            mapi._send("GET", self.test_path)


class TestMediaApi:
    cli = client.MatrixClient("http://example.com")
    user_id = "@alice:example.com"
    mxcurl = "mxc://example.com/OonjUOmcuVpUnmOWKtzPmAFe"

    @responses.activate
    def test_media_download(self):
        media_url = \
            "http://example.com/_matrix/media/r0/download/" + self.mxcurl[6:]
        with open('test/response_examples.py', 'rb') as fil:
            responses.add(
                responses.GET, media_url,
                content_type='application/python',
                body=fil.read(), status=200, stream=True
            )
        resp = self.cli.api.media_download(self.mxcurl, allow_remote=False)
        resp.raw.decode_content = True
        req = responses.calls[0].request
        assert req.url.split('?')[0] == media_url
        assert req.method == 'GET'

    def test_media_download_wrong_url(self):
        with pytest.raises(ValueError):
            self.cli.api.media_download(self.mxcurl[6:])

    @responses.activate
    def test_get_thumbnail(self):
        media_url = \
            "http://example.com/_matrix/media/r0/thumbnail/" + self.mxcurl[6:]
        with open('test/response_examples.py', 'rb') as fil:
            responses.add(
                responses.GET, media_url,
                content_type='application/python',
                body=fil.read(), status=200, stream=True
            )
        resp = self.cli.api.get_thumbnail(
            self.mxcurl, 28, 28, allow_remote=False
        )
        resp.raw.decode_content = True
        req = responses.calls[0].request
        assert req.url.split('?')[0] == media_url
        assert req.method == 'GET'

    def test_get_thumbnail_wrong_url(self):
        with pytest.raises(ValueError):
            self.cli.api.get_thumbnail(self.mxcurl[6:], 28, 28)

    def test_get_thumbnail_wrong_method(self):
        with pytest.raises(ValueError):
            self.cli.api.get_thumbnail(self.mxcurl, 28, 28, 'cut')

    @responses.activate
    def test_get_url_preview(self):
        media_url = \
            "http://example.com/_matrix/media/r0/preview_url"
        preview_url = deepcopy(response_examples.example_preview_url)
        responses.add(
            responses.GET, media_url,
            body=json.dumps(preview_url)
        )
        self.cli.api.get_url_preview("https://google.com/", 1510610716656)
        req = responses.calls[0].request
        assert req.url.split('?')[0] == media_url
        assert req.method == 'GET'


class TestRoomApi:
    cli = client.MatrixClient("http://example.com")
    user_id = "@user:matrix.org"
    room_id = "#foo:matrix.org"

    @responses.activate
    def test_create_room_visibility_public(self):
        create_room_url = "http://example.com" \
            "/_matrix/client/r0/createRoom"
        responses.add(
            responses.POST,
            create_room_url,
            json='{"room_id": "!sefiuhWgwghwWgh:example.com"}'
        )
        self.cli.api.create_room(
            name="test",
            alias="#test:example.com",
            is_public=True
        )
        req = responses.calls[0].request
        assert req.url == create_room_url
        assert req.method == 'POST'
        j = json.loads(req.body)
        assert j["room_alias_name"] == "#test:example.com"
        assert j["visibility"] == "public"
        assert j["name"] == "test"

    @responses.activate
    def test_create_room_visibility_private(self):
        create_room_url = "http://example.com" \
            "/_matrix/client/r0/createRoom"
        responses.add(
            responses.POST,
            create_room_url,
            json='{"room_id": "!sefiuhWgwghwWgh:example.com"}'
        )
        self.cli.api.create_room(
            name="test",
            alias="#test:example.com",
            is_public=False
        )
        req = responses.calls[0].request
        assert req.url == create_room_url
        assert req.method == 'POST'
        j = json.loads(req.body)
        assert j["room_alias_name"] == "#test:example.com"
        assert j["visibility"] == "private"
        assert j["name"] == "test"

    @responses.activate
    def test_create_room_federate_true(self):
        create_room_url = "http://example.com" \
            "/_matrix/client/r0/createRoom"
        responses.add(
            responses.POST,
            create_room_url,
            json='{"room_id": "!sefiuhWgwghwWgh:example.com"}'
        )
        self.cli.api.create_room(
            name="test2",
            alias="#test2:example.com",
            federate=True
        )
        req = responses.calls[0].request
        assert req.url == create_room_url
        assert req.method == 'POST'
        j = json.loads(req.body)
        assert j["creation_content"]["m.federate"]

    @responses.activate
    def test_create_room_federate_false(self):
        create_room_url = "http://example.com" \
            "/_matrix/client/r0/createRoom"
        responses.add(
            responses.POST,
            create_room_url,
            json='{"room_id": "!sefiuhWgwghwWgh:example.com"}'
        )
        self.cli.api.create_room(
            name="test",
            alias="#test:example.com",
            federate=False
        )
        req = responses.calls[0].request
        assert req.url == create_room_url
        assert req.method == 'POST'
        j = json.loads(req.body)
        assert not j["creation_content"]["m.federate"]


class TestWhoamiQuery:
    user_id = "@alice:example.com"
    token = "Dp0YKRXwx0iWDhFj7lg3DVjwsWzGcUIgARljgyAip2JD8qd5dSaW" \
            "cxowTKEFetPulfLijAhv8eOmUSScyGcWgZyNMRTBmoJ0RFc0HotPvTBZ" \
            "U98yKRLtat7V43aCpFmK"

    @responses.activate
    def test_whoami(self):
        mapi = api.MatrixHttpApi("http://example.com", token=self.token)
        whoami_url = "http://example.com/_matrix/client/r0/account/whoami"
        responses.add(
            responses.GET,
            whoami_url,
            body='{"user_id": "%s"}' % self.user_id
        )
        mapi.whoami()
        req = responses.calls[0].request
        assert req.method == 'GET'
        assert whoami_url in req.url

    @responses.activate
    def test_whoami_unauth(self):
        mapi = api.MatrixHttpApi("http://example.com")
        whoami_url = "http://example.com/_matrix/client/r0/account/whoami"
        responses.add(
            responses.GET,
            whoami_url,
            body='{"user_id": "%s"}' % self.user_id
        )
        with pytest.raises(MatrixError):
            mapi.whoami()