Skip to content

Commit fa8d5ed

Browse files
committed
Adjust tests to separate out concerns into separate tests, and add one for the room moderator case
1 parent dc8758b commit fa8d5ed

File tree

1 file changed

+218
-3
lines changed

1 file changed

+218
-3
lines changed

tests/rest/client/test_media_download.py

Lines changed: 218 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,54 @@ def _redact_event(
371371
self.assertEqual(channel.code, expect_code)
372372
return channel.json_body
373373

374-
def test_local_media_download_attached_to_redacted_event(self) -> None:
375-
"""Test that can local media attached to image event can be restricted if redacted"""
374+
def test_local_media_download_attached_to_redacted_event_normal(self) -> None:
375+
"""
376+
Test that can local media attached to image event can be restricted if redacted
377+
"""
378+
mxc_uri = self._create_restricted_media(self.creator)
379+
room_id = self.helper.create_room_as(self.creator, tok=self.creator_tok)
380+
381+
# set room history_visibility to joined, otherwise it will be 'shared'
382+
self.helper.send_state(
383+
room_id=room_id,
384+
event_type=EventTypes.RoomHistoryVisibility,
385+
body={"history_visibility": HistoryVisibility.JOINED},
386+
tok=self.creator_tok,
387+
)
388+
389+
self.helper.join(room_id, self.other_user, tok=self.other_user_tok)
390+
391+
image = {
392+
"body": "test_png_upload",
393+
"info": {"h": 1, "mimetype": "image/png", "size": 67, "w": 1},
394+
"msgtype": "m.image",
395+
"url": str(mxc_uri),
396+
}
397+
json_body = self.helper.send_event(
398+
room_id,
399+
"m.room.message",
400+
content=image,
401+
tok=self.creator_tok,
402+
expect_code=200,
403+
attach_media_mxc=str(mxc_uri),
404+
)
405+
assert "event_id" in json_body
406+
407+
# Both users should be able to see the event
408+
self.fetch_media(mxc_uri)
409+
self.fetch_media(mxc_uri, access_token=self.other_user_tok)
410+
411+
# now, redact that event, and try and retrieve the media again
412+
self._redact_event(self.creator_tok, room_id, json_body["event_id"])
413+
414+
self.fetch_media(mxc_uri, expected_code=404)
415+
self.fetch_media(mxc_uri, access_token=self.other_user_tok, expected_code=404)
416+
417+
def test_local_media_download_attached_to_redacted_event_admin(self) -> None:
418+
"""
419+
Test that can local media attached to image event can be restricted if redacted.
420+
Specifically, test that a system administrator can bypass that if requested
421+
"""
376422
mxc_uri = self._create_restricted_media(self.creator)
377423
room_id = self.helper.create_room_as(self.creator, tok=self.creator_tok)
378424

@@ -418,7 +464,70 @@ def test_local_media_download_attached_to_redacted_event(self) -> None:
418464
# Let's see if the bypass works
419465
self.fetch_media(mxc_uri, access_token=self.admin_tok, attempt_bypass=True)
420466

421-
def test_local_media_download_attached_to_redacted_state_event(self) -> None:
467+
def test_local_media_download_attached_to_redacted_event_room_moderator(
468+
self,
469+
) -> None:
470+
"""
471+
Test that can local media attached to image event can be restricted if redacted.
472+
Specifically, test that a room moderator can bypass that if requested and
473+
empowered to
474+
"""
475+
mxc_uri = self._create_restricted_media(self.creator)
476+
room_id = self.helper.create_room_as(self.creator, tok=self.creator_tok)
477+
478+
# set room history_visibility to joined, otherwise it will be 'shared'
479+
self.helper.send_state(
480+
room_id=room_id,
481+
event_type=EventTypes.RoomHistoryVisibility,
482+
body={"history_visibility": HistoryVisibility.JOINED},
483+
tok=self.creator_tok,
484+
)
485+
486+
# Adjust power levels in the room. Redacting is defaulted to 50, so let's bump
487+
# the other user. "user_default" dictates this was at "0"
488+
pl = self.helper.get_state(
489+
room_id, EventTypes.PowerLevels, tok=self.creator_tok
490+
)
491+
pl["users"][self.other_user] = 50
492+
self.helper.send_state(
493+
room_id, EventTypes.PowerLevels, body=pl, tok=self.creator_tok
494+
)
495+
496+
self.helper.join(room_id, self.other_user, tok=self.other_user_tok)
497+
self.helper.join(room_id, self.admin_user, tok=self.admin_tok)
498+
499+
image = {
500+
"body": "test_png_upload",
501+
"info": {"h": 1, "mimetype": "image/png", "size": 67, "w": 1},
502+
"msgtype": "m.image",
503+
"url": str(mxc_uri),
504+
}
505+
json_body = self.helper.send_event(
506+
room_id,
507+
"m.room.message",
508+
content=image,
509+
tok=self.creator_tok,
510+
expect_code=200,
511+
attach_media_mxc=str(mxc_uri),
512+
)
513+
assert "event_id" in json_body
514+
515+
# Both users should be able to see the event
516+
self.fetch_media(mxc_uri)
517+
self.fetch_media(mxc_uri, access_token=self.other_user_tok)
518+
self.fetch_media(mxc_uri, access_token=self.admin_tok)
519+
520+
# now, redact that event, and try and retrieve the media again
521+
self._redact_event(self.creator_tok, room_id, json_body["event_id"])
522+
523+
self.fetch_media(mxc_uri, expected_code=404)
524+
self.fetch_media(mxc_uri, access_token=self.other_user_tok, expected_code=404)
525+
self.fetch_media(mxc_uri, access_token=self.admin_tok, expected_code=404)
526+
527+
# Let's see if the bypass works
528+
self.fetch_media(mxc_uri, access_token=self.other_user_tok, attempt_bypass=True)
529+
530+
def test_local_media_download_attached_to_redacted_state_event_normal(self) -> None:
422531
"""Test that a simple membership avatar is viewable when appropriate"""
423532
mxc_uri = self._create_restricted_media(self.creator)
424533
room_id = self.helper.create_room_as(self.creator, tok=self.creator_tok)
@@ -431,6 +540,50 @@ def test_local_media_download_attached_to_redacted_state_event(self) -> None:
431540
tok=self.creator_tok,
432541
)
433542

