-
Notifications
You must be signed in to change notification settings - Fork 161
/
room_test.py
715 lines (598 loc) · 23.6 KB
/
room_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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# Copyright © 2021 Famedly GmbH
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import pytest
from helpers import faker
from nio.api import ReceiptType
from nio.events import (
InviteAliasEvent,
InviteMemberEvent,
InviteNameEvent,
Receipt,
ReceiptEvent,
RoomAvatarEvent,
RoomCreateEvent,
RoomGuestAccessEvent,
RoomHistoryVisibilityEvent,
RoomJoinRulesEvent,
RoomMemberEvent,
RoomNameEvent,
RoomSpaceChildEvent,
RoomSpaceParentEvent,
RoomUpgradeEvent,
TypingNoticeEvent,
)
from nio.responses import RoomSummary
from nio.rooms import MatrixInvitedRoom, MatrixRoom
TEST_ROOM = "!test:example.org"
BOB_ID = "@bob:example.org"
ALICE_ID = "@alice:example.org"
class TestClass:
def _create_test_data(self):
pass
@property
def new_user(self):
return faker.mx_id(), faker.name(), faker.avatar_url()
@property
def test_room(self):
room = MatrixRoom(TEST_ROOM, BOB_ID)
room.update_summary(RoomSummary(0, 0, []))
return room
def test_room_creation(self):
room = self.test_room
assert room
def test_adding_members(self):
room = self.test_room
assert not room.users
mx_id, name, avatar = self.new_user
room.add_member(mx_id, name, avatar)
room.summary.heroes.append(mx_id)
room.summary.joined_member_count += 1
assert room.users
assert room.member_count == room.joined_count == 1
assert room.invited_count == 0
room.summary = None
assert room.member_count == room.joined_count == 1
assert room.invited_count == 0
member = list(room.users.values())[0]
assert member.user_id == mx_id
assert member.display_name == name
assert member.avatar_url == avatar
def test_summary_details(self):
room = self.test_room
room.summary = None
with pytest.raises(ValueError, match="Unusable summary"):
assert room._summary_details()
room.summary = RoomSummary(None, None, [])
with pytest.raises(ValueError, match="Unusable summary"):
assert room._summary_details()
room.summary = RoomSummary(0, None, [])
with pytest.raises(ValueError, match="Unusable summary"):
assert room._summary_details()
room.summary = RoomSummary(None, 0, [])
with pytest.raises(ValueError, match="Unusable summary"):
assert room._summary_details()
room.summary = RoomSummary(0, 0, [])
assert room._summary_details() == ([], 0, 0)
def test_named_checks(self):
room = self.test_room
assert not room.is_named
assert room.is_group
room.name = "Test room"
assert room.is_named
assert not room.is_group
def test_name_calculation_when_unnamed(self):
room = self.test_room
assert room.named_room_name() is None
assert room.display_name == "Empty Room"
# Members join
room.add_member(BOB_ID, "Bob", None) # us
room.summary.joined_member_count += 1
assert room.display_name == "Empty Room"
room.add_member("@alice:example.org", "Alice", None)
room.summary.heroes.append("@alice:example.org")
room.summary.joined_member_count += 1
assert room.display_name == "Alice"
room.add_member("@malory:example.org", "Alice", None)
room.summary.heroes.append("@malory:example.org")
room.summary.joined_member_count += 1
assert (
room.display_name
== "Alice (@alice:example.org) and Alice (@malory:example.org)"
)
room.add_member("@steve:example.org", "Steve", None)
room.summary.heroes.append("@steve:example.org")
room.summary.joined_member_count += 1
assert (
room.display_name
== "Alice (@alice:example.org), Alice (@malory:example.org) "
"and Steve"
)
room.add_member("@carol:example.org", "Carol", None)
room.summary.joined_member_count += 1
assert (
room.display_name
== "Alice (@alice:example.org), Alice (@malory:example.org), "
"Steve and 1 other"
)
room.add_member("@dave:example.org", "Dave", None)
room.summary.joined_member_count += 1
assert (
room.display_name
== "Alice (@alice:example.org), Alice (@malory:example.org), "
"Steve and 2 others"
)
room.add_member("@erin:example.org", "Eirin", None)
room.summary.invited_member_count += 1
assert (
room.display_name
== "Alice (@alice:example.org), Alice (@malory:example.org), "
"Steve and 3 others"
)
# Members leave
room.summary.joined_member_count = 1
room.summary.invited_member_count = 0
assert (
room.display_name == "Empty Room (had Alice (@alice:example.org), "
"Alice (@malory:example.org) and Steve)"
)
room.remove_member("@steve:example.org")
room.summary.heroes.remove("@steve:example.org")
assert (
room.display_name == "Empty Room (had Alice (@alice:example.org) and "
"Alice (@malory:example.org))"
)
room.remove_member("@malory:example.org")
room.summary.heroes.remove("@malory:example.org")
assert room.display_name == "Empty Room (had Alice)"
room.remove_member("@alice:example.org")
room.summary.heroes.remove("@alice:example.org")
assert room.display_name == "Empty Room"
room.remove_member("@bob:example.org") # us
assert not room.summary.heroes
assert room.display_name == "Empty Room"
def test_name_calculation_when_unnamed_no_summary(self):
room = self.test_room
room.summary = RoomSummary()
assert room.named_room_name() is None
assert room.display_name == "Empty Room"
# Members join
room.add_member(BOB_ID, "Bob", None) # us
assert room.display_name == "Empty Room"
room.add_member("@alice:example.org", "Alice", None)
assert room.display_name == "Alice"
room.add_member("@malory:example.org", "Alice", None)
assert (
room.display_name
== "Alice (@alice:example.org) and Alice (@malory:example.org)"
)
room.add_member("@steve:example.org", "Steve", None)
room.add_member("@carol:example.org", "Carol", None)
room.add_member("@dave:example.org", "Dave", None)
assert (
room.display_name
== "Alice (@alice:example.org), Alice (@malory:example.org), "
"Carol, Dave and Steve"
)
room.add_member("@erin:example.org", "Eirin", None)
assert (
room.display_name
== "Alice (@alice:example.org), Alice (@malory:example.org), "
"Carol, Dave, Eirin and 1 other"
)
room.add_member("@frank:example.org", "Frank", None)
assert (
room.display_name
== "Alice (@alice:example.org), Alice (@malory:example.org), "
"Carol, Dave, Eirin and 2 others"
)
room.add_member("@gregor:example.org", "Gregor", None)
assert (
room.display_name
== "Alice (@alice:example.org), Alice (@malory:example.org), "
"Carol, Dave, Eirin and 3 others"
)
# Members leave
for member in room.users.copy():
room.remove_member(member)
assert room.display_name == "Empty Room"
def test_name_calculation_with_canonical_alias(self):
room = self.test_room
room.canonical_alias = "#test:termina.org.uk"
assert room.display_name == "#test:termina.org.uk"
def test_name_calculation_prefer_name_over_alias(self):
room = self.test_room
room.canonical_alias = "#test:termina.org.uk"
room.name = "Test room"
assert room.display_name == "Test room"
def test_name_calculation_when_hash_already_prefixed(self):
room = self.test_room
room.name = "#test"
assert room.display_name == "#test"
def test_set_room_avatar(self):
room = self.test_room
room.room_avatar_url = "mxc://foo"
assert room.gen_avatar_url == "mxc://foo"
def test_room_avatar_calculation_when_no_set_avatar(self):
room = self.test_room
assert room.room_avatar_url is None
assert room.summary
assert room.is_group
room.add_member("@bob:example.org", "Bob", "mxc://abc", True) # us
room.summary.joined_member_count += 1
assert room.gen_avatar_url is None
room.add_member("@carol:example.org", "Carol", "mxc://bar", True)
room.summary.invited_member_count += 1
assert room.gen_avatar_url is None
room.summary.heroes.append("@carol:example.org")
assert room.gen_avatar_url == "mxc://bar"
room.name = "Test"
assert not room.is_group
assert room.gen_avatar_url is None
room.name = None
assert room.is_group
assert room.gen_avatar_url == "mxc://bar"
room.add_member("@alice:example.org", "Alice", "mxc://baz")
room.summary.heroes.append("@alice:matrix.org")
room.summary.joined_member_count += 1
assert room.gen_avatar_url is None
def test_room_avatar_calculation_when_no_set_avatar_no_summary(self):
room = self.test_room
room.summary = None
assert room.room_avatar_url is None
assert room.is_group
room.add_member("@bob:example.org", "Bob", "mxc://abc", True) # us
assert room.gen_avatar_url is None
room.add_member("@carol:example.org", "Carol", "mxc://bar", True)
assert room.gen_avatar_url == "mxc://bar"
room.name = "Test"
assert not room.is_group
assert room.gen_avatar_url is None
room.name = None
assert room.is_group
assert room.gen_avatar_url == "mxc://bar"
room.add_member("@alice:example.org", "Alice", "mxc://baz")
assert room.gen_avatar_url is None
def test_user_name_calculation(self):
room = self.test_room
assert room.user_name("@not_in_the_room:example.org") is None
room.add_member("@alice:example.org", "Alice", None)
assert room.user_name("@alice:example.org") == "Alice"
assert room.user_name_clashes("Alice") == ["@alice:example.org"]
room.add_member("@bob:example.org", None, None)
assert room.user_name("@bob:example.org") == "@bob:example.org"
room.add_member("@malory:example.org", "Alice", None)
assert room.user_name("@alice:example.org") == "Alice (@alice:example.org)"
assert room.user_name("@malory:example.org") == "Alice (@malory:example.org)"
assert room.user_name_clashes("Alice") == [
"@alice:example.org",
"@malory:example.org",
]
room.remove_member("@alice:example.org")
assert room.user_name("@malory:example.org") == "Alice"
room.remove_member("@malory:example.org")
room.add_member("@alice:example.org", None, None)
assert room.user_name("@alice:example.org") == "@alice:example.org"
assert room.user_name_clashes("@alice:example.org") == ["@alice:example.org"]
room.add_member("@malory:example.org", "@alice:example.org", None)
assert room.user_name("@alice:example.org") == "@alice:example.org"
assert (
room.user_name("@malory:example.org")
== "@alice:example.org (@malory:example.org)"
)
assert room.user_name_clashes("@alice:example.org") == [
"@alice:example.org",
"@malory:example.org",
]
def test_avatar_url(self):
room = self.test_room
assert room.user_name("@not_in_the_room:example.org") is None
assert room.avatar_url("@not_in_the_room:example.org") is None
room.add_member("@alice:example.org", "Alice", "mxc://foo")
assert room.avatar_url("@alice:example.org") == "mxc://foo"
def test_machine_name(self):
room = self.test_room
assert room.machine_name == TEST_ROOM
room.canonical_alias = "Alias room"
assert room.machine_name == "Alias room"
def test_typing_notice_event(self):
room = self.test_room
assert not room.typing_users
room.handle_ephemeral_event(TypingNoticeEvent([BOB_ID]))
assert room.typing_users == [BOB_ID]
def test_read_receipt_event(self):
"""Verify that ReceiptEvents update a room's [threaded_]read_receipts dict.
Successive receipts should replace the first receipt with the second.
"""
room = self.test_room
assert room.read_receipts == {}
r1 = Receipt("event_id", ReceiptType.read, BOB_ID, 10)
r2 = Receipt("event_id2", ReceiptType.read, BOB_ID, 15)
r3 = Receipt("event_id3", ReceiptType.read_private, BOB_ID, 15, "main")
r4 = Receipt("event_id4", ReceiptType.read_private, BOB_ID, 15, "thread_id")
r5 = Receipt("event_id4", ReceiptType.read_private, BOB_ID, 20, "thread_id")
r1_event = ReceiptEvent([r1])
r2_event = ReceiptEvent([r2])
r345_event = ReceiptEvent([r3, r4, r5])
room.handle_ephemeral_event(r1_event)
assert room.read_receipts == {BOB_ID: r1}
room.handle_ephemeral_event(r2_event)
assert room.read_receipts == {BOB_ID: r2}
room.handle_ephemeral_event(r345_event)
assert room.threaded_read_receipts == {BOB_ID: {"main": r3, "thread_id": r5}}
def test_non_read_receipt_event(self):
"""Verify that non-spec-compliant receipts don't leak into a room's read_receipts dict."""
room = self.test_room
room.handle_ephemeral_event(
ReceiptEvent([Receipt("event_id", "m.downvoted", BOB_ID, 0)])
)
assert room.read_receipts == room.threaded_read_receipts == {}
def test_create_event(self):
room = self.test_room
room.handle_event(
RoomCreateEvent(
{"event_id": "event_id", "sender": BOB_ID, "origin_server_ts": 0}, False
)
)
assert room.federate is False
assert room.room_version == "1"
def test_guest_access_event(self):
room = self.test_room
assert room.guest_access == "forbidden"
room.handle_event(
RoomGuestAccessEvent(
{"event_id": "event_id", "sender": BOB_ID, "origin_server_ts": 0},
"can_join",
)
)
assert room.guest_access == "can_join"
def test_history_visibility_event(self):
room = self.test_room
assert room.history_visibility == "shared"
room.handle_event(
RoomHistoryVisibilityEvent(
{"event_id": "event_id", "sender": BOB_ID, "origin_server_ts": 0},
"invited",
)
)
assert room.history_visibility == "invited"
def test_join_rules_event(self):
room = self.test_room
assert room.join_rule == "invite"
room.handle_event(
RoomJoinRulesEvent(
{"event_id": "event_id", "sender": BOB_ID, "origin_server_ts": 0},
"public",
)
)
assert room.join_rule == "public"
def test_name_event(self):
room = self.test_room
assert not room.name
room.handle_event(
RoomNameEvent(
{"event_id": "event_id", "sender": BOB_ID, "origin_server_ts": 0},
"test name",
)
)
assert room.name == "test name"
def test_space_parent(self):
room = self.test_room
assert room.parents == set()
room.handle_event(
RoomSpaceParentEvent(
{
"event_id": "event_id",
"sender": BOB_ID,
"origin_server_ts": 0,
"content": {},
},
"!X:example.org",
)
)
assert "!X:example.org" not in room.parents
room.handle_event(
RoomSpaceParentEvent(
{
"event_id": "event_id",
"sender": BOB_ID,
"origin_server_ts": 0,
"content": {"via": ["!A:example.org"]},
},
"!X:example.org",
)
)
assert "!X:example.org" in room.parents
room.handle_event(
RoomSpaceParentEvent(
{
"event_id": "event_id",
"sender": BOB_ID,
"origin_server_ts": 0,
"content": {},
},
"!X:example.org",
)
)
assert "!X:example.org" not in room.parents
def test_space_child(self):
room = self.test_room
assert room.children == set()
room.handle_event(
RoomSpaceChildEvent(
{
"event_id": "event_id",
"sender": BOB_ID,
"origin_server_ts": 0,
"content": {},
},
"!X:example.org",
)
)
assert "!X:example.org" not in room.children
room.handle_event(
RoomSpaceChildEvent(
{
"event_id": "event_id",
"sender": BOB_ID,
"origin_server_ts": 0,
"content": {"via": ["!A:example.org"]},
},
"!X:example.org",
)
)
assert "!X:example.org" in room.children
room.handle_event(
RoomSpaceChildEvent(
{
"event_id": "event_id",
"sender": BOB_ID,
"origin_server_ts": 0,
"content": {},
},
"!X:example.org",
)
)
assert "!X:example.org" not in room.children
def test_room_avatar_event(self):
room = self.test_room
assert not room.gen_avatar_url
room.handle_event(
RoomAvatarEvent(
{"event_id": "event_id", "sender": BOB_ID, "origin_server_ts": 0},
"mxc://foo",
)
)
assert room.gen_avatar_url == "mxc://foo"
def test_summary_update(self):
room = self.test_room
room.summary = None
room.update_summary(RoomSummary(1, 2, []))
assert room.invited_count == 1
assert room.joined_count == 2
assert room.member_count == 3
assert room.summary
room.update_summary(RoomSummary(1, 3, ["@alice:example.org"]))
assert room.invited_count == 1
assert room.joined_count == 3
assert room.member_count == 4
assert room.summary.heroes == ["@alice:example.org"]
def test_invited_room(self):
room = MatrixInvitedRoom(TEST_ROOM, BOB_ID)
room.handle_event(
InviteMemberEvent(
{},
"@alice:example.org",
BOB_ID,
"invite",
None,
{"membership": "invite"},
)
)
assert room.inviter == "@alice:example.org"
assert not room.name
room.handle_event(InviteNameEvent({}, BOB_ID, "test name"))
assert room.name == "test name"
assert not room.canonical_alias
room.handle_event(InviteAliasEvent({}, BOB_ID, "test alias"))
assert room.canonical_alias == "test alias"
def test_handle_member_return_value(self):
room = self.test_room
assert not room.users
mx_id, name, avatar = self.new_user
assert room.add_member(mx_id, name, avatar)
assert not room.add_member(mx_id, name, avatar)
assert room.remove_member(mx_id)
assert not room.remove_member(mx_id)
def test_user_membership_changes(self):
invited_event = RoomMemberEvent(
{"event_id": "event1", "sender": BOB_ID, "origin_server_ts": 1},
ALICE_ID,
"invite",
None,
{"membership": "invite", "displayname": "Alice Margarine"},
)
joins_event = RoomMemberEvent(
{"event_id": "event2", "sender": ALICE_ID, "origin_server_ts": 2},
ALICE_ID,
"join",
None,
{
"membership": "join",
"displayname": "Alice Margatroid",
"avatar_url": "mxc://new",
},
)
leaves_event = RoomMemberEvent(
{"event_id": "event3", "sender": ALICE_ID, "origin_server_ts": 3},
ALICE_ID,
"leave",
None,
{"membership": "leave"},
)
unknown_event = RoomMemberEvent(
{"event_id": "event4", "sender": ALICE_ID, "origin_server_ts": 4},
ALICE_ID,
"bad_membership",
None,
{"membership": "bad_membership"},
)
room = self.test_room
assert not room.users
assert not room.invited_users
# Alice is invited, accepts (her name and avatar changed) then leaves
room.handle_membership(invited_event)
assert set(room.users) == {ALICE_ID}
assert set(room.invited_users) == {ALICE_ID}
room.handle_membership(joins_event)
assert set(room.users) == {ALICE_ID}
assert not room.invited_users
assert room.names["Alice Margatroid"] == [ALICE_ID]
assert room.users[ALICE_ID].display_name == "Alice Margatroid"
assert room.users[ALICE_ID].avatar_url == "mxc://new"
room.handle_membership(leaves_event)
assert not room.users
assert not room.invited_users
# Alice is invited and declines
room.handle_membership(invited_event)
assert set(room.users) == {ALICE_ID}
assert set(room.invited_users) == {ALICE_ID}
room.handle_membership(leaves_event)
assert not room.users
assert not room.invited_users
# Alice joins without invite then leaves
room.handle_membership(joins_event)
assert set(room.users) == {ALICE_ID}
assert not room.invited_users
room.handle_membership(leaves_event)
assert not room.users
assert not room.invited_users
# Ensure we get False if we handle an event that changes nothing or
# has an unknown new membership
assert not room.handle_membership(leaves_event)
assert not room.handle_membership(unknown_event)
def test_room_upgrade(self):
room = self.test_room
room.handle_event(
RoomUpgradeEvent(
{
"event_id": "event5",
"sender": ALICE_ID,
"origin_server_ts": 4,
"state_key": "",
},
"This room has been replaced",
"!newroom:example.org",
)
)
assert room.replacement_room == "!newroom:example.org"