2020-10-26 18:02:28 +01:00
|
|
|
#
|
2023-11-21 21:29:58 +01:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2020-10-26 18:02:28 +01:00
|
|
|
#
|
|
|
|
#
|
|
|
|
import os
|
2023-10-06 13:22:55 +02:00
|
|
|
from typing import Dict
|
2020-10-26 18:02:28 +01:00
|
|
|
|
2021-05-26 12:19:47 +02:00
|
|
|
from parameterized import parameterized
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
2023-10-06 13:22:55 +02:00
|
|
|
from twisted.web.resource import Resource
|
2021-12-03 14:57:13 +01:00
|
|
|
|
2020-10-26 18:02:28 +01:00
|
|
|
import synapse.rest.admin
|
|
|
|
from synapse.api.errors import Codes
|
2023-02-27 14:26:05 +01:00
|
|
|
from synapse.media.filepath import MediaFilePaths
|
2021-08-17 13:57:58 +02:00
|
|
|
from synapse.rest.client import login, profile, room
|
2021-12-03 14:57:13 +01:00
|
|
|
from synapse.server import HomeServer
|
|
|
|
from synapse.util import Clock
|
2020-10-26 18:02:28 +01:00
|
|
|
|
|
|
|
from tests import unittest
|
2021-09-16 18:01:14 +02:00
|
|
|
from tests.test_utils import SMALL_PNG
|
2020-10-26 18:02:28 +01:00
|
|
|
|
2021-10-20 16:41:48 +02:00
|
|
|
VALID_TIMESTAMP = 1609459200000 # 2021-01-01 in milliseconds
|
|
|
|
INVALID_TIMESTAMP_IN_S = 1893456000 # 2030-01-01 in seconds
|
|
|
|
|
2020-10-26 18:02:28 +01:00
|
|
|
|
2023-10-06 13:22:55 +02:00
|
|
|
class _AdminMediaTests(unittest.HomeserverTestCase):
|
2020-10-26 18:02:28 +01:00
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
synapse.rest.admin.register_servlets_for_media_repo,
|
|
|
|
login.register_servlets,
|
|
|
|
]
|
|
|
|
|
2023-10-06 13:22:55 +02:00
|
|
|
def create_resource_dict(self) -> Dict[str, Resource]:
|
|
|
|
resources = super().create_resource_dict()
|
|
|
|
resources["/_matrix/media"] = self.hs.get_media_repository_resource()
|
|
|
|
return resources
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteMediaByIDTestCase(_AdminMediaTests):
|
2021-12-03 14:57:13 +01:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
self.server_name = hs.hostname
|
|
|
|
|
|
|
|
self.admin_user = self.register_user("admin", "pass", admin=True)
|
|
|
|
self.admin_user_tok = self.login("admin", "pass")
|
|
|
|
|
2021-09-24 13:25:21 +02:00
|
|
|
self.filepaths = MediaFilePaths(hs.config.media.media_store_path)
|
2020-10-26 18:02:28 +01:00
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_no_auth(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
Try to delete media without authentication.
|
|
|
|
"""
|
|
|
|
url = "/_synapse/admin/v1/media/%s/%s" % (self.server_name, "12345")
|
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request("DELETE", url, b"{}")
|
2020-10-26 18:02:28 +01:00
|
|
|
|
2021-11-30 10:53:54 +01:00
|
|
|
self.assertEqual(
|
2022-08-09 15:56:43 +02:00
|
|
|
401,
|
2021-11-30 10:53:54 +01:00
|
|
|
channel.code,
|
|
|
|
msg=channel.json_body,
|
|
|
|
)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_requester_is_no_admin(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
If the user is not a server admin, an error is returned.
|
|
|
|
"""
|
|
|
|
self.other_user = self.register_user("user", "pass")
|
|
|
|
self.other_user_token = self.login("user", "pass")
|
|
|
|
|
|
|
|
url = "/_synapse/admin/v1/media/%s/%s" % (self.server_name, "12345")
|
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
|
|
|
"DELETE",
|
|
|
|
url,
|
|
|
|
access_token=self.other_user_token,
|
|
|
|
)
|
2020-10-26 18:02:28 +01:00
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(403, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_media_does_not_exist(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
2022-08-09 15:56:43 +02:00
|
|
|
Tests that a lookup for a media that does not exist returns a 404
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
url = "/_synapse/admin/v1/media/%s/%s" % (self.server_name, "12345")
|
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
|
|
|
"DELETE",
|
|
|
|
url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-10-26 18:02:28 +01:00
|
|
|
|
2022-08-09 15:56:43 +02:00
|
|
|
self.assertEqual(404, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_media_is_not_local(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
2022-08-10 20:01:12 +02:00
|
|
|
Tests that a lookup for a media that is not a local returns a 400
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
url = "/_synapse/admin/v1/media/%s/%s" % ("unknown_domain", "12345")
|
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
|
|
|
"DELETE",
|
|
|
|
url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-10-26 18:02:28 +01:00
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual("Can only delete local media", channel.json_body["error"])
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_delete_media(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
Tests that delete a media is successfully
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Upload some media into the room
|
|
|
|
response = self.helper.upload_media(
|
2021-11-30 10:53:54 +01:00
|
|
|
SMALL_PNG,
|
|
|
|
tok=self.admin_user_tok,
|
2022-08-08 22:21:27 +02:00
|
|
|
expect_code=200,
|
2020-10-26 18:02:28 +01:00
|
|
|
)
|
|
|
|
# Extract media ID from the response
|
|
|
|
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'
|
|
|
|
server_name, media_id = server_and_media_id.split("/")
|
|
|
|
|
|
|
|
self.assertEqual(server_name, self.server_name)
|
|
|
|
|
|
|
|
# Attempt to access media
|
2023-10-06 13:22:55 +02:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"GET",
|
2023-10-06 13:22:55 +02:00
|
|
|
f"/_matrix/media/v3/download/{server_and_media_id}",
|
2020-10-26 18:02:28 +01:00
|
|
|
shorthand=False,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Should be successful
|
|
|
|
self.assertEqual(
|
2022-08-08 22:21:27 +02:00
|
|
|
200,
|
2020-10-26 18:02:28 +01:00
|
|
|
channel.code,
|
|
|
|
msg=(
|
2022-08-08 22:21:27 +02:00
|
|
|
"Expected to receive a 200 on accessing media: %s" % server_and_media_id
|
2020-10-26 18:02:28 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test if the file exists
|
|
|
|
local_path = self.filepaths.local_media_filepath(media_id)
|
|
|
|
self.assertTrue(os.path.exists(local_path))
|
|
|
|
|
|
|
|
url = "/_synapse/admin/v1/media/%s/%s" % (self.server_name, media_id)
|
|
|
|
|
|
|
|
# Delete media
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
|
|
|
"DELETE",
|
|
|
|
url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-10-26 18:02:28 +01:00
|
|
|
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(1, channel.json_body["total"])
|
|
|
|
self.assertEqual(
|
|
|
|
media_id,
|
|
|
|
channel.json_body["deleted_media"][0],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Attempt to access media
|
2023-10-06 13:22:55 +02:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"GET",
|
2023-10-06 13:22:55 +02:00
|
|
|
f"/_matrix/media/v3/download/{server_and_media_id}",
|
2020-10-26 18:02:28 +01:00
|
|
|
shorthand=False,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2022-08-09 15:56:43 +02:00
|
|
|
404,
|
2020-10-26 18:02:28 +01:00
|
|
|
channel.code,
|
|
|
|
msg=(
|
2022-08-09 15:56:43 +02:00
|
|
|
"Expected to receive a 404 on accessing deleted media: %s"
|
2020-10-26 18:02:28 +01:00
|
|
|
% server_and_media_id
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test if the file is deleted
|
|
|
|
self.assertFalse(os.path.exists(local_path))
|
|
|
|
|
|
|
|
|
2023-10-06 13:22:55 +02:00
|
|
|
class DeleteMediaByDateSizeTestCase(_AdminMediaTests):
|
2020-10-26 18:02:28 +01:00
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
synapse.rest.admin.register_servlets_for_media_repo,
|
|
|
|
login.register_servlets,
|
|
|
|
profile.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
]
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
self.media_repo = hs.get_media_repository_resource()
|
|
|
|
self.server_name = hs.hostname
|
|
|
|
|
|
|
|
self.admin_user = self.register_user("admin", "pass", admin=True)
|
|
|
|
self.admin_user_tok = self.login("admin", "pass")
|
|
|
|
|
2021-09-24 13:25:21 +02:00
|
|
|
self.filepaths = MediaFilePaths(hs.config.media.media_store_path)
|
2023-02-09 19:04:24 +01:00
|
|
|
self.url = "/_synapse/admin/v1/media/delete"
|
|
|
|
self.legacy_url = "/_synapse/admin/v1/media/%s/delete" % self.server_name
|
2020-10-26 18:02:28 +01:00
|
|
|
|
2021-10-20 16:41:48 +02:00
|
|
|
# Move clock up to somewhat realistic time
|
|
|
|
self.reactor.advance(1000000000)
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_no_auth(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
Try to delete media without authentication.
|
|
|
|
"""
|
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request("POST", self.url, b"{}")
|
2020-10-26 18:02:28 +01:00
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(401, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_requester_is_no_admin(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
If the user is not a server admin, an error is returned.
|
|
|
|
"""
|
|
|
|
self.other_user = self.register_user("user", "pass")
|
|
|
|
self.other_user_token = self.login("user", "pass")
|
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
|
|
|
self.url,
|
|
|
|
access_token=self.other_user_token,
|
|
|
|
)
|
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(403, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_media_is_not_local(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
2022-08-10 20:01:12 +02:00
|
|
|
Tests that a lookup for media that is not local returns a 400
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
url = "/_synapse/admin/v1/media/%s/delete" % "unknown_domain"
|
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
2021-10-20 16:41:48 +02:00
|
|
|
url + f"?before_ts={VALID_TIMESTAMP}",
|
2020-10-26 18:02:28 +01:00
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual("Can only delete local media", channel.json_body["error"])
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_missing_parameter(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
If the parameter `before_ts` is missing, an error is returned.
|
|
|
|
"""
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2020-10-26 18:02:28 +01:00
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(Codes.MISSING_PARAM, channel.json_body["errcode"])
|
|
|
|
self.assertEqual(
|
2021-07-21 20:12:22 +02:00
|
|
|
"Missing integer query parameter 'before_ts'", channel.json_body["error"]
|
2020-10-26 18:02:28 +01:00
|
|
|
)
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_invalid_parameter(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
If parameters are invalid, an error is returned.
|
|
|
|
"""
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
|
|
|
self.url + "?before_ts=-1234",
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
|
|
|
|
self.assertEqual(
|
2021-10-20 16:41:48 +02:00
|
|
|
"Query parameter before_ts must be a positive integer.",
|
2020-10-26 18:02:28 +01:00
|
|
|
channel.json_body["error"],
|
|
|
|
)
|
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
2021-10-20 16:41:48 +02:00
|
|
|
self.url + f"?before_ts={INVALID_TIMESTAMP_IN_S}",
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
2021-10-20 16:41:48 +02:00
|
|
|
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
|
|
|
|
self.assertEqual(
|
|
|
|
"Query parameter before_ts you provided is from the year 1970. "
|
|
|
|
+ "Double check that you are providing a timestamp in milliseconds.",
|
|
|
|
channel.json_body["error"],
|
|
|
|
)
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url + f"?before_ts={VALID_TIMESTAMP}&size_gt=-1234",
|
2020-10-26 18:02:28 +01:00
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
|
|
|
|
self.assertEqual(
|
|
|
|
"Query parameter size_gt must be a string representing a positive integer.",
|
|
|
|
channel.json_body["error"],
|
|
|
|
)
|
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
2021-10-20 16:41:48 +02:00
|
|
|
self.url + f"?before_ts={VALID_TIMESTAMP}&keep_profiles=not_bool",
|
2020-10-26 18:02:28 +01:00
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
2021-12-09 12:23:34 +01:00
|
|
|
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(
|
2021-07-21 20:12:22 +02:00
|
|
|
"Boolean query parameter 'keep_profiles' must be one of ['true', 'false']",
|
2020-10-26 18:02:28 +01:00
|
|
|
channel.json_body["error"],
|
|
|
|
)
|
|
|
|
|
2023-02-09 19:04:24 +01:00
|
|
|
@parameterized.expand([(True,), (False,)])
|
|
|
|
def test_delete_media_never_accessed(self, use_legacy_url: bool) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
Tests that media deleted if it is older than `before_ts` and never accessed
|
|
|
|
`last_access_ts` is `NULL` and `created_ts` < `before_ts`
|
|
|
|
"""
|
2023-02-09 19:04:24 +01:00
|
|
|
url = self.legacy_url if use_legacy_url else self.url
|
2020-10-26 18:02:28 +01:00
|
|
|
|
|
|
|
# upload and do not access
|
|
|
|
server_and_media_id = self._create_media()
|
|
|
|
self.pump(1.0)
|
|
|
|
|
|
|
|
# test that the file exists
|
|
|
|
media_id = server_and_media_id.split("/")[1]
|
|
|
|
local_path = self.filepaths.local_media_filepath(media_id)
|
|
|
|
self.assertTrue(os.path.exists(local_path))
|
|
|
|
|
|
|
|
# timestamp after upload/create
|
|
|
|
now_ms = self.clock.time_msec()
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
2023-02-09 19:04:24 +01:00
|
|
|
url + "?before_ts=" + str(now_ms),
|
2020-10-26 18:02:28 +01:00
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(1, channel.json_body["total"])
|
|
|
|
self.assertEqual(
|
|
|
|
media_id,
|
|
|
|
channel.json_body["deleted_media"][0],
|
|
|
|
)
|
|
|
|
|
|
|
|
self._access_media(server_and_media_id, False)
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_keep_media_by_date(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
Tests that media is not deleted if it is newer than `before_ts`
|
|
|
|
"""
|
|
|
|
|
|
|
|
# timestamp before upload
|
|
|
|
now_ms = self.clock.time_msec()
|
|
|
|
server_and_media_id = self._create_media()
|
|
|
|
|
|
|
|
self._access_media(server_and_media_id)
|
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
|
|
|
self.url + "?before_ts=" + str(now_ms),
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(0, channel.json_body["total"])
|
|
|
|
|
|
|
|
self._access_media(server_and_media_id)
|
|
|
|
|
|
|
|
# timestamp after upload
|
|
|
|
now_ms = self.clock.time_msec()
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
|
|
|
self.url + "?before_ts=" + str(now_ms),
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(1, channel.json_body["total"])
|
|
|
|
self.assertEqual(
|
|
|
|
server_and_media_id.split("/")[1],
|
|
|
|
channel.json_body["deleted_media"][0],
|
|
|
|
)
|
|
|
|
|
|
|
|
self._access_media(server_and_media_id, False)
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_keep_media_by_size(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
Tests that media is not deleted if its size is smaller than or equal
|
|
|
|
to `size_gt`
|
|
|
|
"""
|
|
|
|
server_and_media_id = self._create_media()
|
|
|
|
|
|
|
|
self._access_media(server_and_media_id)
|
|
|
|
|
|
|
|
now_ms = self.clock.time_msec()
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
|
|
|
self.url + "?before_ts=" + str(now_ms) + "&size_gt=67",
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(0, channel.json_body["total"])
|
|
|
|
|
|
|
|
self._access_media(server_and_media_id)
|
|
|
|
|
|
|
|
now_ms = self.clock.time_msec()
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
|
|
|
self.url + "?before_ts=" + str(now_ms) + "&size_gt=66",
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(1, channel.json_body["total"])
|
|
|
|
self.assertEqual(
|
|
|
|
server_and_media_id.split("/")[1],
|
|
|
|
channel.json_body["deleted_media"][0],
|
|
|
|
)
|
|
|
|
|
|
|
|
self._access_media(server_and_media_id, False)
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_keep_media_by_user_avatar(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
Tests that we do not delete media if is used as a user avatar
|
|
|
|
Tests parameter `keep_profiles`
|
|
|
|
"""
|
|
|
|
server_and_media_id = self._create_media()
|
|
|
|
|
|
|
|
self._access_media(server_and_media_id)
|
|
|
|
|
|
|
|
# set media as avatar
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"PUT",
|
|
|
|
"/profile/%s/avatar_url" % (self.admin_user,),
|
2021-11-30 10:53:54 +01:00
|
|
|
content={"avatar_url": "mxc://%s" % (server_and_media_id,)},
|
2020-10-26 18:02:28 +01:00
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
|
|
|
|
now_ms = self.clock.time_msec()
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
|
|
|
self.url + "?before_ts=" + str(now_ms) + "&keep_profiles=true",
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(0, channel.json_body["total"])
|
|
|
|
|
|
|
|
self._access_media(server_and_media_id)
|
|
|
|
|
|
|
|
now_ms = self.clock.time_msec()
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
|
|
|
self.url + "?before_ts=" + str(now_ms) + "&keep_profiles=false",
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(1, channel.json_body["total"])
|
|
|
|
self.assertEqual(
|
|
|
|
server_and_media_id.split("/")[1],
|
|
|
|
channel.json_body["deleted_media"][0],
|
|
|
|
)
|
|
|
|
|
|
|
|
self._access_media(server_and_media_id, False)
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_keep_media_by_room_avatar(self) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
Tests that we do not delete media if it is used as a room avatar
|
|
|
|
Tests parameter `keep_profiles`
|
|
|
|
"""
|
|
|
|
server_and_media_id = self._create_media()
|
|
|
|
|
|
|
|
self._access_media(server_and_media_id)
|
|
|
|
|
|
|
|
# set media as room avatar
|
|
|
|
room_id = self.helper.create_room_as(self.admin_user, tok=self.admin_user_tok)
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"PUT",
|
|
|
|
"/rooms/%s/state/m.room.avatar" % (room_id,),
|
2021-11-30 10:53:54 +01:00
|
|
|
content={"url": "mxc://%s" % (server_and_media_id,)},
|
2020-10-26 18:02:28 +01:00
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
|
|
|
|
now_ms = self.clock.time_msec()
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
|
|
|
self.url + "?before_ts=" + str(now_ms) + "&keep_profiles=true",
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(0, channel.json_body["total"])
|
|
|
|
|
|
|
|
self._access_media(server_and_media_id)
|
|
|
|
|
|
|
|
now_ms = self.clock.time_msec()
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"POST",
|
|
|
|
self.url + "?before_ts=" + str(now_ms) + "&keep_profiles=false",
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2020-10-26 18:02:28 +01:00
|
|
|
self.assertEqual(1, channel.json_body["total"])
|
|
|
|
self.assertEqual(
|
|
|
|
server_and_media_id.split("/")[1],
|
|
|
|
channel.json_body["deleted_media"][0],
|
|
|
|
)
|
|
|
|
|
|
|
|
self._access_media(server_and_media_id, False)
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def _create_media(self) -> str:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
Create a media and return media_id and server_and_media_id
|
|
|
|
"""
|
|
|
|
# Upload some media into the room
|
|
|
|
response = self.helper.upload_media(
|
2021-11-30 10:53:54 +01:00
|
|
|
SMALL_PNG,
|
|
|
|
tok=self.admin_user_tok,
|
2022-08-08 22:21:27 +02:00
|
|
|
expect_code=200,
|
2020-10-26 18:02:28 +01:00
|
|
|
)
|
|
|
|
# Extract media ID from the response
|
|
|
|
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'
|
|
|
|
server_name = server_and_media_id.split("/")[0]
|
|
|
|
|
|
|
|
# Check that new media is a local and not remote
|
|
|
|
self.assertEqual(server_name, self.server_name)
|
|
|
|
|
|
|
|
return server_and_media_id
|
|
|
|
|
2021-12-16 20:59:56 +01:00
|
|
|
def _access_media(
|
|
|
|
self, server_and_media_id: str, expect_success: bool = True
|
|
|
|
) -> None:
|
2020-10-26 18:02:28 +01:00
|
|
|
"""
|
|
|
|
Try to access a media and check the result
|
|
|
|
"""
|
|
|
|
media_id = server_and_media_id.split("/")[1]
|
|
|
|
local_path = self.filepaths.local_media_filepath(media_id)
|
|
|
|
|
2023-10-06 13:22:55 +02:00
|
|
|
channel = self.make_request(
|
2020-10-26 18:02:28 +01:00
|
|
|
"GET",
|
2023-10-06 13:22:55 +02:00
|
|
|
f"/_matrix/media/v3/download/{server_and_media_id}",
|
2020-10-26 18:02:28 +01:00
|
|
|
shorthand=False,
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
if expect_success:
|
|
|
|
self.assertEqual(
|
2022-08-08 22:21:27 +02:00
|
|
|
200,
|
2020-10-26 18:02:28 +01:00
|
|
|
channel.code,
|
|
|
|
msg=(
|
2022-08-08 22:21:27 +02:00
|
|
|
"Expected to receive a 200 on accessing media: %s"
|
2020-10-26 18:02:28 +01:00
|
|
|
% server_and_media_id
|
|
|
|
),
|
|
|
|
)
|
|
|
|
# Test that the file exists
|
|
|
|
self.assertTrue(os.path.exists(local_path))
|
|
|
|
else:
|
|
|
|
self.assertEqual(
|
2022-08-09 15:56:43 +02:00
|
|
|
404,
|
2020-10-26 18:02:28 +01:00
|
|
|
channel.code,
|
|
|
|
msg=(
|
2022-08-09 15:56:43 +02:00
|
|
|
"Expected to receive a 404 on accessing deleted media: %s"
|
2020-10-26 18:02:28 +01:00
|
|
|
% (server_and_media_id)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
# Test that the file is deleted
|
|
|
|
self.assertFalse(os.path.exists(local_path))
|
2021-05-26 12:19:47 +02:00
|
|
|
|
|
|
|
|
2023-10-06 13:22:55 +02:00
|
|
|
class QuarantineMediaByIDTestCase(_AdminMediaTests):
|
2021-12-03 14:57:13 +01:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2022-02-23 12:04:02 +01:00
|
|
|
self.store = hs.get_datastores().main
|
2021-06-02 19:50:35 +02:00
|
|
|
self.server_name = hs.hostname
|
|
|
|
|
|
|
|
self.admin_user = self.register_user("admin", "pass", admin=True)
|
|
|
|
self.admin_user_tok = self.login("admin", "pass")
|
|
|
|
|
|
|
|
# Upload some media into the room
|
|
|
|
response = self.helper.upload_media(
|
2021-11-30 10:53:54 +01:00
|
|
|
SMALL_PNG,
|
|
|
|
tok=self.admin_user_tok,
|
2022-08-08 22:21:27 +02:00
|
|
|
expect_code=200,
|
2021-06-02 19:50:35 +02:00
|
|
|
)
|
|
|
|
# Extract media ID from the response
|
|
|
|
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'
|
|
|
|
self.media_id = server_and_media_id.split("/")[1]
|
|
|
|
|
|
|
|
self.url = "/_synapse/admin/v1/media/%s/%s/%s"
|
|
|
|
|
|
|
|
@parameterized.expand(["quarantine", "unquarantine"])
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_no_auth(self, action: str) -> None:
|
2021-06-02 19:50:35 +02:00
|
|
|
"""
|
|
|
|
Try to protect media without authentication.
|
|
|
|
"""
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url % (action, self.server_name, self.media_id),
|
|
|
|
b"{}",
|
|
|
|
)
|
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(401, channel.code, msg=channel.json_body)
|
2021-06-02 19:50:35 +02:00
|
|
|
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
|
|
|
|
|
|
|
|
@parameterized.expand(["quarantine", "unquarantine"])
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_requester_is_no_admin(self, action: str) -> None:
|
2021-06-02 19:50:35 +02:00
|
|
|
"""
|
|
|
|
If the user is not a server admin, an error is returned.
|
|
|
|
"""
|
|
|
|
self.other_user = self.register_user("user", "pass")
|
|
|
|
self.other_user_token = self.login("user", "pass")
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url % (action, self.server_name, self.media_id),
|
|
|
|
access_token=self.other_user_token,
|
|
|
|
)
|
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(403, channel.code, msg=channel.json_body)
|
2021-06-02 19:50:35 +02:00
|
|
|
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_quarantine_media(self) -> None:
|
2021-06-02 19:50:35 +02:00
|
|
|
"""
|
|
|
|
Tests that quarantining and remove from quarantine a media is successfully
|
|
|
|
"""
|
|
|
|
|
|
|
|
media_info = self.get_success(self.store.get_local_media(self.media_id))
|
2022-04-01 18:04:16 +02:00
|
|
|
assert media_info is not None
|
2023-11-09 17:00:30 +01:00
|
|
|
self.assertFalse(media_info.quarantined_by)
|
2021-06-02 19:50:35 +02:00
|
|
|
|
|
|
|
# quarantining
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url % ("quarantine", self.server_name, self.media_id),
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2021-06-02 19:50:35 +02:00
|
|
|
self.assertFalse(channel.json_body)
|
|
|
|
|
|
|
|
media_info = self.get_success(self.store.get_local_media(self.media_id))
|
2022-04-01 18:04:16 +02:00
|
|
|
assert media_info is not None
|
2023-11-09 17:00:30 +01:00
|
|
|
self.assertTrue(media_info.quarantined_by)
|
2021-06-02 19:50:35 +02:00
|
|
|
|
|
|
|
# remove from quarantine
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url % ("unquarantine", self.server_name, self.media_id),
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2021-06-02 19:50:35 +02:00
|
|
|
self.assertFalse(channel.json_body)
|
|
|
|
|
|
|
|
media_info = self.get_success(self.store.get_local_media(self.media_id))
|
2022-04-01 18:04:16 +02:00
|
|
|
assert media_info is not None
|
2023-11-09 17:00:30 +01:00
|
|
|
self.assertFalse(media_info.quarantined_by)
|
2021-06-02 19:50:35 +02:00
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_quarantine_protected_media(self) -> None:
|
2021-06-02 19:50:35 +02:00
|
|
|
"""
|
|
|
|
Tests that quarantining from protected media fails
|
|
|
|
"""
|
|
|
|
|
|
|
|
# protect
|
|
|
|
self.get_success(self.store.mark_local_media_as_safe(self.media_id, safe=True))
|
|
|
|
|
|
|
|
# verify protection
|
|
|
|
media_info = self.get_success(self.store.get_local_media(self.media_id))
|
2022-04-01 18:04:16 +02:00
|
|
|
assert media_info is not None
|
2023-11-09 17:00:30 +01:00
|
|
|
self.assertTrue(media_info.safe_from_quarantine)
|
2021-06-02 19:50:35 +02:00
|
|
|
|
|
|
|
# quarantining
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url % ("quarantine", self.server_name, self.media_id),
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2021-06-02 19:50:35 +02:00
|
|
|
self.assertFalse(channel.json_body)
|
|
|
|
|
|
|
|
# verify that is not in quarantine
|
|
|
|
media_info = self.get_success(self.store.get_local_media(self.media_id))
|
2022-04-01 18:04:16 +02:00
|
|
|
assert media_info is not None
|
2023-11-09 17:00:30 +01:00
|
|
|
self.assertFalse(media_info.quarantined_by)
|
2021-06-02 19:50:35 +02:00
|
|
|
|
|
|
|
|
2023-10-06 13:22:55 +02:00
|
|
|
class ProtectMediaByIDTestCase(_AdminMediaTests):
|
2021-12-03 14:57:13 +01:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2023-10-06 13:22:55 +02:00
|
|
|
hs.get_media_repository_resource()
|
2022-02-23 12:04:02 +01:00
|
|
|
self.store = hs.get_datastores().main
|
2021-05-26 12:19:47 +02:00
|
|
|
|
|
|
|
self.admin_user = self.register_user("admin", "pass", admin=True)
|
|
|
|
self.admin_user_tok = self.login("admin", "pass")
|
|
|
|
|
|
|
|
# Upload some media into the room
|
|
|
|
response = self.helper.upload_media(
|
2021-11-30 10:53:54 +01:00
|
|
|
SMALL_PNG,
|
|
|
|
tok=self.admin_user_tok,
|
2022-08-08 22:21:27 +02:00
|
|
|
expect_code=200,
|
2021-05-26 12:19:47 +02:00
|
|
|
)
|
|
|
|
# Extract media ID from the response
|
|
|
|
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'
|
|
|
|
self.media_id = server_and_media_id.split("/")[1]
|
|
|
|
|
|
|
|
self.url = "/_synapse/admin/v1/media/%s/%s"
|
|
|
|
|
|
|
|
@parameterized.expand(["protect", "unprotect"])
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_no_auth(self, action: str) -> None:
|
2021-05-26 12:19:47 +02:00
|
|
|
"""
|
|
|
|
Try to protect media without authentication.
|
|
|
|
"""
|
|
|
|
|
|
|
|
channel = self.make_request("POST", self.url % (action, self.media_id), b"{}")
|
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(401, channel.code, msg=channel.json_body)
|
2021-05-26 12:19:47 +02:00
|
|
|
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
|
|
|
|
|
|
|
|
@parameterized.expand(["protect", "unprotect"])
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_requester_is_no_admin(self, action: str) -> None:
|
2021-05-26 12:19:47 +02:00
|
|
|
"""
|
|
|
|
If the user is not a server admin, an error is returned.
|
|
|
|
"""
|
|
|
|
self.other_user = self.register_user("user", "pass")
|
|
|
|
self.other_user_token = self.login("user", "pass")
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url % (action, self.media_id),
|
|
|
|
access_token=self.other_user_token,
|
|
|
|
)
|
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(403, channel.code, msg=channel.json_body)
|
2021-05-26 12:19:47 +02:00
|
|
|
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_protect_media(self) -> None:
|
2021-05-26 12:19:47 +02:00
|
|
|
"""
|
|
|
|
Tests that protect and unprotect a media is successfully
|
|
|
|
"""
|
|
|
|
|
|
|
|
media_info = self.get_success(self.store.get_local_media(self.media_id))
|
2022-04-01 18:04:16 +02:00
|
|
|
assert media_info is not None
|
2023-11-09 17:00:30 +01:00
|
|
|
self.assertFalse(media_info.safe_from_quarantine)
|
2021-05-26 12:19:47 +02:00
|
|
|
|
|
|
|
# protect
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url % ("protect", self.media_id),
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2021-05-26 12:19:47 +02:00
|
|
|
self.assertFalse(channel.json_body)
|
|
|
|
|
|
|
|
media_info = self.get_success(self.store.get_local_media(self.media_id))
|
2022-04-01 18:04:16 +02:00
|
|
|
assert media_info is not None
|
2023-11-09 17:00:30 +01:00
|
|
|
self.assertTrue(media_info.safe_from_quarantine)
|
2021-05-26 12:19:47 +02:00
|
|
|
|
|
|
|
# unprotect
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url % ("unprotect", self.media_id),
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
2022-08-08 22:21:27 +02:00
|
|
|
self.assertEqual(200, channel.code, msg=channel.json_body)
|
2021-05-26 12:19:47 +02:00
|
|
|
self.assertFalse(channel.json_body)
|
|
|
|
|
|
|
|
media_info = self.get_success(self.store.get_local_media(self.media_id))
|
2022-04-01 18:04:16 +02:00
|
|
|
assert media_info is not None
|
2023-11-09 17:00:30 +01:00
|
|
|
self.assertFalse(media_info.safe_from_quarantine)
|
2021-10-20 16:41:48 +02:00
|
|
|
|
|
|
|
|
2023-10-06 13:22:55 +02:00
|
|
|
class PurgeMediaCacheTestCase(_AdminMediaTests):
|
2021-10-20 16:41:48 +02:00
|
|
|
servlets = [
|
|
|
|
synapse.rest.admin.register_servlets,
|
|
|
|
synapse.rest.admin.register_servlets_for_media_repo,
|
|
|
|
login.register_servlets,
|
|
|
|
profile.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
]
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2021-10-20 16:41:48 +02:00
|
|
|
self.media_repo = hs.get_media_repository_resource()
|
|
|
|
self.server_name = hs.hostname
|
|
|
|
|
|
|
|
self.admin_user = self.register_user("admin", "pass", admin=True)
|
|
|
|
self.admin_user_tok = self.login("admin", "pass")
|
|
|
|
|
|
|
|
self.filepaths = MediaFilePaths(hs.config.media.media_store_path)
|
|
|
|
self.url = "/_synapse/admin/v1/purge_media_cache"
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_no_auth(self) -> None:
|
2021-10-20 16:41:48 +02:00
|
|
|
"""
|
|
|
|
Try to delete media without authentication.
|
|
|
|
"""
|
|
|
|
|
|
|
|
channel = self.make_request("POST", self.url, b"{}")
|
|
|
|
|
2021-11-30 10:53:54 +01:00
|
|
|
self.assertEqual(
|
2022-08-09 15:56:43 +02:00
|
|
|
401,
|
2021-11-30 10:53:54 +01:00
|
|
|
channel.code,
|
|
|
|
msg=channel.json_body,
|
|
|
|
)
|
2021-10-20 16:41:48 +02:00
|
|
|
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_requester_is_not_admin(self) -> None:
|
2021-10-20 16:41:48 +02:00
|
|
|
"""
|
|
|
|
If the user is not a server admin, an error is returned.
|
|
|
|
"""
|
|
|
|
self.other_user = self.register_user("user", "pass")
|
|
|
|
self.other_user_token = self.login("user", "pass")
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url,
|
|
|
|
access_token=self.other_user_token,
|
|
|
|
)
|
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(403, channel.code, msg=channel.json_body)
|
2021-10-20 16:41:48 +02:00
|
|
|
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
|
|
|
|
|
2021-12-03 14:57:13 +01:00
|
|
|
def test_invalid_parameter(self) -> None:
|
2021-10-20 16:41:48 +02:00
|
|
|
"""
|
|
|
|
If parameters are invalid, an error is returned.
|
|
|
|
"""
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url + "?before_ts=-1234",
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
2021-10-20 16:41:48 +02:00
|
|
|
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
|
|
|
|
self.assertEqual(
|
|
|
|
"Query parameter before_ts must be a positive integer.",
|
|
|
|
channel.json_body["error"],
|
|
|
|
)
|
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"POST",
|
|
|
|
self.url + f"?before_ts={INVALID_TIMESTAMP_IN_S}",
|
|
|
|
access_token=self.admin_user_tok,
|
|
|
|
)
|
|
|
|
|
2022-08-10 20:01:12 +02:00
|
|
|
self.assertEqual(400, channel.code, msg=channel.json_body)
|
2021-10-20 16:41:48 +02:00
|
|
|
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
|
|
|
|
self.assertEqual(
|
|
|
|
"Query parameter before_ts you provided is from the year 1970. "
|
|
|
|
+ "Double check that you are providing a timestamp in milliseconds.",
|
|
|
|
channel.json_body["error"],
|
|
|
|
)
|