Skip to content

Commit

Permalink
Update pushover send_notification and add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
sheriferson committed Jan 22, 2024
1 parent ea8b1ff commit 6609cb0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/scrobble/pushover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 20 additions & 7 deletions src/scrobble/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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
Expand Down Expand Up @@ -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.")

52 changes: 52 additions & 0 deletions tests/test__pushover.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 6609cb0

Please sign in to comment.