# BSD 2-Clause License # # Apprise - Push Notification Library. # Copyright (c) 2026, Chris Caron # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. import errno import gc from json import dumps, loads # Disable logging for a cleaner testing output import logging import os from typing import Union from unittest import mock from helpers import AppriseURLTester import pytest import requests from apprise import ( Apprise, AppriseAsset, AppriseAttachment, NotifyType, PersistentStoreMode, ) from apprise.exception import ( AppriseImproperlyConfigured, AppriseInvalidData, ApprisePluginException, ) from apprise.plugins.base import _delivery_tracker from apprise.plugins.matrix import ( MatrixDiscoveryException, NotifyMatrix, sas as matrix_sas, ) logging.disable(logging.CRITICAL) class _Response(mock.Mock): """Mock an HTTP response that supports Requests-style chunked reads.""" def iter_content(self, chunk_size=None): content = self.content if isinstance(content, str): content = content.encode("utf-8") return iter([content] if content else []) MATRIX_GOOD_RESPONSE = dumps( { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost", "!def456:localhost"], "access_token": "abcd1234", "home_server": "localhost", # user_id is returned by /login and /account/whoami; including it # here ensures self.user_id is set after login so _whoami() is not # called unnecessarily (it only fires when user_id is still absent). "user_id": "@apprise:localhost", # Simulate .well-known "m.homeserver": {"base_url": "https://matrix.example.com"}, "m.identity_server": {"base_url": "https://vector.im"}, } ) def test_plugin_matrix_discovery_exception(): """Matrix discovery failures use the common plugin exception.""" exc = MatrixDiscoveryException("discovery failed") assert isinstance(exc, ApprisePluginException) assert exc.error_code == 600 # Attachment Directory TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") def _force_del_cleanup(): """Run a garbage collection pass right after `del obj`. This is required otherwise the timing of the systems garbage collection can cause issues with the testing """ gc.collect() # Our Testing URLs apprise_url_tests = ( ################################## # NotifyMatrix ################################## ( "matrix://", { "instance": None, }, ), ( "matrixs://", { "instance": None, }, ), ( "matrix://localhost?mode=off", { # treats it as a anonymous user to register "instance": NotifyMatrix, # response is false because we have nothing to notify "response": False, }, ), ( "matrix://localhost", { # Initialization as t2bot fails because localhost is too short # to be an API key. "instance": AppriseImproperlyConfigured }, ), ( "matrix://user:pass@localhost/#room1/#room2/#room3", { "instance": NotifyMatrix, "response": False, "requests_response_code": requests.codes.internal_server_error, }, ), ( "matrix://user:pass@localhost/#room1/#room2/!room1", { "instance": NotifyMatrix, # throw a bizarre code forcing us to fail to look it up "response": False, "requests_response_code": 999, }, ), ( "matrix://user:pass@localhost:1234/#room", { "instance": NotifyMatrix, # Throws a series of i/o exceptions with this flag # is set and tests that we gracefully handle them "test_requests_exceptions": True, # Our expected url(privacy=True) startswith() response: "privacy_url": "matrix://user:****@localhost:1234/", }, ), # Matrix supports webhooks too; the following tests this now: ( "matrix://user:token@localhost?mode=matrix&format=text", { # user and token correctly specified with webhook "instance": NotifyMatrix, "response": False, }, ), ( "matrix://user:token@localhost?mode=matrix&format=html", { # user and token correctly specified with webhook "instance": NotifyMatrix, }, ), ( "matrix://user:token@localhost:123/#general/?version=3", { # Provide version over-ride (using version=) "instance": NotifyMatrix, # Our response expected server response "requests_response_text": MATRIX_GOOD_RESPONSE, "privacy_url": "matrix://user:****@localhost:123", }, ), ( "matrixs://user:token@localhost/#general?v=2", { # Provide version over-ride (using v=) "instance": NotifyMatrix, # Our response expected server response "requests_response_text": MATRIX_GOOD_RESPONSE, "privacy_url": "matrixs://user:****@localhost", }, ), ( "matrix://user:token@localhost:123/#general/?v=invalid", { # Invalid version specified "instance": AppriseImproperlyConfigured }, ), ( "matrix://user:token@localhost?mode=slack&format=text", { # user and token correctly specified with webhook "instance": NotifyMatrix, }, ), ( "matrixs://user:token@localhost?mode=SLACK&format=markdown", { # user and token specified; slack webhook still detected # despite uppercase characters "instance": NotifyMatrix, }, ), ( "matrix://user@localhost?mode=SLACK&format=markdown&token=mytoken", { # user and token specified; slack webhook still detected # despite uppercase characters; token also set on URL as arg "instance": NotifyMatrix, }, ), ( "matrix://_?mode=t2bot&token={}".format("b" * 64), { # Testing t2bot initialization and setting the password using the # token directive "instance": NotifyMatrix, # Our expected url(privacy=True) startswith() response: "privacy_url": "matrix://b...b/", }, ), ( ( "matrixs://hookuser:hooktoken@hookshot.example" "?mode=hookshot&path=%2Fpublic-hooks" ), { "instance": NotifyMatrix, "privacy_url": ( "matrixs://hookuser:****@hookshot.example/" "?image=no&mode=hookshot" ), }, ), ( ( "matrixs://hookuser:hooktoken@hookshot.example" "?mode=hookshot&path=public-hooks" ), { "instance": NotifyMatrix, }, ), # Image Reference ( "matrixs://user:token@localhost?mode=slack&format=markdown&image=True", { # user and token specified; image set to True "instance": NotifyMatrix, }, ), ( ( "matrixs://user:token@localhost?mode=slack" "&format=markdown&image=False" ), { # user and token specified; image set to True "instance": NotifyMatrix, }, ), # A Bunch of bad ports ( "matrixs://user:pass@hostname:port/#room_alias", { # Invalid Port specified (was a string) "instance": AppriseImproperlyConfigured, }, ), ( "matrixs://user:pass@hostname:0/#room_alias", { # Invalid Port specified (was a string) "instance": AppriseImproperlyConfigured, }, ), ( "matrixs://user:pass@hostname:65536/#room_alias", { # Invalid Port specified (was a string) "instance": AppriseImproperlyConfigured, }, ), # More general testing... ( "matrixs://user@{}?mode=t2bot&format=markdown&image=True".format( "a" * 64 ), { # user and token specified; image set to True "instance": NotifyMatrix }, ), ( "matrix://user@{}?mode=t2bot&format=html&image=False".format("z" * 64), { # user and token specified; image set to True "instance": NotifyMatrix }, ), # This will default to t2bot because no targets were specified and no # password ( "matrixs://{}".format("c" * 64), { "instance": NotifyMatrix, # Throws a series of i/o exceptions with this flag # is set and tests that we gracefully handle them "test_requests_exceptions": True, }, ), # Test Native URL ( "https://webhooks.t2bot.io/api/v1/matrix/hook/{}/".format("d" * 64), { # user and token specified; image set to True "instance": NotifyMatrix, }, ), ( "matrix://user:token@localhost?mode=On", { # invalid webhook specified (unexpected boolean) "instance": AppriseImproperlyConfigured, }, ), ( "matrix://token@localhost/?mode=Matrix", { "instance": NotifyMatrix, "response": False, "requests_response_code": requests.codes.internal_server_error, }, ), ( "matrix://user:token@localhost/mode=matrix", { "instance": NotifyMatrix, # throw a bizarre code forcing us to fail to look it up "response": False, "requests_response_code": 999, }, ), ( "matrix://token@localhost:8080/?mode=slack", { "instance": NotifyMatrix, # Throws a series of i/o exceptions with this flag # is set and tests that we gracefully handle them "test_requests_exceptions": True, }, ), ( "matrix://{}/?mode=t2bot".format("b" * 64), { "instance": NotifyMatrix, # Throws a series of i/o exceptions with this flag # is set and tests that we gracefully handle them "test_requests_exceptions": True, }, ), ) def test_plugin_matrix_urls(): """NotifyMatrix() Apprise URLs.""" # Run our general tests AppriseURLTester(tests=apprise_url_tests).run_all() @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_general(mock_post, mock_get, mock_put): """NotifyMatrix() General Tests.""" response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost", "!def456:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok # Prepare Mock mock_get.return_value = request mock_post.return_value = request mock_put.return_value = request # Variation Initializations obj = NotifyMatrix(host="host", targets="#abcd") assert isinstance(obj, NotifyMatrix) assert isinstance(obj.url(), str) # Registration successful assert obj.send(body="test") is True del obj obj = NotifyMatrix(host="host", user="user", targets="#abcd") assert isinstance(obj, NotifyMatrix) assert isinstance(obj.url(), str) # Registration successful assert obj.send(body="test") is True del obj obj = NotifyMatrix(host="host", password="passwd", targets="#abcd") assert isinstance(obj, NotifyMatrix) assert isinstance(obj.url(), str) # A username gets automatically generated in these cases assert obj.send(body="test") is True del obj obj = NotifyMatrix( host="host", user="user", password="passwd", targets="#abcd" ) assert isinstance(obj.url(), str) assert isinstance(obj, NotifyMatrix) # Registration Successful assert obj.send(body="test") is True del obj # Test sending other format types kwargs = NotifyMatrix.parse_url( "matrix://user:passwd@hostname/#abcd?format=html" ) obj = NotifyMatrix(**kwargs) assert isinstance(obj.url(), str) assert isinstance(obj, NotifyMatrix) assert obj.send(body="test") is True assert obj.send(title="title", body="test") is True del obj kwargs = NotifyMatrix.parse_url( "matrix://user:passwd@hostname/#abcd/#abcd:localhost?format=markdown" ) obj = NotifyMatrix(**kwargs) assert isinstance(obj.url(), str) assert isinstance(obj, NotifyMatrix) assert obj.send(body="test") is True assert obj.send(title="title", body="test") is True del obj kwargs = NotifyMatrix.parse_url( "matrix://user:passwd@hostname/#abcd/!abcd:localhost?format=text" ) obj = NotifyMatrix(**kwargs) assert isinstance(obj.url(), str) assert isinstance(obj, NotifyMatrix) is True assert obj.send(body="test") is True assert obj.send(title="title", body="test") is True del obj # Test notice type notifications kwargs = NotifyMatrix.parse_url( "matrix://user:passwd@hostname/#abcd?msgtype=notice" ) obj = NotifyMatrix(**kwargs) assert isinstance(obj.url(), str) is True assert isinstance(obj, NotifyMatrix) is True assert obj.send(body="test") is True assert obj.send(title="title", body="test") is True with pytest.raises(AppriseImproperlyConfigured): # invalid message type specified kwargs = NotifyMatrix.parse_url( "matrix://user:passwd@hostname/#abcd?msgtype=invalid" ) NotifyMatrix(**kwargs) # Force a failed login ro = response_obj.copy() del ro["access_token"] request.content = dumps(ro) request.status_code = 404 # Fails because we couldn't register because of 404 errors assert obj.send(body="test") is False del obj obj = NotifyMatrix(host="host", user="test", targets="#abcd") assert isinstance(obj, NotifyMatrix) is True # Fails because we still couldn't register assert obj.send(user="test", password="passwd", body="test") is False del obj obj = NotifyMatrix( host="host", user="test", password="passwd", targets="#abcd" ) assert isinstance(obj, NotifyMatrix) is True # Fails because we still couldn't register assert obj.send(body="test") is False del obj obj = NotifyMatrix(host="host", password="passwd", targets="#abcd") # Fails because we still couldn't register assert isinstance(obj, NotifyMatrix) is True assert obj.send(body="test") is False # Force a empty joined list response ro = response_obj.copy() ro["joined_rooms"] = [] request.content = dumps(ro) assert obj.send(user="test", password="passwd", body="test") is False # Fall back to original template request.content = dumps(response_obj) request.status_code = requests.codes.ok # update our response object so logins now succeed response_obj["user_id"] = "@apprise:localhost" # Login was successful but not get a room_id ro = response_obj.copy() del ro["room_id"] request.content = dumps(ro) assert obj.send(user="test", password="passwd", body="test") is False # Fall back to original template request.content = dumps(response_obj) request.status_code = requests.codes.ok del obj obj = NotifyMatrix(host="host", targets=None) assert isinstance(obj, NotifyMatrix) is True # Force a empty joined list response ro = response_obj.copy() ro["joined_rooms"] = [] request.content = dumps(ro) assert obj.send(user="test", password="passwd", body="test") is False # Fall back to original template request.content = dumps(response_obj) request.status_code = requests.codes.ok # our room list is empty so we'll have retrieved the joined_list # as our backup assert obj.send(user="test", password="passwd", body="test") is True del obj @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_fetch(mock_post, mock_get, mock_put): """NotifyMatrix() Server Fetch/API Tests.""" response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost", "!def456:localhost"], # Login details "access_token": "abcd1234", "user_id": "@apprise:localhost", "home_server": "localhost", } def fetch_failed(url, *args, **kwargs): # Default configuration request = _Response() request.status_code = requests.codes.ok request.content = dumps(response_obj) if url.find("/rooms/") > -1: # over-ride on room query request.status_code = 403 request.content = dumps( { "errcode": "M_UNKNOWN", "error": "Internal server error", } ) return request mock_put.side_effect = fetch_failed mock_get.side_effect = fetch_failed mock_post.side_effect = fetch_failed obj = NotifyMatrix( host="host", user="user", password="passwd", include_image=True ) assert isinstance(obj, NotifyMatrix) is True # We would hve failed to send our image notification assert obj.send(user="test", password="passwd", body="test") is False del obj # Do the same query with no images to fetch asset = AppriseAsset(image_path_mask=False, image_url_mask=False) obj = NotifyMatrix( host="host", user="user", password="passwd", asset=asset ) assert isinstance(obj, NotifyMatrix) is True # We would hve failed to send our notification assert obj.send(user="test", password="passwd", body="test") is False del obj response_obj = { # Registration "access_token": "abcd1234", "user_id": "@apprise:localhost", "home_server": "localhost", # For room joining "room_id": "!abc123:localhost", } # Default configuration mock_get.side_effect = None mock_post.side_effect = None mock_put.side_effect = None request = _Response() request.status_code = requests.codes.ok request.content = dumps(response_obj) mock_post.return_value = request mock_get.return_value = request mock_put.return_value = request obj = NotifyMatrix(host="host", include_image=True) assert isinstance(obj, NotifyMatrix) is True assert obj.access_token is None assert obj._register() is True assert obj.access_token is not None # Cause retries request.status_code = 429 request.content = dumps( { "retry_after_ms": 1, } ) postokay, _response, _ = obj._fetch("/retry/apprise/unit/test") assert postokay is False request.content = dumps( { "error": { "retry_after_ms": 1, } } ) postokay, _response, _ = obj._fetch("/retry/apprise/unit/test") assert postokay is False request.content = dumps({"error": {}}) postokay, _response, _ = obj._fetch("/retry/apprise/unit/test") assert postokay is False # A caller with its own tight time budget can cap how long a 429's # own server-requested wait is allowed to throttle for. request.content = dumps({"retry_after_ms": 999999}) with mock.patch.object(obj, "throttle") as throttle: postokay, _response, _ = obj._fetch( "/retry/apprise/unit/test", max_retry_wait=1 ) assert postokay is False # The second throttle uses the remaining shared 429 budget. second_call_wait = throttle.call_args_list[1].kwargs["wait"] assert second_call_wait == pytest.approx(1.0, abs=0.1) # Restore success so object cleanup does not honor the mocked long retry. request.status_code = requests.codes.ok request.content = dumps(response_obj) del obj @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_shared_retry_budget(mock_post, mock_get, mock_put): """Every 429 response draws from one shared retry-wait budget.""" request = _Response() request.status_code = 429 request.content = dumps({"retry_after_ms": 999999}) mock_post.return_value = request obj = NotifyMatrix(host="host", user="user", password="passwd") obj.access_token = "token" waits = [] def fake_throttle(wait=None): if wait is not None: waits.append(wait) with ( mock.patch.object(obj, "throttle", side_effect=fake_throttle), mock.patch( "apprise.plugins.matrix.base.monotonic", # retry_deadline anchor, then each 429 draws a remaining-wait # check before sleeping and a deadline recheck right after. side_effect=[0, 0.5, 0.6, 1.8, 1.9], ), ): postokay, _response, _ = obj._fetch( "/retry/apprise/unit/test", max_retry_wait=2 ) assert postokay is False # The two waits use the remaining portions of the same two-second budget. assert waits[0] == pytest.approx(1.5) assert waits[1] == pytest.approx(0.2) # Restore success so object cleanup does not honor the mocked long retry. request.status_code = requests.codes.ok request.content = dumps( { "access_token": "abcd1234", "user_id": "@apprise:localhost", "home_server": "localhost", } ) del obj @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_exhausted_retry_budget(mock_post, mock_get, mock_put): """An exhausted retry-wait budget ends the attempt without a new request. Once no time remains, a 429 must not be followed by another sleep and another request; it must give up immediately instead. """ request = _Response() request.status_code = 429 request.content = dumps({"retry_after_ms": 999999}) mock_post.return_value = request obj = NotifyMatrix(host="host", user="user", password="passwd") obj.access_token = "token" with ( mock.patch.object(obj, "throttle") as throttle, mock.patch( "apprise.plugins.matrix.base.monotonic", # retry_deadline anchor, then the very first remaining-wait # check already finds the budget spent. side_effect=[0, 2.0], ), ): postokay, _response, _ = obj._fetch( "/retry/apprise/unit/test", max_retry_wait=2 ) assert postokay is False # throttle() is always called once before the initial request; giving # up on the exhausted budget must not add a second, 429-driven wait. throttle.assert_called_once_with() assert mock_post.call_count == 1 # Restore success so object cleanup does not honor the mocked long retry. request.status_code = requests.codes.ok request.content = dumps( { "access_token": "abcd1234", "user_id": "@apprise:localhost", "home_server": "localhost", } ) del obj @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_retry_sleep_exhausts_budget( mock_post, mock_get, mock_put ): """A budget consumed by the sleep itself also stops the retry loop. The deadline is checked before sleeping and again right after -- a wait that lands exactly on (or past) the deadline must not be followed by one more request. """ request = _Response() request.status_code = 429 request.content = dumps({"retry_after_ms": 999999}) mock_post.return_value = request obj = NotifyMatrix(host="host", user="user", password="passwd") obj.access_token = "token" with ( mock.patch.object(obj, "throttle") as throttle, mock.patch( "apprise.plugins.matrix.base.monotonic", # retry_deadline anchor, a remaining-wait check that is still # positive (so the wait proceeds), then the post-sleep # recheck lands exactly on the deadline. side_effect=[0, 0.5, 1.0], ), ): postokay, _response, _ = obj._fetch( "/retry/apprise/unit/test", max_retry_wait=1 ) assert postokay is False # The 429 wait happens once; no second request follows it. assert throttle.call_args_list[-1] == mock.call(wait=0.5) assert mock_post.call_count == 1 # Restore success so object cleanup does not honor the mocked long retry. request.status_code = requests.codes.ok request.content = dumps( { "access_token": "abcd1234", "user_id": "@apprise:localhost", "home_server": "localhost", } ) del obj @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_fetch_response_limit(mock_post, mock_get, mock_put): """Discard and close a response that exceeds the caller's limit.""" request = mock.Mock() request.status_code = requests.codes.ok request.iter_content.return_value = iter([b"x" * 1000, b"x" * 1001]) mock_post.return_value = request obj = NotifyMatrix(host="host", user="user", password="passwd") obj.access_token = "token" postokay, response, status_code = obj._fetch( "/some/path", max_response_bytes=2000 ) assert postokay is False assert response == {} assert status_code == requests.codes.ok request.close.assert_called_once() del obj @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_fetch_response_within_limit( mock_post, mock_get, mock_put ): """Parse a bounded response while ignoring empty stream chunks.""" request = mock.Mock() request.status_code = requests.codes.ok request.iter_content.return_value = iter( [b"", dumps({"ok": True}).encode()] ) mock_post.return_value = request obj = NotifyMatrix(host="host", user="user", password="passwd") obj.access_token = "token" postokay, response, _status_code = obj._fetch( "/some/path", max_response_bytes=2000 ) assert postokay is True assert response == {"ok": True} # The stream is closed either way, not just on the oversized path. request.close.assert_called_once() del obj @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_fetch_stream_error(mock_post, mock_get, mock_put): """Handle streamed download errors and close the response.""" request = mock.Mock() request.status_code = requests.codes.ok def broken_iter_content(chunk_size=None): yield b"partial" raise requests.exceptions.ChunkedEncodingError("broken transfer") request.iter_content.side_effect = broken_iter_content mock_post.return_value = request obj = NotifyMatrix(host="host", user="user", password="passwd") obj.access_token = "token" postokay, response, status_code = obj._fetch( "/some/path", max_response_bytes=2000 ) assert postokay is False assert response == {} assert status_code == requests.codes.ok request.close.assert_called_once() del obj @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_auth(mock_post, mock_get, mock_put): """NotifyMatrix() Server Authentication.""" response_obj = { # Registration "access_token": "abcd1234", "user_id": "@apprise:localhost", "home_server": "localhost", } # Default configuration request = _Response() request.status_code = requests.codes.ok request.content = dumps(response_obj) mock_post.return_value = request mock_get.return_value = request mock_put.return_value = request obj = NotifyMatrix(host="localhost") assert isinstance(obj, NotifyMatrix) is True assert obj.access_token is None # logging out without an access_token is silently a success assert obj._logout() is True assert obj.access_token is None assert obj._register() is True assert obj.access_token is not None # Logging in is silently treated as a success because we # already had success registering assert obj._login() is True assert obj.access_token is not None # However if we log out assert obj._logout() is True assert obj.access_token is None # And set ourselves up for failure request.status_code = 403 assert obj._login() is False assert obj.access_token is None # Reset our token obj.access_token = None # Adjust our response to be invalid - missing access_token in response request.status_code = requests.codes.ok ro = response_obj.copy() del ro["access_token"] request.content = dumps(ro) # Our registration will fail now assert obj._register() is False assert obj.access_token is None del obj # So will login obj = NotifyMatrix(host="host", user="user", password="password") assert isinstance(obj, NotifyMatrix) is True assert obj._login() is False assert obj.access_token is None # Adjust our response to be invalid - invalid json response request.content = "{" # Our registration will fail now assert obj._register() is False assert obj.access_token is None request.status_code = requests.codes.ok request.content = dumps(response_obj) assert obj._register() is True assert obj.access_token is not None # Test logoff when getting a 403 error request.status_code = 403 assert obj._logout() is False assert obj.access_token is not None request.status_code = requests.codes.ok request.content = dumps(response_obj) assert obj._register() is True assert obj.access_token is not None request.status_code = 403 request.content = dumps( { "errcode": "M_UNKNOWN_TOKEN", "error": "Access Token unknown or expired", } ) # Test logoff when getting a 403 error; but if we have the right error # code in the response, then we return a True assert obj._logout() is True assert obj.access_token is None del obj # Modern Matrix servers omit home_server from login/register responses. # Apprise must derive it from user_id so room-alias resolution works. modern_resp = { "access_token": "tok99", "user_id": "@bot:matrix.org", "device_id": "DEVXYZ", # no "home_server" key -- matches real matrix.org behaviour } request.status_code = requests.codes.ok request.content = dumps(modern_resp) # No home_server in response + no prior home_server -> derive from user_id obj2 = NotifyMatrix(host="matrix.org", user="bot", password="pass") assert obj2._login() is True assert obj2.home_server == "matrix.org" obj3 = NotifyMatrix(host="matrix.org", user="bot", password="pass") assert obj3._register() is True assert obj3.home_server == "matrix.org" # No home_server in response + home_server already set -> not overwritten obj4 = NotifyMatrix(host="matrix.org", user="bot", password="pass") obj4.home_server = "already.set" assert obj4._login() is True assert obj4.home_server == "already.set" obj5 = NotifyMatrix(host="matrix.org", user="bot", password="pass") obj5.home_server = "already.set" assert obj5._register() is True assert obj5.home_server == "already.set" # No home_server in response + user_id has no colon -> # home_server stays None no_colon_resp = { "access_token": "tok88", "user_id": "@nocoilon", "device_id": "DEV88", } request.content = dumps(no_colon_resp) obj6 = NotifyMatrix(host="matrix.org", user="bot", password="pass") assert obj6._login() is True assert obj6.home_server is None obj7 = NotifyMatrix(host="matrix.org", user="bot", password="pass") assert obj7._register() is True assert obj7.home_server is None del obj2, obj3, obj4, obj5, obj6, obj7 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_rooms(mock_post, mock_get, mock_put): """NotifyMatrix() Room Testing.""" response_obj = { # Registration "access_token": "abcd1234", "user_id": "@apprise:localhost", "home_server": "localhost", # For joined_room response "joined_rooms": ["!abc123", "!def456:localhost"], # For room joining "room_id": "!abc123", } # Default configuration request = _Response() request.status_code = requests.codes.ok request.content = dumps(response_obj) mock_post.return_value = request mock_get.return_value = request mock_put.return_value = request obj = NotifyMatrix(host="host") assert isinstance(obj, NotifyMatrix) is True assert obj.access_token is None # Can't get room listing if we're not connnected assert obj._room_join("#abc123") is None assert obj._register() is True assert obj.access_token is not None assert obj._room_join("!abc123") == response_obj["room_id"] # Use cache to get same results assert obj.store.get("!abc123") is None # However this is how the cache entry gets stored assert obj.store.get("!abc123:localhost") is not None assert obj.store.get("!abc123:localhost")["id"] == response_obj["room_id"] # When hsreq=yes, legacy behaviour is restored and a homeserver is # automatically appended to room IDs that omit it. obj.store.clear() obj.hsreq = True mock_post.reset_mock() assert obj._room_join("!abc123") == response_obj["room_id"] assert mock_post.call_count == 1 assert ( mock_post.call_args_list[0][0][0] == "http://host/_matrix/client/v3/join/%21abc123%3Alocalhost" ) # When hsreq=no, we honour the raw !room identifier exactly as provided # and do not suffix it with :homeserver. obj.store.clear() obj.hsreq = False def _join_side_effect(url, *args, **kwargs): r = _Response() # With hsreq disabled, only the raw form should be attempted: if url.endswith("/_matrix/client/v3/join/%21abc123"): r.status_code = requests.codes.ok r.content = dumps(response_obj).encode("utf-8") return r # Default ok for any other unexpected call r.status_code = requests.codes.ok r.content = dumps(response_obj).encode("utf-8") return r mock_post.reset_mock() mock_post.side_effect = _join_side_effect assert obj._room_join("!abc123") == response_obj["room_id"] assert mock_post.call_count == 1 assert ( mock_post.call_args_list[0][0][0] == "http://host/_matrix/client/v3/join/%21abc123" ) mock_post.reset_mock() assert obj._room_join("!abc123") == response_obj["room_id"] # Cache is used assert mock_post.call_count == 0 # Still using cache assert obj._room_join("!abc123:localhost") == response_obj["room_id"] assert mock_post.call_count == 0 # Toggle our settings back obj.hsreq = True mock_post.reset_mock() mock_post.side_effect = _join_side_effect assert obj._room_join("!abc123") == response_obj["room_id"] # We still no longer need to fetch as we know the info already assert mock_post.call_count == 0 # Restore defaults for remaining tests mock_post.side_effect = None obj.hsreq = True # Use cache to get same results (no additional HTTP call) mock_post.reset_mock() assert obj._room_join("!abc123") == response_obj["room_id"] assert mock_post.call_count == 0 obj.store.clear() assert obj._room_join("!abc123:localhost") == response_obj["room_id"] assert obj.store.get("!abc123:localhost") is not None assert obj.store.get("!abc123:localhost")["id"] == response_obj["room_id"] # Use cache to get same results assert obj._room_join("!abc123:localhost") == response_obj["room_id"] obj.store.clear() assert obj._room_join("abc123") == response_obj["room_id"] # Use cache to get same results assert obj.store.get("#abc123:localhost") is not None assert obj.store.get("#abc123:localhost")["id"] == response_obj["room_id"] assert obj._room_join("abc123") == response_obj["room_id"] # If home_server is missing, recover it from user_id so we never try # to join a '#room:None' alias. obj.store.clear() obj.home_server = None obj.user_id = "@apprise:localhost" mock_post.reset_mock() assert obj._room_join("abc123") == response_obj["room_id"] assert mock_post.call_count == 1 assert ( mock_post.call_args_list[0][0][0] == "http://host/_matrix/client/v3/join/%23abc123%3Alocalhost" ) obj.store.clear() assert obj._room_join("abc123:localhost") == response_obj["room_id"] # Use cache to get same results assert obj.store.get("#abc123:localhost") is not None assert obj.store.get("#abc123:localhost")["id"] == response_obj["room_id"] assert obj._room_join("abc123:localhost") == response_obj["room_id"] obj.store.clear() assert obj._room_join("#abc123:localhost") == response_obj["room_id"] # Use cache to get same results assert obj.store.get("#abc123:localhost") is not None assert obj.store.get("#abc123:localhost")["id"] == response_obj["room_id"] assert obj._room_join("#abc123:localhost") == response_obj["room_id"] obj.store.clear() assert obj._room_join("%") is None assert obj._room_join(None) is None obj.store.clear() obj.home_server = None obj.user_id = "@apprise" mock_post.reset_mock() assert obj._room_join("abc123") is None assert mock_post.call_count == 0 # 403 response; this will push for a room creation for alias based rooms # and these will fail request.status_code = 403 obj.store.clear() assert obj._room_join("!abc123") is None obj.store.clear() assert obj._room_join("!abc123:localhost") is None obj.store.clear() assert obj._room_join("abc123") is None obj.store.clear() assert obj._room_join("abc123:localhost") is None obj.store.clear() assert obj._room_join("#abc123:localhost") is None del obj # Room creation request.status_code = requests.codes.ok obj = NotifyMatrix(host="host") assert isinstance(obj, NotifyMatrix) is True assert obj.access_token is None # Can't get room listing if we're not connnected assert obj._room_create("#abc123") is None assert obj._register() is True assert obj.access_token is not None # You can't add room_id's, they must be aliases assert obj._room_create("!abc123") is None assert obj._room_create("!abc123:localhost") is None obj.store.clear() assert obj._room_create("abc123") == response_obj["room_id"] obj.store.clear() assert obj._room_create("abc123:localhost") == response_obj["room_id"] obj.store.clear() assert obj._room_create("#abc123:localhost") == response_obj["room_id"] obj.store.clear() assert obj._room_create("%") is None assert obj._room_create(None) is None # 403 response; this will push for a room creation for alias based rooms # and these will fail request.status_code = 403 obj.store.clear() assert obj._room_create("abc123") is None obj.store.clear() assert obj._room_create("abc123:localhost") is None obj.store.clear() assert obj._room_create("#abc123:localhost") is None request.status_code = 403 request.content = dumps( { "errcode": "M_ROOM_IN_USE", "error": "Room alias already taken", } ) obj.store.clear() # This causes us to look up a channel ID if we get a ROOM_IN_USE response assert obj._room_create("#abc123:localhost") is None del obj # Room detection request.status_code = requests.codes.ok request.content = dumps(response_obj) obj = NotifyMatrix(host="localhost") assert isinstance(obj, NotifyMatrix) is True assert obj.access_token is None # No rooms if we're not connected response = obj._joined_rooms() assert isinstance(response, list) is True assert len(response) == 0 # register our account assert obj._register() is True assert obj.access_token is not None response = obj._joined_rooms() assert isinstance(response, list) is True assert len(response) == len(response_obj["joined_rooms"]) for r in response: assert r in response_obj["joined_rooms"] request.status_code = 403 response = obj._joined_rooms() assert isinstance(response, list) is True assert len(response) == 0 del obj # Room id lookup request.status_code = requests.codes.ok obj = NotifyMatrix(host="localhost") assert isinstance(obj, NotifyMatrix) is True assert obj.access_token is None # Can't get room listing if we're not connnected assert obj._room_id("#abc123") is None assert obj._register() is True assert obj.access_token is not None # You can't add room_id's, they must be aliases assert obj._room_id("!abc123") is None assert obj._room_id("!abc123:localhost") is None obj.store.clear() assert obj._room_id("abc123") == response_obj["room_id"] obj.store.clear() assert obj._room_id("abc123:localhost") == response_obj["room_id"] obj.store.clear() assert obj._room_id("#abc123:localhost") == response_obj["room_id"] obj.store.clear() assert obj._room_id("%") is None assert obj._room_id(None) is None # If we can't look the code up, we return None request.status_code = 403 obj.store.clear() assert obj._room_id("#abc123:localhost") is None # Force a object removal (thus a logout call) del obj _force_del_cleanup() def test_plugin_matrix_url_parsing(): """NotifyMatrix() URL Testing.""" result = NotifyMatrix.parse_url("matrix://user:token@localhost?to=#room") assert isinstance(result, dict) is True assert len(result["targets"]) == 1 assert "#room" in result["targets"] result = NotifyMatrix.parse_url( "matrix://user:token@localhost?to=#room&hsreq=yes" ) assert isinstance(result, dict) is True assert result.get("hsreq") is True result = NotifyMatrix.parse_url( "matrix://user:token@localhost?to=#room1,#room2,#room3" ) assert isinstance(result, dict) is True assert len(result["targets"]) == 3 assert "#room1" in result["targets"] assert "#room2" in result["targets"] assert "#room3" in result["targets"] # Mixed-case alias with underscore should parse result = NotifyMatrix.parse_url( "matrix://user:token@localhost?to=#Dev_Room:localhost" ) assert isinstance(result, dict) is True assert len(result["targets"]) == 1 assert "#Dev_Room:localhost" in result["targets"] # Mixed-case room id with underscore should be accepted by _room_join from apprise.plugins.matrix import IS_ROOM_ID # local alias nm = NotifyMatrix(host="localhost") nm.access_token = "abc" # simulate logged-in nm.home_server = "localhost" # this should NOT be rejected by the regex assert IS_ROOM_ID.match("!Jm_LvU1nas_8KJPBmN9n:nginx.eu") @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_image_errors(mock_post, mock_get, mock_put): """NotifyMatrix() Image Error Handling.""" def mock_function_handing(url, data, **kwargs): """Dummy function for handling image posts (as a failure)""" response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost", "!def456:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok if "m.image" in data: # Fail for images request.status_code = 400 return request # Prepare Mock mock_get.side_effect = mock_function_handing mock_post.side_effect = mock_function_handing mock_put.side_effect = mock_function_handing obj = NotifyMatrix(host="host", include_image=True, version="2") assert isinstance(obj, NotifyMatrix) is True assert obj.access_token is None # Notification was successful, however we could not post image and since # we had post errors (of any kind) we still report a failure. assert bool(obj.notify("test", "test")) is False del obj obj = NotifyMatrix(host="host", include_image=False, version="2") assert isinstance(obj, NotifyMatrix) is True assert obj.access_token is None # We didn't post an image (which was set to fail) and therefore our # post was okay assert bool(obj.notify("test", "test")) is True # Force a object removal (thus a logout call) del obj _force_del_cleanup() def mock_function_handing(url, data, **kwargs): """Dummy function for handling image posts (successfully)""" response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost", "!def456:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok return request # Prepare Mock mock_get.side_effect = mock_function_handing mock_put.side_effect = mock_function_handing mock_post.side_effect = mock_function_handing obj = NotifyMatrix(host="host", include_image=True) assert isinstance(obj, NotifyMatrix) is True assert obj.access_token is None assert bool(obj.notify("test", "test")) is True del obj obj = NotifyMatrix(host="host", include_image=False) assert isinstance(obj, NotifyMatrix) is True assert obj.access_token is None assert bool(obj.notify("test", "test")) is True # Force a object removal (thus a logout call) del obj _force_del_cleanup() @mock.patch("requests.put") @mock.patch("requests.post") def test_plugin_matrix_attachments_api_v3(mock_post, mock_put): """NotifyMatrix() Attachment Checks (v3)""" # Prepare a good response response = _Response() response.status_code = requests.codes.ok response.content = MATRIX_GOOD_RESPONSE.encode("utf-8") # Prepare a bad response bad_response = _Response() bad_response.status_code = requests.codes.internal_server_error # Prepare Mock return object mock_post.return_value = response mock_put.return_value = response # Instantiate our object obj = Apprise.instantiate("matrix://user:pass@localhost/#general?v=3") # attach our content attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) assert ( bool( obj.notify( body="body", title="title", notify_type=NotifyType.INFO, attach=attach, ) ) is True ) attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) # Test our call count assert mock_put.call_count == 2 assert mock_post.call_count == 3 assert ( mock_post.call_args_list[0][0][0] == "http://localhost/_matrix/client/v3/login" ) assert ( mock_post.call_args_list[1][0][0] == "http://localhost/_matrix/client/v3/join/%23general%3Alocalhost" ) assert ( mock_post.call_args_list[2][0][0] == "http://localhost/_matrix/media/v3/upload" ) assert ( mock_put.call_args_list[0][0][0] == "http://localhost/_matrix/client/v3/rooms/%21abc123%3Alocalhost/" "send/m.room.message/0" ) assert ( mock_put.call_args_list[1][0][0] == "http://localhost/_matrix/client/v3/rooms/%21abc123%3Alocalhost/" "send/m.room.message/1" ) # Attach a zip file type attach = AppriseAttachment( os.path.join(TEST_VAR_DIR, "apprise-archive.zip") ) assert ( bool( obj.notify( body="body", title="title", notify_type=NotifyType.INFO, attach=attach, ) ) is True ) # An invalid attachment will cause a failure path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") attach = AppriseAttachment(path) assert ( bool( obj.notify( body="body", title="title", notify_type=NotifyType.INFO, attach=path, ) ) is False ) # update our attachment to be valid attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) mock_put.return_value = None mock_post.return_value = None # Throw an exception on the first call to requests.post() for side_effect in (requests.RequestException(), OSError(), bad_response): # Reset our value mock_put.reset_mock() mock_post.reset_mock() mock_post.side_effect = [side_effect] assert obj.send(body="test", attach=attach) is False # Throw an exception on the second call to requests.post() for side_effect in (requests.RequestException(), OSError(), bad_response): # Reset our value mock_put.reset_mock() mock_post.reset_mock() mock_put.side_effect = [side_effect, response] mock_post.side_effect = [response, side_effect, response] # We'll fail now because of our error handling assert obj.send(body="test", attach=attach) is False # handle a bad response mock_put.side_effect = [bad_response, response] mock_post.side_effect = [response, bad_response, response] # We'll fail now because of an internal exception assert obj.send(body="test", attach=attach) is False # Force a object removal (thus a logout call) del obj _force_del_cleanup() @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_discovery_service(mock_post, mock_get, mock_put): """NotifyMatrix() Discovery Service.""" # Prepare a good response response = _Response() response.status_code = requests.codes.ok response.content = MATRIX_GOOD_RESPONSE.encode("utf-8") # Prepare a good response bad_response = _Response() bad_response.status_code = requests.codes.unauthorized bad_response.content = MATRIX_GOOD_RESPONSE.encode("utf-8") # Prepare Mock return object mock_post.return_value = response mock_get.return_value = response mock_put.return_value = response # Instantiate our object obj = Apprise.instantiate( "matrixs://user:pass@example.com/#general?v=2&discovery=yes" ) assert bool(obj.notify("body")) is True response = _Response() response.status_code = requests.codes.unavailable resp = loads(MATRIX_GOOD_RESPONSE) mock_get.return_value = response mock_post.return_value = response obj = Apprise.instantiate( "matrixs://user:pass@example.com/#general?v=2&discovery=yes" ) obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key ) # Invalid host / fallback is to resolve our own host with pytest.raises(MatrixDiscoveryException): _ = obj.base_url # Verify cache is not saved assert NotifyMatrix.discovery_base_key not in obj.store assert NotifyMatrix.discovery_identity_key not in obj.store response.status_code = requests.codes.ok obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key ) # bad data resp["m.homeserver"] = "!garbage!:303" response.content = dumps(resp).encode("utf-8") obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key ) with pytest.raises(MatrixDiscoveryException): _ = obj.base_url # Verify cache is not saved assert NotifyMatrix.discovery_base_key not in obj.store assert NotifyMatrix.discovery_identity_key not in obj.store # We fail our discovery and therefore can't send our notification assert bool(obj.notify("hello world")) is False # bad key resp["m.homeserver"] = {} response.content = dumps(resp).encode("utf-8") obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key ) with pytest.raises(MatrixDiscoveryException): _ = obj.base_url # Verify cache is not saved assert NotifyMatrix.discovery_base_key not in obj.store assert NotifyMatrix.discovery_identity_key not in obj.store resp["m.homeserver"] = {"base_url": "https://nuxref.com/base"} response.content = dumps(resp).encode("utf-8") obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key ) assert obj.base_url == "https://nuxref.com/base" assert obj.identity_url == "https://vector.im" # Verify cache saved assert NotifyMatrix.discovery_base_key in obj.store assert NotifyMatrix.discovery_identity_key in obj.store # Discovery passes so notifications work too assert bool(obj.notify("hello world")) is True # bad data resp["m.identity_server"] = "!garbage!:303" response.content = dumps(resp).encode("utf-8") obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key ) with pytest.raises(MatrixDiscoveryException): _ = obj.base_url # Verify cache is not saved assert NotifyMatrix.discovery_base_key not in obj.store assert NotifyMatrix.discovery_identity_key not in obj.store # no key resp["m.identity_server"] = {} response.content = dumps(resp).encode("utf-8") obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key ) with pytest.raises(MatrixDiscoveryException): _ = obj.base_url # Verify cache is not saved assert NotifyMatrix.discovery_base_key not in obj.store assert NotifyMatrix.discovery_identity_key not in obj.store # remove del resp["m.identity_server"] response.content = dumps(resp).encode("utf-8") obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key ) assert obj.base_url == "https://nuxref.com/base" assert obj.identity_url == "https://nuxref.com/base" # restore resp["m.identity_server"] = {"base_url": "https://vector.im"} response.content = dumps(resp).encode("utf-8") # Not found is an acceptable response (no exceptions thrown) response.status_code = requests.codes.not_found obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key ) assert obj.base_url == "https://example.com" assert obj.identity_url == "https://example.com" # Verify cache saved assert NotifyMatrix.discovery_base_key in obj.store assert NotifyMatrix.discovery_identity_key in obj.store # Discovery passes so notifications work too response.status_code = requests.codes.ok assert bool(obj.notify("hello world")) is True response.status_code = requests.codes.ok mock_get.return_value = None mock_get.side_effect = (response, bad_response) obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key ) with pytest.raises(MatrixDiscoveryException): _ = obj.base_url # Verify cache is not saved assert NotifyMatrix.discovery_base_key not in obj.store assert NotifyMatrix.discovery_identity_key not in obj.store # Test case where ourIdentity URI fails to do it's check mock_get.side_effect = (response, response, bad_response) obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key ) with pytest.raises(MatrixDiscoveryException): _ = obj.base_url # Verify cache is not saved assert NotifyMatrix.discovery_base_key not in obj.store assert NotifyMatrix.discovery_identity_key not in obj.store # Test an empty block response response.status_code = requests.codes.ok response.content = "" mock_get.return_value = response mock_get.side_effect = None mock_post.return_value = response mock_post.side_effect = None obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key ) assert obj.base_url == "https://example.com" assert obj.identity_url == "https://example.com" # Verify cache saved assert NotifyMatrix.discovery_base_key in obj.store assert NotifyMatrix.discovery_identity_key in obj.store del obj @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_rejects_unsafe_discovery(mock_post, mock_get, mock_put): """Reject unsafe addresses returned by Matrix discovery.""" # Prepare a good response response = _Response() response.status_code = requests.codes.ok response.content = MATRIX_GOOD_RESPONSE.encode("utf-8") mock_post.return_value = response mock_get.return_value = response mock_put.return_value = response obj = Apprise.instantiate( "matrixs://user:pass@example.com/#general?v=2&discovery=yes" ) # Each of these is an address we refuse to send our token to for base_url in ( # An unencrypted address "http://matrix.example.com", # Embedded credentials "https://attacker:secret@matrix.example.com", # Not a host we can make sense of "https://-nope-.example.com", ): resp = loads(MATRIX_GOOD_RESPONSE) resp["m.homeserver"] = {"base_url": base_url} response.content = dumps(resp).encode("utf-8") obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key, ) with pytest.raises(MatrixDiscoveryException): _ = obj.base_url # Nothing is cached from a failed discovery assert NotifyMatrix.discovery_base_key not in obj.store assert NotifyMatrix.discovery_identity_key not in obj.store # The identity server address is held to the same standard resp = loads(MATRIX_GOOD_RESPONSE) resp["m.identity_server"] = {"base_url": "http://identity.example.com"} response.content = dumps(resp).encode("utf-8") obj.store.clear( NotifyMatrix.discovery_base_key, NotifyMatrix.discovery_identity_key ) with pytest.raises(MatrixDiscoveryException): _ = obj.base_url assert NotifyMatrix.discovery_base_key not in obj.store assert NotifyMatrix.discovery_identity_key not in obj.store del obj _force_del_cleanup() @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_attachments_api_v2(mock_post, mock_get, mock_put): """NotifyMatrix() Attachment Checks (v2)""" # Prepare a good response response = _Response() response.status_code = requests.codes.ok response.content = MATRIX_GOOD_RESPONSE.encode("utf-8") # Prepare a bad response bad_response = _Response() bad_response.status_code = requests.codes.internal_server_error # Prepare Mock return object mock_post.return_value = response mock_get.return_value = response mock_put.return_value = response # Instantiate our object obj = Apprise.instantiate("matrix://user:pass@localhost/#general?v=2") # attach our content attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) assert ( bool( obj.notify( body="body", title="title", notify_type=NotifyType.INFO, attach=attach, ) ) is True ) attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) # Attach an unsupported file mock_post.return_value = response mock_get.return_value = response mock_post.side_effect = None mock_get.side_effect = None # Force a object removal (thus a logout call) del obj _force_del_cleanup() # Instantiate our object obj = Apprise.instantiate("matrixs://user:pass@localhost/#general?v=2") # Reset our object mock_post.reset_mock() mock_get.reset_mock() mock_put.reset_mock() assert ( bool( obj.notify( body="body", title="title", notify_type=NotifyType.INFO, attach=attach, ) ) is True ) # Test our call count -- login, join, upload use POST; sends use PUT assert mock_post.call_count == 3 assert ( mock_post.call_args_list[0][0][0] == "https://matrix.example.com/_matrix/client/r0/login" ) assert ( mock_post.call_args_list[1][0][0] == "https://matrix.example.com/_matrix/client/r0/" "join/%23general%3Alocalhost" ) assert ( mock_post.call_args_list[2][0][0] == "https://matrix.example.com/_matrix/media/r0/upload" ) assert mock_put.call_count == 2 assert ( mock_put.call_args_list[0][0][0] == "https://matrix.example.com/_matrix/client/r0" "/rooms/%21abc123%3Alocalhost/send/m.room.message/0" ) assert ( mock_put.call_args_list[1][0][0] == "https://matrix.example.com/_matrix/client/r0/" "rooms/%21abc123%3Alocalhost/send/m.room.message/1" ) # Attach an unsupported file type; these are skipped attach = AppriseAttachment( os.path.join(TEST_VAR_DIR, "apprise-archive.zip") ) assert ( bool( obj.notify( body="body", title="title", notify_type=NotifyType.INFO, attach=attach, ) ) is True ) # An invalid attachment will cause a failure path = os.path.join(TEST_VAR_DIR, "/invalid/path/to/an/invalid/file.jpg") attach = AppriseAttachment(path) assert ( bool( obj.notify( body="body", title="title", notify_type=NotifyType.INFO, attach=path, ) ) is False ) # update our attachment to be valid attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) mock_post.return_value = None mock_get.return_value = None # Throw an exception on the first call to requests.post() for side_effect in (requests.RequestException(), OSError(), bad_response): # Reset our value mock_post.reset_mock() mock_get.reset_mock() mock_post.side_effect = [side_effect, response] mock_get.side_effect = [side_effect, response] assert obj.send(body="test", attach=attach) is False # Throw an exception on the attachment send (now uses PUT) for side_effect in (requests.RequestException(), OSError(), bad_response): # Reset our value mock_post.reset_mock() mock_get.reset_mock() mock_put.reset_mock() mock_post.side_effect = [response] # upload ok mock_put.side_effect = [side_effect, side_effect] # sends fail # We'll fail now because of our error handling assert obj.send(body="test", attach=attach) is False # handle a bad response on the attachment send (now PUT) mock_post.side_effect = [response] # upload ok mock_put.side_effect = [bad_response, response] # attachment fails mock_get.side_effect = None # We'll fail now because of an internal exception assert obj.send(body="test", attach=attach) is False # Force a object removal (thus a logout call) del obj _force_del_cleanup() # Instantiate our object (no discovery required) obj = Apprise.instantiate( "matrixs://user:pass@localhost/#general?v=2&discovery=no&image=y" ) # Reset our object mock_post.reset_mock() mock_get.reset_mock() mock_put.reset_mock() # login + join succeed; image inline send (now PUT) fails mock_post.side_effect = [response, response] mock_get.side_effect = None mock_put.side_effect = [bad_response] # image attachment didn't succeed assert ( bool( obj.notify(body="body", title="title", notify_type=NotifyType.INFO) ) is False ) # All calls now succeed mock_post.return_value = response mock_get.return_value = response mock_put.return_value = response mock_post.side_effect = None mock_get.side_effect = None mock_put.side_effect = None # We'll fail now because of an internal exception assert obj.send(body="test", attach=attach) is True # Force __del__() call del obj @mock.patch("requests.put") @mock.patch("requests.post") def test_plugin_matrix_v2_compliance(mock_post, mock_put): """NotifyMatrix() Verify V2 uses PUT and TID for standard messages.""" # Setup compliant response response = _Response() response.status_code = requests.codes.ok response.content = MATRIX_GOOD_RESPONSE.encode("utf-8") mock_post.return_value = response mock_put.return_value = response # Instantiate as V2 obj = Apprise.instantiate("matrix://user:pass@localhost/#general?v=2") # Send a standard notification assert bool(obj.notify(body="test message")) is True # Confirm the fix: # 1. Path contains the transaction ID '0' # 2. Method is PUT assert mock_put.call_count == 1 assert "/_matrix/client/r0/rooms/" in mock_put.call_args_list[0][0][0] assert "/send/m.room.message/0" in mock_put.call_args_list[0][0][0] @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_v2_token_mode_no_txn_increment( mock_post, mock_get, mock_put ): """Token mode (access_token == password) skips transaction ID increment.""" response = _Response() response.status_code = requests.codes.ok response.content = MATRIX_GOOD_RESPONSE.encode("utf-8") mock_post.return_value = response mock_get.return_value = response mock_put.return_value = response # Token mode: user omitted, password treated as access token # (parse_url swaps user->password when no password supplied) obj = Apprise.instantiate( "matrixs://my_access_token@localhost/#general?v=2&image=y" ) assert obj is not None # Send with image inline enabled assert bool(obj.notify(body="token mode image test")) is True # Send with an attachment attach = AppriseAttachment(os.path.join(TEST_VAR_DIR, "apprise-test.gif")) assert obj.send(body="token mode attach test", attach=attach) is True def test_plugin_matrix_parse_native_url_no_match(): """parse_native_url returns None for non-t2bot URLs.""" assert ( NotifyMatrix.parse_native_url("https://not-a-t2bot-url.com/some/path") is None ) @mock.patch("requests.post") def test_plugin_matrix_hookshot_webhook(mock_post): """matrix-hookshot webhook mode uses hookshot URL/payload conventions.""" response = _Response() response.status_code = requests.codes.ok response.content = b"{}" mock_post.return_value = response obj = Apprise.instantiate( "matrixs://apprise:supersecret@hookshot.example" "?mode=hookshot&format=html&path=%2Fpublic-hooks" ) assert obj is not None assert bool(obj.notify(title="Title", body="Body")) is True assert mock_post.call_args.args[0] == ( "https://hookshot.example/public-hooks/supersecret" ) payload = loads(mock_post.call_args.kwargs["data"]) assert payload["username"] == "apprise" # Undeclared passthrough content remains untouched. assert payload["text"] == "Title\r\nBody" assert payload["html"] == "