543+
self.helper.join(room_id, self.other_user, tok=self.other_user_tok)
544+
545+
membership_content = {
546+
EventContentFields.MEMBERSHIP: Membership.JOIN,
547+
"avatar_url": str(mxc_uri),
548+
}
549+
json_body = self.helper.send_state(
550+
room_id,
551+
EventTypes.Member,
552+
body=membership_content,
553+
tok=self.creator_tok,
554+
expect_code=200,
555+
state_key=self.creator,
556+
attach_media_mxc=str(mxc_uri),
557+
)
558+
assert "event_id" in json_body
559+
560+
# Both users should be able to see the media
561+
self.fetch_media(mxc_uri)
562+
self.fetch_media(mxc_uri, access_token=self.other_user_tok)
563+
564+
# now, redact that event, and try and retrieve the media again
565+
self._redact_event(self.creator_tok, room_id, json_body["event_id"])
566+
567+
self.fetch_media(mxc_uri, expected_code=404)
568+
self.fetch_media(mxc_uri, access_token=self.other_user_tok, expected_code=404)
569+
570+
def test_local_media_download_attached_to_redacted_state_event_admin(self) -> None:
571+
"""
572+
Test that a simple membership avatar is viewable when appropriate. Specifically,
573+
test that a system administrator can bypass that if requested
574+
575+
"""
576+
mxc_uri = self._create_restricted_media(self.creator)
577+
room_id = self.helper.create_room_as(self.creator, tok=self.creator_tok)
578+
579+
# set room history_visibility to joined
580+
self.helper.send_state(
581+
room_id=room_id,
582+
event_type=EventTypes.RoomHistoryVisibility,
583+
body={"history_visibility": HistoryVisibility.JOINED},
584+
tok=self.creator_tok,
585+
)
586+
434587
self.helper.join(room_id, self.other_user, tok=self.other_user_tok)
435588
self.helper.join(room_id, self.admin_user, tok=self.admin_tok)
436589

@@ -463,3 +616,65 @@ def test_local_media_download_attached_to_redacted_state_event(self) -> None:
463616

464617
# Let's see if the bypass works
465618
self.fetch_media(mxc_uri, access_token=self.admin_tok, attempt_bypass=True)
619+
620+
def test_local_media_download_attached_to_redacted_state_event_room_moderator(
621+
self,
622+
) -> None:
623+
"""
624+
Test that a simple membership avatar is viewable when appropriate.
625+
Specifically, test that a room moderator can bypass that if requested and
626+
empowered to
627+
"""
628+
mxc_uri = self._create_restricted_media(self.creator)
629+
room_id = self.helper.create_room_as(self.creator, tok=self.creator_tok)
630+
631+
# set room history_visibility to joined
632+
self.helper.send_state(
633+
room_id=room_id,
634+
event_type=EventTypes.RoomHistoryVisibility,
635+
body={"history_visibility": HistoryVisibility.JOINED},
636+
tok=self.creator_tok,
637+
)
638+
639+
# Adjust power levels in the room. Redacting is defaulted to 50, so let's bump
640+
# the other user. "user_default" dictates this was at "0"
641+
pl = self.helper.get_state(
642+
room_id, EventTypes.PowerLevels, tok=self.creator_tok
643+
)
644+
pl["users"][self.other_user] = 50
645+
self.helper.send_state(
646+
room_id, EventTypes.PowerLevels, body=pl, tok=self.creator_tok
647+
)
648+
649+
self.helper.join(room_id, self.other_user, tok=self.other_user_tok)
650+
self.helper.join(room_id, self.admin_user, tok=self.admin_tok)
651+
652+
membership_content = {
653+
EventContentFields.MEMBERSHIP: Membership.JOIN,
654+
"avatar_url": str(mxc_uri),
655+
}
656+
json_body = self.helper.send_state(
657+
room_id,
658+
EventTypes.Member,
659+
body=membership_content,
660+
tok=self.creator_tok,
661+
expect_code=200,
662+
state_key=self.creator,
663+
attach_media_mxc=str(mxc_uri),
664+
)
665+
assert "event_id" in json_body
666+
667+
# Both users should be able to see the media
668+
self.fetch_media(mxc_uri)
669+
self.fetch_media(mxc_uri, access_token=self.other_user_tok)
670+
self.fetch_media(mxc_uri, access_token=self.admin_tok)
671+
672+
# now, redact that event, and try and retrieve the media again
673+
self._redact_event(self.creator_tok, room_id, json_body["event_id"])
674+
675+
self.fetch_media(mxc_uri, expected_code=404)
676+
self.fetch_media(mxc_uri, access_token=self.other_user_tok, expected_code=404)
677+
self.fetch_media(mxc_uri, access_token=self.admin_tok, expected_code=404)
678+
679+
# Let's see if the bypass works
680+
self.fetch_media(mxc_uri, access_token=self.other_user_tok, attempt_bypass=True)

0 commit comments

Comments
 (0)