Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fix that sending server notices fail if avatar is None (#13566)
Browse files Browse the repository at this point in the history
Indroduced in #11846.
  • Loading branch information
dklimpel authored Aug 23, 2022
1 parent 9385c41 commit 37f329c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.d/13566.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug introduced in Synapse 1.52.0 where sending server notices fails if `max_avatar_size` or `allowed_avatar_mimetypes` is set and not `system_mxid_avatar_url`.
2 changes: 1 addition & 1 deletion synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ async def update_membership_locked(
errcode=Codes.BAD_JSON,
)

if "avatar_url" in content:
if "avatar_url" in content and content.get("avatar_url") is not None:
if not await self.profile_handler.check_avatar_size_and_mime_type(
content["avatar_url"],
):
Expand Down
56 changes: 56 additions & 0 deletions tests/rest/admin/test_server_notice.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,62 @@ def test_invalid_parameter(self) -> None:
self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"])
self.assertEqual("'msgtype' not in content", channel.json_body["error"])

@override_config(
{
"server_notices": {
"system_mxid_localpart": "notices",
"system_mxid_avatar_url": "somthingwrong",
},
"max_avatar_size": "10M",
}
)
def test_invalid_avatar_url(self) -> None:
"""If avatar url in homeserver.yaml is invalid and
"check avatar size and mime type" is set, an error is returned.
TODO: Should be checked when reading the configuration."""
channel = self.make_request(
"POST",
self.url,
access_token=self.admin_user_tok,
content={
"user_id": self.other_user,
"content": {"msgtype": "m.text", "body": "test msg"},
},
)

self.assertEqual(500, channel.code, msg=channel.json_body)
self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"])

@override_config(
{
"server_notices": {
"system_mxid_localpart": "notices",
"system_mxid_display_name": "test display name",
"system_mxid_avatar_url": None,
},
"max_avatar_size": "10M",
}
)
def test_displayname_is_set_avatar_is_none(self) -> None:
"""
Tests that sending a server notices is successfully,
if a display_name is set, avatar_url is `None` and
"check avatar size and mime type" is set.
"""
channel = self.make_request(
"POST",
self.url,
access_token=self.admin_user_tok,
content={
"user_id": self.other_user,
"content": {"msgtype": "m.text", "body": "test msg"},
},
)
self.assertEqual(200, channel.code, msg=channel.json_body)

# user has one invite
self._check_invite_and_join_status(self.other_user, 1, 0)

def test_server_notice_disabled(self) -> None:
"""Tests that server returns error if server notice is disabled"""
channel = self.make_request(
Expand Down
9 changes: 6 additions & 3 deletions tests/server_notices/test_resource_limits_server_notices.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest.mock import Mock

from twisted.test.proto_helpers import MemoryReactor

from synapse.api.constants import EventTypes, LimitBlockingTypes, ServerNoticeMsgType
from synapse.api.errors import ResourceLimitError
from synapse.rest import admin
from synapse.rest.client import login, room, sync
from synapse.server import HomeServer
from synapse.server_notices.resource_limits_server_notices import (
ResourceLimitsServerNotices,
)
from synapse.util import Clock

from tests import unittest
from tests.test_utils import make_awaitable
Expand Down Expand Up @@ -52,7 +55,7 @@ def default_config(self):

return config

def prepare(self, reactor, clock, hs):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.server_notices_sender = self.hs.get_server_notices_sender()

# relying on [1] is far from ideal, but the only case where
Expand Down Expand Up @@ -251,7 +254,7 @@ def default_config(self):
c["admin_contact"] = "mailto:user@test.com"
return c

def prepare(self, reactor, clock, hs):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.store = self.hs.get_datastores().main
self.server_notices_sender = self.hs.get_server_notices_sender()
self.server_notices_manager = self.hs.get_server_notices_manager()
Expand Down

0 comments on commit 37f329c

Please sign in to comment.