Skip to content

Commit

Permalink
Normalize clients test_authentication() results (#9801)
Browse files Browse the repository at this point in the history
* Normalize clients test_authentication() results

* Improve torrent test auth consistency

* Use f-strings for nzbget
  • Loading branch information
medariox committed Aug 16, 2021
1 parent 3408507 commit a5633bb
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 42 deletions.
39 changes: 24 additions & 15 deletions medusa/clients/nzb/nzbget.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,39 @@ def nzb_connection(url):
nzb_get_rpc = ServerProxy(url)
try:
if nzb_get_rpc.writelog('INFO', 'Medusa connected to test connection.'):
log.debug('Successfully connected to NZBget')
msg = 'Successfully connected to NZBget'
log.debug(msg)
else:
log.warning('Successfully connected to NZBget but unable to'
' send a message')
return True
msg = 'Successfully connected to NZBget but unable to send a message'
log.warning(msg)

return True, msg

except ProtocolError as error:
if error.errmsg == 'Unauthorized':
log.warning('NZBget username or password is incorrect.')
msg = 'NZBget username or password is incorrect.'
log.warning(msg)
else:
log.error('Protocol Error: {msg}', {'msg': error.errmsg})
return False
msg = f'Protocol Error: {error.errmsg}'
log.error(msg)

return False, msg

except Error as error:
log.warning('Please check your NZBget host and port (if it is running).'
' NZBget is not responding to this combination.'
' Error: {msg}', {'msg': error.errmsg})
return False
msg = ('Please check your NZBget host and port (if it is running).'
' NZBget is not responding to this combination.'
f' Error: {error}')
log.warning(msg)

return False, msg

except socket.error as error:
log.warning('Please check your NZBget host and port (if it is running).'
' NZBget is not responding to this combination.'
' Socket Error: {msg}', {'msg': error})
return False
msg = ('Please check your NZBget host and port (if it is running).'
' NZBget is not responding to this combination.'
f' Socket Error: {error}')
log.warning(msg)

return False, msg


def test_authentication(host=None, username=None, password=None, use_https=False):
Expand Down
6 changes: 4 additions & 2 deletions medusa/clients/nzb/sab.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,13 @@ def test_authentication(host=None, username=None, password=None, apikey=None):
data = session.get_json(url, verify=False)

if not data:
log.info('Error connecting to sab, no data returned')
msg = 'Error connecting to SABnzbd, no data returned'
log.warning(msg)
return False, msg
else:
# check the result and determine if it's good or not
result, text = _check_sab_response(data)
return result, 'success' if result else text
return result, 'Successfully connected to SABnzbd' if result else text


@ttl_cache(60.0)
Expand Down
2 changes: 1 addition & 1 deletion medusa/clients/torrent/deluged.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def test_authentication(self):
if self.connect(True) and self.drpc.test():
return True, 'Success: Connected and Authenticated'
else:
return False, 'Error: Unable to Authenticate! Please check your config!'
return False, 'Error: Unable to Authenticate! Please check your config!'

def remove_ratio_reached(self):
"""Remove all Medusa torrents that ratio was reached.
Expand Down
2 changes: 1 addition & 1 deletion medusa/clients/torrent/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def test_authentication(self):
"""
r_code = self._get_auth()
if self.message:
return r_code, self.message
return bool(r_code), self.message
elif not self._check_path():
return False, self.message
if r_code:
Expand Down
6 changes: 3 additions & 3 deletions medusa/clients/torrent/rtorrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ def test_authentication(self):
try:
self.auth = None
self._get_auth()
except Exception: # pylint: disable=broad-except
return False, 'Error: Unable to connect to {name}'.format(name=self.name)
except Exception:
return False, f'Error: Unable to connect to {self.name}'
else:
if self.auth is None:
return False, 'Error: Unable to get {name} Authentication, check your config!'.format(name=self.name)
return False, f'Error: Unable to get {self.name} Authentication, check your config!'
else:
return True, 'Success: Connected and Authenticated'

Expand Down
17 changes: 0 additions & 17 deletions medusa/schedulers/download_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,27 +347,10 @@ def _postprocess(self, path, info_hash, resource_name, failed=False):
queue_item = PostProcessQueueItem(path, info_hash, resource_name=resource_name, failed=failed, episodes=episodes)
app.post_processor_queue_scheduler.action.add_item(queue_item)

@staticmethod
def _test_connection(client, client_type):
"""Need to structure, because of some subtle differences between clients."""
if client_type == 'torrent':
return client.test_authentication()

result = client.test_authentication()
if not result:
return False
return result[0]

def _clean(self, client):
"""Update status in the history table for torrents/nzb's that can't be located anymore."""
client_type = 'torrent' if isinstance(client, GenericClient) else 'nzb'

# Make sure the client can be reached. As we don't want to change the state for downloads
# because the client is temporary unavailable.
if not self._test_connection(client, client_type):
log.warning('The client cannot be reached or authentication is failing. Abandon cleanup.')
return

for history_result in self._get_history_results_from_db(client_type):
try:
if not client.get_status(history_result['info_hash']):
Expand Down
6 changes: 3 additions & 3 deletions medusa/server/web/home/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def testSABnzbd(host=None, username=None, password=None, apikey=None):
return 'Error while testing connection. Check warning logs.'

if connection:
authed, auth_msg = sab.test_authentication(host, username, password, apikey) # @UnusedVariable
authed, auth_msg = sab.test_authentication(host, username, password, apikey)
if authed:
return 'Success. Connected and authenticated'
else:
Expand All @@ -210,7 +210,7 @@ def testSABnzbd(host=None, username=None, password=None, apikey=None):
@staticmethod
def testNZBget(host=None, username=None, password=None, use_https=False):
try:
connected_status = nzbget.test_authentication(
connected_status, error_msg = nzbget.test_authentication(
host, username, password, config.checkbox_to_value(use_https)
)
except Exception as error:
Expand All @@ -219,7 +219,7 @@ def testNZBget(host=None, username=None, password=None, use_https=False):
if connected_status:
return 'Success. Connected and authenticated'
else:
return 'Unable to connect to host'
return 'Unable to connect to host. Error: {msg}'.format(msg=error_msg)

@staticmethod
def testTorrent(torrent_method=None, host=None, username=None, password=None):
Expand Down

0 comments on commit a5633bb

Please sign in to comment.