#598 Handle exception when FAS auth is invalid
Opened 3 years ago by abompard. Modified 3 years ago
abompard/fedora-hubs fix/fas-auth-error  into  develop

file modified
+4 -1
@@ -5,6 +5,7 @@ 

  import re

  

  import fedmsg.meta

+ import fedora.client

  import flask

  import pymongo

  import requests.exceptions
@@ -23,7 +24,9 @@ 

      # User hubs

      try:

          usernames = fedmsg.meta.msg2usernames(msg)

-     except requests.exceptions.ConnectionError:

+     except (requests.exceptions.ConnectionError, fedora.client.AuthError) as e:

+         log.warning("Could not extract usernames from message: %s",

+                     e, exc_info=True)

          usernames = []

      for username in usernames:

          user = User.query.get(username)

file modified
+19
@@ -2,7 +2,9 @@ 

  

  import json

  

+ import fedora.client

  import pymongo

+ import requests.exceptions

  from mock import MagicMock, Mock, patch

  

  from hubs.app import app
@@ -363,3 +365,20 @@ 

              "msg": {},

          }

          self.assertListEqual(get_hubs_for_msg(msg), [])

+ 

+     @patch("hubs.feed.fedmsg.meta.msg2usernames")

+     def test_connection_error(self, msg2usernames):

+         msg2usernames.side_effect = requests.exceptions.ConnectionError(

+             "Test connection error")

+         try:

+             self.assertListEqual(get_hubs_for_msg(self.dummy_msg), [])

+         except requests.exceptions.ConnectionError:

+             self.fail("ConnectionError was not caught")

+ 

+     @patch("hubs.feed.fedmsg.meta.msg2usernames")

+     def test_auth_error(self, msg2usernames):

+         msg2usernames.side_effect = fedora.client.AuthError("Test auth error")

+         try:

+             self.assertListEqual(get_hubs_for_msg(self.dummy_msg), [])

+         except fedora.client.AuthError:

Why not use assertRaises?

+             self.fail("AuthError was not caught")

Related traceback:

14:55:24 triage.1       | [2018-03-11 14:55:24][moksha.hub    INFO] Downloading FAS cache for a*
14:55:26 triage.1       | Traceback (most recent call last):
14:55:26 triage.1       |   File "/usr/local/bin/fedora-hubs", line 11, in <module>
14:55:26 triage.1       |     load_entry_point('fedora-hubs', 'console_scripts', 'fedora-hubs')()
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/click/core.py", line 722, in __call__
14:55:26 triage.1       |     return self.main(*args, **kwargs)
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/click/core.py", line 697, in main
14:55:26 triage.1       |     rv = self.invoke(ctx)
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
14:55:26 triage.1       |     return _process_result(sub_ctx.command.invoke(sub_ctx))
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
14:55:26 triage.1       |     return _process_result(sub_ctx.command.invoke(sub_ctx))
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/click/core.py", line 895, in invoke
14:55:26 triage.1       |     return ctx.invoke(self.callback, **ctx.params)
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/click/core.py", line 535, in invoke
14:55:26 triage.1       |     return callback(*args, **kwargs)
14:55:26 triage.1       |   File "/srv/hubs/fedora-hubs/hubs/backend/triage.py", line 211, in main
14:55:26 triage.1       |     for task in triage(msg):
14:55:26 triage.1       |   File "/srv/hubs/fedora-hubs/hubs/backend/triage.py", line 140, in triage
14:55:26 triage.1       |     msg["_hubs"] = hubs.feed.get_hubs_for_msg(msg)
14:55:26 triage.1       |   File "/srv/hubs/fedora-hubs/hubs/feed.py", line 25, in get_hubs_for_msg
14:55:26 triage.1       |     usernames = fedmsg.meta.msg2usernames(msg)
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/fedmsg/meta/__init__.py", line 152, in __wrapper
14:55:26 triage.1       |     return f(msg, **config)
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/fedmsg/meta/__init__.py", line 171, in __wrapper
14:55:26 triage.1       |     return f(msg, processor=processor, **config)
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/fedmsg/meta/__init__.py", line 295, in msg2usernames
14:55:26 triage.1       |     return processor.usernames(msg, **config)
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/fedmsg_meta_fedora_infrastructure/mailman3.py", line 106, in usernames
14:55:26 triage.1       |     user = _email_to_username(_full_email_to_email(full_from))
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/fedmsg_meta_fedora_infrastructure/mailman3.py", line 39, in _email_to_username
14:55:26 triage.1       |     return email2fas(email, **config)
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/fedmsg_meta_fedora_infrastructure/fasshim.py", line 171, in email2fas
14:55:26 triage.1       |     fas_cache = make_fas_cache(**config)
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/fedmsg_meta_fedora_infrastructure/fasshim.py", line 129, in make_fas_cache
14:55:26 triage.1       |     auth=True)
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/fedora/client/baseclient.py", line 367, in send_request
14:55:26 triage.1       |     auth_params=auth_params, retries=retries, timeout=timeout)
14:55:26 triage.1       |   File "/usr/lib/python3.6/site-packages/fedora/client/proxyclient.py", line 462, in send_request
14:55:26 triage.1       |     'Unable to log into server.  Invalid'
14:55:26 triage.1       | fedora.client.AuthError: Unable to log into server.  Invalid authentication tokens.  Send new username and password
14:55:26 triage.1       | process terminated

Why not use assertRaises?

Metadata