# 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 ctypes from importlib import reload # Disable logging for a cleaner testing output import logging import os import sys from unittest import mock from helpers import environ import pytest from apprise import Apprise, AppriseAsset, locale logging.disable(logging.CRITICAL) # True when the catalogs have been compiled (tox -e compile). They are not # kept in git and CI does not build them, so add() results depend on this. CATALOG_COMPILED = os.path.isfile( os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "apprise", "i18n", "en", "LC_MESSAGES", "apprise.mo", ) ) def test_apprise_trans(): """ API: Test apprise locale object """ # Text with no translation reads the same in every language text = "A string Apprise has no translation for" lazytrans = locale.LazyTranslation(text) assert str(lazytrans) == text @pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") def test_apprise_trans_gettext_init(): """ API: Handle gettext """ # Toggle locale.GETTEXT_LOADED = False # Objects can still be created al = locale.AppriseLocale() with al.lang_at("en") as _: # functions still behave as normal assert _ is None # Lazy strings hand back the English source text they were given assert str(locale.gettext_lazy("Token")) == "Token" # Restore the object locale.GETTEXT_LOADED = True @pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") @mock.patch("gettext.translation") @mock.patch("locale.getlocale") def test_apprise_trans_gettext_translations( mock_getlocale, mock_gettext_trans ): """ API: Apprise() Gettext translations """ # Set the gettext locale returned by the mock. mock_getlocale.return_value = ("en_US", "UTF-8") mock_gettext_trans.side_effect = FileNotFoundError() # This throws internally but we handle it gracefully al = locale.AppriseLocale() with al.lang_at("en"): # functions still behave as normal pass # This throws internally but we handle it gracefully locale.AppriseLocale(language="fr") @pytest.mark.skipif(hasattr(ctypes, "windll"), reason="Unique Nux test cases") @pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") @mock.patch("locale.getlocale") def test_apprise_trans_gettext_lang_at(mock_getlocale): """ API: Apprise() Gettext lang_at """ # Set the gettext locale returned by the mock. mock_getlocale.return_value = ("en_CA", "UTF-8") # Clear the language variables so only the mock above is detected with environ("LANGUAGE", "LC_ALL", "LC_MESSAGES", "LC_CTYPE", "LANG"): # This throws internally but we handle it gracefully al = locale.AppriseLocale() # We detected en_CA, so plain en needs a compiled catalog assert al.add("en", set_default=False) is CATALOG_COMPILED assert al.add("en", set_default=True) is CATALOG_COMPILED with al.lang_at("en"): # functions still behave as normal pass # This throws internally but we handle it gracefully locale.AppriseLocale(language="fr") with al.lang_at("en") as _: # functions still behave as normal assert callable(_) with al.lang_at("es") as _: # functions still behave as normal assert callable(_) with al.lang_at("fr") as _: # functions still behave as normal assert callable(_) # Test our initialization when our fallback is a language we do # not have. This is only done to test edge cases when for whatever # reason the person who set up apprise does not have the languages # installed. fallback = locale.AppriseLocale._default_language mock_getlocale.return_value = None with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): # Our default language locale.AppriseLocale._default_language = "zz" # We will detect the zz since there were no environment variables to # help us otherwise assert locale.AppriseLocale.detect_language() is None al = locale.AppriseLocale() # No language can be set because its locale directory does not exist. assert al.lang is None with al.lang_at(None) as _: # functions still behave as normal assert callable(_) with al.lang_at("en") as _: # functions still behave as normal assert callable(_) with al.lang_at("es") as _: # functions still behave as normal assert callable(_) with al.lang_at("fr") as _: # functions still behave as normal assert callable(_) # We can still perform simple lookups; they access a dummy wrapper: assert al.gettext("test") == "test" locale.AppriseLocale._default_language = "fr" with environ( "LANGUAGE", "LC_MESSAGES", "LC_CTYPE", LC_ALL="C.UTF-8", LANG="en_CA", ): # LC_ALL is authoritative, and the C locale it names is a request for # no translation at all. LANG is not consulted, and we settle on the # language configured as our default. assert locale.AppriseLocale.detect_language() == "fr" with environ( "LANGUAGE", "LC_ALL", "LC_MESSAGES", "LC_CTYPE", LANG="en_CA" ): # With no C locale in the way, LANG decides and overrides the # default. The region is kept so that an en_CA translation is # preferred over the en translation. assert locale.AppriseLocale.detect_language() == "en_CA" al = locale.AppriseLocale() assert al.lang == "en_CA" assert al.gettext("test") == "test" # Test case with set_default set to False (so we're still set to 'fr') assert al.add("zy", set_default=False) is False assert al.gettext("test") == "test" al.add("ab", set_default=True) assert al.gettext("test") == "test" assert al.add("zy", set_default=False) is False locale.AppriseLocale._default_language = fallback def test_normalize_language(): """Both the 2 and 5 letter language forms are accepted.""" # A plain language stays as it is, and case never matters for entry in ("en", "EN", " en "): assert locale.AppriseLocale.normalize_language(entry) == "en" # A region may be separated by a hyphen or an underscore for entry in ("en-CA", "en_CA", "EN-ca", "en-ca"): assert locale.AppriseLocale.normalize_language(entry) == "en_CA" # A codeset (.UTF-8) and modifier (@euro) are both dropped for entry in ( "en_CA.UTF-8", "en_CA.utf8", "en_CA.ISO8859-1", "en_CA.ANSI_X3.4-1968", "en_CA@euro", "en_CA.UTF-8@euro", ): assert locale.AppriseLocale.normalize_language(entry) == "en_CA" # A modifier is dropped even without a codeset assert locale.AppriseLocale.normalize_language("sr_RS@latin") == "sr_RS" assert locale.AppriseLocale.normalize_language("fr@euro") == "fr" # The form locale.normalize() returns for ca_ES@valencia assert ( locale.AppriseLocale.normalize_language("ca_ES.UTF-8@valencia") == "ca_ES" ) # A codeset or modifier on its own is not a language for entry in (".UTF-8", "@euro", "garbage.stuff_more"): assert locale.AppriseLocale.normalize_language(entry) is None # Anything beyond a 2 letter region is dropped, leaving the language assert locale.AppriseLocale.normalize_language("zh-Hans") == "zh" assert locale.AppriseLocale.normalize_language("zh-Hans-CN") == "zh" # Only the first entry of a preference list is honored, whether it is # comma separated like an Accept-Language header or colon separated like # a LANGUAGE= environment variable assert ( locale.AppriseLocale.normalize_language("en-US,en;q=0.9,de;q=0.5") == "en_US" ) assert locale.AppriseLocale.normalize_language("fr:de") == "fr" # A colon is a list separator, so it never introduces a region assert locale.AppriseLocale.normalize_language("en:CA") == "en" # Values we can not work with for entry in ("", "garbage", "!", "C", "C.UTF-8", None, 42): assert locale.AppriseLocale.normalize_language(entry) is None def test_is_ansii_locale(): """The C locale is recognized under each of its spellings.""" # Every spelling, with or without a codeset or modifier for entry in ( "C", "c", "POSIX", "posix", "C.UTF-8", "C.utf8", "C.ANSI_X3.4-1968", "POSIX.UTF-8", "C@euro", ): assert locale.AppriseLocale.is_ansii_locale(entry) is True # A language is not the C locale, including the ones that start with a c for entry in ("en", "ca", "cs", "ca_ES", "cat", "", None, 42): assert locale.AppriseLocale.is_ansii_locale(entry) is False def test_language_candidates(): """A region is always retried without it before we give up.""" # A plain language has nowhere else to look assert locale.AppriseLocale.language_candidates("fr") == ["fr"] # A region is preferred, but the language itself is the backup assert locale.AppriseLocale.language_candidates("pt-BR") == [ "pt_BR", "pt", ] # Nothing to search for assert locale.AppriseLocale.language_candidates("garbage") == [] @pytest.mark.skipif(sys.platform == "win32", reason="Unique Nux test cases") @mock.patch("locale.getlocale") def test_detect_language_precedence(mock_getlocale): """The environment is read in the order gettext itself reads it.""" # Never reached while an environment variable answers for us mock_getlocale.return_value = (None, None) with environ( "LANGUAGE", "LC_ALL", "LC_MESSAGES", "LC_CTYPE", LANG="en_CA" ): # LANG on its own assert locale.AppriseLocale.detect_language() == "en_CA" with environ( "LANGUAGE", "LC_ALL", "LC_CTYPE", LANG="en_CA", LC_MESSAGES="fr_CA" ): # LC_MESSAGES is the category that governs translated text assert locale.AppriseLocale.detect_language() == "fr_CA" with environ( "LANGUAGE", "LC_CTYPE", LANG="en_CA", LC_MESSAGES="fr_CA", LC_ALL="de_DE", ): # LC_ALL over-rides every category assert locale.AppriseLocale.detect_language() == "de_DE" with environ( "LC_CTYPE", LANG="en_CA", LC_MESSAGES="fr_CA", LC_ALL="de_DE", LANGUAGE="es", ): # LANGUAGE wins over all of them assert locale.AppriseLocale.detect_language() == "es" with environ( "LANGUAGE", "LC_ALL", "LC_MESSAGES", LANG="en_CA", LC_CTYPE="de_DE" ): # LC_CTYPE governs character handling, not the language of text assert locale.AppriseLocale.detect_language() == "en_CA" with environ( "LC_ALL", "LC_MESSAGES", "LC_CTYPE", LANG="en_CA", LANGUAGE="fr:de" ): # A LANGUAGE list is colon separated; its first entry is used assert locale.AppriseLocale.detect_language() == "fr" with environ( "LANGUAGE", "LC_ALL", "LC_MESSAGES", "LC_CTYPE", LANG="fr_FR.ISO8859-1@euro", ): # The codeset and modifier are dropped assert locale.AppriseLocale.detect_language() == "fr_FR" default = locale.AppriseLocale._default_language for value in ( "C", "POSIX", "C.UTF-8", "posix", "C.ANSI_X3.4-1968", "C@euro", ): with environ( "LC_MESSAGES", "LC_CTYPE", LC_ALL=value, LANG="fr_FR.UTF-8", LANGUAGE="de", ): # Asking for the C locale is asking for untranslated text, so # nothing below LC_ALL gets a say, LANGUAGE included assert locale.AppriseLocale.detect_language() == default with environ("LC_ALL", "LC_CTYPE", LANG="C", LC_MESSAGES="fr_CA"): # A higher priority category still decides; only the locale actually # in charge of messages can silence translation assert locale.AppriseLocale.detect_language() == "fr_CA" with environ( "LC_ALL", "LC_MESSAGES", "LC_CTYPE", LANGUAGE="garbage", LANG="fr_CA" ): # A value we can make no sense of is stepped over rather than # stopping us; only the C locale does that assert locale.AppriseLocale.detect_language() == "fr_CA" @pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") def test_lang_at_invalid_entries(): """An unusable language falls back instead of raising.""" al = locale.AppriseLocale() # None of these can be turned into a language, yet each still hands # back a usable translation function for entry in (None, "", " ", "garbage", "!", 42): with al.lang_at(entry) as fn: assert callable(fn) # The same holds true for the public details() call obj = Apprise() assert isinstance(obj.details(lang="invalid!"), dict) @pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") @mock.patch("gettext.translation") def test_lazy_translation_context(mock_translation): """A lang_at() block decides how lazy strings are translated.""" # Our stand-in catalog marks anything it translates dummy = mock.Mock() dummy.gettext = lambda text: f"xx:{text}" mock_translation.return_value = dummy al = locale.AppriseLocale() entry = locale.gettext_lazy("Token") # Our own singleton is untouched outside of the block before = str(entry) with al.lang_at("fr"): assert str(entry) == "xx:Token" # The language in effect before us is restored assert str(entry) == before # Asking for a function other than gettext leaves lazy strings alone with al.lang_at("fr", mapto="ngettext"): assert str(entry) == before @pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") @mock.patch("gettext.translation") def test_asset_language_honored(mock_translation): """An asset language is used by the details() it returns.""" # Our stand-in catalog marks anything it translates dummy = mock.Mock() dummy.gettext = lambda text: f"xx:{text}" mock_translation.return_value = dummy obj = Apprise(asset=AppriseAsset(language="fr-CA")) # Our asset decided the language of the object we built assert obj.locale.lang == "fr_CA" # Every human readable entry came back translated names = [ token["name"] for schema in obj.details()["schemas"] for token in schema["details"]["tokens"].values() ] assert names assert all(str(name).startswith("xx:") for name in names) @pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") @mock.patch("locale.getlocale") def test_apprise_trans_add(mock_getlocale): """ API: Apprise() Gettext add """ # Used when no language variable is set. Windows asks the system # first, but always gets a regional answer (en_US), so the checks below # still hold. mock_getlocale.return_value = ("en_CA", "UTF-8") with environ("LANGUAGE", "LC_ALL", "LC_MESSAGES", "LC_CTYPE", "LANG"): # We start on en_CA, so plain en needs a compiled catalog al = locale.AppriseLocale() assert al.add("en") is CATALOG_COMPILED with environ( "LANGUAGE", "LC_ALL", "LC_MESSAGES", "LC_CTYPE", LANG="C.UTF-8" ): # The C locale starts on our default language, so en is loaded al = locale.AppriseLocale() assert al.lang == locale.AppriseLocale._default_language assert al.add("en") is True with environ( "LANGUAGE", "LC_ALL", "LC_MESSAGES", "LC_CTYPE", LANG="en_CA.UTF-8" ): # Test English Environment al = locale.AppriseLocale() assert al.add("en") is CATALOG_COMPILED # Double add (copy of above) to access logic that prevents adding it # again assert al.add("en") is CATALOG_COMPILED # Invalid Language assert al.add("bad") is False # A value that is not a language at all never reaches gettext; there is # no catalog name that could be built from it for entry in ("C", "!", "1"): assert al.add(entry) is False # An empty value is not a failure; it selects the default language assert al.add("") is CATALOG_COMPILED @pytest.mark.skipif( not hasattr(ctypes, "windll"), reason="Unique Windows test cases" ) @pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") @mock.patch("locale.getlocale") def test_apprise_trans_windows_users_win(mock_getlocale): """ API: Apprise() Windows Locale Testing (Win version) """ # Set the gettext locale returned by the mock. mock_getlocale.return_value = ("fr_CA", "UTF-8") with mock.patch( "ctypes.windll.kernel32.GetUserDefaultUILanguage" ) as ui_lang: # 4105 = en_CA ui_lang.return_value = 4105 with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): # Our default language locale.AppriseLocale._default_language = "zz" # The Windows locale supplies English. assert locale.AppriseLocale.detect_language() == "en_CA" # Environment variables take precedence over the Windows locale. with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", LANG="es_AR"): assert locale.AppriseLocale.detect_language() == "es_AR" # Without environment variables, use the Windows locale. with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): assert locale.AppriseLocale.detect_language() == "en_CA" assert ( locale.AppriseLocale.detect_language(detect_fallback=False) is None ) # 0 = IndexError ui_lang.return_value = 0 with environ("LANGUAGE", "LANG", "LC_ALL", "LC_CTYPE"): # Fall back to the POSIX locale. assert locale.AppriseLocale.detect_language() == "fr_CA" @pytest.mark.skipif(hasattr(ctypes, "windll"), reason="Unique Nux test cases") @pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") @mock.patch("locale.getlocale") def test_apprise_trans_windows_users_nux(mock_getlocale): """ API: Apprise() Windows Locale Testing (Nux version) """ # Set the gettext locale returned by the mock. mock_getlocale.return_value = ("fr_CA", "UTF-8") # Emulate a windows environment windll = mock.Mock() ctypes.windll = windll # 4105 = en_CA windll.kernel32.GetUserDefaultUILanguage.return_value = 4105 # Store default value to not break other tests default_language = locale.AppriseLocale._default_language with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): # Our default language locale.AppriseLocale._default_language = "zz" # The Windows locale supplies English. assert locale.AppriseLocale.detect_language() == "en_CA" # Environment variables take precedence over the Windows locale. with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", LANG="es_AR"): assert locale.AppriseLocale.detect_language() == "es_AR" # Without environment variables, use the Windows locale. with environ("LANGUAGE", "LC_ALL", "LC_CTYPE", "LANG"): assert locale.AppriseLocale.detect_language() == "en_CA" assert locale.AppriseLocale.detect_language(detect_fallback=False) is None # 0 = IndexError windll.kernel32.GetUserDefaultUILanguage.return_value = 0 with environ("LANGUAGE", "LANG", "LC_ALL", "LC_CTYPE"): # Fall back to the POSIX locale. assert locale.AppriseLocale.detect_language() == "fr_CA" del ctypes.windll # Restore default value locale.AppriseLocale._default_language = default_language @pytest.mark.skipif(sys.platform == "win32", reason="Unique Nux test cases") @mock.patch("locale.getlocale") def test_detect_language_using_env(mock_getlocale): """Test the reading of information from an environment variable.""" # Set the gettext locale returned by the mock. mock_getlocale.return_value = ("en_CA", "UTF-8") # The below accesses the windows fallback code and fail # then it will resort to the environment variables. with environ("LANG", "LANGUAGE", "LC_ALL", "LC_CTYPE"): # Language can now be detected in this case assert isinstance(locale.AppriseLocale.detect_language(), str) # Detect French language. with environ("LANGUAGE", "LC_ALL", LC_CTYPE="garbage", LANG="fr_CA"): assert locale.AppriseLocale.detect_language() == "fr_CA" # A system reporting the C locale falls back to our default language mock_getlocale.return_value = ("C", "UTF-8") with environ("LANG", "LANGUAGE", "LC_ALL", "LC_CTYPE"): assert ( locale.AppriseLocale.detect_language() == locale.AppriseLocale._default_language ) # Restore what the rest of this test expects mock_getlocale.return_value = ("en_CA", "UTF-8") # The following unsets all environment variables and sets LC_CTYPE # This was causing Python 2.7 to internally parse UTF-8 as an invalid # locale and throw an uncaught ValueError; Python v2 support has been # dropped, but just to ensure this issue does not come back, we keep # this test: with environ(*list(os.environ.keys()), LC_CTYPE="UTF-8"): assert isinstance(locale.AppriseLocale.detect_language(), str) # Test with no environment variables. with environ(*list(os.environ.keys())): assert isinstance(locale.AppriseLocale.detect_language(), str) # Handle case where getlocale() can't be detected mock_getlocale.return_value = None with environ("LC_ALL", "LC_CTYPE", "LANG", "LANGUAGE"): assert locale.AppriseLocale.detect_language() is None mock_getlocale.return_value = (None, None) with environ("LC_ALL", "LC_CTYPE", "LANG", "LANGUAGE"): assert locale.AppriseLocale.detect_language() is None # if detect_language and windows env fail us, then we don't # set up a default language on first load locale.AppriseLocale() @pytest.mark.skipif("gettext" not in sys.modules, reason="Requires gettext") def test_apprise_trans_gettext_missing(tmpdir): """Verify we can still operate without the gettext library.""" # Reloading a module rebinds every class it defines. Anything that # imported one of them earlier keeps the original, and the two no longer # compare as the same object, which breaks isinstance() checks and # pickling for the rest of the run. Only what Apprise hands out by # reference is restored afterwards; the modules this one imports are # left as the reload found them so they can still be mocked. restore = { name: getattr(sys.modules["apprise.locale"], name) for name in ( "ACTIVE_GETTEXT", "AppriseLocale", "LOCALE", "LazyTranslation", "Translatable", "gettext_lazy", ) } # remove gettext from our system enviroment del sys.modules["gettext"] try: # Put a failing fake gettext module ahead of the real one. gettext_dir = tmpdir.mkdir("gettext") gettext_dir.join("__init__.py").write("") gettext_dir.join("gettext.py").write("""raise ImportError()""") # Update our path to point path to head sys.path.insert(0, str(gettext_dir)) # reload our module (forcing the import error when it tries to load # gettext reload(sys.modules["apprise.locale"]) from apprise import locale assert locale.GETTEXT_LOADED is False # Now roll our changes back sys.path.pop(0) # Reload again (reverting back) reload(sys.modules["apprise.locale"]) from apprise import locale assert locale.GETTEXT_LOADED is True finally: # Hand back the classes the rest of Apprise is already holding module = sys.modules["apprise.locale"] for name, value in restore.items(): setattr(module, name, value) @mock.patch("gettext.translation") def test_apprise_locale_add_existing_language(mock_translation): """Re-add an existing language to hit fallback mapping path.""" # Return a fake gettext object with a valid gettext() method dummy = mock.Mock() dummy.gettext = lambda x: f"tr:{x}" mock_translation.return_value = dummy al = locale.AppriseLocale() assert al.add("en", set_default=True) is True assert al.add("en", set_default=False) is True @mock.patch("gettext.translation") @mock.patch("locale.getlocale") def test_apprise_trans_successful_translation( mock_getlocale, mock_translation ): """Trigger logger.trace by mocking successful gettext.translation()""" mock_getlocale.return_value = ("en_CA", "UTF-8") dummy_translation = mock.Mock() dummy_translation.gettext = lambda x: f"tr:{x}" mock_translation.return_value = dummy_translation al = locale.AppriseLocale() assert al.add("en") is True assert al.gettext("test") == "tr:test"