From 8dc7ba7bdb38f2c75782e6f99cd41858d58bc59f Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Wed, 30 Jul 2025 22:25:50 -0400 Subject: [PATCH] minor fixes lingering from #1368 --- .github/PULL_REQUEST_TEMPLATE.md | 23 +++++++++++++-------- apprise/apprise.py | 14 ++++++++++++- tests/test_api.py | 30 +++++++++++++++++++++++++-- tests/test_apprise_cli.py | 35 ++++++++++++++++++++++++++++++-- tox.ini | 8 ++++++++ 5 files changed, 96 insertions(+), 14 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 6172f812..e8089b74 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -2,11 +2,13 @@ **Related issue (if applicable):** # - ## New Service Completion Status * [ ] apprise/plugins/.py @@ -55,16 +55,18 @@ apprise -vv -t "Title" -b "Message content" \ * [ ] packaging/redhat/python-apprise.spec - add new service into the `%global common_description` + -- END OF NEW PLUGIN SECTION - REMOVE ABOVE SECION IF NOT A NEW PLUGIN --> + ## Checklist * [ ] The code change is tested and works locally. * [ ] There is no commented out code in this PR. -* [ ] No lint errors (use `tox -e lint`) -* [ ] 100% test coverage (use `tox -e minimal`) +* [ ] No lint errors (use `tox -e lint` and even `tox -e format` to autofix what it can) +* [ ] Test coverage added (use `tox -e minimal`) ## Testing + it would be really helpful to define this here --> Anyone can help test this source code as follows: ```bash # Create a virtual environment to work in as follows: @@ -83,5 +85,8 @@ pip install git+https://github.com/caronc/apprise.git@ apprise -t "Test Title" -b "Test Message" \ +# If you have cloned the branch and have tox available to you +# the following can also allow you to test: +tox -e apprise -- -t "Test Title" -b "Test Message" \ + ``` - diff --git a/apprise/apprise.py b/apprise/apprise.py index bca04a37..67b99427 100644 --- a/apprise/apprise.py +++ b/apprise/apprise.py @@ -395,7 +395,7 @@ class Apprise: self, body: Union[str, bytes], title: Union[str, bytes] = "", - notify_type: common.NotifyType = common.NotifyType.INFO, + notify_type: Union[str, common.NotifyType] = common.NotifyType.INFO, body_format: Optional[str] = None, tag: Any = common.MATCH_ALL_TAG, match_always: bool = True, @@ -526,6 +526,18 @@ class Apprise: logger.error(msg) raise TypeError(msg) + try: + notify_type = ( + notify_type if isinstance(notify_type, common.NotifyType) + else common.NotifyType(notify_type.lower()) + ) + + except (AttributeError, ValueError, TypeError): + err = ( + f"An invalid notification type ({notify_type}) was " + "specified.") + raise TypeError(err) from None + try: if title and isinstance(title, bytes): title = title.decode(self.asset.encoding) diff --git a/tests/test_api.py b/tests/test_api.py index 2f49cd97..68edbbd5 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -286,10 +286,36 @@ def apprise_test(do_notify): assert a.add("good://localhost") is True assert len(a) == 1 - # Bad Notification Type is still allowed as it is presumed the user - # know's what their doing + # Bad Notification Types are not accepted assert ( do_notify(a, title="my title", body="my body", notify_type="bad") + is False + ) + + # Notifications of string type are accepted + assert ( + do_notify(a, title="my title", body="my body", notify_type="warning") + is True + ) + + # Notifications of string type are accepted + assert ( + do_notify(a, title="my title", body="my body", notify_type="warning") + is True + ) + + # Notifications of string type are accepted + assert ( + do_notify(a, title="my title", body="my body", notify_type="warning") + is True + ) + + # Notifications where notify_type is of the NotifyType object is the + # preferred choice + assert ( + do_notify( + a, title="my title", body="my body", + notify_type=NotifyType.WARNING) is True ) diff --git a/tests/test_apprise_cli.py b/tests/test_apprise_cli.py index f9d4cc7f..dddb4394 100644 --- a/tests/test_apprise_cli.py +++ b/tests/test_apprise_cli.py @@ -2008,7 +2008,7 @@ def test_apprise_cli_plugin_loading(mock_post, tmpdir): @notify(on="clihook") def mywrapper(body, title, notify_type, *args, **kwargs): # A simple test - print to screen - print("{}: {} - {}".format(notify_type, title, body)) + print("{}: {} - {}".format(notify_type.value, title, body)) # No return (so a return of None) get's translated to True @@ -2016,7 +2016,7 @@ def test_apprise_cli_plugin_loading(mock_post, tmpdir): @notify(on="clihookA") def mywrapper(body, title, notify_type, *args, **kwargs): # A simple test - print to screen - print("!! {}: {} - {}".format(notify_type, title, body)) + print("!! {}: {} - {}".format(notify_type.value, title, body)) # No return (so a return of None) get's translated to True """)) @@ -2286,6 +2286,37 @@ def test_apprise_cli_plugin_loading(mock_post, tmpdir): # to the CLI assert result.exit_code == 0 + + result = runner.invoke( + cli.main, + [ + "--plugin-path", + join(str(tmpdir), "complex"), + "--notification-type", "invalid", + "-b", + "test body", + # our clihook that returns true + "clihook1://", + ], + ) + # Bad notification type specified + assert result.exit_code == 2 + + result = runner.invoke( + cli.main, + [ + "--plugin-path", + join(str(tmpdir), "complex"), + "-b", + "-i", "warning" + "test body", + # our clihook that returns true + "clihook1://", + ], + ) + # Bad notification type specified + assert result.exit_code == 0 + result = runner.invoke( cli.main, [ diff --git a/tox.ini b/tox.ini index 78bca548..24ee94cc 100644 --- a/tox.ini +++ b/tox.ini @@ -87,6 +87,13 @@ commands = coverage run --source=apprise -m pytest tests {posargs} coverage report +[testenv:test] +description = Run simplified tests without coverage +extras = dev,all-plugins +commands = + pip install --no-cache-dir -e ".[dev,all-plugins]" + pytest --tb=short -q {posargs} + [testenv:clean] description = Remove build artifacts and cache files skip_install = true @@ -96,6 +103,7 @@ allowlist_externals = commands = find . -type f -name "*.pyc" -delete find . -type f -name "*.pyo" -delete + find . -type f -name "*.orig" -delete find . -type d -name "__pycache__" -delete rm -rf BUILD SOURCES SRPMS BUILDROOT .cache .ruff_cache .coverage-reports .coverage coverage.xml dist build apprise.egg-info .mypy_cache .pytest_cache