Title

Body" @mock.patch("requests.post") def test_plugin_matrix_hookshot_webhook_empty_title(mock_post): """Hookshot webhook mode avoids extra separators for empty titles.""" from apprise.common import NotifyFormat response = _Response() response.status_code = requests.codes.ok response.content = b"{}" mock_post.return_value = response obj = Apprise.instantiate( "matrixs://apprise:supersecret@hookshot.example" "?mode=hookshot&format=markdown&path=%2Fpublic-hooks" ) assert obj is not None # Declaring Markdown enables HTML rendering. assert ( bool(obj.notify(body="**Body**", body_format=NotifyFormat.MARKDOWN)) is True ) payload = loads(mock_post.call_args.kwargs["data"]) assert payload["username"] == "apprise" # The text fallback comes from the rendered HTML. assert payload["text"] == "Body" assert payload["html"] == "

Body

" @mock.patch("requests.post") def test_plugin_matrix_slack_webhook_markdown_untouched(mock_post): """Slack webhooks forward CommonMark for Slack to render.""" from apprise.common import NotifyFormat response = _Response() response.status_code = requests.codes.ok response.content = b"{}" mock_post.return_value = response obj = Apprise.instantiate( "matrixs://apprise:supersecret@slack.example?mode=slack&format=markdown" ) assert obj is not None assert ( bool(obj.notify(body="**Body**", body_format=NotifyFormat.MARKDOWN)) is True ) payload = loads(mock_post.call_args.kwargs["data"]) assert payload["mrkdwn"] is True # Slack receives the original CommonMark. assert payload["attachments"][0]["text"] == "**Body**" def test_plugin_matrix_hookshot_path_normalization(): """Hookshot webhook paths normalize missing leading slashes.""" obj = Apprise.instantiate( "matrixs://apprise:supersecret@hookshot.example" "?mode=hookshot&path=public-hooks" ) assert obj is not None assert obj.webhook_path == "/public-hooks" @mock.patch("requests.post") def test_plugin_matrix_hookshot_root_path_text(mock_post): """Hookshot root paths and text mode stay literal.""" response = _Response() response.status_code = requests.codes.ok response.content = b"{}" mock_post.return_value = response obj = Apprise.instantiate( "matrixs://apprise:supersecret@hookshot.example" "?mode=hookshot&format=text&path=%2F" ) assert obj is not None assert bool(obj.notify(body="Body")) is True assert mock_post.call_args.args[0] == ( "https://hookshot.example/supersecret" ) assert loads(mock_post.call_args.kwargs["data"])["html"] == ( "<b>Body</b>" ) @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_transaction_ids_api_v3_no_cache( mock_post, mock_get, mock_put ): """NotifyMatrix() Transaction ID Checks (v3)""" # Prepare a good response response = _Response() response.status_code = requests.codes.ok response.content = MATRIX_GOOD_RESPONSE.encode("utf-8") # Prepare a bad response bad_response = _Response() bad_response.status_code = requests.codes.internal_server_error # Prepare Mock return object mock_post.return_value = response mock_get.return_value = response mock_put.return_value = response # For each element is 1 batch that is ran # the number defined is the number of notifications to send batch = [10, 1, 5] for notifications in batch: # Instantiate our object obj = Apprise.instantiate("matrix://user:pass@localhost/#general?v=3") # Ensure mode is memory assert obj.store.mode == PersistentStoreMode.MEMORY # Performs a login assert ( bool( obj.notify( body="body", title="title", notify_type=NotifyType.INFO ) ) is True ) assert mock_get.call_count == 0 assert mock_post.call_count == 2 assert ( mock_post.call_args_list[0][0][0] == "http://localhost/_matrix/client/v3/login" ) assert ( mock_post.call_args_list[1][0][0] == "http://localhost/_matrix/client/v3/join/%23general%3Alocalhost" ) assert mock_put.call_count == 1 assert ( mock_put.call_args_list[0][0][0] == "http://localhost/_matrix/client/v3/rooms/" + "%21abc123%3Alocalhost/send/m.room.message/0" ) for no, _ in enumerate(range(notifications), start=1): # Clean our slate mock_post.reset_mock() mock_get.reset_mock() mock_put.reset_mock() assert ( bool( obj.notify( body="body", title="title", notify_type=NotifyType.INFO ) ) is True ) assert mock_get.call_count == 0 assert mock_post.call_count == 0 assert mock_put.call_count == 1 assert ( mock_put.call_args_list[0][0][0] == "http://localhost/_matrix/client/v3/rooms/" + f"%21abc123%3Alocalhost/send/m.room.message/{no}" ) mock_post.reset_mock() mock_get.reset_mock() mock_put.reset_mock() # Force a object removal (thus a logout call) del obj _force_del_cleanup() assert mock_get.call_count == 0 assert mock_post.call_count == 1 assert ( mock_post.call_args_list[0][0][0] == "http://localhost/_matrix/client/v3/logout" ) mock_post.reset_mock() assert mock_put.call_count == 0 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_transaction_ids_api_v3_w_cache( mock_post, mock_get, mock_put, tmpdir ): """NotifyMatrix() Transaction ID Checks (v3)""" # Prepare a good response response = _Response() response.status_code = requests.codes.ok response.content = MATRIX_GOOD_RESPONSE.encode("utf-8") # Prepare a bad response bad_response = _Response() bad_response.status_code = requests.codes.internal_server_error # Prepare Mock return object mock_post.return_value = response mock_get.return_value = response mock_put.return_value = response # For each element is 1 batch that is ran # the number defined is the number of notifications to send batch = [10, 1, 5] mock_post.reset_mock() mock_get.reset_mock() mock_put.reset_mock() asset = AppriseAsset( storage_mode=PersistentStoreMode.FLUSH, storage_path=str(tmpdir), ) # Message Counter transaction_id = 1 for no, notifications in enumerate(batch): # Instantiate our object obj = Apprise.instantiate( "matrix://user:pass@localhost/#general?v=3", asset=asset ) # Ensure mode is flush assert obj.store.mode == PersistentStoreMode.FLUSH # Performs a login assert ( bool( obj.notify( body="body", title="title", notify_type=NotifyType.INFO ) ) is True ) assert mock_get.call_count == 0 if no == 0: # first entry assert mock_post.call_count == 2 assert ( mock_post.call_args_list[0][0][0] == "http://localhost/_matrix/client/v3/login" ) assert ( mock_post.call_args_list[1][0][0] == "http://localhost/_matrix/client/v3/" "join/%23general%3Alocalhost" ) assert mock_put.call_count == 1 assert ( mock_put.call_args_list[0][0][0] == "http://localhost/_matrix/client/v3/rooms/" + "%21abc123%3Alocalhost/send/m.room.message/0" ) for no, _ in enumerate(range(notifications), start=transaction_id): # Clean our slate mock_post.reset_mock() mock_get.reset_mock() mock_put.reset_mock() assert ( bool( obj.notify( body="body", title="title", notify_type=NotifyType.INFO ) ) is True ) # Increment transaction counter transaction_id += 1 assert mock_get.call_count == 0 assert mock_post.call_count == 0 assert mock_put.call_count == 1 assert ( mock_put.call_args_list[0][0][0] == "http://localhost/_matrix/client/v3/rooms/" + f"%21abc123%3Alocalhost/send/m.room.message/{no}" ) # Increment transaction counter transaction_id += 1 mock_post.reset_mock() mock_get.reset_mock() mock_put.reset_mock() # Force a object removal # Biggest takeaway is that a logout no longer happens del obj assert mock_get.call_count == 0 assert mock_post.call_count == 0 assert mock_put.call_count == 0 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_v3_url_with_port_assembly( mock_post, mock_get, mock_put, tmpdir ): """NotifyMatrix() URL with Port Assembly Checks (v3)""" # Prepare a good response response = _Response() response.status_code = requests.codes.ok response.content = MATRIX_GOOD_RESPONSE.encode("utf-8") # Prepare Mock return object mock_post.return_value = response mock_get.return_value = response mock_put.return_value = response asset = AppriseAsset( storage_mode=PersistentStoreMode.FLUSH, storage_path=str(tmpdir), ) # Instantiate our object obj = Apprise.instantiate( "matrixs://user1:pass123@example.ca:8080/#general?v=3", asset=asset ) # Performs a login assert ( bool( obj.notify(body="body", title="title", notify_type=NotifyType.INFO) ) is True ) # Secure Connections have a bit of additional overhead to verify # the authenticity of the server through discovery assert mock_get.call_count == 3 assert ( mock_get.call_args_list[0][0][0] == "https://example.ca:8080/.well-known/matrix/client" ) assert ( mock_get.call_args_list[1][0][0] == "https://matrix.example.com/_matrix/client/versions" ) assert ( mock_get.call_args_list[2][0][0] == "https://vector.im/_matrix/identity/v2" ) assert mock_post.call_count == 2 # matrix.example.com comes from our MATRIX_GOOD_RESPONSE # response which defines wht our .well-known returned to us assert ( mock_post.call_args_list[0][0][0] == "https://matrix.example.com/_matrix/client/v3/login" ) assert ( mock_post.call_args_list[1][0][0] == "https://matrix.example.com/_matrix/client/v3/" "join/%23general%3Alocalhost" ) assert mock_put.call_count == 1 assert ( mock_put.call_args_list[0][0][0] == "https://matrix.example.com/_matrix/client/v3/rooms/" + "%21abc123%3Alocalhost/send/m.room.message/0" ) mock_post.reset_mock() mock_get.reset_mock() mock_put.reset_mock() assert obj.base_url == "https://matrix.example.com" # Cache is used under the hood; no second discover is performed assert mock_put.call_count == 0 assert mock_get.call_count == 0 assert mock_post.call_count == 0 # Cleanup del obj @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_no_room_create_on_non_not_found_join( mock_post: mock.Mock, mock_get: mock.Mock, mock_put: mock.Mock, ) -> None: """No room creation when join fails with 400 (or other non-404). A join failure can occur for many reasons, such as auth failure or invite required. In these cases, attempting to create the room is incorrect and produces misleading follow-up errors like M_ROOM_IN_USE. """ def _resp(status_code: int, content: Union[str, bytes]) -> mock.Mock: r = _Response() r.status_code = status_code if isinstance(content, str): r.content = content.encode("utf-8") else: r.content = content return r login = dumps( { "access_token": "t", "home_server": "hs", "user_id": "@u:hs", } ) # POST sequence: # 1. /login succeeds # 2. /join/#backup fails with 400 and an empty body # 3. /logout succeeds during object cleanup remembering token mock_post.side_effect = [ _resp(requests.codes.ok, login), _resp(requests.codes.bad_request, b""), _resp(requests.codes.ok, dumps({})), ] # No other requests should be needed (no createRoom, no directory lookup) mock_get.return_value = _resp(requests.codes.internal_server_error, b"") mock_put.return_value = _resp(requests.codes.ok, dumps({})) ap = Apprise() ap.add("matrixs://user:pass@matrix.vip/#backup?discovery=no") assert bool(ap.notify(title="t", body="b")) is False # Cleanup explicitly to ensure __del__ executes while mocks are active. import gc del ap gc.collect() # login + join + logout assert mock_post.call_count == 3 assert mock_get.call_count == 0 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_room_create_on_not_found_join( mock_post: mock.Mock, mock_get: mock.Mock, mock_put: mock.Mock, ) -> None: """Attempt room creation only when join reports 404 / M_NOT_FOUND.""" def _resp(status_code: int, content: Union[str, bytes]) -> mock.Mock: r = _Response() r.status_code = status_code if isinstance(content, str): r.content = content.encode("utf-8") else: r.content = content return r login = dumps( { "access_token": "t", "home_server": "hs", "user_id": "@u:hs", } ) # POST sequence: # 1. /login succeeds # 2. /join/#backup returns 404 not found # 3. /createRoom returns alias in use, triggering alias resolution # 4. /logout succeeds during object cleanup mock_post.side_effect = [ _resp(requests.codes.ok, login), _resp( requests.codes.not_found, dumps({"errcode": "M_NOT_FOUND", "error": "Not found"}), ), _resp( requests.codes.bad_request, dumps( { "errcode": "M_ROOM_IN_USE", "error": "Room alias already taken", } ), ), _resp(requests.codes.ok, dumps({})), ] # Directory lookup returns the room id mock_get.return_value = _resp( requests.codes.ok, dumps({"room_id": "!abc123:matrix.vip"}), ) # Sending the message succeeds (v3 uses PUT) mock_put.return_value = _resp(requests.codes.ok, dumps({})) ap = Apprise() ap.add("matrixs://user:pass@matrix.vip/#backup?discovery=no") assert bool(ap.notify(title="t", body="b")) is True import gc del ap gc.collect() # login + join + createRoom + logout assert mock_post.call_count == 4 assert mock_get.call_count == 1 assert mock_put.call_count == 1 # --------------------------------------------------------------------------- # E2EE unit tests (require the cryptography package) # --------------------------------------------------------------------------- try: import cryptography # noqa: F401 CRYPTOGRAPHY_AVAILABLE = True except ImportError: CRYPTOGRAPHY_AVAILABLE = False @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_room_create_e2ee_initial_state( mock_post, mock_get, mock_put ): """_room_create with e2ee=True/secure=True embeds m.room.encryption in initial_state and pre-seeds the room encryption cache.""" response_obj = { "access_token": "tok", "user_id": "@u:localhost", "home_server": "localhost", "room_id": "!abc123", } r = _Response() r.status_code = requests.codes.ok r.content = dumps(response_obj).encode() mock_post.return_value = r mock_get.return_value = r mock_put.return_value = r obj = NotifyMatrix( host="localhost", user="u", password="p", e2ee=True, secure=True, discovery=False, ) obj.access_token = "tok" obj.home_server = "localhost" captured_payloads = [] _orig_fetch = obj._fetch def _capture_fetch(path, payload=None, **kw): if path == "/createRoom": captured_payloads.append(payload) return _orig_fetch(path, payload=payload, **kw) obj._fetch = _capture_fetch room_id = obj._room_create("e2ee_room") assert room_id == response_obj["room_id"] assert len(captured_payloads) == 1 init_state = captured_payloads[0].get("initial_state", []) assert any(s.get("type") == "m.room.encryption" for s in init_state) assert obj.store.get("e2ee_room_enc_{}".format(room_id)) is True def _rand_b64_32(): """32 bytes of random data encoded as unpadded base64.""" import base64 return base64.b64encode(os.urandom(32)).decode() def _make_signed_otk(account, user_id, device_id, key_b64=None): """Return a signed_curve25519 OTK dict signed by *account*. Produces {"key": ..., "signatures": {user_id: {"ed25519:DEV": ...}}} suitable for inclusion in a /keys/claim mock response. """ from apprise.plugins.matrix.e2ee import _canonical_json if key_b64 is None: key_b64 = _rand_b64_32() payload = {"key": key_b64} sig = account.sign(_canonical_json(payload)) payload["signatures"] = {user_id: {"ed25519:{}".format(device_id): sig}} return payload @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_helpers(): """e2ee helper functions.""" from apprise.plugins.matrix.e2ee import ( _b64dec, _b64enc, _canonical_json, _hmac_sha256, _pb_bytes, _pb_varint_field, _varint, ) # _b64enc / _b64dec round-trip raw = b"\x00\x01\x02\x03" assert _b64dec(_b64enc(raw)) == raw # _b64dec tolerates missing padding and URL-safe chars # "AA==" -> decode "AA" (2 chars, pad=2) assert _b64dec("AA") == b"\x00" # URL-safe chars are normalised assert _b64dec("AA--") == _b64dec("AA++") # _hmac_sha256 returns 32 bytes mac = _hmac_sha256(b"\x00" * 32, b"data") assert isinstance(mac, bytes) and len(mac) == 32 # _varint: n==0 -> single zero byte assert _varint(0) == b"\x00" # n>0 is encoded as multi-byte varint assert _varint(128) == b"\x80\x01" # _pb_bytes / _pb_varint_field produce bytes pb = _pb_bytes(1, b"hello") assert isinstance(pb, bytes) and len(pb) > 5 pv = _pb_varint_field(2, 42) assert isinstance(pv, bytes) # _canonical_json produces sorted-key JSON bytes data = {"z": 1, "a": 2} out = _canonical_json(data) assert out.startswith(b'{"a"') @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_responder_roundtrip(): """The custom SAS responder interoperates without libolm.""" import hashlib from cryptography.hazmat.primitives.asymmetric.x25519 import ( X25519PrivateKey, X25519PublicKey, ) from cryptography.hazmat.primitives.serialization import ( Encoding, PublicFormat, ) from apprise.plugins.matrix.e2ee import ( MatrixOlmAccount, MatrixSASVerification, _b64dec, _b64enc, _canonical_json, _hkdf_sha256, _hmac_sha256, ) own_user = peer_user = "@u:h" own_device = "APPRISE" peer_device = "ELEMENT" transaction_id = "sas-transaction" start = { "from_device": peer_device, "transaction_id": transaction_id, "method": "m.sas.v1", "key_agreement_protocols": ["curve25519-hkdf-sha256"], "hashes": ["sha256"], "message_authentication_codes": ["hkdf-hmac-sha256.v2"], "short_authentication_string": ["decimal", "emoji"], } sas = MatrixSASVerification( own_user, own_device, peer_user, peer_device, start, ) accept = sas.accept_content() commitment = hashlib.sha256( sas.public_key.encode("ascii") + _canonical_json(start) ).digest() assert accept["commitment"] == _b64enc(commitment) assert accept["message_authentication_code"] == ("hkdf-hmac-sha256.v2") peer_private = X25519PrivateKey.generate() peer_public = _b64enc( peer_private.public_key().public_bytes(Encoding.Raw, PublicFormat.Raw) ) sas.receive_key(peer_public) assert sas.key_content()["key"] == sas.public_key shared_secret = peer_private.exchange( X25519PublicKey.from_public_bytes(_b64dec(sas.public_key)) ) def peer_mac(value, key_id): info = ( "MATRIX_KEY_VERIFICATION_MAC" + peer_user + peer_device + own_user + own_device + transaction_id + key_id ).encode("utf-8") key = _hkdf_sha256(shared_secret, 32, salt=None, info=info) return _b64enc(_hmac_sha256(key, value.encode("utf-8"))) own_account = MatrixOlmAccount() own_mac = sas.mac_content(own_account.signing_key) assert "ed25519:APPRISE" in own_mac["mac"] peer_account = MatrixOlmAccount() peer_key_id = "ed25519:{}".format(peer_device) peer_content = { "transaction_id": transaction_id, "mac": {peer_key_id: peer_mac(peer_account.signing_key, peer_key_id)}, "keys": peer_mac(peer_key_id, "KEY_IDS"), } tampered = dict(peer_content) tampered["keys"] = "invalid" with pytest.raises(ValueError, match="key list MAC"): sas.verify_peer_mac(tampered, {peer_key_id: peer_account.signing_key}) assert sas.verify_peer_mac( peer_content, {peer_key_id: peer_account.signing_key} ) assert sas.state == "verified" @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_init_validation(): """MatrixSASVerification rejects malformed SAS start content.""" from apprise.plugins.matrix.e2ee import MatrixSASVerification own_user = peer_user = "@u:h" own_device, peer_device = "APPRISE", "ELEMENT" base_start = { "from_device": peer_device, "transaction_id": "tx", "method": "m.sas.v1", "key_agreement_protocols": ["curve25519-hkdf-sha256"], "hashes": ["sha256"], "message_authentication_codes": ["hkdf-hmac-sha256.v2"], "short_authentication_string": ["decimal", "emoji"], } def build(**overrides): content = dict(base_start) content.update(overrides) return content cases = ( ({"transaction_id": ""}, "Missing SAS transaction"), ({"from_device": "OTHER"}, "does not match request device"), ({"method": "m.other.v1"}, "Unsupported SAS verification method"), ( {"key_agreement_protocols": ["other"]}, "Unsupported SAS key agreement", ), ({"hashes": ["sha1"]}, "Unsupported SAS hash"), ( {"message_authentication_codes": ["other"]}, "Unsupported SAS MAC", ), ( {"short_authentication_string": ["emoji"]}, "does not offer decimal", ), ) for overrides, match in cases: with pytest.raises(ValueError, match=match): MatrixSASVerification( own_user, own_device, peer_user, peer_device, build(**overrides), ) # A peer device ID is required with the transaction ID. with pytest.raises(ValueError, match="Missing SAS transaction"): MatrixSASVerification(own_user, own_device, peer_user, "", base_start) def _make_sas(own_user, own_device, peer_user, peer_device, transaction_id): """Build a fresh MatrixSASVerification responder for *transaction_id*.""" from apprise.plugins.matrix.e2ee import MatrixSASVerification start = { "from_device": peer_device, "transaction_id": transaction_id, "method": "m.sas.v1", "key_agreement_protocols": ["curve25519-hkdf-sha256"], "hashes": ["sha256"], "message_authentication_codes": ["hkdf-hmac-sha256.v2"], "short_authentication_string": ["decimal", "emoji"], } return MatrixSASVerification( own_user, own_device, peer_user, peer_device, start ) def _completed_sas_pair( own_user, own_device, peer_user, peer_device, transaction_id, own_account, peer_account, ): """Build a responder and matching peer at the MAC stage. The returned helper creates additional valid MAC values for edge cases. """ from cryptography.hazmat.primitives.asymmetric.x25519 import ( X25519PrivateKey, X25519PublicKey, ) from cryptography.hazmat.primitives.serialization import ( Encoding, PublicFormat, ) from apprise.plugins.matrix.e2ee import ( _b64dec, _b64enc, _hkdf_sha256, _hmac_sha256, ) sas = _make_sas( own_user, own_device, peer_user, peer_device, transaction_id ) sas.accept_content() peer_private = X25519PrivateKey.generate() peer_public = _b64enc( peer_private.public_key().public_bytes(Encoding.Raw, PublicFormat.Raw) ) sas.receive_key(peer_public) sas.mac_content(own_account.signing_key) shared_secret = peer_private.exchange( X25519PublicKey.from_public_bytes(_b64dec(sas.public_key)) ) def peer_mac(value, key_id): info = ( "MATRIX_KEY_VERIFICATION_MAC" + peer_user + peer_device + own_user + own_device + transaction_id + key_id ).encode("utf-8") key = _hkdf_sha256(shared_secret, 32, salt=None, info=info) return _b64enc(_hmac_sha256(key, value.encode("utf-8"))) key_id = "ed25519:{}".format(peer_device) content = { "transaction_id": transaction_id, "mac": {key_id: peer_mac(peer_account.signing_key, key_id)}, "keys": peer_mac(key_id, "KEY_IDS"), } peer_keys = {key_id: peer_account.signing_key} return sas, content, peer_keys, peer_mac @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_verification_state_guards(): """Out-of-order calls against MatrixSASVerification are rejected.""" own_user = peer_user = "@u:h" own_device, peer_device = "APPRISE", "ELEMENT" sas = _make_sas(own_user, own_device, peer_user, peer_device, "tx") with pytest.raises(ValueError, match="peer key has not been received"): sas.key_content() with pytest.raises(ValueError, match="peer key has not been received"): sas.mac_content("sig") with pytest.raises(ValueError, match="Unexpected SAS key event"): sas.receive_key("AAAA") sas.accept_content() with pytest.raises(ValueError, match="already accepted"): sas.accept_content() with pytest.raises(ValueError, match="Unexpected SAS MAC event"): sas.verify_peer_mac({}, {}) with pytest.raises( ValueError, match="shared secret has not been established" ): sas.decimal_sas() @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_invalid_peer_key(): """Reject malformed peer keys before decoding them.""" own_user = peer_user = "@u:h" own_device, peer_device = "APPRISE", "ELEMENT" for bad_key in ("", "A" * 513, None, 12345): sas = _make_sas(own_user, own_device, peer_user, peer_device, "tx") sas.accept_content() with pytest.raises(ValueError, match="missing or malformed"): sas.receive_key(bad_key) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_mac_requires_secret(): """_calculate_mac refuses to run before the SAS key exchange.""" sas = _make_sas("@u:h", "APPRISE", "@u:h", "ELEMENT", "tx") # Reach the MAC guard without creating a shared secret. sas.state = "key_received" with pytest.raises( ValueError, match="shared secret has not been established" ): sas._calculate_mac("v", "kid", "@u:h", "APPRISE", "@u:h", "ELEMENT") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_decimal_sas(): """decimal_sas derives the 3-number code the Matrix spec defines.""" from cryptography.hazmat.primitives.asymmetric.x25519 import ( X25519PrivateKey, ) from cryptography.hazmat.primitives.serialization import ( Encoding, PublicFormat, ) from apprise.plugins.matrix.e2ee import _b64enc sas = _make_sas("@u:h", "APPRISE", "@u:h", "ELEMENT", "tx") sas.accept_content() peer_private = X25519PrivateKey.generate() peer_public = _b64enc( peer_private.public_key().public_bytes(Encoding.Raw, PublicFormat.Raw) ) sas.receive_key(peer_public) # Check the decimal bit splitting independently with five known bytes. with mock.patch( "apprise.plugins.matrix.e2ee._hkdf_sha256", return_value=bytes([0xFF] * 5), ) as hkdf: # All 40 bits set -> each 13-bit group is 8191 -> +1000 = 9191. assert sas.decimal_sas() == (9191, 9191, 9191) assert hkdf.call_args.args[1] == 5 with mock.patch( "apprise.plugins.matrix.e2ee._hkdf_sha256", return_value=bytes(5), ): # All bits clear -> each 13-bit group is 0 -> +1000 = 1000. assert sas.decimal_sas() == (1000, 1000, 1000) with mock.patch( "apprise.plugins.matrix.e2ee._hkdf_sha256", return_value=b"\x00\x01\x00\x00\x00", ) as hkdf: # Cross-check against the spec's byte-by-byte formula. b0, b1, b2, b3, b4 = b"\x00\x01\x00\x00\x00" expected = ( ((b0 << 5) | (b1 >> 3)) + 1000, (((b1 & 0x7) << 10) | (b2 << 2) | (b3 >> 6)) + 1000, (((b3 & 0x3F) << 7) | (b4 >> 1)) + 1000, ) assert sas.decimal_sas() == expected # Require the spec's exact separators and initiator-first field order. info = hkdf.call_args.kwargs["info"] assert info == ( "MATRIX_KEY_VERIFICATION_SAS|" "@u:h|ELEMENT|" + peer_public + "|" "@u:h|APPRISE|" + sas.public_key + "|" "tx" ).encode("utf-8") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_verify_peer_mac_errors(): """verify_peer_mac rejects tampered or incomplete MAC content.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount own_user = peer_user = "@u:h" own_device, peer_device = "APPRISE", "ELEMENT" own_account = MatrixOlmAccount() peer_account = MatrixOlmAccount() def build(): return _completed_sas_pair( own_user, own_device, peer_user, peer_device, "tx", own_account, peer_account, ) # Wrong transaction id sas, content, peer_keys, _mac = build() content["transaction_id"] = "other" with pytest.raises(ValueError, match="transaction does not match"): sas.verify_peer_mac(content, peer_keys) # Missing/empty mac dict sas, content, peer_keys, _mac = build() content["mac"] = {} with pytest.raises(ValueError, match="contains no keys"): sas.verify_peer_mac(content, peer_keys) # mac dict present but missing the peer's own device key id sas, content, peer_keys, _mac = build() content["mac"] = {"ed25519:someone-else": "AAAA"} with pytest.raises(ValueError, match="omits the peer device key"): sas.verify_peer_mac(content, peer_keys) # An oversized key list is rejected before it is ever sorted/hashed. sas, content, peer_keys, _mac = build() device_key_id = next(iter(content["mac"])) content["mac"] = { device_key_id: content["mac"][device_key_id], **{ f"ed25519:junk-{i}": "AAAA" for i in range(matrix_sas.MatrixSASVerification.MAX_MAC_KEYS) }, } with pytest.raises(ValueError, match="too many keys"): sas.verify_peer_mac(content, peer_keys) # peer_keys is not a mapping at all (TypeError branch) sas, content, peer_keys, _mac = build() with pytest.raises(ValueError, match="unknown key"): sas.verify_peer_mac(content, None) # Reference an unknown key while keeping the key-list MAC valid. sas, content, peer_keys, peer_mac = build() device_key_id = next(iter(content["mac"])) unknown_key_id = "ed25519:unknown-device" macs = { device_key_id: content["mac"][device_key_id], unknown_key_id: peer_mac("garbage", unknown_key_id), } tampered = { "transaction_id": "tx", "mac": macs, "keys": peer_mac(",".join(sorted(macs)), "KEY_IDS"), } with pytest.raises(ValueError, match="unknown key"): sas.verify_peer_mac(tampered, peer_keys) # Tampered per-device MAC value sas, content, peer_keys, _mac = build() content["mac"][device_key_id] = "tampered-value" with pytest.raises(ValueError, match="device key MAC does not match"): sas.verify_peer_mac(content, peer_keys) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_mac_field_limits(): """Reject oversized MAC fields before processing them.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount own_user = peer_user = "@u:h" own_device, peer_device = "APPRISE", "ELEMENT" own_account = MatrixOlmAccount() peer_account = MatrixOlmAccount() def build(): return _completed_sas_pair( own_user, own_device, peer_user, peer_device, "tx", own_account, peer_account, ) # Oversized MAC value for an otherwise-valid key ID. sas, content, peer_keys, _mac = build() device_key_id = next(iter(content["mac"])) content["mac"][device_key_id] = "A" * 513 with pytest.raises(ValueError, match="malformed entry"): sas.verify_peer_mac(content, peer_keys) # Oversized key ID replacing the real one. sas, content, peer_keys, _mac = build() device_key_id = next(iter(content["mac"])) content["mac"] = {"ed25519:" + "A" * 513: content["mac"][device_key_id]} with pytest.raises(ValueError, match="malformed entry"): sas.verify_peer_mac(content, peer_keys) # Oversized aggregate key-list MAC. sas, content, peer_keys, _mac = build() content["keys"] = "A" * 513 with pytest.raises(ValueError, match="malformed key list MAC"): sas.verify_peer_mac(content, peer_keys) # Missing key-list MAC. sas, content, peer_keys, _mac = build() del content["keys"] with pytest.raises(ValueError, match="malformed key list MAC"): sas.verify_peer_mac(content, peer_keys) # An oversized public key returned for the peer's device. sas, content, peer_keys, _mac = build() device_key_id = next(iter(content["mac"])) peer_keys = dict(peer_keys, **{device_key_id: "A" * 513}) with pytest.raises(ValueError, match="malformed key"): sas.verify_peer_mac(content, peer_keys) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_binding_key(): """_e2ee_binding_key reflects the current device/account identity.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], secure=True ) assert obj._e2ee_binding_key() == "" obj._e2ee_account = MatrixOlmAccount() obj.user_id = "@u:h" obj.device_id = "APPRISE" assert obj._e2ee_binding_key() == "{}|{}|{}|{}".format( "@u:h", "APPRISE", obj._e2ee_account.identity_key, obj._e2ee_account.signing_key, ) def test_plugin_matrix_autoverify_implies_e2ee(): """autoverify=yes implies e2ee=yes unless e2ee is set explicitly.""" # autoverify alone turns e2ee on without spelling it out. obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], secure=True, autoverify=True, ) assert obj.autoverify is True assert obj.e2ee is True # An explicit e2ee=no is still honored even with autoverify=yes. obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], secure=True, autoverify=True, e2ee=False, ) assert obj.autoverify is True assert obj.e2ee is False # An explicit e2ee=yes alongside autoverify=yes is unaffected. obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], secure=True, autoverify=True, e2ee=True, ) assert obj.autoverify is True assert obj.e2ee is True # Without autoverify, e2ee keeps its own (already-True) default. obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], secure=True ) assert obj.autoverify is False assert obj.e2ee is True # The implied e2ee=yes survives a URL round trip: autoverify=yes is # emitted, e2ee=no is not, and re-parsing yields the same identity. obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], secure=True, autoverify=True, ) url = obj.url() assert "autoverify=yes" in url assert "e2ee=no" not in url results = NotifyMatrix.parse_url(url) obj2 = NotifyMatrix(**results) assert obj2.autoverify is True assert obj2.e2ee is True assert obj.url_identifier == obj2.url_identifier def _matrix_sas_plugin(user_id="@u:h", device_id="APPRISE"): """A NotifyMatrix instance with a ready-to-use E2EE identity.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, autoverify=True, secure=True, ) obj.user_id = user_id obj.device_id = device_id obj._e2ee_account = MatrixOlmAccount() return obj @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_send_event_and_cancel(): """_send_event builds the /sendToDevice request; _cancel/_fail no-op without an active transaction.""" import re obj = _matrix_sas_plugin() verifier = matrix_sas.MatrixSASAutoVerifier(obj) assert verifier._cancel("m.timeout", "x") is False assert verifier._fail("m.timeout", "x") is False with mock.patch.object( obj, "_fetch", return_value=(True, {}, requests.codes.ok) ) as fetch: assert ( verifier._send_event( "m.key.verification.ready", "@peer:h", "DEV", {"a": 1} ) is True ) args, kwargs = fetch.call_args assert re.match(r"^/sendToDevice/[^/]+/[0-9a-f]{32}$", args[0]) assert kwargs["method"] == "PUT" assert kwargs["payload"] == {"messages": {"@peer:h": {"DEV": {"a": 1}}}} # To-device sends use the same bounded verification budget as sync. assert kwargs["timeout"][0] > 0 assert kwargs["timeout"][1] > 0 assert 0 < kwargs["max_retry_wait"] <= obj.default_autoverify_timeout_sec with mock.patch.object(obj, "_fetch", return_value=(False, {}, 500)): assert ( verifier._send_event("m.key.verification.ready", "u", "d", {}) is False ) verifier.active = { "transaction_id": "tx", "user_id": "@peer:h", "device_id": "DEV", } with mock.patch.object( obj, "_fetch", return_value=(True, {}, requests.codes.ok) ) as fetch: assert verifier._cancel("m.timeout", "reason") is True _, kwargs = fetch.call_args assert kwargs["payload"] == { "messages": { "@peer:h": { "DEV": { "transaction_id": "tx", "code": "m.timeout", "reason": "reason", } } } } assert verifier._fail("m.timeout", "reason") is False @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_peer_keys_branches(): """_peer_keys covers every failure and success path.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount obj = _matrix_sas_plugin() verifier = matrix_sas.MatrixSASAutoVerifier(obj) with mock.patch.object( obj, "_fetch", return_value=(False, {}, 500) ) as fetch: assert verifier._peer_keys("@peer:h", "DEV") is None # Key queries use the same bounded verification budget. kwargs = fetch.call_args.kwargs assert kwargs["timeout"][0] > 0 assert kwargs["timeout"][1] > 0 assert 0 < kwargs["max_retry_wait"] <= obj.default_autoverify_timeout_sec with mock.patch.object( obj, "_fetch", return_value=(True, [], requests.codes.ok) ): assert verifier._peer_keys("@peer:h", "DEV") is None with mock.patch.object( obj, "_fetch", return_value=(True, {"device_keys": {}}, requests.codes.ok), ): assert verifier._peer_keys("@peer:h", "DEV") is None # Null server fields are malformed but must not crash verification. for malformed_response in ( {"device_keys": None}, {"device_keys": {"@peer:h": None}}, {"device_keys": "not-a-dict"}, ): with mock.patch.object( obj, "_fetch", return_value=(True, malformed_response, requests.codes.ok), ): assert verifier._peer_keys("@peer:h", "DEV") is None bad_device = { "user_id": "@peer:h", "device_id": "DEV", "keys": {}, "signatures": {}, } with mock.patch.object( obj, "_fetch", return_value=( True, {"device_keys": {"@peer:h": {"DEV": bad_device}}}, requests.codes.ok, ), ): assert verifier._peer_keys("@peer:h", "DEV") is None peer_account = MatrixOlmAccount() device = peer_account.device_keys_payload("@peer:h", "DEV") response = { "device_keys": {"@peer:h": {"DEV": device}}, "master_keys": {"@peer:h": {"keys": {"ed25519:master": "abc"}}}, } with mock.patch.object( obj, "_fetch", return_value=(True, response, requests.codes.ok) ): keys = verifier._peer_keys("@peer:h", "DEV") assert keys["ed25519:master"] == "abc" assert "ed25519:DEV" in keys # An implausibly large keys map is rejected rather than copied; a real # device or master-key object only ever advertises a couple of keys. oversized_keys = { f"curve25519:{i}": "k" for i in range(matrix_sas.MAX_DEVICE_KEYS + 1) } oversized_device = dict(device, keys=oversized_keys) with ( mock.patch.object(matrix_sas, "verify_device_keys", return_value=True), mock.patch.object( obj, "_fetch", return_value=( True, {"device_keys": {"@peer:h": {"DEV": oversized_device}}}, requests.codes.ok, ), ), ): keys = verifier._peer_keys("@peer:h", "DEV") assert keys == {} oversized_master = dict( response, master_keys={"@peer:h": {"keys": oversized_keys}}, ) with mock.patch.object( obj, "_fetch", return_value=(True, oversized_master, requests.codes.ok) ): keys = verifier._peer_keys("@peer:h", "DEV") assert "curve25519:0" not in keys assert "ed25519:DEV" in keys malformed = dict(response, master_keys={"@peer:h": "not-a-dict"}) with mock.patch.object( obj, "_fetch", return_value=(True, malformed, requests.codes.ok) ): keys = verifier._peer_keys("@peer:h", "DEV") assert "ed25519:master" not in keys # Treat null or malformed optional master keys as absent. for master_keys_value in (None, {"@peer:h": {"keys": None}}): with mock.patch.object( obj, "_fetch", return_value=( True, dict(response, master_keys=master_keys_value), requests.codes.ok, ), ): keys = verifier._peer_keys("@peer:h", "DEV") assert "ed25519:master" not in keys assert "ed25519:DEV" in keys @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_handle_request_branches(): """_handle_request covers ignore/fail/success outcomes.""" from time import time obj = _matrix_sas_plugin() verifier = matrix_sas.MatrixSASAutoVerifier(obj) now_ms = int(time() * 1000) assert verifier._handle_request("@u:h", {"timestamp": "nope"}) is None stale = { "timestamp": now_ms - 20 * 60 * 1000, "transaction_id": "tx", "from_device": "DEV", "methods": ["m.sas.v1"], } assert verifier._handle_request("@u:h", stale) is None future = dict(stale, timestamp=now_ms + 20 * 60 * 1000) assert verifier._handle_request("@u:h", future) is None missing_tx = { "timestamp": now_ms, "from_device": "DEV", "methods": ["m.sas.v1"], } assert verifier._handle_request("@u:h", missing_tx) is None missing_device = { "timestamp": now_ms, "transaction_id": "tx", "methods": ["m.sas.v1"], } assert verifier._handle_request("@u:h", missing_device) is None wrong_method = { "timestamp": now_ms, "transaction_id": "tx", "from_device": "DEV", "methods": ["other"], } assert verifier._handle_request("@u:h", wrong_method) is None # A present-but-null "methods" field must not crash the "in" check. null_methods = { "timestamp": now_ms, "transaction_id": "tx", "from_device": "DEV", "methods": None, } assert verifier._handle_request("@u:h", null_methods) is None # Identifiers are validated by type and length, not just truthiness. for bad_transaction_id, bad_from_device in ( (["tx"], "DEV"), ("x" * (matrix_sas.MAX_ID_LEN + 1), "DEV"), ("tx", 12345), ("tx", "x" * (matrix_sas.MAX_ID_LEN + 1)), ): bad_ids = { "timestamp": now_ms, "transaction_id": bad_transaction_id, "from_device": bad_from_device, "methods": ["m.sas.v1"], } assert verifier._handle_request("@u:h", bad_ids) is None assert verifier.active is None valid = { "timestamp": now_ms, "transaction_id": "tx", "from_device": "DEV", "methods": ["m.sas.v1"], } with mock.patch.object(verifier, "_peer_keys", return_value=None): assert verifier._handle_request("@u:h", valid) is False assert verifier.active is None # Reject a new request without changing the active transaction. active = { "transaction_id": "other", "user_id": "@u:h", "device_id": "OLD", } verifier.active = active with mock.patch.object(verifier, "_send_event", return_value=True) as se: assert verifier._handle_request("@u:h", valid) is None se.assert_called_once_with( matrix_sas.EVENT_CANCEL, "@u:h", "DEV", { "transaction_id": "tx", "code": "m.unexpected_message", "reason": "Another verification is already active", }, ) assert verifier.active is active verifier.active = None # An exact replay of the request that started the active transaction # is ignored silently -- it must not cancel our own exchange. active = { "transaction_id": "tx", "user_id": "@u:h", "device_id": "DEV", } verifier.active = active with mock.patch.object(verifier, "_send_event") as se: assert verifier._handle_request("@u:h", valid) is None se.assert_not_called() assert verifier.active is active verifier.active = None # A second transaction from the same device cancels both attempts. active = { "transaction_id": "other", "user_id": "@u:h", "device_id": "DEV", } verifier.active = active with ( mock.patch.object(verifier, "_cancel", return_value=True) as cancel, mock.patch.object(verifier, "_send_event", return_value=True) as se, ): assert verifier._handle_request("@u:h", valid) is None cancel.assert_called_once_with( "m.unexpected_message", "A newer verification request superseded this one", ) se.assert_called_once_with( matrix_sas.EVENT_CANCEL, "@u:h", "DEV", { "transaction_id": "tx", "code": "m.unexpected_message", "reason": "Another verification is already active", }, ) assert verifier.active is None with ( mock.patch.object( verifier, "_peer_keys", return_value={"ed25519:DEV": "k"} ), mock.patch.object(verifier, "_send_event", return_value=False) as se, ): assert verifier._handle_request("@u:h", valid) is False se.assert_called_once() verifier.active = None with ( mock.patch.object( verifier, "_peer_keys", return_value={"ed25519:DEV": "k"} ), mock.patch.object(verifier, "_send_event", return_value=True), ): assert verifier._handle_request("@u:h", valid) is None assert verifier.active["transaction_id"] == "tx" @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_handle_cancel(): """_handle_cancel always reports failure.""" obj = _matrix_sas_plugin() verifier = matrix_sas.MatrixSASAutoVerifier(obj) assert verifier._handle_cancel({"reason": "user declined"}) is False assert verifier._handle_cancel({}) is False @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_handle_start_branches(): """_handle_start covers the invalid-params, send-failure, and success outcomes.""" obj = _matrix_sas_plugin() verifier = matrix_sas.MatrixSASAutoVerifier(obj) verifier.active = { "transaction_id": "tx", "user_id": "@peer:h", "device_id": "DEV", } with mock.patch.object(verifier, "_cancel", return_value=True) as cancel: assert ( verifier._handle_start("@peer:h", {"transaction_id": "tx"}) is False ) cancel.assert_called_once_with( "m.unknown_method", "Unsupported SAS verification parameters" ) valid_start = { "from_device": "DEV", "transaction_id": "tx", "method": "m.sas.v1", "key_agreement_protocols": ["curve25519-hkdf-sha256"], "hashes": ["sha256"], "message_authentication_codes": ["hkdf-hmac-sha256.v2"], "short_authentication_string": ["decimal", "emoji"], } with mock.patch.object(verifier, "_send_event", return_value=False): assert verifier._handle_start("@peer:h", valid_start) is False assert verifier.active["sas"] is not None verifier.active["sas"] = None with mock.patch.object(verifier, "_send_event", return_value=True): assert verifier._handle_start("@peer:h", valid_start) is None assert verifier.active["sas"] is not None first_sas = verifier.active["sas"] # A second start must not replace an already committed temporary key. with mock.patch.object(verifier, "_cancel", return_value=True) as cancel: assert verifier._handle_start("@peer:h", valid_start) is False cancel.assert_called_once_with( "m.unexpected_message", "SAS verification was already started" ) assert verifier.active["sas"] is first_sas @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_handle_key_branches(): """_handle_key covers the invalid-key and send-failure/success paths.""" from cryptography.hazmat.primitives.asymmetric.x25519 import ( X25519PrivateKey, ) from cryptography.hazmat.primitives.serialization import ( Encoding, PublicFormat, ) from apprise.plugins.matrix.e2ee import _b64enc obj = _matrix_sas_plugin() verifier = matrix_sas.MatrixSASAutoVerifier(obj) verifier.active = { "transaction_id": "tx", "user_id": "@peer:h", "device_id": "DEV", } invalid_sas = _make_sas(obj.user_id, obj.device_id, "@peer:h", "DEV", "tx") invalid_sas.accept_content() with mock.patch.object(verifier, "_cancel", return_value=True) as cancel: assert ( verifier._handle_key( invalid_sas, "@peer:h", {"key": "not-valid-base64!!"} ) is False ) cancel.assert_called_once_with( "m.invalid_message", "Invalid SAS public key" ) # A present-but-null "key" field must not crash before it is rejected # as an invalid public key. null_key_sas = _make_sas( obj.user_id, obj.device_id, "@peer:h", "DEV", "tx" ) null_key_sas.accept_content() with mock.patch.object(verifier, "_cancel", return_value=True): assert ( verifier._handle_key(null_key_sas, "@peer:h", {"key": None}) is False ) peer_private = X25519PrivateKey.generate() peer_public = _b64enc( peer_private.public_key().public_bytes(Encoding.Raw, PublicFormat.Raw) ) key_fail_sas = _make_sas( obj.user_id, obj.device_id, "@peer:h", "DEV", "tx" ) key_fail_sas.accept_content() with mock.patch.object(verifier, "_send_event", return_value=False): assert ( verifier._handle_key(key_fail_sas, "@peer:h", {"key": peer_public}) is False ) mac_fail_sas = _make_sas( obj.user_id, obj.device_id, "@peer:h", "DEV", "tx" ) mac_fail_sas.accept_content() with mock.patch.object(verifier, "_send_event", side_effect=[True, False]): assert ( verifier._handle_key(mac_fail_sas, "@peer:h", {"key": peer_public}) is False ) ok_sas = _make_sas(obj.user_id, obj.device_id, "@peer:h", "DEV", "tx") ok_sas.accept_content() with ( mock.patch.object(verifier, "_send_event", return_value=True), mock.patch.object(obj.logger, "info") as log_info, ): assert ( verifier._handle_key(ok_sas, "@peer:h", {"key": peer_public}) is None ) assert any( "SAS code" in str(call.args[0]) for call in log_info.call_args_list ) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_handle_mac_branches(): """_handle_mac covers the mismatch, send-failure, and completion outcomes.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount obj = _matrix_sas_plugin() verifier = matrix_sas.MatrixSASAutoVerifier(obj) own_account = obj._e2ee_account peer_account = MatrixOlmAccount() def build_active(peer_done): sas, content, peer_keys, _mac = _completed_sas_pair( obj.user_id, obj.device_id, "@peer:h", "DEV", "tx", own_account, peer_account, ) verifier.active = { "transaction_id": "tx", "user_id": "@peer:h", "device_id": "DEV", "peer_keys": peer_keys, "peer_done": peer_done, } return sas, content sas, _content = build_active(False) with mock.patch.object(verifier, "_cancel", return_value=True) as cancel: assert ( verifier._handle_mac(sas, {"transaction_id": "tx", "mac": {}}) is False ) cancel.assert_called_once_with( "m.key_mismatch", "SAS device key MAC did not match" ) sas, content = build_active(False) with mock.patch.object(verifier, "_send_event", return_value=False): assert verifier._handle_mac(sas, content) is False sas, content = build_active(True) with ( mock.patch.object(verifier, "_send_event", return_value=True), mock.patch.object( matrix_sas, "store_verified_binding", return_value=True ) as store, ): assert verifier._handle_mac(sas, content) is True store.assert_called_once_with(obj) # A failed persistence write must not be reported as success. sas, content = build_active(True) with ( mock.patch.object(verifier, "_send_event", return_value=True), mock.patch.object( matrix_sas, "store_verified_binding", return_value=False ), ): assert verifier._handle_mac(sas, content) is False sas, content = build_active(False) with mock.patch.object(verifier, "_send_event", return_value=True): assert verifier._handle_mac(sas, content) is None @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_handle_done_branches(): """_handle_done only reports success once the local SAS is verified.""" obj = _matrix_sas_plugin() verifier = matrix_sas.MatrixSASAutoVerifier(obj) verifier.active = {"peer_done": False} class _FakeSAS: state = "mac_sent" fake = _FakeSAS() assert verifier._handle_done(fake) is None assert verifier.active["peer_done"] is True fake.state = "verified" verifier.active["peer_done"] = False with mock.patch.object( matrix_sas, "store_verified_binding", return_value=True ) as store: assert verifier._handle_done(fake) is True store.assert_called_once_with(obj) # A failed persistence write must not be reported as success. verifier.active["peer_done"] = False with mock.patch.object( matrix_sas, "store_verified_binding", return_value=False ): assert verifier._handle_done(fake) is False @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_handle_event_dispatch(): """_handle_event routes every to-device event type correctly.""" obj = _matrix_sas_plugin() verifier = matrix_sas.MatrixSASAutoVerifier(obj) assert verifier._handle_event("not-a-dict") is None assert verifier._handle_event({"type": "x", "sender": "@other:h"}) is None assert ( verifier._handle_event( {"type": "x", "sender": obj.user_id, "content": "nope"} ) is None ) with mock.patch.object(verifier, "_handle_request", return_value="R") as h: result = verifier._handle_event( { "type": matrix_sas.EVENT_REQUEST, "sender": obj.user_id, "content": {"a": 1}, } ) assert result == "R" h.assert_called_once_with(obj.user_id, {"a": 1}) # No active transaction yet -- every non-request event is ignored. assert ( verifier._handle_event( { "type": matrix_sas.EVENT_START, "sender": obj.user_id, "content": {"transaction_id": "tx"}, } ) is None ) verifier.active = { "transaction_id": "tx", "user_id": obj.user_id, "sas": None, } assert ( verifier._handle_event( { "type": matrix_sas.EVENT_START, "sender": obj.user_id, "content": {"transaction_id": "other"}, } ) is None ) # Defensive same-transaction-owner check. verifier.active["user_id"] = "@different:h" assert ( verifier._handle_event( { "type": matrix_sas.EVENT_START, "sender": obj.user_id, "content": {"transaction_id": "tx"}, } ) is None ) verifier.active["user_id"] = obj.user_id with mock.patch.object( verifier, "_handle_cancel", return_value=False ) as h: result = verifier._handle_event( { "type": matrix_sas.EVENT_CANCEL, "sender": obj.user_id, "content": {"transaction_id": "tx"}, } ) assert result is False h.assert_called_once() with mock.patch.object(verifier, "_handle_start", return_value=None) as h: result = verifier._handle_event( { "type": matrix_sas.EVENT_START, "sender": obj.user_id, "content": {"transaction_id": "tx"}, } ) assert result is None h.assert_called_once() with mock.patch.object(verifier, "_cancel", return_value=True) as cancel: result = verifier._handle_event( { "type": matrix_sas.EVENT_KEY, "sender": obj.user_id, "content": {"transaction_id": "tx"}, } ) assert result is False cancel.assert_called_once_with( "m.unexpected_message", "SAS verification has not started" ) verifier.active["sas"] = mock.Mock() for event_type, method in ( (matrix_sas.EVENT_KEY, "_handle_key"), (matrix_sas.EVENT_MAC, "_handle_mac"), (matrix_sas.EVENT_DONE, "_handle_done"), ): with mock.patch.object(verifier, method, return_value=None) as h: result = verifier._handle_event( { "type": event_type, "sender": obj.user_id, "content": {"transaction_id": "tx"}, } ) assert result is None h.assert_called_once() assert ( verifier._handle_event( { "type": "m.some.other.event", "sender": obj.user_id, "content": {"transaction_id": "tx"}, } ) is None ) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_run_loop_mechanics(): """run() covers sync failure, malformed responses, the since token, early termination, and timeout handling.""" obj = _matrix_sas_plugin() # Sync itself fails outright. verifier = matrix_sas.MatrixSASAutoVerifier(obj) with mock.patch.object(obj, "_fetch", return_value=(False, {}, 500)): assert verifier.run() is False # Sync succeeds but returns a non-dict body. verifier = matrix_sas.MatrixSASAutoVerifier(obj) with mock.patch.object( obj, "_fetch", return_value=(True, [], requests.codes.ok) ): assert verifier.run() is False # Reject missing or invalid sync tokens instead of polling repeatedly. for bad_batch_response in ( {"to_device": {"events": []}}, {"next_batch": None, "to_device": {"events": []}}, {"next_batch": "", "to_device": {"events": []}}, {"next_batch": 123, "to_device": {"events": []}}, ): verifier = matrix_sas.MatrixSASAutoVerifier(obj) with mock.patch.object( obj, "_fetch", return_value=(True, bad_batch_response, requests.codes.ok), ): assert verifier.run() is False # A non-list "events" value is treated as empty rather than crashing. verifier = matrix_sas.MatrixSASAutoVerifier(obj) responses = iter( [ ( True, { "next_batch": "1", "to_device": {"events": "not-a-list"}, }, requests.codes.ok, ), (False, {}, 500), ] ) with mock.patch.object( obj, "_fetch", side_effect=lambda *a, **k: next(responses) ): assert verifier.run() is False # A present-but-null "to_device" field is treated the same way. verifier = matrix_sas.MatrixSASAutoVerifier(obj) responses = iter( [ (True, {"next_batch": "1", "to_device": None}, requests.codes.ok), (False, {}, 500), ] ) with mock.patch.object( obj, "_fetch", side_effect=lambda *a, **k: next(responses) ): assert verifier.run() is False # Skip malformed events and continue polling. verifier = matrix_sas.MatrixSASAutoVerifier(obj) responses = iter( [ ( True, { "next_batch": "1", "to_device": {"events": [{"type": "x"}]}, }, requests.codes.ok, ), (False, {}, 500), ] ) with ( mock.patch.object( obj, "_fetch", side_effect=lambda *a, **k: next(responses) ), mock.patch.object( verifier, "_handle_event", side_effect=AttributeError("boom") ), ): assert verifier.run() is False # Stop processing an oversized event batch at the deadline. verifier = matrix_sas.MatrixSASAutoVerifier(obj) # Set the construction-time deadline directly for this mocked clock. verifier.deadline = 100 with ( mock.patch.object( obj, "_fetch", return_value=( True, { "next_batch": "1", "to_device": {"events": [{"type": "a"}, {"type": "b"}]}, }, requests.codes.ok, ), ), mock.patch.object(verifier, "_handle_event", return_value=None) as h, mock.patch( "apprise.plugins.matrix.sas.monotonic", # while-check, remaining_ms, _bounded_fetch's own check, # post-event-1 check, post-event-2 check (breaks), while # re-check (exits). side_effect=[1, 1, 1, 50, 150, 200], ), ): assert verifier.run() is False # The expired batch must not trigger another sync. assert h.call_count == 2 # A terminal per-event result short-circuits the loop. verifier = matrix_sas.MatrixSASAutoVerifier(obj) with ( mock.patch.object( obj, "_fetch", return_value=( True, { "next_batch": "1", "to_device": {"events": [{"type": "x"}]}, }, requests.codes.ok, ), ), mock.patch.object(verifier, "_handle_event", return_value=True), ): assert verifier.run() is True # The since token from one iteration is threaded into the next, and # the reduced long-poll timeout is what actually gets sent. verifier = matrix_sas.MatrixSASAutoVerifier(obj) calls = [] def fake_fetch(path, params=None, method="GET", **kwargs): calls.append(params) if len(calls) == 1: return ( True, {"next_batch": "abc", "to_device": {"events": []}}, requests.codes.ok, ) return (False, {}, 500) with mock.patch.object(obj, "_fetch", side_effect=fake_fetch): assert verifier.run() is False assert "since" not in calls[0] assert calls[0]["timeout"] == int(obj.socket_read_timeout * 1000 / 2) assert calls[1]["since"] == "abc" # Deadline reached with no active transaction -- fails closed with no # network call for the no-op cancellation. Configure it before creation. obj.default_autoverify_timeout_sec = 0 verifier = matrix_sas.MatrixSASAutoVerifier(obj) with mock.patch.object(obj, "_fetch") as fetch: assert verifier.run() is False fetch.assert_not_called() # Deadline reached with an active transaction -- the peer is told. verifier = matrix_sas.MatrixSASAutoVerifier(obj) verifier.active = { "transaction_id": "tx", "user_id": "@peer:h", "device_id": "DEV", } with mock.patch.object( obj, "_fetch", return_value=(True, {}, requests.codes.ok) ) as fetch: assert verifier.run() is False assert fetch.call_args[0][0].startswith( "/sendToDevice/m.key.verification.cancel/" ) obj.default_autoverify_timeout_sec = ( NotifyMatrix.default_autoverify_timeout_sec ) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_poll_timeout_margin(): """Sync uses bounded timeouts and does not mark the account online.""" import json obj = _matrix_sas_plugin() verifier = matrix_sas.MatrixSASAutoVerifier(obj) with mock.patch.object( obj, "_fetch", return_value=(False, {}, 500) ) as fetch: assert verifier.run() is False sent_params = fetch.call_args.kwargs["params"] assert sent_params["timeout"] == int(obj.socket_read_timeout * 1000 / 2) assert sent_params["timeout"] < obj.socket_read_timeout * 1000 assert sent_params["set_presence"] == "offline" # Both HTTP timeouts use the sanitized polling budget. fetch_timeout = fetch.call_args.kwargs["timeout"] expected = pytest.approx(sent_params["timeout"] / 1000 + 2) assert fetch_timeout[0] == expected assert fetch_timeout[1] == expected assert fetch_timeout[1] < obj.socket_read_timeout * 10 # sane, not huge # A 429 mid-poll must not be allowed to wait out the server's own # arbitrary requested delay past what remains of this attempt. assert fetch.call_args.kwargs["max_retry_wait"] == pytest.approx( obj.default_autoverify_timeout_sec, abs=1 ) # Check filter behavior, including that any limits are positive. parsed_filter = json.loads(sent_params["filter"]) assert parsed_filter["room"]["rooms"] == [] assert parsed_filter["presence"]["types"] == [] assert parsed_filter["account_data"]["types"] == [] def _walk_limits(node): if isinstance(node, dict): if "limit" in node: yield node["limit"] for value in node.values(): yield from _walk_limits(value) elif isinstance(node, list): for value in node: yield from _walk_limits(value) assert all(limit > 0 for limit in _walk_limits(parsed_filter)) # A custom ?rto= override scales the poll with it. custom = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], rto=10.0 ) custom.user_id = obj.user_id custom.device_id = obj.device_id custom._e2ee_account = obj._e2ee_account custom_verifier = matrix_sas.MatrixSASAutoVerifier(custom) with mock.patch.object( custom, "_fetch", return_value=(False, {}, 500) ) as fetch: assert custom_verifier.run() is False assert fetch.call_args.kwargs["params"]["timeout"] == 5000 @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_expired_fetch(): """Do not start helper requests after the attempt deadline.""" obj = _matrix_sas_plugin() verifier = matrix_sas.MatrixSASAutoVerifier(obj) verifier.deadline = 0 # already expired relative to real monotonic time with mock.patch.object(obj, "_fetch") as fetch: result = verifier._bounded_fetch("/keys/query", payload={}) fetch.assert_not_called() assert result == (False, {}, None) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_expired_best_effort_fetch(): """A best-effort call still fires once the deadline has passed. The terminal cancellation notice is deliberately sent past the deadline -- that is exactly why the attempt is ending. """ obj = _matrix_sas_plugin() verifier = matrix_sas.MatrixSASAutoVerifier(obj) verifier.deadline = 0 # already expired relative to real monotonic time with mock.patch.object( obj, "_fetch", return_value=(True, {}, requests.codes.ok) ) as fetch: result = verifier._bounded_fetch( "/keys/query", payload={}, best_effort=True ) fetch.assert_called_once() assert result == (True, {}, requests.codes.ok) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_fetch_response_limit(): """Every SAS request bounds how large a response it will accept.""" obj = _matrix_sas_plugin() verifier = matrix_sas.MatrixSASAutoVerifier(obj) with mock.patch.object( obj, "_fetch", return_value=(True, {}, requests.codes.ok) ) as fetch: verifier._bounded_fetch("/keys/query", payload={}) assert ( fetch.call_args.kwargs["max_response_bytes"] == matrix_sas.MAX_RESPONSE_BYTES ) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_invalid_read_timeout(): """Invalid read timeouts use the safe polling fallback.""" obj = _matrix_sas_plugin() for bad_value in ( float("nan"), float("inf"), float("-inf"), 0.0, -5.0, # Finite on its own, but 1e306 * 1000 overflows to inf -- this # must be caught too, not just outright non-finite input. 1e306, ): obj.socket_read_timeout = bad_value assert ( matrix_sas._sync_poll_ms(obj) == matrix_sas.FALLBACK_SYNC_POLL_MS ) obj.socket_read_timeout = 8.0 assert matrix_sas._sync_poll_ms(obj) == 4000 # A tiny-but-valid timeout floors to a usable minimum poll rather # than truncating toward a near-zero, busy-looping value. obj.socket_read_timeout = 0.0001 assert matrix_sas._sync_poll_ms(obj) == matrix_sas.MIN_SYNC_POLL_MS # The full run() loop must not raise either. obj.socket_read_timeout = float("nan") verifier = matrix_sas.MatrixSASAutoVerifier(obj) with mock.patch.object( obj, "_fetch", return_value=(False, {}, 500) ) as fetch: assert verifier.run() is False assert ( fetch.call_args.kwargs["params"]["timeout"] == matrix_sas.FALLBACK_SYNC_POLL_MS ) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_poll_capped_by_deadline(): """The verification deadline also caps each poll request.""" obj = _matrix_sas_plugin() obj.socket_read_timeout = 600.0 # would otherwise poll for 300s obj.default_autoverify_timeout_sec = 5 verifier = matrix_sas.MatrixSASAutoVerifier(obj) with mock.patch.object( obj, "_fetch", return_value=(False, {}, 500) ) as fetch: assert verifier.run() is False sent_timeout = fetch.call_args.kwargs["params"]["timeout"] assert sent_timeout <= 5000 assert sent_timeout < int(obj.socket_read_timeout * 1000 / 2) obj.default_autoverify_timeout_sec = ( NotifyMatrix.default_autoverify_timeout_sec ) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_auto_verify_fast_paths(): """auto_verify() short-circuits when already verified or when the plugin identity is not ready yet.""" obj = _matrix_sas_plugin() binding = obj._e2ee_binding_key() obj.store.set("e2ee_device_binding", binding, expires=60) obj.store.set("e2ee_verified_binding", binding, expires=60) with mock.patch.object(obj, "_fetch") as fetch: assert matrix_sas.auto_verify(obj) is True fetch.assert_not_called() obj2 = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, autoverify=True, ) assert matrix_sas.auto_verify(obj2) is False obj3 = _matrix_sas_plugin() obj3.store.set("e2ee_device_binding", "unverified", expires=60) with mock.patch.object( matrix_sas.MatrixSASAutoVerifier, "run", return_value=True ) as run: assert matrix_sas.auto_verify(obj3) is True run.assert_called_once() @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_auto_verify_cooldown(): """A failed attempt sets a cooldown so every later send is not blocked for the full timeout until it expires.""" obj = _matrix_sas_plugin() binding = obj._e2ee_binding_key() obj.store.set("e2ee_device_binding", binding, expires=60) with mock.patch.object( matrix_sas.MatrixSASAutoVerifier, "run", return_value=False ) as run: assert matrix_sas.auto_verify(obj) is False run.assert_called_once() # The next call in the cooldown window returns immediately -- no new # attempt, no network call. with mock.patch.object(matrix_sas.MatrixSASAutoVerifier, "run") as run: assert matrix_sas.auto_verify(obj) is False run.assert_not_called() # A changed identity is not blocked by another identity's cooldown. obj.store.set("e2ee_device_binding", "a-different-binding", expires=60) with mock.patch.object( matrix_sas.MatrixSASAutoVerifier, "run", return_value=True ) as run: assert matrix_sas.auto_verify(obj) is True run.assert_called_once() @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_auto_verify_concurrency(): """Let concurrent callers deliver while one caller verifies.""" import threading as _threading import time as _time obj = _matrix_sas_plugin() obj.store.set("e2ee_device_binding", obj._e2ee_binding_key(), expires=60) state_lock = _threading.Lock() active = 0 max_concurrent = 0 calls = 0 def fake_run(_self): nonlocal active, max_concurrent, calls with state_lock: active += 1 max_concurrent = max(max_concurrent, active) calls += 1 # Long enough that a blocking wait for this call would make the # elapsed-time assertion below fail. _time.sleep(1.0) with state_lock: active -= 1 return True results = [] def call_auto_verify(): results.append(matrix_sas.auto_verify(obj)) with mock.patch.object(matrix_sas.MatrixSASAutoVerifier, "run", fake_run): threads = [ _threading.Thread(target=call_auto_verify) for _ in range(4) ] start = _time.monotonic() for thread in threads: thread.start() for thread in threads: thread.join(timeout=15) elapsed = _time.monotonic() - start # The group finishes near the single verifier's running time. assert elapsed < 2.5 assert calls == 1 assert max_concurrent == 1 assert results.count(True) == 1 assert results.count(False) == 3 @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_refresh_concurrency(): """A concurrent refresh does not wait on an in-progress verification. The verification lock can be held by a live SAS exchange for up to the full attempt timeout; refreshing trust must not block behind it. """ import time as _time obj = _matrix_sas_plugin() # Simulate a verification attempt already holding the lock. obj._autoverify_lock.acquire() try: start = _time.monotonic() result = matrix_sas.refresh_verified_state(obj) elapsed = _time.monotonic() - start finally: obj._autoverify_lock.release() assert result is False # Non-blocking lock acquisition never waits on the held lock; this is # only guarding against an accidental blocking wait being introduced, # so leave generous headroom for slow/loaded CI machines. assert elapsed < 2.0 @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_store_verified_binding(): """store_verified_binding no-ops without a device binding to trust.""" obj = _matrix_sas_plugin() assert matrix_sas.store_verified_binding(obj) is False obj.store.set("e2ee_device_binding", "B", expires=60) assert matrix_sas.store_verified_binding(obj) is True assert obj.store.get("e2ee_verified_binding") == "B" @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_refresh_state(): """Trust refresh requires matching identities and successful writes.""" obj = _matrix_sas_plugin() assert matrix_sas.refresh_verified_state(obj) is False binding = obj._e2ee_binding_key() obj.store.set("e2ee_device_binding", binding, expires=60) assert matrix_sas.refresh_verified_state(obj) is False obj.store.set("e2ee_verified_binding", binding, expires=60) obj.device_id = "OTHERDEV" assert matrix_sas.refresh_verified_state(obj) is False obj.device_id = "APPRISE" with mock.patch.object( obj.store, "set", side_effect=[True, False, True, True] ): assert matrix_sas.refresh_verified_state(obj) is False assert matrix_sas.refresh_verified_state(obj) is True @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_auto_verify_flow(): """Exercise the full same-user SAS event flow.""" import re from time import time from cryptography.hazmat.primitives.asymmetric.x25519 import ( X25519PrivateKey, X25519PublicKey, ) from cryptography.hazmat.primitives.serialization import ( Encoding, PublicFormat, ) from apprise.plugins.matrix.e2ee import ( MatrixOlmAccount, _b64dec, _b64enc, _hkdf_sha256, _hmac_sha256, ) user_id = "@u:h" own_device = "APPRISE" peer_device = "ELEMENT" transaction_id = "sas-flow" own_account = MatrixOlmAccount() peer_account = MatrixOlmAccount() peer_private = X25519PrivateKey.generate() peer_public = _b64enc( peer_private.public_key().public_bytes(Encoding.Raw, PublicFormat.Raw) ) peer_device_keys = peer_account.device_keys_payload(user_id, peer_device) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, autoverify=True, ) obj.user_id = user_id obj.device_id = own_device obj._e2ee_account = own_account binding = obj._e2ee_binding_key() obj.store.set("e2ee_device_binding", binding, expires=60) sent = [] sync_index = 0 def fake_fetch(path, payload=None, params=None, method="POST", **kwargs): nonlocal sync_index if path == "/keys/query": return ( True, {"device_keys": {user_id: {peer_device: peer_device_keys}}}, requests.codes.ok, ) if path.startswith("/sendToDevice/"): assert re.match(r"^/sendToDevice/[^/]+/[0-9a-f]{32}$", path) event_type = path.split("/")[2] target_user, devices = next(iter(payload["messages"].items())) _target_device, content = next(iter(devices.items())) sent.append((event_type, target_user, content)) return (True, {}, requests.codes.ok) assert path == "/sync" if sync_index == 0: event_type = "m.key.verification.request" content = { "from_device": peer_device, "transaction_id": transaction_id, "methods": ["m.sas.v1"], "timestamp": int(time() * 1000), } elif sync_index == 1: event_type = "m.key.verification.start" content = { "from_device": peer_device, "transaction_id": transaction_id, "method": "m.sas.v1", "key_agreement_protocols": ["curve25519-hkdf-sha256"], "hashes": ["sha256"], "message_authentication_codes": ["hkdf-hmac-sha256.v2"], "short_authentication_string": ["decimal", "emoji"], } elif sync_index == 2: event_type = "m.key.verification.key" content = { "transaction_id": transaction_id, "key": peer_public, } elif sync_index == 3: event_type = "m.key.verification.mac" own_key_event = next( item for item in sent if item[0] == "m.key.verification.key" ) own_public = own_key_event[2]["key"] shared_secret = peer_private.exchange( X25519PublicKey.from_public_bytes(_b64dec(own_public)) ) key_id = "ed25519:{}".format(peer_device) def calculate_mac(value, info_key_id): info = ( "MATRIX_KEY_VERIFICATION_MAC" + user_id + peer_device + user_id + own_device + transaction_id + info_key_id ).encode("utf-8") key = _hkdf_sha256(shared_secret, 32, salt=None, info=info) return _b64enc(_hmac_sha256(key, value.encode("utf-8"))) content = { "transaction_id": transaction_id, "mac": { key_id: calculate_mac(peer_account.signing_key, key_id) }, "keys": calculate_mac(key_id, "KEY_IDS"), } else: event_type = "m.key.verification.done" content = {"transaction_id": transaction_id} sync_index += 1 return ( True, { "next_batch": "sync-{}".format(sync_index), "to_device": { "events": [ { "type": event_type, "sender": user_id, "content": content, } ] }, }, requests.codes.ok, ) with mock.patch.object(obj, "_fetch", side_effect=fake_fetch): assert obj._e2ee_auto_verify() is True assert [item[0] for item in sent] == [ "m.key.verification.ready", "m.key.verification.accept", "m.key.verification.key", "m.key.verification.mac", "m.key.verification.done", ] assert obj.store.get("e2ee_verified_binding") == binding @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_failure_allows_send(): """A failed SAS bootstrap does not prevent message delivery.""" obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, autoverify=True, secure=True, ) obj.access_token = "token" obj.user_id = "@u:h" obj.device_id = "APPRISE" with ( mock.patch.object(obj, "_e2ee_setup", return_value=True), mock.patch.object( obj, "_e2ee_auto_verify", return_value=False ) as verify, mock.patch.object(obj, "_room_join", return_value="!r:h") as join, mock.patch.object(obj, "_e2ee_room_encrypted", return_value=True), mock.patch.object( obj, "_e2ee_send_to_room", return_value=True ) as send, mock.patch.object( obj, "_e2ee_refresh_verified_state", return_value=False ), ): assert obj._send_server_notification(body="test") is True verify.assert_called_once() join.assert_called_once() send.assert_called_once() @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_refresh_is_minimal(): """Only identity state required to retain SAS trust is refreshed.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, autoverify=True, secure=True, ) obj.user_id = "@u:h" obj.device_id = "APPRISE" obj._e2ee_account = MatrixOlmAccount() binding = "{}|{}|{}|{}".format( obj.user_id, obj.device_id, obj._e2ee_account.identity_key, obj._e2ee_account.signing_key, ) obj.store.set("e2ee_device_binding", binding, expires=60) obj.store.set("e2ee_verified_binding", binding, expires=60) with mock.patch.object(obj.store, "set", wraps=obj.store.set) as store_set: assert obj._e2ee_refresh_verified_state() is True assert [call.args[0] for call in store_set.call_args_list] == [ "device_id", "e2ee_account", "e2ee_device_binding", "e2ee_verified_binding", "e2ee_autoverify_refreshed_at", ] assert all( call.kwargs["expires"] == obj.default_cache_expiry_sec for call in store_set.call_args_list ) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_refresh_throttled(): """A recent refresh is skipped entirely -- no rewrite of the serialized E2EE account (or anything else) on every single send.""" from time import time as _time obj = _matrix_sas_plugin() binding = obj._e2ee_binding_key() obj.store.set("e2ee_device_binding", binding, expires=60) obj.store.set("e2ee_verified_binding", binding, expires=60) obj.store.set("e2ee_autoverify_refreshed_at", _time(), expires=60) with mock.patch.object(obj.store, "set") as store_set: assert matrix_sas.refresh_verified_state(obj) is True store_set.assert_not_called() # Once the interval has genuinely elapsed, it refreshes normally. obj.store.set( "e2ee_autoverify_refreshed_at", _time() - obj.default_autoverify_refresh_interval_sec - 1, expires=60, ) with mock.patch.object(obj.store, "set", wraps=obj.store.set) as store_set: assert matrix_sas.refresh_verified_state(obj) is True assert store_set.call_count == 5 @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_refresh_trust_write_failure(): """A failed write of the trust marker itself must not be reported as success, and must not go on to touch the refresh bookkeeping key.""" obj = _matrix_sas_plugin() binding = obj._e2ee_binding_key() obj.store.set("e2ee_device_binding", binding, expires=60) obj.store.set("e2ee_verified_binding", binding, expires=60) real_set = obj.store.set def flaky_set(key, *args, **kwargs): if key == "e2ee_verified_binding": return False return real_set(key, *args, **kwargs) with mock.patch.object( obj.store, "set", side_effect=flaky_set ) as store_set: assert matrix_sas.refresh_verified_state(obj) is False assert "e2ee_autoverify_refreshed_at" not in [ call.args[0] for call in store_set.call_args_list ] @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_refresh_invalid_timestamp(): """A malformed refresh timestamp is treated as missing.""" obj = _matrix_sas_plugin() binding = obj._e2ee_binding_key() obj.store.set("e2ee_device_binding", binding, expires=60) obj.store.set("e2ee_verified_binding", binding, expires=60) obj.store.set("e2ee_autoverify_refreshed_at", "not-a-number", expires=60) assert matrix_sas.refresh_verified_state(obj) is True @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_sas_refresh_writes_short_circuit(): """A failure partway through the identity refresh stops immediately instead of continuing to write (and serialize) the rest regardless.""" obj = _matrix_sas_plugin() binding = obj._e2ee_binding_key() write_order = [ "device_id", "e2ee_account", "e2ee_device_binding", "e2ee_verified_binding", ] for fail_at in write_order: obj.store.set("e2ee_device_binding", binding, expires=60) obj.store.set("e2ee_verified_binding", binding, expires=60) def flaky_set( key, *args, _fail_at=fail_at, _real_set=obj.store.set, **kwargs ): if key == _fail_at: return False return _real_set(key, *args, **kwargs) with mock.patch.object( obj.store, "set", side_effect=flaky_set ) as store_set: assert matrix_sas.refresh_verified_state(obj) is False called_keys = [call.args[0] for call in store_set.call_args_list] # Every key up to and including the failed one was attempted, in # order, and nothing after it was. assert called_keys == write_order[: write_order.index(fail_at) + 1] @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") @pytest.mark.parametrize("delivery_ok", [True, False]) def test_plugin_matrix_sas_refresh_after_success(delivery_ok): """A failed Matrix delivery must not extend SAS trust.""" obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, autoverify=True, secure=True, ) obj.access_token = "token" obj.user_id = "@u:h" obj.device_id = "APPRISE" with ( mock.patch.object(obj, "_e2ee_setup", return_value=True), mock.patch.object(obj, "_e2ee_auto_verify", return_value=True), mock.patch.object(obj, "_room_join", return_value="!r:h"), mock.patch.object(obj, "_e2ee_room_encrypted", return_value=True), mock.patch.object(obj, "_e2ee_send_to_room", return_value=delivery_ok), mock.patch.object( obj, "_e2ee_refresh_verified_state", return_value=True ) as refresh, ): assert obj._send_server_notification(body="test") is delivery_ok assert refresh.call_count == (1 if delivery_ok else 0) def test_plugin_matrix_e2ee_no_cryptography(): """MATRIX_E2EE_SUPPORT is False when cryptography is unavailable.""" import importlib import sys # Block 'cryptography' so the ImportError branch is taken on reload saved = { k: v for k, v in sys.modules.items() if k.startswith("cryptography") } for key in list(saved): sys.modules[key] = None # type: ignore[assignment] try: import apprise.plugins.matrix.e2ee as e2ee_mod importlib.reload(e2ee_mod) assert e2ee_mod.MATRIX_E2EE_SUPPORT is False finally: # Restore everything so subsequent tests are not broken for key in list(saved): if saved[key] is None: del sys.modules[key] else: sys.modules[key] = saved[key] # Force a clean reload so MATRIX_E2EE_SUPPORT is True again importlib.reload(e2ee_mod) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_olm_account(): """MatrixOlmAccount creation, signing, serialisation.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount # Fresh account acct = MatrixOlmAccount() assert isinstance(acct.identity_key, str) assert isinstance(acct.signing_key, str) # sign() accepts str and bytes sig = acct.sign("hello") assert isinstance(sig, str) sig2 = acct.sign(b"hello") assert isinstance(sig2, str) # to_dict / from_dict round-trip d = acct.to_dict() assert "ik" in d and "sk" in d restored = MatrixOlmAccount.from_dict(d) assert restored.identity_key == acct.identity_key assert restored.signing_key == acct.signing_key # Explicit key construction from stored keys acct2 = MatrixOlmAccount( ik_priv_b64=d["ik"], sk_priv_b64=d["sk"], ) assert acct2.identity_key == acct.identity_key # device_keys_payload produces a signed dict payload = acct.device_keys_payload("@u:h", "DEVID") assert "signatures" in payload assert "algorithms" in payload # fallback_keys_payload fb1 = acct.fallback_keys_payload("@u:h", "DEVID") assert len(fb1) == 1 fb_key_id1 = next(iter(fb1.keys())) assert fb_key_id1.startswith("signed_curve25519:") fb2 = acct.fallback_keys_payload("@u:h", "DEVID") # second call returns the same key id (key not regenerated) assert next(iter(fb2.keys())) == fb_key_id1 @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_olm_session(): """MatrixOlmSession encrypts and produces correct wire format.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount, _b64dec acct = MatrixOlmAccount() # Create a second account to act as recipient recipient = MatrixOlmAccount() session = acct.create_outbound_session( recipient.identity_key, recipient.identity_key, # reuse IK as a stand-in OTK ) assert session.their_identity_key == recipient.identity_key # str plaintext result = session.encrypt("test plaintext") assert result["type"] == 0 assert isinstance(result["body"], str) # Decode and check the outer pre-key wire format. # Outer: version(0x03) | field1(OTK) | field2(EK) | field3(IK) | # field4(inner+mac) # Inner (in field 4): version(0x03) | field1(ratchet_key) | # field2(chain_index) | field4(ciphertext,tag=0x22) | inner_mac(8) raw = _b64dec(result["body"]) assert raw[0:1] == b"\x03", "outer version must be 0x03" # field 4, wire-type 2 -> tag = (4<<3)|2 = 0x22 -- ciphertext in inner msg assert b"\x22" in raw, "ciphertext field tag 0x22 must be present" # Outer field 1 (OTK, tag 0x0A), field 2 (EK, tag 0x12) must appear assert b"\x0a" in raw, "outer OTK field (tag 0x0A) must be present" assert b"\x12" in raw, "outer EK field (tag 0x12) must be present" # bytes plaintext also accepted result2 = session.encrypt(b"bytes input") assert result2["type"] == 0 @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_olm_roundtrip(): """Confirm that Bob can decrypt an Olm message from Alice. The receiver-side reconstruction verifies compatible X3DH output. It also confirms the outer base key matches the inner ratchet key, which receivers need to reconstruct the session. """ import hmac as _hmac_stdlib from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes, hmac as _hmac_mod from cryptography.hazmat.primitives.asymmetric.x25519 import ( X25519PrivateKey, ) from cryptography.hazmat.primitives.ciphers import ( Cipher, algorithms, modes, ) from cryptography.hazmat.primitives.kdf.hkdf import HKDF from cryptography.hazmat.primitives.padding import PKCS7 from cryptography.hazmat.primitives.serialization import ( Encoding, PublicFormat, ) from apprise.plugins.matrix.e2ee import ( MatrixOlmAccount, _b64dec, _b64enc, ) # -- helpers (Bob-side) ------------------------------------------- def _hmac(key, data): h = _hmac_mod.HMAC(key, hashes.SHA256(), backend=default_backend()) h.update(data) return h.finalize() def _hkdf(ikm, length, salt, info): return HKDF( algorithm=hashes.SHA256(), length=length, salt=salt, info=info, backend=default_backend(), ).derive(ikm) def _aes_cbc_decrypt(key, iv, ciphertext): cipher = Cipher( algorithms.AES(key), modes.CBC(iv), backend=default_backend() ) dec = cipher.decryptor() padded = dec.update(ciphertext) + dec.finalize() unpadder = PKCS7(128).unpadder() return unpadder.update(padded) + unpadder.finalize() def _read_varint(data, pos): val, shift = 0, 0 while True: b = data[pos] pos += 1 val |= (b & 0x7F) << shift shift += 7 if not (b & 0x80): return val, pos def _parse_pb_fields(data): """Parse length-delimited protobuf body; return {field_num: bytes}.""" fields = {} pos = 0 while pos < len(data): tag, pos = _read_varint(data, pos) field_num = tag >> 3 wire_type = tag & 0x07 if wire_type == 2: length, pos = _read_varint(data, pos) fields[field_num] = data[pos : pos + length] pos += length elif wire_type == 0: val, pos = _read_varint(data, pos) fields[field_num] = val else: raise ValueError("Unexpected wire type {}".format(wire_type)) return fields # -- test setup --------------------------------------------------- # Alice's account (sender) alice = MatrixOlmAccount() # Bob's account: identity key + a separate one-time key bob = MatrixOlmAccount() bob_otk_priv = X25519PrivateKey.generate() bob_otk_pub = bob_otk_priv.public_key().public_bytes( Encoding.Raw, PublicFormat.Raw ) bob_otk_pub_b64 = _b64enc(bob_otk_pub) # Bob's identity-key private (for DH computation) bob_ik_priv = bob._ik # Alice creates an outbound session to Bob using his real OTK session = alice.create_outbound_session( bob.identity_key, # Bob's Curve25519 identity key bob_otk_pub_b64, # Bob's one-time key ) plaintext = "hello from Alice" result = session.encrypt(plaintext) assert result["type"] == 0 # -- Bob-side decryption ------------------------------------------ raw = _b64dec(result["body"]) # The outer message contains the one-time, base, identity, and inner # message fields. Only the inner message has a MAC. assert raw[0:1] == b"\x03", "outer version must be 0x03" # Parse the full outer protobuf body (all bytes after the version byte) outer_fields = _parse_pb_fields(raw[1:]) otk_pub_in_msg = outer_fields[1] # field 1: Bob's OTK public bytes base_key = outer_fields[2] # field 2: Alice's ephemeral E_A alice_ik_pub = outer_fields[3] # field 3: Alice's identity key # The OTK bytes in the message must equal Bob's OTK public key assert otk_pub_in_msg == bob_otk_pub, ( "OTK in pre-key message must match the OTK that was claimed" ) # Bob computes X3DH (mirroring Alice's DH operations) # DH1 = X25519(OTK_B_priv, IK_A_pub) # DH2 = X25519(IK_B_priv, E_A_pub) # DH3 = X25519(OTK_B_priv, E_A_pub) from cryptography.hazmat.primitives.asymmetric.x25519 import ( X25519PublicKey, ) alice_ik = X25519PublicKey.from_public_bytes(alice_ik_pub) e_a = X25519PublicKey.from_public_bytes(base_key) dh1 = bob_otk_priv.exchange(alice_ik) dh2 = bob_ik_priv.exchange(e_a) dh3 = bob_otk_priv.exchange(e_a) ikm = dh1 + dh2 + dh3 keys64 = _hkdf(ikm, 64, salt=None, info=b"OLM_ROOT") root_key = keys64[:32] chain_key = keys64[32:] del root_key # not needed for first-message decryption # Derive message keys from the initial chain key msg_key = _hmac(chain_key, b"\x01") keys80 = _hkdf(msg_key, 80, salt=b"\x00" * 32, info=b"OLM_KEYS") aes_key = keys80[:32] mac_key = keys80[32:64] iv = keys80[64:80] # Field 4 = inner_normal_message || 8-byte MAC. # The inner normal message itself starts with version byte 0x03 followed # by the protobuf body; the MAC covers the version + protobuf body. inner_with_mac = outer_fields[4] inner_body = inner_with_mac[:-8] # version(1) + pb_fields inner_mac_received = inner_with_mac[-8:] # Verify the inner MAC (HMAC-SHA-256 of inner_body, truncated to 8 bytes) inner_mac_expected = _hmac(mac_key, inner_body)[:8] assert _hmac_stdlib.compare_digest( inner_mac_expected, inner_mac_received ), "inner MAC verification failed" # Parse inner fields # inner_body = version(0x03) | pb_fields assert inner_body[0:1] == b"\x03", "inner version must be 0x03" inner_fields = _parse_pb_fields(inner_body[1:]) # CRITICAL invariant: # ratchet_key (inner field 1) == base_key (outer field 2) ratchet_key = inner_fields[1] assert ratchet_key == base_key, ( "Olm invariant violated: inner ratchet_key must equal outer base_key " "(E_A must be used in BOTH positions per Olm spec Section 5.1). " "Got ratchet_key={} base_key={}".format( _b64enc(ratchet_key), _b64enc(base_key) ) ) # Decrypt the AES-256-CBC ciphertext (inner field 4) ciphertext = inner_fields[4] decrypted = _aes_cbc_decrypt(aes_key, iv, ciphertext) assert decrypted.decode("utf-8") == plaintext, ( "round-trip decryption failed: got {!r}".format(decrypted) ) @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_verify_keys(): """verify_device_keys and verify_signed_otk accept/reject signatures.""" from apprise.plugins.matrix.e2ee import ( MatrixOlmAccount, _canonical_json, verify_device_keys, verify_signed_otk, ) acct = MatrixOlmAccount() uid = "@alice:h" dev = "DEVID" # --- verify_device_keys --- # Valid self-signed device keys -> True dk = acct.device_keys_payload(uid, dev) assert verify_device_keys(dk, uid, dev) is True # Payload user_id doesn't match -> False (identity binding check) mismatched_uid = {**dk, "user_id": "@other:h"} assert verify_device_keys(mismatched_uid, uid, dev) is False # Payload device_id doesn't match -> False (identity binding check) mismatched_dev = {**dk, "device_id": "WRONGDEV"} assert verify_device_keys(mismatched_dev, uid, dev) is False # Missing user_id in payload -> False no_user_id = {k: v for k, v in dk.items() if k != "user_id"} assert verify_device_keys(no_user_id, uid, dev) is False # Missing signatures field -> False no_sig = {k: v for k, v in dk.items() if k != "signatures"} assert verify_device_keys(no_sig, uid, dev) is False # Wrong user_id in signatures -> False wrong_uid = { **dk, "signatures": {"@wrong:h": dk["signatures"][uid]}, } assert verify_device_keys(wrong_uid, uid, dev) is False # Tampered key value -> False tampered = {**dk, "keys": {**dk["keys"], f"ed25519:{dev}": "bad"}} assert verify_device_keys(tampered, uid, dev) is False # Missing ed25519 key entry -> False no_keys = {**dk, "keys": {f"curve25519:{dev}": acct.identity_key}} assert verify_device_keys(no_keys, uid, dev) is False # Malformed homeserver keys must fail closed. assert verify_device_keys(None, uid, dev) is False assert verify_device_keys("not-a-dict", uid, dev) is False assert verify_device_keys({**dk, "keys": None}, uid, dev) is False assert verify_device_keys({**dk, "keys": "not-a-dict"}, uid, dev) is False assert verify_device_keys({**dk, "signatures": None}, uid, dev) is False assert verify_device_keys({**dk, "signatures": [1, 2]}, uid, dev) is False assert ( verify_device_keys({**dk, "signatures": {uid: None}}, uid, dev) is False ) # --- verify_signed_otk --- key_b64 = _rand_b64_32() otk_obj = {"key": key_b64} sig = acct.sign(_canonical_json(otk_obj)) signed_otk = { **otk_obj, "signatures": {uid: {f"ed25519:{dev}": sig}}, } # Valid OTK -> True assert verify_signed_otk(signed_otk, uid, dev, acct.signing_key) is True # Missing signatures -> False assert verify_signed_otk(otk_obj, uid, dev, acct.signing_key) is False # Wrong signing key -> False other = MatrixOlmAccount() assert verify_signed_otk(signed_otk, uid, dev, other.signing_key) is False # Tampered key value -> False tampered_otk = {**signed_otk, "key": _rand_b64_32()} assert verify_signed_otk(tampered_otk, uid, dev, acct.signing_key) is False @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_megolm_session(): """MatrixMegOlmSession encrypt, session_key, rotation, serialisation.""" from apprise.plugins.matrix.e2ee import ( MATRIX_MEGOLM_STORE_VERSION, MEGOLM_ROTATION_MSGS, MatrixMegOlmSession, ) s = MatrixMegOlmSession() assert isinstance(s.session_id, str) # encrypt returns base64 ciphertext ct = s.encrypt({"type": "m.room.message", "content": {}}) assert isinstance(ct, str) # Verify MegOLM wire format (libolm message.cpp): # version(0x03) | field1_varint(msg_index,tag=0x08) | # field2_bytes(ciphertext,tag=0x12) | mac(8) | sig(64) from apprise.plugins.matrix.e2ee import _b64dec raw = _b64dec(ct) assert raw[0:1] == b"\x03", "MegOLM version must be 0x03" assert raw[1:2] == b"\x08", "MegOLM field 1 tag must be 0x08 (varint)" # After tag 0x08: the varint counter value (0 -> single byte 0x00), # then the ciphertext tag 0x12. assert b"\x12" in raw, "MegOLM ciphertext field tag 0x12 must be present" # session_key returns base64 -- verify wire format per libolm # outbound_group_session.c: version(1) + counter(4) + ratchet(128) # + ed25519_pub(32) + ed25519_sig(64) = 229 bytes raw = 308 base64 chars sk = s.session_key() assert isinstance(sk, str) sk_raw = _b64dec(sk) assert len(sk_raw) == 229, "session_key must be 229 bytes (sig included)" assert sk_raw[0:1] == b"\x02", "session_key version must be 0x02" # to_dict / from_dict round-trip d = s.to_dict() assert d["version"] == MATRIX_MEGOLM_STORE_VERSION s2 = MatrixMegOlmSession.from_dict(d) assert s2.session_id == s.session_id assert s2._counter == s._counter # Cache data from an unsupported version is invalid, not a setting error. with pytest.raises( AppriseInvalidData, match="Incompatible MegOLM session cache format" ) as exc_info: MatrixMegOlmSession.from_dict({"version": -1}) assert exc_info.value.error_code == errno.EINVAL # should_rotate: explicit count threshold assert not s.should_rotate(msg_count=MEGOLM_ROTATION_MSGS - 1) assert s.should_rotate(msg_count=MEGOLM_ROTATION_MSGS) # should_rotate: time threshold (old created_at) old = MatrixMegOlmSession(created_at=0.0) assert old.should_rotate() # _advance branches -- cascade fires when (n+1) % threshold == 0, # i.e. when the counter is one less than a threshold multiple. # -- normal step (else branch) s3 = MatrixMegOlmSession() s3._advance() # counter 0 -> 1 (1 % 256 != 0, else: R[3] only) # -- %256 branch: next counter will be 256 s4 = MatrixMegOlmSession() s4._counter = 255 s4._advance() # -- %65536 branch: next counter will be 65536 s5 = MatrixMegOlmSession() s5._counter = 65535 s5._advance() # -- %2^24 branch: next counter will be 2^24 s6 = MatrixMegOlmSession() s6._counter = (1 << 24) - 1 s6._advance() # --------------------------------------------------------------------------- # E2EE integration tests -- URL parsing and send flow # --------------------------------------------------------------------------- @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_url_roundtrip(): """e2ee default is True; disabled state survives url() / parse_url().""" # Default (no e2ee param) is now True obj = NotifyMatrix( host="matrix.example.com", user="user", password="pass", targets=["#room"], ) assert obj.e2ee is True # Enabled is the default -- not written to URL assert "e2ee" not in obj.url() # Disabled survives round-trip obj2 = NotifyMatrix( host="matrix.example.com", user="user", password="pass", targets=["#room"], e2ee=False, ) assert obj2.e2ee is False u2 = obj2.url() assert "e2ee=no" in u2 result = NotifyMatrix.parse_url(u2) assert result is not None assert result.get("e2ee") is False or str(result.get("e2ee")).lower() in ( "false", "no", "0", ) # Automatic verification is opt-in and survives URL round-trip. obj3 = NotifyMatrix( host="matrix.example.com", user="user", password="pass", targets=["#room"], autoverify=True, ) assert obj3.autoverify is True u3 = obj3.url() assert "autoverify=yes" in u3 result = NotifyMatrix.parse_url(u3) assert result is not None assert result.get("autoverify") is True @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_insecure_connection(mock_post, mock_get, mock_put): """e2ee=yes over insecure (matrix://) falls back to unencrypted.""" resp = _Response() resp.status_code = requests.codes.ok resp.content = dumps( { "access_token": "tok", "user_id": "@u:h", "home_server": "h", "device_id": "DEV", "room_id": "!r:h", } ).encode() mock_post.return_value = resp mock_get.return_value = resp mock_put.return_value = resp # matrix:// (not matrixs://) -- E2EE silently skipped, plain delivery obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#room"], e2ee=True, # secure=False is the default for matrix:// URLs ) assert not obj.secure assert obj.send(body="test") is True @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_no_support(mock_post, mock_get, mock_put): """e2ee=yes with MATRIX_E2EE_SUPPORT=False sends unencrypted.""" resp = _Response() resp.status_code = requests.codes.ok resp.content = dumps( { "access_token": "tok", "user_id": "@u:h", "home_server": "h", "device_id": "DEV", "room_id": "!r:h", } ).encode() mock_post.return_value = resp mock_get.return_value = resp mock_put.return_value = resp with mock.patch( "apprise.plugins.matrix.base.MATRIX_E2EE_SUPPORT", False, ): obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#room"], e2ee=True, ) # Should succeed (falls back to unencrypted) assert obj.send(body="test") is True @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_send(mock_post, mock_get, mock_put): """Full E2EE send path via _send_server_notification.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount # Recipient device (acts as a room member) recipient = MatrixOlmAccount() login_resp = { "access_token": "tok123", "user_id": "@sender:localhost", "home_server": "localhost", "device_id": "SENDERDEV", } members_resp = { "joined": {"@other:localhost": {}}, } # Properly signed device keys (required by verify_device_keys) signed_dev = recipient.device_keys_payload("@other:localhost", "OTHERDEV") query_resp = { "device_keys": {"@other:localhost": {"OTHERDEV": signed_dev}} } # Properly signed OTK (required by verify_signed_otk) signed_otk = _make_signed_otk(recipient, "@other:localhost", "OTHERDEV") claim_resp = { "one_time_keys": { "@other:localhost": { "OTHERDEV": {"signed_curve25519:KEYID": signed_otk} } } } def _mk_resp(d): r = _Response() r.status_code = requests.codes.ok r.content = dumps(d).encode() return r # POST sequence: # login, keys/upload (with OTK count), join, keys/query, keys/claim, # keys/upload (OTK replenishment), logout mock_post.side_effect = [ _mk_resp(login_resp), _mk_resp({"one_time_key_counts": {"signed_curve25519": 1}}), _mk_resp({"room_id": "!room:localhost"}), # join _mk_resp(query_resp), # keys/query _mk_resp(claim_resp), # keys/claim # Replenishment: remaining = 1 - 1 (built_count) = 0 < threshold _mk_resp( { "one_time_key_counts": { "signed_curve25519": NotifyMatrix.default_e2ee_otk_count, } } ), _mk_resp({}), # logout ] mock_get.return_value = _mk_resp(members_resp) mock_put.return_value = _mk_resp({}) obj = NotifyMatrix( host="localhost", user="sender", password="pass", targets=["#room"], e2ee=True, secure=True, discovery=False, ) assert obj.send(body="hello", title="hi") is True # PUT was called: sendToDevice + rooms/send/encrypted assert mock_put.call_count >= 2 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_send_cached_session(mock_post, mock_get, mock_put): """E2EE send skips key-share when session key already shared.""" login_resp = { "access_token": "tok", "user_id": "@u:h", "home_server": "h", "device_id": "DEV", } def _mk_resp(d): r = _Response() r.status_code = requests.codes.ok r.content = dumps(d).encode() return r # POST: login, keys/upload, join, logout mock_post.side_effect = [ _mk_resp(login_resp), _mk_resp({}), # keys/upload _mk_resp({"room_id": "!r:h"}), # join _mk_resp({}), # logout ] mock_put.return_value = _mk_resp({}) from apprise.plugins.matrix.e2ee import MatrixMegOlmSession obj = NotifyMatrix( host="h", user="u", password="pass", targets=["!r:h"], e2ee=True, secure=True, discovery=False, ) # Pre-seed room encryption state, MegOLM session, and key-shared flag # so that no additional GETs or key-exchange PUTs are issued. _sess = MatrixMegOlmSession() obj.store.set("e2ee_room_enc_!r:h", True) obj.store.set("e2ee_megolm_!r:h", _sess.to_dict()) obj.store.set("e2ee_key_shared_!r:h", _sess.session_id) assert obj.send(body="silent") is True # Only one PUT for the encrypted message itself assert mock_put.call_count == 1 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_send_legacy_shared_flag_rekeys( mock_post, mock_get, mock_put ): """Legacy boolean key-share cache forces a fresh room-key share.""" from apprise.plugins.matrix.e2ee import ( MatrixMegOlmSession, MatrixOlmAccount, ) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, ) obj.access_token = "tok" obj.home_server = "h" obj.user_id = "@u:h" obj.device_id = "DEV" obj._e2ee_account = MatrixOlmAccount() session = MatrixMegOlmSession() obj.store.set("e2ee_megolm_!r:h", session.to_dict()) obj.store.set("e2ee_key_shared_!r:h", True) with ( mock.patch.object( obj, "_e2ee_share_room_key", return_value=True ) as share, mock.patch.object(obj, "_fetch", return_value=(True, {}, 200)), ): assert ( obj._e2ee_send_to_room("!r:h", "body", "", NotifyType.INFO) is True ) assert share.call_count == 1 assert obj.store.get("e2ee_key_shared_!r:h") == session.session_id @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_setup_restored(): """E2EE account restores from store via _e2ee_setup and __init__.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount acct = MatrixOlmAccount() obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, ) # Seed account + mark keys already uploaded obj.store.set("e2ee_account", acct.to_dict()) obj.store.set("e2ee_keys_uploaded", True) obj.store.set( "e2ee_device_binding", "{}|{}|{}|{}".format( obj.user_id or "", obj.device_id or "", acct.identity_key, acct.signing_key, ), ) # Simulate state where account is not yet loaded in memory obj._e2ee_account = None # _e2ee_setup should restore the account from the store assert obj._e2ee_setup() is True assert obj._e2ee_account is not None assert obj._e2ee_account.identity_key == acct.identity_key # Bad account data in store -> _e2ee_setup creates a fresh account; # the try/except in _e2ee_setup is exercised when from_dict raises. obj._e2ee_account = None obj.store.set("e2ee_account", {"bad": "data"}) obj.store.clear("e2ee_keys_uploaded") with mock.patch.object(obj, "_e2ee_upload_keys", return_value=True): result = obj._e2ee_setup() assert result is True assert obj._e2ee_account is not None # Patch the store so __init__ sees a valid pre-existing account, # exercising the success path of the contextlib.suppress block. valid_data = obj._e2ee_account.to_dict() with mock.patch( "apprise.persistent_store.PersistentStore.get", return_value=valid_data, ): obj2 = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, ) assert obj2._e2ee_account is not None @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_setup_reuploads_on_binding_change(): """Changed device/account identity forces a fresh key upload.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount acct = MatrixOlmAccount() obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, ) obj.user_id = "@u:h" obj.device_id = "DEV_NEW" obj._e2ee_account = acct obj.store.set("e2ee_account", acct.to_dict()) obj.store.set("e2ee_keys_uploaded", True) obj.store.set("e2ee_device_binding", "@u:h|DEV_OLD|oldik|oldsk") with mock.patch.object( obj, "_e2ee_upload_keys", return_value=True ) as upload: assert obj._e2ee_setup() is True assert upload.call_count == 1 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_upload_keys_no_ids(mock_post, mock_get, mock_put): """_e2ee_upload_keys returns False when user_id/device_id missing.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, ) obj._e2ee_account = MatrixOlmAccount() # No user_id / device_id set assert obj._e2ee_upload_keys() is False @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_upload_keys_http_fail( mock_post, mock_get, mock_put ): """_e2ee_upload_keys returns False on HTTP error.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount resp = _Response() resp.status_code = 500 resp.content = dumps({}).encode() mock_post.return_value = resp obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, ) obj._e2ee_account = MatrixOlmAccount() obj.access_token = "tok" obj.user_id = "@u:h" obj.device_id = "DEV" obj.home_server = "h" assert obj._e2ee_upload_keys() is False @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_upload_keys_success_marks_published( mock_post, mock_get, mock_put ): """Successful upload clears the current unpublished OTK batch.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount resp = _Response() resp.status_code = requests.codes.ok resp.content = dumps( { "one_time_key_counts": { "signed_curve25519": NotifyMatrix.default_e2ee_otk_count, } } ).encode() mock_post.return_value = resp obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, ) obj._e2ee_account = MatrixOlmAccount() obj.access_token = "tok" obj.user_id = "@u:h" obj.device_id = "DEV" obj.home_server = "h" # Pre-generate one-time keys so we can verify they are marked published. obj._e2ee_account.one_time_keys_payload( obj.user_id, obj.device_id, count=3 ) assert obj._e2ee_account._otks assert obj._e2ee_upload_keys() is True assert obj._e2ee_account._otks == {} # Server OTK count from the response is persisted for threshold checks. assert obj.store.get("e2ee_otk_server_count") == ( NotifyMatrix.default_e2ee_otk_count ) @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_whoami(mock_post, mock_get, mock_put): """_whoami() resolves user_id and device_id from GET /account/whoami.""" def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r obj = NotifyMatrix( host="h", password="rawtoken", targets=["#r"], e2ee=True, ) obj.access_token = "rawtoken" obj.home_server = "h" # Success: server returns user_id and device_id; home_server is # extracted from user_id so DM targets resolve correctly. mock_get.return_value = _mk_resp( {"user_id": "@tok:h", "device_id": "TOKDEV"} ) assert obj._whoami() is True assert obj.user_id == "@tok:h" assert obj.device_id == "TOKDEV" assert obj.home_server == "h" assert obj.store.get("user_id") == "@tok:h" assert obj.store.get("device_id") == "TOKDEV" assert obj.store.get("home_server") == "h" # user_id without homeserver component (@alice, no colon) -> home_server # is not extracted (branch where len(parts) != 2 is not taken). obj.user_id = None obj.home_server = None mock_get.return_value = _mk_resp( {"user_id": "@noserver", "device_id": "DEV2"} ) assert obj._whoami() is True assert obj.user_id == "@noserver" assert obj.home_server is None # no colon -> not extracted # home_server already known -> not overwritten obj.home_server = "existing.h" mock_get.return_value = _mk_resp( {"user_id": "@tok:other.h", "device_id": "DEV3"} ) assert obj._whoami() is True assert obj.home_server == "existing.h" # Failure: server returns non-200 obj.user_id = None obj.device_id = None mock_get.return_value = _mk_resp({}, code=401) assert obj._whoami() is False @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_send_cached_token_recovers_home_server( mock_post, mock_get, mock_put ): """Cached auth without home_server should be repaired via _whoami().""" obj = NotifyMatrix( host="matrix.org", user="apprise", password="pass", targets=["#general"], ) obj.access_token = "cached-token" obj.user_id = None obj.device_id = None obj.home_server = None whoami_called = {"count": 0} def _fake_whoami(): whoami_called["count"] += 1 obj.user_id = "@apprise:matrix.org" obj.device_id = "DEV42" obj.home_server = "matrix.org" return True with ( mock.patch.object(obj, "_room_join", return_value="!room:matrix.org"), mock.patch.object(obj, "_whoami", side_effect=_fake_whoami), mock.patch.object( obj, "_fetch", return_value=(True, {"event_id": "$1"}, 200) ), ): assert obj.send("body", title="test") is True assert whoami_called["count"] == 1 assert obj.home_server == "matrix.org" assert obj.user_id == "@apprise:matrix.org" @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_get_megolm_rotation(mock_post, mock_get, mock_put): """_e2ee_get_megolm rotates session after threshold.""" from apprise.plugins.matrix.e2ee import ( MEGOLM_ROTATION_MSGS, MatrixMegOlmSession, ) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, ) # Seed a session that is already past the rotation threshold stale = MatrixMegOlmSession() stale._counter = MEGOLM_ROTATION_MSGS obj.store.set("e2ee_megolm_!r:h", stale.to_dict()) obj.store.set("e2ee_key_shared_!r:h", True) new_session = obj._e2ee_get_megolm("!r:h") assert new_session.session_id != stale.session_id # The shared flag should be cleared on rotation assert obj.store.get("e2ee_key_shared_!r:h") is None # Legacy cached sessions without the current version marker are # invalidated and replaced with a fresh session. legacy = stale.to_dict() del legacy["version"] obj.store.set("e2ee_megolm_!r:h", legacy) obj.store.set("e2ee_key_shared_!r:h", stale.session_id) replaced = obj._e2ee_get_megolm("!r:h") assert replaced.session_id != stale.session_id assert obj.store.get("e2ee_key_shared_!r:h") is None @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_room_encrypted(mock_post, mock_get, mock_put): """_e2ee_room_encrypted: cached, GET success, GET failure paths.""" def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, ) obj.access_token = "tok" obj.home_server = "h" # Cache hit -- no GET fired obj.store.set("e2ee_room_enc_!r:h", True) assert obj._e2ee_room_encrypted("!r:h") is True assert mock_get.call_count == 0 # Cache miss, GET returns 200 with content -> True obj.store.clear("e2ee_room_enc_!r:h") mock_get.return_value = _mk_resp({"algorithm": "m.megolm.v1.aes-sha2"}) assert obj._e2ee_room_encrypted("!r:h") is True assert mock_get.call_count == 1 # Cache is now populated assert obj.store.get("e2ee_room_enc_!r:h") is True # Cache miss, GET returns 404 -> False obj.store.clear("e2ee_room_enc_!r:h") mock_get.return_value = _mk_resp({}, code=404) assert obj._e2ee_room_encrypted("!r:h") is False assert obj.store.get("e2ee_room_enc_!r:h") is False @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_room_members(mock_post, mock_get, mock_put): """_e2ee_room_members handles HTTP failures and empty rooms.""" def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, ) obj.access_token = "tok" obj.home_server = "h" # joined_members HTTP failure -> None mock_get.return_value = _mk_resp({}, code=500) assert obj._e2ee_room_members("!r:h") is None # No joined members -> empty dict mock_get.return_value = _mk_resp({"joined": {}}) assert obj._e2ee_room_members("!r:h") == {} # keys/query failure -> None mock_get.return_value = _mk_resp({"joined": {"@u:h": {}}}) mock_post.return_value = _mk_resp({}, code=500) assert obj._e2ee_room_members("!r:h") is None # Unsigned device key -> device is rejected (empty result for that uid) from apprise.plugins.matrix.e2ee import MatrixOlmAccount unsigned_resp = { "device_keys": { "@u:h": { "DEV": { "keys": { "curve25519:DEV": "IK", "ed25519:DEV": "SK", } # no "signatures" field } } } } mock_get.return_value = _mk_resp({"joined": {"@u:h": {}}}) mock_post.return_value = _mk_resp(unsigned_resp) result = obj._e2ee_room_members("!r:h") assert result["@u:h"] == {} # Properly signed device key -> device included acct = MatrixOlmAccount() signed_dev = acct.device_keys_payload("@u:h", "DEV") query_resp = {"device_keys": {"@u:h": {"DEV": signed_dev}}} mock_get.return_value = _mk_resp({"joined": {"@u:h": {}}}) mock_post.return_value = _mk_resp(query_resp) result = obj._e2ee_room_members("!r:h") assert "@u:h" in result assert result["@u:h"]["DEV"]["curve25519"] == acct.identity_key @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_share_room_key_branches( mock_post, mock_get, mock_put ): """_e2ee_share_room_key edge cases.""" from apprise.plugins.matrix.e2ee import ( MatrixMegOlmSession, MatrixOlmAccount, ) def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, ) obj.access_token = "tok" obj.home_server = "h" obj.user_id = "@sender:h" obj.device_id = "SDEV" obj._e2ee_account = MatrixOlmAccount() session = MatrixMegOlmSession() # members failure -> False with mock.patch.object(obj, "_e2ee_room_members", return_value=None): assert obj._e2ee_share_room_key("!r:h", session) is False # empty members -> True (nothing to send) with mock.patch.object(obj, "_e2ee_room_members", return_value={}): assert obj._e2ee_share_room_key("!r:h", session) is True # keys/claim failure -> False members = {"@other:h": {"DEV": {"curve25519": "IK", "ed25519": "SK"}}} mock_post.return_value = _mk_resp({}, code=500) with mock.patch.object(obj, "_e2ee_room_members", return_value=members): assert obj._e2ee_share_room_key("!r:h", session) is False # keys/claim succeeds but server reports failures{} for remote servers failures_resp = { "one_time_keys": {}, "failures": {"remote.example": {"errcode": "M_UNREACHABLE"}}, } mock_post.return_value = _mk_resp(failures_resp) with mock.patch.object(obj, "_e2ee_room_members", return_value=members): assert obj._e2ee_share_room_key("!r:h", session) is True # own device is skipped (sender uid + device match) own_members = { "@sender:h": {"SDEV": {"curve25519": "IK", "ed25519": "SK"}} } otk_resp = { "one_time_keys": { "@sender:h": {"SDEV": {"signed_curve25519:KID": _rand_b64_32()}} } } mock_post.return_value = _mk_resp(otk_resp) mock_put.return_value = _mk_resp({}) with mock.patch.object( obj, "_e2ee_room_members", return_value=own_members ): # All devices skipped -> no PUT; still returns True assert obj._e2ee_share_room_key("!r:h", session) is True # missing curve25519 key -> device skipped no_ik = {"@other:h": {"DEV": {"curve25519": "", "ed25519": "SK"}}} with mock.patch.object(obj, "_e2ee_room_members", return_value=no_ik): mock_post.return_value = _mk_resp( {"one_time_keys": {"@other:h": {"DEV": {}}}} ) assert obj._e2ee_share_room_key("!r:h", session) is True # dict-valued OTK with valid signature succeeds end-to-end recipient = MatrixOlmAccount() their_ik = recipient.identity_key their_otk_b64 = _rand_b64_32() signed_otk_dict = _make_signed_otk( recipient, "@other:h", "DEV", their_otk_b64 ) dict_members = { "@other:h": { "DEV": { "curve25519": their_ik, "ed25519": recipient.signing_key, } } } dict_otk = { "one_time_keys": { "@other:h": {"DEV": {"signed_curve25519:KID": signed_otk_dict}} } } mock_post.return_value = _mk_resp(dict_otk) mock_put.return_value = _mk_resp({}) with mock.patch.object( obj, "_e2ee_room_members", return_value=dict_members ): assert obj._e2ee_share_room_key("!r:h", session) is True payload = loads(mock_put.call_args.kwargs["data"]) dev_payload = payload["messages"]["@other:h"]["DEV"] assert dev_payload["sender_key"] == obj._e2ee_account.identity_key # recipient_key and org.matrix.msgid are non-standard fields # that were removed; verify they are absent assert "recipient_key" not in dev_payload assert "org.matrix.msgid" not in dev_payload # dict-valued OTK with no signatures -> OTK rejected, device skipped unsigned_otk_dict = {"key": their_otk_b64} # no "signatures" field unsigned_otk_resp = { "one_time_keys": { "@other:h": {"DEV": {"signed_curve25519:KID": unsigned_otk_dict}} } } mock_post.return_value = _mk_resp(unsigned_otk_resp) with mock.patch.object( obj, "_e2ee_room_members", return_value=dict_members ): # No valid OTK -> no messages built -> True (nothing to send) assert obj._e2ee_share_room_key("!r:h", session) is True # Plain-string OTK for signed_curve25519 -> rejected (not a KeyObject) str_members = { "@other:h": { "DEV": { "curve25519": their_ik, "ed25519": recipient.signing_key, } } } str_otk = { "one_time_keys": { "@other:h": {"DEV": {"signed_curve25519:KID": their_otk_b64}} } } mock_post.return_value = _mk_resp(str_otk) with mock.patch.object( obj, "_e2ee_room_members", return_value=str_members ): # No valid KeyObject OTK -> no messages built -> True (nothing sent) assert obj._e2ee_share_room_key("!r:h", session) is True # OTK key not prefixed with "signed_curve25519:" -> skipped, no OTK found bad_key_otk = { "one_time_keys": {"@other:h": {"DEV": {"ed25519:KID": their_otk_b64}}} } mock_post.return_value = _mk_resp(bad_key_otk) with mock.patch.object( obj, "_e2ee_room_members", return_value=str_members ): assert obj._e2ee_share_room_key("!r:h", session) is True # OTK entry is not a dict (unexpected server response format) non_dict_otk = {"one_time_keys": {"@other:h": {"DEV": "invalid_format"}}} mock_post.return_value = _mk_resp(non_dict_otk) with mock.patch.object( obj, "_e2ee_room_members", return_value=str_members ): assert obj._e2ee_share_room_key("!r:h", session) is True # OTK value is an integer (unknown format) -> skipped non_str_otk = { "one_time_keys": {"@other:h": {"DEV": {"signed_curve25519:KID": 42}}} } mock_post.return_value = _mk_resp(non_str_otk) with mock.patch.object( obj, "_e2ee_room_members", return_value=str_members ): assert obj._e2ee_share_room_key("!r:h", session) is True # create_outbound_session raises -> device is skipped gracefully # Use a properly signed OTK so verification passes and the Olm call # is actually reached. create_fail_signed = _make_signed_otk( recipient, "@other:h", "DEV", their_otk_b64 ) create_fail_otk = { "one_time_keys": { "@other:h": {"DEV": {"signed_curve25519:KID": create_fail_signed}} } } mock_post.return_value = _mk_resp(create_fail_otk) with ( mock.patch.object( obj, "_e2ee_room_members", return_value=dict_members ), mock.patch.object( obj._e2ee_account, "create_outbound_session", side_effect=Exception("olm error"), ), ): assert obj._e2ee_share_room_key("!r:h", session) is True # sendToDevice PUT fails -> returns False (key-shared flag not set) # Uses a signed OTK so the full Olm path is exercised before the PUT full_signed_otk = _make_signed_otk( recipient, "@other:h", "DEV", their_otk_b64 ) full_otk_resp = { "one_time_keys": { "@other:h": {"DEV": {"signed_curve25519:KID": full_signed_otk}} } } mock_post.return_value = _mk_resp(full_otk_resp) mock_put.return_value = _mk_resp({}, code=500) with mock.patch.object( obj, "_e2ee_room_members", return_value=dict_members ): assert obj._e2ee_share_room_key("!r:h", session) is False # token-as-password mode: transaction_id is a UUID, not an int. # Previously, the unconditional `+= 1` raised TypeError (UUID + int). # With the guard in place the UUID is used as-is and the send succeeds. import uuid as _uuid_mod uuid_obj = NotifyMatrix( host="h", password="rawtoken", targets=["#r"], e2ee=True ) uuid_obj.access_token = "rawtoken" uuid_obj.password = "rawtoken" uuid_obj.transaction_id = _uuid_mod.uuid4() # UUID, not int uuid_obj.home_server = "h" uuid_obj.user_id = "@tok:h" uuid_obj.device_id = "TOKDEV" uuid_obj._e2ee_account = MatrixOlmAccount() uuid_session = MatrixMegOlmSession() uuid_otk = _make_signed_otk(recipient, "@other:h", "DEV", their_otk_b64) uuid_claim = { "one_time_keys": { "@other:h": {"DEV": {"signed_curve25519:KID": uuid_otk}} } } mock_post.return_value = _mk_resp(uuid_claim) mock_put.return_value = _mk_resp({}) with mock.patch.object( uuid_obj, "_e2ee_room_members", return_value=dict_members ): # Must not raise TypeError; transaction_id stays as UUID assert uuid_obj._e2ee_share_room_key("!r:h", uuid_session) is True assert isinstance(uuid_obj.transaction_id, _uuid_mod.UUID) @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_replenish_otks(mock_post): """_e2ee_replenish_otks: threshold logic, diagnostics, and error paths.""" from apprise.plugins.matrix.e2ee import MatrixOlmAccount otk_count = NotifyMatrix.default_e2ee_otk_count otk_threshold = NotifyMatrix.default_e2ee_otk_replenish_threshold def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r def _make_obj(): obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True ) obj.user_id = "@u:h" obj.device_id = "DEV" obj._e2ee_account = MatrixOlmAccount() return obj # --- missing credentials: no network call, returns False --- obj = _make_obj() obj.user_id = None assert obj._e2ee_replenish_otks() is False assert mock_post.call_count == 0 obj = _make_obj() obj.device_id = None assert obj._e2ee_replenish_otks() is False assert mock_post.call_count == 0 # --- pool sufficient: skip upload --- # Store a count well above threshold; claimed_count=1 -> remaining is # above default_e2ee_otk_replenish_threshold so no upload should happen. obj = _make_obj() obj.store.set("e2ee_otk_server_count", otk_threshold + otk_count) result = obj._e2ee_replenish_otks(claimed_count=1, skipped_no_otk=0) assert result is True assert mock_post.call_count == 0 # --- pool low: remaining < threshold triggers upload --- obj = _make_obj() obj.store.set("e2ee_otk_server_count", otk_threshold - 1) mock_post.return_value = _mk_resp( {"one_time_key_counts": {"signed_curve25519": otk_count}} ) assert obj._e2ee_replenish_otks(claimed_count=0, skipped_no_otk=0) is True assert mock_post.call_count == 1 assert obj.store.get("e2ee_otk_server_count") == otk_count assert obj.store.get("e2ee_account") is not None mock_post.reset_mock() # --- unknown server count (no store entry): replenish as precaution --- obj = _make_obj() # e2ee_otk_server_count not set -> unknown mock_post.return_value = _mk_resp( {"one_time_key_counts": {"signed_curve25519": otk_count}} ) assert obj._e2ee_replenish_otks(claimed_count=0, skipped_no_otk=0) is True assert mock_post.call_count == 1 mock_post.reset_mock() # --- skipped_no_otk > 0: always replenishes regardless of count --- obj = _make_obj() obj.store.set("e2ee_otk_server_count", otk_threshold + otk_count) mock_post.return_value = _mk_resp( {"one_time_key_counts": {"signed_curve25519": otk_count}} ) assert obj._e2ee_replenish_otks(claimed_count=0, skipped_no_otk=2) is True assert mock_post.call_count == 1 mock_post.reset_mock() # --- upload failure: returns False, logs warning --- obj = _make_obj() obj.store.set("e2ee_otk_server_count", 0) mock_post.return_value = _mk_resp({}, code=500) assert obj._e2ee_replenish_otks(claimed_count=0, skipped_no_otk=0) is False mock_post.reset_mock() # --- response missing one_time_key_counts: new count defaults to 0 --- obj = _make_obj() obj.store.set("e2ee_otk_server_count", 0) mock_post.return_value = _mk_resp({}) # no one_time_key_counts key assert obj._e2ee_replenish_otks(claimed_count=0, skipped_no_otk=0) is True assert obj.store.get("e2ee_otk_server_count") == 0 mock_post.reset_mock() @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_send_to_room_formats( mock_post, mock_get, mock_put ): """_e2ee_send_to_room: HTML/Markdown format paths and key-share fail.""" from apprise.common import NotifyFormat from apprise.plugins.matrix.e2ee import ( MatrixMegOlmSession, MatrixOlmAccount, ) def _mk_resp(d): r = _Response() r.status_code = requests.codes.ok r.content = dumps(d).encode() return r mock_put.return_value = _mk_resp({}) def _make_obj(fmt): obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, ) obj.notify_format = fmt obj.access_token = "tok" obj.home_server = "h" obj.user_id = "@u:h" obj.device_id = "DEV" obj._e2ee_account = MatrixOlmAccount() return obj # HTML format obj = _make_obj(NotifyFormat.HTML) session = MatrixMegOlmSession() obj.store.set("e2ee_megolm_!r:h", session.to_dict()) obj.store.set("e2ee_key_shared_!r:h", session.session_id) assert ( obj._e2ee_send_to_room( "!r:h", "body", "

