From 6609cb0ee3e36a3de2fb78e738b8571e0f317213 Mon Sep 17 00:00:00 2001 From: Sherif Soliman Date: Sun, 21 Jan 2024 19:15:41 -0800 Subject: [PATCH] Update pushover send_notification and add test. --- src/scrobble/pushover.py | 6 ++--- src/scrobble/utils.py | 27 +++++++++++++++------ tests/test__pushover.py | 52 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/scrobble/pushover.py b/src/scrobble/pushover.py index 567b809..a34e6ef 100644 --- a/src/scrobble/pushover.py +++ b/src/scrobble/pushover.py @@ -4,10 +4,10 @@ from scrobble.musicbrainz import CD from scrobble.utils import Config -config = Config() - -def send_notification(cd: CD): +def send_notification(cd: CD, config=None): + if not config: + config = Config() conn = http.client.HTTPSConnection("api.pushover.net:443") query_strings = { "token": config.pushover_token, diff --git a/src/scrobble/utils.py b/src/scrobble/utils.py index c8b8f0c..54287c7 100644 --- a/src/scrobble/utils.py +++ b/src/scrobble/utils.py @@ -49,6 +49,13 @@ def has_lastfm_username(self): def lastfm_username(self): return self.lastfmapi['username'] + @lastfm_username.setter + def lastfm_username(self, new_lastfm_username): + if new_lastfm_username: + self.lastfmapi['username'] = new_lastfm_username + else: + raise ValueError('You cannot set the Last.fm username to an empty value.') + @property def lastfm_api_key(self): return self.lastfmapi['api_key'] @@ -61,17 +68,24 @@ def lastfm_api_secret(self): def pushover_token(self): return self.pushoverapi['token'] + @pushover_token.setter + def pushover_token(self, new_token_value: str): + if new_token_value: + self.pushoverapi['token'] = new_token_value + else: + raise ValueError('You cannot set the Pushover token to an empty value.') + @property def pushover_user(self): return self.pushoverapi['user_key'] - def read_api_keys(self, config_path: str) -> dict: - if not os.path.exists(config_path): - raise FileNotFoundError(f'.toml config file not found in {config_path}') - with open(config_path, 'rb') as config_file: - keys = tomllib.load(config_file) + @pushover_user.setter + def pushover_user(self, new_pushover_user: str): + if new_pushover_user: + self.pushoverapi['user_key'] = new_pushover_user + else: + raise ValueError('You cannot set the Pushover user key to an empty value.') - return keys def prepare_tracks(cd: CD, tracks: list[Track], playback_end: str = 'now') -> list[dict]: total_run_time: int = 0 @@ -137,4 +151,3 @@ def choose_tracks(tracks: list[Track]) -> list[Track]: else: raise NotImplementedError("Track choosing without charmbracelet/gum installation is not implemented yet.") - diff --git a/tests/test__pushover.py b/tests/test__pushover.py index e69de29..7006c53 100644 --- a/tests/test__pushover.py +++ b/tests/test__pushover.py @@ -0,0 +1,52 @@ +from scrobble.pushover import send_notification +from scrobble.utils import Config +from scrobble.musicbrainz import CD, UserAgent, init_musicbrainz +import importlib.metadata +from urllib.parse import quote_plus + +import unittest +from unittest.mock import patch, Mock + +USERAGENT = UserAgent('scrobble (PyPI) (tests)', + importlib.metadata.version('scrobble'), # scrobble version + 'https://github.com/sheriferson' + ) + +init_musicbrainz(USERAGENT) + +TEST_CD = CD.find_cd(7277017746006, choice=False) + + +class TestSendNotification(unittest.TestCase): + + @patch('http.client.HTTPSConnection') + def test_send_notification(self, mock_https_connection): + # Mocking the connection + mock_conn_instance = Mock() + mock_https_connection.return_value = mock_conn_instance + + # Mocking the response + mock_response = Mock() + mock_conn_instance.getresponse.return_value = mock_response + + # Example CD and Config objects + config = Config() + config.pushover_token = 'test_token' + config.pushover_user = 'test_user' + + # Call the function with the mocked connection + response = send_notification(TEST_CD, config) + + # Assert that conn.request was called with the expected arguments + mock_conn_instance.request.assert_called_once_with( + "POST", + "/1/messages.json", + f"token={config.pushover_token}&user={config.pushover_user}&message={quote_plus(TEST_CD.title)}+%28{TEST_CD.year}%29+by+{quote_plus(TEST_CD.artist)}+scrobbled+to+your+account.&url=https%3A%2F%2Flast.fm%2Fuser%2F{config.lastfm_username}", + {"Content-type": "application/x-www-form-urlencoded"} + ) + + # Assert that conn.getresponse was called + mock_conn_instance.getresponse.assert_called_once() + + # Assert that the function returned the expected response + self.assertEqual(response, mock_response)