title

", NotifyType.INFO ) is True ) # Markdown format obj = _make_obj(NotifyFormat.MARKDOWN) session = MatrixMegOlmSession() obj.store.set("e2ee_megolm_!r:h", session.to_dict()) obj.store.set("e2ee_key_shared_!r:h", session.session_id) assert ( obj._e2ee_send_to_room("!r:h", "**bold**", "title", NotifyType.INFO) is True ) # key-share flags set after successful share (no pre-seeded flag) obj = _make_obj(NotifyFormat.TEXT) session = MatrixMegOlmSession() obj.store.set("e2ee_megolm_!r:h", session.to_dict()) with mock.patch.object(obj, "_e2ee_share_room_key", return_value=True): assert ( obj._e2ee_send_to_room("!r:h", "body", "", NotifyType.INFO) is True ) # token-as-password mode: access_token == password skips txn increment obj = _make_obj(NotifyFormat.TEXT) obj.access_token = "same" obj.password = "same" session2 = MatrixMegOlmSession() obj.store.set("e2ee_megolm_!r:h", session2.to_dict()) obj.store.set("e2ee_key_shared_!r:h", session2.session_id) assert obj._e2ee_send_to_room("!r:h", "body", "", NotifyType.INFO) is True # key-share failure aborts send obj = _make_obj(NotifyFormat.TEXT) obj.store.clear("e2ee_key_shared_!r:h") with mock.patch.object(obj, "_e2ee_share_room_key", return_value=False): assert ( obj._e2ee_send_to_room("!r:h", "body", "", NotifyType.INFO) is False ) @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_setup_failure(mock_post, mock_get, mock_put): """When E2EE setup fails, send falls back to unencrypted.""" resp = _Response() resp.status_code = requests.codes.ok resp.content = dumps( { "access_token": "tok", "user_id": "@u:h", "home_server": "h", "device_id": "DEV", "room_id": "!r:h", } ).encode() # upload keys call fails fail_resp = _Response() fail_resp.status_code = 500 fail_resp.content = dumps({}).encode() mock_put.return_value = resp mock_post.side_effect = [ resp, # login fail_resp, # keys/upload -> failure resp, # join resp, # logout ] mock_get.return_value = resp obj = NotifyMatrix( host="h", user="u", password="pass", targets=["!r:h"], e2ee=True, secure=True, discovery=False, ) # Falls back to unencrypted -> send succeeds assert obj.send(body="fallback") is True @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_attachment_encrypted( mock_post, mock_get, mock_put ): """E2EE mode encrypts and delivers attachments.""" import tempfile login_resp = { "access_token": "tok", "user_id": "@u:h", "home_server": "h", "device_id": "DEV", } upload_resp = {"content_uri": "mxc://h/enc123"} def _mk_resp(d): r = _Response() r.status_code = requests.codes.ok r.content = dumps(d).encode() return r mock_post.side_effect = [ _mk_resp(login_resp), _mk_resp({}), # keys/upload _mk_resp({"room_id": "!r:h"}), # join _mk_resp(upload_resp), # encrypted attachment upload _mk_resp({}), # logout ] mock_get.return_value = _mk_resp({"joined": {}}) mock_put.return_value = _mk_resp({}) with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as f: f.write(b"test attachment") attach_path = f.name try: attach = AppriseAttachment() attach.add(attach_path) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["!r:h"], e2ee=True, secure=True, discovery=False, ) from apprise.plugins.matrix.e2ee import MatrixMegOlmSession _s = MatrixMegOlmSession() obj.store.set("e2ee_room_enc_!r:h", True) obj.store.set("e2ee_megolm_!r:h", _s.to_dict()) obj.store.set("e2ee_key_shared_!r:h", _s.session_id) assert obj.send(body="body", attach=attach) is True # Two PUTs: encrypted message + encrypted attachment event assert mock_put.call_count == 2 finally: os.unlink(attach_path) @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_send_attachment_errors( mock_post, mock_get, mock_put ): """_e2ee_send_attachment error branches: OSError, network, HTTP fail.""" import tempfile from apprise.plugins.matrix.e2ee import ( MatrixMegOlmSession, MatrixOlmAccount, ) def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r obj = NotifyMatrix( host="h", user="u", password="pass", targets=["!r:h"], e2ee=True, secure=True, discovery=False, ) obj.access_token = "tok" obj.home_server = "h" obj.user_id = "@u:h" obj.device_id = "DEV" obj._e2ee_account = MatrixOlmAccount() session = MatrixMegOlmSession() # OSError reading file -> False with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as f: f.write(b"data") tmp_path = f.name try: class _BadAttach: path = tmp_path name = "f.txt" mimetype = "text/plain" def __len__(self): return 0 with mock.patch("builtins.open", side_effect=OSError("no read")): assert ( obj._e2ee_send_attachment(_BadAttach(), "!r:h", session) is False ) # Network error during upload -> False with mock.patch( "requests.post", side_effect=requests.RequestException("boom"), ): assert ( obj._e2ee_send_attachment(_BadAttach(), "!r:h", session) is False ) # HTTP error from media server -> False with mock.patch("requests.post", return_value=_mk_resp({}, code=500)): assert ( obj._e2ee_send_attachment(_BadAttach(), "!r:h", session) is False ) # Upload succeeds but response body is not valid JSON -> treated as # missing content_uri -> False bad_json_resp = _Response() bad_json_resp.status_code = requests.codes.ok bad_json_resp.content = b"not-json" with mock.patch("requests.post", return_value=bad_json_resp): assert ( obj._e2ee_send_attachment(_BadAttach(), "!r:h", session) is False ) # base_url raises -> False with mock.patch( "apprise.plugins.matrix.base.NotifyMatrix.base_url", new_callable=mock.PropertyMock, side_effect=Exception("discovery fail"), ): assert ( obj._e2ee_send_attachment(_BadAttach(), "!r:h", session) is False ) # No access_token -> Authorization header omitted, upload still works obj_noauth = NotifyMatrix( host="h", user="u", password="pass", targets=["!r:h"], e2ee=True, secure=True, discovery=False, ) obj_noauth.access_token = None obj_noauth.home_server = "h" obj_noauth.user_id = "@u:h" obj_noauth.device_id = "DEV" obj_noauth._e2ee_account = MatrixOlmAccount() sess_noauth = MatrixMegOlmSession() with mock.patch( "requests.post", return_value=_mk_resp({"content_uri": "mxc://h/y"}), ): mock_put.return_value = _mk_resp({}) assert ( obj_noauth._e2ee_send_attachment( _BadAttach(), "!r:h", sess_noauth ) is True ) # access_token == password -> transaction ID not incremented obj2 = NotifyMatrix( host="h", user="u", password="tok", targets=["!r:h"], e2ee=True, secure=True, discovery=False, ) obj2.access_token = "tok" obj2.password = "tok" obj2.home_server = "h" obj2.user_id = "@u:h" obj2.device_id = "DEV" obj2._e2ee_account = MatrixOlmAccount() sess2 = MatrixMegOlmSession() txn_before = obj2.transaction_id upload_r = _mk_resp({"content_uri": "mxc://h/x"}) put_r = _mk_resp({}) with mock.patch("requests.post", return_value=upload_r): mock_put.return_value = put_r result = obj2._e2ee_send_attachment(_BadAttach(), "!r:h", sess2) assert result is True assert obj2.transaction_id == txn_before # Image mimetype -> is_image branch (no 'filename' field added) class _ImageAttach: path = tmp_path name = "photo.png" mimetype = "image/png" def __len__(self): return 4 with mock.patch("requests.post", return_value=upload_r): mock_put.return_value = put_r result = obj2._e2ee_send_attachment(_ImageAttach(), "!r:h", sess2) assert result is True finally: os.unlink(tmp_path) @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_send_attachment_invalid( mock_post, mock_get, mock_put ): """Invalid attachment and _e2ee_send_attachment failure mark has_error.""" import tempfile login_resp = { "access_token": "tok", "user_id": "@u:h", "home_server": "h", "device_id": "DEV", } def _mk_resp(d): r = _Response() r.status_code = requests.codes.ok r.content = dumps(d).encode() return r mock_post.side_effect = [ _mk_resp(login_resp), _mk_resp({}), # keys/upload _mk_resp({"room_id": "!r:h"}), # join _mk_resp({}), # logout ] mock_get.return_value = _mk_resp({}) mock_put.return_value = _mk_resp({}) with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as f: f.write(b"data") attach_path = f.name try: from apprise.plugins.matrix.e2ee import MatrixMegOlmSession obj = NotifyMatrix( host="h", user="u", password="pass", targets=["!r:h"], e2ee=True, secure=True, discovery=False, ) _s = MatrixMegOlmSession() obj.store.set("e2ee_room_enc_!r:h", True) obj.store.set("e2ee_megolm_!r:h", _s.to_dict()) obj.store.set("e2ee_key_shared_!r:h", _s.session_id) attach = AppriseAttachment() attach.add(attach_path) # _e2ee_send_attachment fails -> send returns False with mock.patch.object( obj, "_e2ee_send_attachment", return_value=False ): assert obj.send(body="body", attach=attach) is False # Invalid attachment (falsy) in list -> has_error set and loop breaks mock_post.reset_mock() mock_post.side_effect = [ _mk_resp(login_resp), _mk_resp({}), # keys/upload _mk_resp({"room_id": "!r:h"}), # join _mk_resp({}), # logout ] obj2 = NotifyMatrix( host="h", user="u", password="pass", targets=["!r:h"], e2ee=True, secure=True, discovery=False, ) _s2 = MatrixMegOlmSession() obj2.store.set("e2ee_room_enc_!r:h", True) obj2.store.set("e2ee_megolm_!r:h", _s2.to_dict()) obj2.store.set("e2ee_key_shared_!r:h", _s2.session_id) # Add a non-existent file to get a falsy attachment bad_attach = AppriseAttachment() bad_attach.add("/nonexistent/path/file.txt") with mock.patch.object(obj2, "_e2ee_send_to_room", return_value=True): assert obj2.send(body="body", attach=bad_attach) is False finally: os.unlink(attach_path) @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_send_room_failure(mock_post, mock_get, mock_put): """E2EE path returns False when _e2ee_send_to_room fails.""" login_resp = { "access_token": "tok", "user_id": "@u:h", "home_server": "h", "device_id": "DEV", } def _mk_resp(d): r = _Response() r.status_code = requests.codes.ok r.content = dumps(d).encode() return r mock_post.side_effect = [ _mk_resp(login_resp), _mk_resp({}), # keys/upload _mk_resp({"room_id": "!r:h"}), # join _mk_resp({}), # logout ] mock_get.return_value = _mk_resp({}) mock_put.return_value = _mk_resp({}) from apprise.plugins.matrix.e2ee import MatrixMegOlmSession obj = NotifyMatrix( host="h", user="u", password="pass", targets=["!r:h"], e2ee=True, secure=True, discovery=False, ) obj.store.set("e2ee_room_enc_!r:h", True) _s = MatrixMegOlmSession() obj.store.set("e2ee_megolm_!r:h", _s.to_dict()) obj.store.set("e2ee_key_shared_!r:h", _s.session_id) with mock.patch.object(obj, "_e2ee_send_to_room", return_value=False): assert obj.send(body="fail") is False @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_register_with_device_id(mock_post, mock_get, mock_put): """Registration response that includes device_id persists it.""" r = _Response() r.status_code = requests.codes.ok r.content = dumps( { "access_token": "tok", "user_id": "@u:h", "home_server": "h", "device_id": "REG_DEV", } ).encode() mock_post.return_value = r obj = NotifyMatrix(host="h", discovery=False) assert obj._register() is True assert obj.device_id == "REG_DEV" assert obj.store.get("device_id") == "REG_DEV" @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_login_reuses_device_id(mock_post, mock_get, mock_put): """Login payload reuses the stored device ID when present.""" r = _Response() r.status_code = requests.codes.ok r.content = dumps( { "access_token": "tok", "user_id": "@u:h", "home_server": "h", "device_id": "DEV_REUSED", } ).encode() mock_post.return_value = r obj = NotifyMatrix(host="h", user="u", password="pass", discovery=False) obj.device_id = "DEV_REUSED" assert obj._login() is True payload = mock_post.call_args.kwargs["data"] assert '"device_id": "DEV_REUSED"' in payload @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_register_reuses_device_id( mock_post, mock_get, mock_put ): """Register payload reuses the stored device ID when present.""" r = _Response() r.status_code = requests.codes.ok r.content = dumps( { "access_token": "tok", "user_id": "@u:h", "home_server": "h", "device_id": "DEV_REUSED", } ).encode() mock_post.return_value = r obj = NotifyMatrix(host="h", user="u", password="pass", discovery=False) obj.device_id = "DEV_REUSED" assert obj._register() is True payload = mock_post.call_args.kwargs["data"] assert '"device_id": "DEV_REUSED"' in payload @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_account_bad_store_data(): """Corrupt e2ee_account in store is silently ignored.""" obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, ) obj.store.set("e2ee_account", {"ik": "NOTVALID", "sk": "NOTVALID"}) # Re-instantiate with the same store to trigger the restore path obj2 = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, store=obj.store, ) # Bad data is suppressed; account is None until setup is called assert obj2._e2ee_account is None def test_plugin_matrix_del_suppresses_logout_exceptions(): """__del__ should never raise if logout fails during GC cleanup.""" obj = NotifyMatrix(host="h", user="u", password="pass", targets=["#r"]) obj.access_token = "tok" with mock.patch.object(obj, "_logout", side_effect=RuntimeError("boom")): obj.__del__() # --------------------------------------------------------------------------- # DM / direct-message tests # --------------------------------------------------------------------------- def test_plugin_matrix_dm_target_separation(): """@user targets are separated from room targets at init time.""" obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#general", "@alice", "@bob:other.com", "!room:h"], discovery=False, ) assert "#general" in obj.rooms assert "!room:h" in obj.rooms assert "@alice" in obj.users assert "@bob:other.com" in obj.users assert len(obj.rooms) == 2 assert len(obj.users) == 2 # __len__ accounts for both rooms and users assert len(obj) == 4 def test_plugin_matrix_dm_url_roundtrip(): """@user targets survive a url() -> parse_url() -> NotifyMatrix cycle.""" obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#room", "@alice"], discovery=False, secure=True, ) url = obj.url() # @ is URL-encoded to %40 inside the path segment assert "%40alice" in url result = NotifyMatrix.parse_url(url) assert result is not None obj2 = NotifyMatrix(**result) assert "@alice" in obj2.users assert any("room" in r for r in obj2.rooms) @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_dm_room_find_cached(mock_post, mock_get, mock_put): """_dm_room_find_or_create returns cached room ID without HTTP calls.""" def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r mock_post.return_value = _mk_resp({}) mock_get.return_value = _mk_resp({}) mock_put.return_value = _mk_resp({}) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["@alice"], discovery=False, ) obj.access_token = "tok" obj.user_id = "@u:h" obj.home_server = "h" # Seed the cache obj.store.set("dm_room_@alice:h", "!dm123:h") room_id = obj._dm_room_find_or_create("@alice") assert room_id == "!dm123:h" # No HTTP calls should have been made assert mock_get.call_count == 0 assert mock_post.call_count == 0 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_dm_room_find_via_mdirect(mock_post, mock_get, mock_put): """_dm_room_find_or_create finds existing room from m.direct data.""" def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r mdirect = {"@alice:h": ["!existing_dm:h"]} mock_get.return_value = _mk_resp(mdirect) mock_post.return_value = _mk_resp({}) mock_put.return_value = _mk_resp({}) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["@alice"], discovery=False, ) obj.access_token = "tok" obj.user_id = "@u:h" obj.home_server = "h" room_id = obj._dm_room_find_or_create("@alice") assert room_id == "!existing_dm:h" # Result is now cached assert obj.store.get("dm_room_@alice:h") == "!existing_dm:h" # No createRoom POST should have been made assert mock_post.call_count == 0 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_dm_room_create_new(mock_post, mock_get, mock_put): """_dm_room_find_or_create creates a new room when none exists.""" def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r # GET m.direct returns empty (no existing DM for alice) mock_get.return_value = _mk_resp({}) # First POST = createRoom, second = (not called) mock_post.return_value = _mk_resp({"room_id": "!new_dm:h"}) mock_put.return_value = _mk_resp({}) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["@alice"], discovery=False, ) obj.access_token = "tok" obj.user_id = "@u:h" obj.home_server = "h" room_id = obj._dm_room_find_or_create("@alice") assert room_id == "!new_dm:h" # Should be cached assert obj.store.get("dm_room_@alice:h") == "!new_dm:h" # m.direct PUT should have been called to update the mapping assert mock_put.call_count >= 1 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_dm_room_create_failure(mock_post, mock_get, mock_put): """_dm_room_find_or_create returns None when createRoom fails.""" def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r mock_get.return_value = _mk_resp({}) mock_post.return_value = _mk_resp( {"errcode": "M_FORBIDDEN"}, code=requests.codes.forbidden ) mock_put.return_value = _mk_resp({}) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["@alice"], discovery=False, ) obj.access_token = "tok" obj.user_id = "@u:h" obj.home_server = "h" assert obj._dm_room_find_or_create("@alice") is None @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_dm_room_create_no_room_id( mock_post, mock_get, mock_put ): """_dm_room_find_or_create returns None when createRoom omits room_id.""" def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r mock_get.return_value = _mk_resp({}) # createRoom returns 200 but no room_id key mock_post.return_value = _mk_resp({}) mock_put.return_value = _mk_resp({}) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["@alice"], discovery=False, ) obj.access_token = "tok" obj.user_id = "@u:h" obj.home_server = "h" assert obj._dm_room_find_or_create("@alice") is None def test_plugin_matrix_dm_invalid_user(): """_dm_room_find_or_create returns None for a malformed user target.""" obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], discovery=False, ) obj.access_token = "tok" obj.user_id = "@u:h" obj.home_server = "h" # No @ prefix -- not a valid user target assert obj._dm_room_find_or_create("notauser") is None @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_dm_send_notification(mock_post, mock_get, mock_put): """Sending to @user target resolves DM room and delivers message.""" def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r mock_get.return_value = _mk_resp({}) mock_put.return_value = _mk_resp({}) mock_post.side_effect = [ _mk_resp( {"access_token": "tok", "user_id": "@u:h", "home_server": "h"} ), # login _mk_resp({"room_id": "!new_dm:h"}), # createRoom _mk_resp({}), # logout ] obj = NotifyMatrix( host="h", user="u", password="pass", targets=["@alice"], discovery=False, e2ee=False, ) # Pre-seed DM room so _room_join succeeds def _join_side(*a, **kw): return "!new_dm:h" with ( mock.patch.object(obj, "_room_join", side_effect=_join_side), mock.patch.object( obj, "_dm_room_find_or_create", return_value="!new_dm:h" ), ): assert obj.send(body="hello DM") is True def test_plugin_matrix_failed_dm_is_retried(): """A resolved DM is recorded only after its message succeeds.""" obj = NotifyMatrix( host="h", user="u", password="pass", targets=["@alice"], discovery=False, e2ee=False, ) obj.access_token = "tok" obj.user_id = "@u:h" obj.home_server = "h" tracker_token = _delivery_tracker.set(set()) try: with ( mock.patch.object( obj, "_dm_room_find_or_create", return_value="!dm:h", ) as resolve, mock.patch.object(obj, "_room_join", return_value="!dm:h"), mock.patch.object( obj, "_fetch", side_effect=[ (False, {}, requests.codes.bad_request), (True, {}, requests.codes.ok), ], ), ): assert obj.send(body="hello") is False assert obj.is_delivered(("user", "@alice")) is False assert obj.send(body="hello") is True assert resolve.call_count == 2 assert obj.is_delivered(("user", "@alice")) is True finally: _delivery_tracker.reset(tracker_token) def test_plugin_matrix_retry_does_not_repeat_attachment_message(): """Retry the failed body without repeating a visible attachment.""" obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#general"], discovery=False, e2ee=False, ) obj.access_token = "tok" obj.user_id = "@u:h" obj.home_server = "h" attachment_payload = { "msgtype": "m.file", "body": "report.pdf", "url": "mxc://h/file", } tracker_token = _delivery_tracker.set(set()) try: with ( mock.patch.object(obj, "_room_join", return_value="!room:h"), mock.patch.object( obj, "_send_attachments", return_value=[attachment_payload.copy()], ), mock.patch.object( obj, "_fetch", side_effect=[ (True, {}, requests.codes.ok), (False, {}, requests.codes.bad_request), (True, {}, requests.codes.ok), ], ) as fetch, ): assert obj.send(body="hello", attach=[mock.Mock()]) is False assert obj.send(body="hello", attach=[mock.Mock()]) is True finally: _delivery_tracker.reset(tracker_token) attachment_calls = [ call for call in fetch.call_args_list if call.kwargs.get("payload", {}).get("msgtype") == "m.file" ] assert len(attachment_calls) == 1 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_dm_only_users_no_rooms_skips_joined( mock_post, mock_get, mock_put ): """When only @user targets are present, _joined_rooms is not queried.""" def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r mock_get.return_value = _mk_resp({}) mock_put.return_value = _mk_resp({}) mock_post.side_effect = [ _mk_resp( {"access_token": "tok", "user_id": "@u:h", "home_server": "h"} ), # login _mk_resp({}), # logout ] obj = NotifyMatrix( host="h", user="u", password="pass", targets=["@alice"], discovery=False, e2ee=False, ) with ( mock.patch.object(obj, "_joined_rooms", return_value=[]) as mock_jr, mock.patch.object(obj, "_dm_room_find_or_create", return_value=None), ): obj.send(body="hi") # _joined_rooms must NOT have been called assert mock_jr.call_count == 0 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_dm_room_create_mdirect_get_fails( mock_post, mock_get, mock_put ): """_dm_room_find_or_create still creates room if m.direct GET fails.""" def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r # GET fails (404) mock_get.return_value = _mk_resp({}, code=404) mock_post.return_value = _mk_resp({"room_id": "!fallback:h"}) mock_put.return_value = _mk_resp({}) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["@alice"], discovery=False, ) obj.access_token = "tok" obj.user_id = "@u:h" obj.home_server = "h" room_id = obj._dm_room_find_or_create("@alice") assert room_id == "!fallback:h" @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_dm_room_create_no_user_id( mock_post, mock_get, mock_put ): """_dm_room_find_or_create creates room when user_id is None (skips the m.direct GET and PUT).""" def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r mock_get.return_value = _mk_resp({}) mock_post.return_value = _mk_resp({"room_id": "!nodm:h"}) mock_put.return_value = _mk_resp({}) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["@alice"], discovery=False, ) obj.access_token = "tok" # user_id intentionally left as None so the m.direct branch is skipped obj.home_server = "h" room_id = obj._dm_room_find_or_create("@alice") assert room_id == "!nodm:h" # No GET and no PUT for m.direct since user_id is None assert mock_get.call_count == 0 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_dm_room_create_e2ee(mock_post, mock_get, mock_put): """_dm_room_find_or_create with e2ee=True/secure=True embeds initial_state and pre-seeds the room encryption cache.""" from apprise.plugins.matrix.base import MATRIX_E2EE_SUPPORT if not MATRIX_E2EE_SUPPORT: pytest.skip("cryptography package not installed") def _mk_resp(d, code=requests.codes.ok): r = _Response() r.status_code = code r.content = dumps(d).encode() return r mock_get.return_value = _mk_resp({}) mock_post.return_value = _mk_resp({"room_id": "!dm_e2ee:h"}) mock_put.return_value = _mk_resp({}) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["@alice"], e2ee=True, secure=True, discovery=False, ) obj.access_token = "tok" obj.user_id = "@u:h" obj.home_server = "h" captured_payloads = [] _orig_fetch = obj._fetch def _capture_fetch(path, payload=None, **kw): if path == "/createRoom": captured_payloads.append(payload or {}) return _orig_fetch(path, payload=payload, **kw) obj._fetch = _capture_fetch room_id = obj._dm_room_find_or_create("@alice") assert room_id == "!dm_e2ee:h" # Verify initial_state was sent with m.room.encryption assert len(captured_payloads) == 1 init_state = captured_payloads[0].get("initial_state", []) assert any(s.get("type") == "m.room.encryption" for s in init_state) # Room encryption cache must be pre-seeded assert obj.store.get("e2ee_room_enc_!dm_e2ee:h") is True # --------------------------------------------------------------------------- # home_server recovery-from-user_id coverage tests # --------------------------------------------------------------------------- def test_plugin_matrix_init_recovers_home_server_from_user_id(tmpdir): """__init__ extracts home_server from a cached user_id when home_server was not stored (older cache entries that predate the home_server field).""" # A disk-backed asset ensures two separate NotifyMatrix instances that # share the same url_id() also share the same persistent store file. asset = AppriseAsset( storage_mode=PersistentStoreMode.FLUSH, storage_path=str(tmpdir), ) # First instance: simulate a login that wrote user_id/access_token but # did NOT write home_server (older Apprise behaviour). obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], asset=asset ) obj.store.set("user_id", "@u:recovered.com") obj.store.set("access_token", "tok") # Flush to disk before the second instance reads it. obj.store.flush() # Second instance with the same credentials (same url_id) reads the store. # The recovery path should derive home_server from the stored user_id. obj2 = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], asset=asset ) assert obj2.home_server == "recovered.com" def test_plugin_matrix_init_no_home_server_recovery_without_colon(tmpdir): """__init__ leaves home_server as None when the cached user_id has no colon -- the recovery split yields a single part and the assignment is skipped.""" asset = AppriseAsset( storage_mode=PersistentStoreMode.FLUSH, storage_path=str(tmpdir), ) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], asset=asset ) # user_id with no colon -- split(":", 1) produces only one element. obj.store.set("user_id", "nocolon") obj.store.set("access_token", "tok") obj.store.flush() obj2 = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], asset=asset ) # No colon in user_id means home_server cannot be extracted. assert obj2.home_server is None @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_whoami_recovers_home_server_from_user_id( mock_post, mock_get, mock_put ): """_whoami() extracts home_server from user_id when the server does not return a home_server field and self.home_server is unset.""" r = _Response() r.status_code = requests.codes.ok r.content = dumps( {"user_id": "@u:whoami.example.com", "device_id": "DEVX"} ).encode() mock_get.return_value = r obj = NotifyMatrix(host="h", user="u", password="pass", discovery=False) obj.access_token = "tok" obj.home_server = None assert obj._whoami() is True assert obj.home_server == "whoami.example.com" def test_plugin_matrix_room_create_recovers_home_server_from_user_id(): """_room_create() falls back to extracting home_server from user_id when both the room alias and self.home_server carry no homeserver part.""" obj = NotifyMatrix(host="h", user="u", password="pass", targets=["#r"]) obj.access_token = "tok" obj.home_server = None obj.user_id = "@u:create.example.com" # Capture the /createRoom POST so we can verify the room name used. captured = {} def _fake_fetch(path, payload=None, **kw): if path == "/createRoom": captured["payload"] = payload return True, {"room_id": "!newroom:create.example.com"}, 200 return False, {}, 404 with mock.patch.object(obj, "_fetch", side_effect=_fake_fetch): room_id = obj._room_create("#bare_alias") assert room_id == "!newroom:create.example.com" # The room name in the request must use the homeserver from user_id. assert captured["payload"]["room_alias_name"] == "bare_alias" def test_plugin_matrix_room_create_returns_none_without_home_server(): """_room_create() returns None when neither the alias, self.home_server, nor self.user_id can supply a homeserver component.""" obj = NotifyMatrix(host="h", user="u", password="pass", targets=["#r"]) obj.access_token = "tok" obj.home_server = None # user_id with no colon -> extraction yields nothing. obj.user_id = "nocolon" result = obj._room_create("#bare_alias") assert result is None def test_plugin_matrix_room_id_recovers_home_server_from_user_id(): """_room_id() falls back to extracting home_server from user_id when both the alias and self.home_server carry no homeserver part.""" obj = NotifyMatrix(host="h", user="u", password="pass", targets=["#r"]) obj.access_token = "tok" obj.home_server = None obj.user_id = "@u:lookup.example.com" def _fake_fetch(path, payload=None, **kw): return True, {"room_id": "!found:lookup.example.com"}, 200 with mock.patch.object(obj, "_fetch", side_effect=_fake_fetch): room_id = obj._room_id("#bare_alias") assert room_id == "!found:lookup.example.com" def test_plugin_matrix_room_id_returns_none_without_home_server(): """_room_id() returns None when neither the alias, self.home_server, nor self.user_id can supply a homeserver component.""" obj = NotifyMatrix(host="h", user="u", password="pass", targets=["#r"]) obj.access_token = "tok" obj.home_server = None obj.user_id = "nocolon" result = obj._room_id("#bare_alias") assert result is None @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_html_plain_fallback(mock_post, mock_get, mock_put): """Declared HTML receives a plain-text fallback.""" from apprise.common import NotifyFormat response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok mock_get.return_value = request mock_post.return_value = request mock_put.return_value = request kwargs = NotifyMatrix.parse_url( "matrix://user:passwd@hostname/#abcd?format=html" ) obj = NotifyMatrix(**kwargs) # Declaring the source confirms that the content is HTML. assert ( bool( obj.notify( title="Title", body="Bold text", body_format=NotifyFormat.HTML, ) ) is True ) payload = loads(mock_put.call_args.kwargs["data"]) assert payload["formatted_body"] == "

Title

Bold text" # The plain-text fallback must not carry the raw markup a second time. assert "" not in payload["body"] assert "Bold text" in payload["body"] @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_html_passthrough_untouched( mock_post, mock_get, mock_put ): """Undeclared content remains unchanged when routed as rich text.""" response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok mock_get.return_value = request mock_post.return_value = request mock_put.return_value = request kwargs = NotifyMatrix.parse_url( "matrix://user:passwd@hostname/#abcd?format=html" ) obj = NotifyMatrix(**kwargs) # No body_format declared: a passthrough source. assert bool(obj.notify(title="Title", body="Bold text")) is True payload = loads(mock_put.call_args.kwargs["data"]) assert payload["formatted_body"] == "

Title

Bold text" # Preserve the source when its format is unknown. assert payload["body"] == "# Title\r\nBold text" @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_html_plain_fallback(mock_post, mock_get, mock_put): """Encrypted HTML also receives a plain-text fallback.""" from apprise.common import NotifyFormat from apprise.plugins.matrix.e2ee import ( MatrixMegOlmSession, MatrixOlmAccount, ) def _mk_resp(d): r = _Response() r.status_code = requests.codes.ok r.content = dumps(d).encode() return r mock_put.return_value = _mk_resp({}) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True ) obj.access_token = "tok" obj.home_server = "h" obj.user_id = "@u:h" obj.device_id = "DEV" obj._e2ee_account = MatrixOlmAccount() session = MatrixMegOlmSession() obj.store.set("e2ee_megolm_!r:h", session.to_dict()) obj.store.set("e2ee_key_shared_!r:h", session.session_id) # Capture the event before encryption. captured = {} original_encrypt = MatrixMegOlmSession.encrypt def _spy_encrypt(self, event): captured["event"] = event return original_encrypt(self, event) with mock.patch.object(MatrixMegOlmSession, "encrypt", _spy_encrypt): assert ( obj._e2ee_send_to_room( "!r:h", "Bold text", "Title", NotifyType.INFO, body_format=NotifyFormat.HTML, body_passthrough=False, ) is True ) msg_content = captured["event"]["content"] assert msg_content["formatted_body"] == "

Title

Bold text" assert "" not in msg_content["body"] assert "Bold text" in msg_content["body"] def test_plugin_matrix_e2ee_body_limit(): """Encrypted messages use a smaller limit when E2EE is available.""" obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=False, secure=True, ) assert obj.e2ee is False assert obj.body_maxlen == obj.body_maxlen_default # Non-TLS connections cannot use encryption. obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, secure=False, ) assert obj.e2ee is True assert obj.body_maxlen == obj.body_maxlen_default obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], e2ee=True, secure=True, ) assert obj.e2ee is True if CRYPTOGRAPHY_AVAILABLE: assert obj.body_maxlen == obj.body_maxlen_e2ee assert obj.body_maxlen < obj.body_maxlen_default else: # Encryption also requires the optional cryptography package. assert obj.body_maxlen == obj.body_maxlen_default def test_plugin_matrix_webhook_mode_body_maxlen_unaffected(): """Webhook modes keep their flat limit for their distinct payloads.""" from apprise.common import NotifyFormat, OverflowMode from apprise.plugins.matrix import base as matrix_base from apprise.plugins.matrix.base import MatrixWebhookMode for mode in ( MatrixWebhookMode.SLACK, MatrixWebhookMode.MATRIX, MatrixWebhookMode.HOOKSHOT, ): obj = NotifyMatrix( host="h", user="u", token="tok", mode=mode, secure=True ) # Webhooks ignore the default E2EE setting. assert obj.e2ee is True assert obj.body_maxlen == obj.body_maxlen_default list( obj._build_send_calls( body="hello world " * 20, title="", body_format=NotifyFormat.HTML, overflow=OverflowMode.SPLIT, ) ) # Webhook preparation does not activate content-aware sizing. assert matrix_base._matrix_effective_body_maxlen_var.get() is None assert obj.body_maxlen == obj.body_maxlen_default def test_plugin_matrix_body_maxlen_content_aware(): """Direct sends choose a content-aware limit while preparing chunks.""" from apprise.common import OverflowMode from apprise.plugins.matrix import base as matrix_base obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], secure=True, e2ee=False, ) # No send in progress: fall back to the flat default. assert matrix_base._matrix_effective_body_maxlen_var.get() is None assert obj.body_maxlen == obj.body_maxlen_default observed = {} # Capture the computed limit while the generator is active. original_property = NotifyMatrix.body_maxlen.fget def _spy_body_maxlen(self): observed["body_maxlen"] = ( matrix_base._matrix_effective_body_maxlen_var.get() ) return original_property(self) with mock.patch.object( NotifyMatrix, "body_maxlen", property(_spy_body_maxlen) ): list( obj._build_send_calls( # This length makes UTF-8 density determine the limit. body="\U0001f600" * 20000, title="", overflow=OverflowMode.SPLIT, ) ) # Emoji require four UTF-8 bytes and therefore a smaller limit. emoji_limit = observed["body_maxlen"] assert emoji_limit < obj.body_maxlen_default # Restored after the call completes. assert matrix_base._matrix_effective_body_maxlen_var.get() is None with mock.patch.object( NotifyMatrix, "body_maxlen", property(_spy_body_maxlen) ): list( obj._build_send_calls( body="hello world " * 10000, title="", overflow=OverflowMode.SPLIT, ) ) # Plain ASCII content allows a much larger character limit. assert observed["body_maxlen"] > emoji_limit assert matrix_base._matrix_effective_body_maxlen_var.get() is None @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_concentrated_density_split( mock_post, mock_get, mock_put ): """Dense Unicode regions must fit even when ASCII lowers the average.""" from apprise.common import OverflowMode response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok mock_get.return_value = request mock_post.return_value = request mock_put.return_value = request obj = NotifyMatrix(host="hostname", user="user", password="passwd") original_body = ("\U0001f600" * 20000) + ("a" * 80000) assert ( bool(obj.notify(body=original_body, overflow=OverflowMode.SPLIT)) is True ) assert mock_put.call_count > 1 reconstructed = "" for call in mock_put.call_args_list: payload = loads(call.kwargs["data"]) encoded_len = len(dumps(payload, ensure_ascii=False).encode("utf-8")) assert encoded_len <= 65536 reconstructed += payload["body"] # No content was dropped or duplicated across the split events. assert reconstructed == original_body @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_json_escape_heavy_split(mock_post, mock_get, mock_put): """Backslash-heavy bodies must fit after JSON escape expansion.""" from apprise.common import OverflowMode response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok mock_get.return_value = request mock_post.return_value = request mock_put.return_value = request obj = NotifyMatrix(host="hostname", user="user", password="passwd") original_body = "\\" * 70000 assert ( bool(obj.notify(body=original_body, overflow=OverflowMode.SPLIT)) is True ) assert mock_put.call_count > 1 reconstructed = "" for call in mock_put.call_args_list: payload = loads(call.kwargs["data"]) encoded_len = len(dumps(payload, ensure_ascii=False).encode("utf-8")) assert encoded_len <= 65536 reconstructed += payload["body"] assert reconstructed == original_body @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_mixed_escape_and_unicode_split( mock_post, mock_get, mock_put ): """Mixed JSON escapes and Unicode must remain within the byte limit.""" from apprise.common import OverflowMode response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok mock_get.return_value = request mock_post.return_value = request mock_put.return_value = request obj = NotifyMatrix(host="hostname", user="user", password="passwd") # Include escapes, a non-whitespace control, CJK, and emoji characters. # Whitespace controls are avoided because chunk splitting may trim them. cluster = '"\\\x01δΈ­\U0001f600' original_body = cluster * 15000 assert ( bool(obj.notify(body=original_body, overflow=OverflowMode.SPLIT)) is True ) assert mock_put.call_count > 1 reconstructed = "" for call in mock_put.call_args_list: payload = loads(call.kwargs["data"]) encoded_len = len(dumps(payload, ensure_ascii=False).encode("utf-8")) assert encoded_len <= 65536 reconstructed += payload["body"] assert reconstructed == original_body @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_html_dual_representation_split( mock_post, mock_get, mock_put ): """HTML events fit with both plain and formatted representations.""" from apprise.common import NotifyFormat, OverflowMode response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok mock_get.return_value = request mock_post.return_value = request mock_put.return_value = request obj = NotifyMatrix(host="hostname", user="user", password="passwd") body = ("word " * 12999) + "end" assert ( bool( obj.notify( body=body, body_format=NotifyFormat.HTML, overflow=OverflowMode.SPLIT, ) ) is True ) assert mock_put.call_count > 1 for call in mock_put.call_args_list: payload = loads(call.kwargs["data"]) assert "formatted_body" in payload encoded_len = len(dumps(payload, ensure_ascii=False).encode("utf-8")) assert encoded_len <= 65536 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_html_split(mock_post, mock_get, mock_put): """Encrypted HTML chunks fit after formatting and MegOLM expansion.""" from apprise.common import NotifyFormat, OverflowMode from apprise.plugins.matrix.e2ee import MatrixMegOlmSession def _mk_resp(d): r = _Response() r.status_code = requests.codes.ok r.content = dumps(d).encode() return r login_resp = { "access_token": "tok", "user_id": "@u:h", "home_server": "h", "device_id": "DEV", } mock_post.side_effect = [ _mk_resp(login_resp), _mk_resp({}), # keys/upload _mk_resp({"room_id": "!r:h"}), # join _mk_resp({}), # logout ] mock_get.return_value = _mk_resp({}) mock_put.return_value = _mk_resp({}) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["!r:h"], e2ee=True, secure=True, discovery=False, ) # Pre-seed encryption state so the test only sends message events. session = MatrixMegOlmSession() obj.store.set("e2ee_room_enc_!r:h", True) obj.store.set("e2ee_megolm_!r:h", session.to_dict()) obj.store.set("e2ee_key_shared_!r:h", session.session_id) body = ("hello world this is a test " * 2000).strip() assert ( bool( obj.notify( body=body, body_format=NotifyFormat.HTML, overflow=OverflowMode.SPLIT, ) ) is True ) assert mock_put.call_count > 1 for call in mock_put.call_args_list: payload = loads(call.kwargs["data"]) assert payload["algorithm"] == "m.megolm.v1.aes-sha2" encoded_len = len(dumps(payload, ensure_ascii=False).encode("utf-8")) assert encoded_len <= 65536 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") @pytest.mark.parametrize("device_id_len", [3, 128, 255, 1000]) def test_plugin_matrix_e2ee_long_device_id_and_title( mock_post, mock_get, mock_put, device_id_len ): """Long device IDs and escaped titles must fit encrypted events. Both values are measured because neither raw character counts nor a fixed envelope estimate represent their final JSON byte cost. """ from apprise.common import NotifyFormat, OverflowMode from apprise.plugins.matrix.e2ee import MatrixMegOlmSession def _mk_resp(d): r = _Response() r.status_code = requests.codes.ok r.content = dumps(d).encode() return r mock_get.return_value = _mk_resp({}) mock_put.return_value = _mk_resp({}) mock_post.return_value = _mk_resp({}) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["!r:h"], e2ee=True, secure=True, discovery=False, ) session = MatrixMegOlmSession() obj.store.set("e2ee_room_enc_!r:h", True) obj.store.set("e2ee_megolm_!r:h", session.to_dict()) obj.store.set("e2ee_key_shared_!r:h", session.session_id) obj.access_token = "tok" obj.home_server = "h" obj.user_id = "@u:h" # A known device_id (as if cached from a prior login) is measured # exactly rather than falling back to the generous default. obj.device_id = "D" * device_id_len title = "\x01" * 250 # near title_maxlen, maximal 6-byte JSON escape body = "\x01" * 200000 assert ( bool( obj.notify( body=body, title=title, body_format=NotifyFormat.HTML, overflow=OverflowMode.SPLIT, ) ) is True ) assert mock_put.call_count > 1 for call in mock_put.call_args_list: payload = loads(call.kwargs["data"]) assert payload["algorithm"] == "m.megolm.v1.aes-sha2" encoded_len = len(dumps(payload, ensure_ascii=False).encode("utf-8")) assert encoded_len <= 65536 @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_e2ee_rejects_late_oversize(): """Refuse a first-login event that becomes too large after sizing. The final device ID must not consume the safety margin reserved for homeserver-added metadata. """ from apprise.common import NotifyFormat, OverflowMode from apprise.plugins.matrix.base import ( MATRIX_EVENT_BYTE_LIMIT, MATRIX_EVENT_SAFETY_MARGIN, ) from apprise.plugins.matrix.e2ee import ( MatrixMegOlmSession, MatrixOlmAccount, ) safe_byte_budget = MATRIX_EVENT_BYTE_LIMIT - MATRIX_EVENT_SAFETY_MARGIN obj = NotifyMatrix( host="h", user="u", password="pass", targets=["!r:h"], e2ee=True, secure=True, discovery=False, ) obj.access_token = "tok" obj.home_server = "h" obj.user_id = "@u:h" # Unknown at sizing time: _build_send_calls() uses the fallback. obj.device_id = None obj._e2ee_account = MatrixOlmAccount() # A small, fixed body/title: content isn't what's under test here, # device_id's contribution is. device_id sits outside the encrypted # ciphertext (a plain sibling JSON field), so every extra character # added to it adds exactly one byte to the final encrypted payload, # letting the boundary below be targeted precisely. calls = list( obj._build_send_calls( body="hello", title="", overflow=OverflowMode.SPLIT ) ) first = calls[0] session = MatrixMegOlmSession() obj.store.set("e2ee_room_enc_!r:h", True) obj.store.set("e2ee_megolm_!r:h", session.to_dict()) obj.store.set("e2ee_key_shared_!r:h", session.session_id) def send_with_device_id(device_id_len): # The real device_id becomes known only now, as if login # happened after sizing already ran. obj.device_id = "D" * device_id_len with ( mock.patch.object(obj, "_e2ee_share_room_key", return_value=True), mock.patch.object( obj, "_fetch", return_value=(True, {}, 200) ) as mock_fetch, ): result = obj._e2ee_send_to_room( "!r:h", first["body"], first["title"], NotifyType.INFO, NotifyFormat.HTML, False, ) sent = mock_fetch.call_count > 0 assert result is sent if sent: payload = mock_fetch.call_args.kwargs["payload"] return len(dumps(payload, ensure_ascii=False).encode("utf-8")) return None # Measure this content's baseline (device_id excluded) once. baseline_bytes = send_with_device_id(0) # Exactly at the safe budget: still accepted. at_budget_len = safe_byte_budget - baseline_bytes assert send_with_device_id(at_budget_len) == safe_byte_budget # One byte into the reserved margin -- between the safe budget and # the hard ceiling, previously accepted by mistake -- must now be # refused. past_budget_len = at_budget_len + 1 assert send_with_device_id(past_budget_len) is None # Comfortably past the full ceiling: also refused. assert send_with_device_id(MATRIX_EVENT_BYTE_LIMIT) is None @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") @pytest.mark.skipif(not CRYPTOGRAPHY_AVAILABLE, reason="Requires cryptography") def test_plugin_matrix_surrogate_sanitized_end_to_end( mock_post, mock_get, mock_put ): """A lone UTF-16 surrogate in the body/title must not crash delivery on any send path -- direct-server, E2EE, or webhook.""" from apprise.common import NotifyFormat from apprise.plugins.matrix.base import MatrixWebhookMode from apprise.plugins.matrix.e2ee import MatrixMegOlmSession def _mk_resp(d): r = _Response() r.status_code = requests.codes.ok r.content = dumps(d).encode() return r lone_surrogate_body = "hello " + chr(0xD800) + " world" lone_surrogate_title = "t" + chr(0xDC00) + "t" # Direct-server plaintext path. response_obj = { "room_id": "!abc:localhost", "room_alias": "#abc:localhost", "joined_rooms": ["!abc:localhost"], "access_token": "abcd1234", "home_server": "localhost", } mock_get.return_value = _mk_resp(response_obj) mock_post.return_value = _mk_resp(response_obj) mock_put.return_value = _mk_resp({}) obj = NotifyMatrix(host="hostname", user="user", password="passwd") assert bool(obj.notify(body=lone_surrogate_body)) is True payload = loads(mock_put.call_args.kwargs["data"]) # The lone surrogate is replaced, never crashing serialization. payload["body"].encode("utf-8") # E2EE path. mock_get.return_value = _mk_resp({}) mock_put.return_value = _mk_resp({}) mock_post.return_value = _mk_resp({}) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["!r:h"], e2ee=True, secure=True, discovery=False, ) session = MatrixMegOlmSession() obj.store.set("e2ee_room_enc_!r:h", True) obj.store.set("e2ee_megolm_!r:h", session.to_dict()) obj.store.set("e2ee_key_shared_!r:h", session.session_id) obj.access_token = "tok" obj.home_server = "h" obj.user_id = "@u:h" obj.device_id = "DEV" assert ( bool( obj.notify( body="hello", title=lone_surrogate_title, body_format=NotifyFormat.HTML, ) ) is True ) # Webhook (Slack-compatible) path. mock_post.return_value = _mk_resp({}) obj = NotifyMatrix( host="h", user="u", token="tok", mode=MatrixWebhookMode.SLACK ) assert bool(obj.notify(body=lone_surrogate_body)) is True @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_caps_before_scanning_density( mock_post, mock_get, mock_put ): """The payload cap is applied before UTF-8 density is scanned.""" from apprise.asset import AppriseAsset from apprise.common import OverflowMode response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok mock_get.return_value = request mock_post.return_value = request mock_put.return_value = request asset = AppriseAsset(payload_max_size=100) obj = NotifyMatrix( host="hostname", user="user", password="passwd", asset=asset ) # Only the capped 100 characters should be scanned. huge_body = "a" * 10_000_000 assert ( bool(obj.notify(body=huge_body, overflow=OverflowMode.SPLIT)) is True ) # Capped to 100 characters up front, so a single small event is sent. assert mock_put.call_count == 1 payload = loads(mock_put.call_args.kwargs["data"]) assert len(payload["body"]) <= 100 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_caps_before_sanitizing(mock_post, mock_get, mock_put): """Apply the payload cap before scanning text for invalid surrogates. Webhook mode isolates the cap performed by ``_build_send_calls()``. """ from apprise.asset import AppriseAsset from apprise.plugins.matrix.base import MatrixWebhookMode request = _Response() request.content = dumps({}) request.status_code = requests.codes.ok mock_post.return_value = request asset = AppriseAsset(payload_max_size=100) obj = NotifyMatrix( host="h", user="u", token="tok", mode=MatrixWebhookMode.SLACK, asset=asset, ) seen_lengths = [] from apprise.plugins.matrix import ( base as matrix_base, sizing as matrix_sizing, ) real_sanitize_fn = matrix_sizing.sanitize_text def spy_sanitize(text): seen_lengths.append(len(text) if text else 0) return real_sanitize_fn(text) huge_body = "a" * 10_000_000 with ( mock.patch.object(matrix_sizing, "sanitize_text", spy_sanitize), mock.patch.object(matrix_base, "_matrix_sanitize_text", spy_sanitize), ): assert bool(obj.notify(body=huge_body)) is True # Every call into the sanitizer saw only the already-capped text, never # the original 10,000,000-character body. assert seen_lengths assert max(seen_lengths) <= 100 def test_plugin_matrix_preview_avoids_extra_full_strip(): """Avoid stripping and copying a large input during sizing. The framework performs the one required full-input strip. The sizing preview finds trim boundaries without making another full-size copy. """ from apprise.asset import AppriseAsset from apprise.common import OverflowMode calls = [] class _TrackingStr(str): def rstrip(self, *args, **kwargs): calls.append(("rstrip", len(self))) return super().rstrip(*args, **kwargs) def strip(self, *args, **kwargs): calls.append(("strip", len(self))) return super().strip(*args, **kwargs) asset = AppriseAsset(payload_max_size=100) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], asset=asset ) huge_body = _TrackingStr("a" * 10_000_000 + " ") huge_title = _TrackingStr(" " * 10_000_000 + "hi") list( obj._build_send_calls( body=huge_body, title=huge_title, overflow=OverflowMode.SPLIT, ) ) full_length_rstrip_calls = [ c for c in calls if c == ("rstrip", len(huge_body)) ] full_length_strip_calls = [ c for c in calls if c == ("strip", len(huge_title)) ] assert len(full_length_rstrip_calls) == 1 assert len(full_length_strip_calls) == 1 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_whitespace_title_does_not_steal_cap( mock_post, mock_get, mock_put ): """Whitespace-only titles must not consume the payload cap. The sizing preview follows the framework's strip-then-cap order so the useful body receives the space vacated by the title. """ from apprise.asset import AppriseAsset from apprise.common import OverflowMode response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok mock_get.return_value = request mock_post.return_value = request mock_put.return_value = request asset = AppriseAsset(payload_max_size=300) obj = NotifyMatrix( host="hostname", user="user", password="passwd", asset=asset ) title = " " * 250 body = "x" * 300 assert bool( obj.notify(body=body, title=title, overflow=OverflowMode.SPLIT) ) payload = loads(mock_put.call_args_list[0].kwargs["data"]) # The whitespace-only title contributes nothing once stripped, so the # full body -- not a truncated fragment -- fits within the cap. assert payload["body"] == body def test_plugin_matrix_title_leading_whitespace_past_cap(): """Keep meaningful title text beyond leading whitespace. The preview must follow the framework's trim-then-cap behavior so title overhead is measured correctly. """ from apprise.asset import AppriseAsset from apprise.common import NotifyFormat, OverflowMode asset = AppriseAsset(payload_max_size=100) obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], asset=asset ) # More leading whitespace than the cap, followed by an escape-heavy # meaningful title (each char costs 6 JSON-escaped bytes). title = (" " * 150) + ("\x01" * 50) body = "hello world" from apprise.plugins.matrix import sizing as matrix_sizing observed = {} real_overhead_fn = matrix_sizing.title_overhead_bytes def spy_title_overhead(title_arg, body_format, escape_html): observed["title_arg"] = title_arg return real_overhead_fn(title_arg, body_format, escape_html) with mock.patch.object( matrix_sizing, "title_overhead_bytes", spy_title_overhead ): list( obj._build_send_calls( body=body, title=title, body_format=NotifyFormat.HTML, overflow=OverflowMode.SPLIT, ) ) # The preview must have seen the real, meaningful title content -- # not an empty string produced by slicing into the leading whitespace. assert observed["title_arg"] == "\x01" * 50 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_truncation_warning_fires(mock_post, mock_get, mock_put): """A genuinely oversized payload must still log the framework's normal truncation warning -- sizing this content-aware must not suppress it by pre-trimming before the framework's own check ever runs.""" from apprise.asset import AppriseAsset from apprise.common import OverflowMode response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok mock_get.return_value = request mock_post.return_value = request mock_put.return_value = request asset = AppriseAsset(payload_max_size=100) obj = NotifyMatrix( host="hostname", user="user", password="passwd", asset=asset ) with mock.patch.object(obj, "logger") as mock_logger: assert bool( obj.notify( body="x" * 300, title="Alert", overflow=OverflowMode.SPLIT ) ) assert any( "trimmed" in call.args[0] for call in mock_logger.warning.call_args_list ) def test_plugin_matrix_body_limit_thread_isolation(): """Keep content-aware body limits isolated across threads. The context variable is exercised directly so the test is deterministic and does not depend on reproducing a timing-sensitive race. """ import threading from apprise.plugins.matrix import base as matrix_base obj = NotifyMatrix( host="h", user="u", password="pass", targets=["#r"], secure=True ) # Sizing state lives in a context var, not on the instance itself. assert "_matrix_effective_body_maxlen" not in obj.__dict__ var = matrix_base._matrix_effective_body_maxlen_var seen_in_other_thread = {} def set_and_check_from_other_thread(): token = var.set(999) try: seen_in_other_thread["value"] = var.get() finally: var.reset(token) token = var.set(111) try: t = threading.Thread(target=set_and_check_from_other_thread) t.start() t.join(timeout=5) # The other thread's write must not have leaked into this one. assert var.get() == 111 finally: var.reset(token) # And this thread's write must not have leaked into the other one. assert seen_in_other_thread["value"] == 999 @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_emoji_split_preserves_content( mock_post, mock_get, mock_put ): """Emoji splitting preserves content within the byte limit.""" from apprise.common import OverflowMode response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok mock_get.return_value = request mock_post.return_value = request mock_put.return_value = request obj = NotifyMatrix(host="hostname", user="user", password="passwd") original_body = "\U0001f600" * 20000 assert ( bool(obj.notify(body=original_body, overflow=OverflowMode.SPLIT)) is True ) # Each event must independently fit Matrix's byte limit. assert mock_put.call_count > 1 reconstructed = "" for call in mock_put.call_args_list: payload = loads(call.kwargs["data"]) encoded_len = len(dumps(payload, ensure_ascii=False).encode("utf-8")) assert encoded_len <= 65536 reconstructed += payload["body"] # No emoji was dropped or duplicated across the split events. assert reconstructed == original_body @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_emoji_split_counters(mock_post, mock_get, mock_put): """Emoji split counters match the number of events sent.""" from apprise.common import OverflowMode response_obj = { "room_id": "!abc123:localhost", "room_alias": "#abc123:localhost", "joined_rooms": ["!abc123:localhost"], "access_token": "abcd1234", "home_server": "localhost", } request = _Response() request.content = dumps(response_obj) request.status_code = requests.codes.ok mock_get.return_value = request mock_post.return_value = request mock_put.return_value = request obj = NotifyMatrix(host="hostname", user="user", password="passwd") assert ( bool( obj.notify( title="Title", body="\U0001f600" * 20000, overflow=OverflowMode.SPLIT, ) ) is True ) total = mock_put.call_count assert total > 1 # Counters use the framework's zero-padded width. digits = len(str(total)) last_payload = loads(mock_put.call_args_list[-1].kwargs["data"]) assert f"[{total:0{digits}d}/{total:0{digits}d}]" in last_payload["body"] @mock.patch("requests.put") @mock.patch("requests.get") @mock.patch("requests.post") def test_plugin_matrix_discovery_is_unauthenticated( mock_post, mock_get, mock_put ): """Keep the access token out of every discovery request.""" def _resp(payload): response = _Response() response.status_code = requests.codes.ok response.content = dumps(payload).encode("utf-8") response.headers = {} return response well_known = { "m.homeserver": {"base_url": "https://matrix.example.com"}, "m.identity_server": {"base_url": "https://identity.example.com"}, } mock_get.side_effect = [_resp(well_known), _resp({}), _resp({})] mock_post.return_value = _resp({}) mock_put.return_value = _resp({}) obj = Apprise.instantiate( "matrixs://user:pass@example.com/#general?v=2&discovery=yes" ) # Pretend we already hold a session from an earlier notification obj.access_token = "secret-token" assert obj.base_url == "https://matrix.example.com" # The well-known lookup and both probes went out without it assert mock_get.call_count == 3 for call in mock_get.call_args_list: assert "Authorization" not in call[1]["headers"] del obj _force_del_cleanup()