Ejemplo n.º 1
0
 def test_logs_success(self):
     self.mock_select_user_by_api_key.return_value.fetchone.return_value = create_user_db_record(
     )
     users.authenticate_via_api_key(API_KEY)
     self.assertEqual([
         'INFO - Users service auth api key',
     ], self.logger.lines)
Ejemplo n.º 2
0
 def test_logs_failure_from_malformed_api_key(self):
     self.mock_select_user_by_api_key.return_value.fetchone.return_value = None
     with self.assertRaises(users.MalformedAPIKey):
         users.authenticate_via_api_key('definitely not correctly formed')
     self.assertEqual([
         'INFO - Users service auth api key',
         'ERROR - Cannot verify malformed API key: "definitely not correctly formed"'
     ], self.logger.lines)
Ejemplo n.º 3
0
 def test_logs_failure_from_unauthorized_api_key(self):
     self.mock_select_user_by_api_key.return_value.fetchone.return_value = None
     with self.assertRaises(users.Unauthorized):
         users.authenticate_via_api_key(API_KEY)
     self.assertEqual([
         'INFO - Users service auth api key',
         'ERROR - Unauthorized API key "0123456789abcdef0123456789abcdef"'
     ], self.logger.lines)
Ejemplo n.º 4
0
 def test_logs_failure_from_database_select(self):
     self.mock_select_user_by_api_key.side_effect = helpers.create_database_error(
     )
     with self.assertRaises(DatabaseError):
         users.authenticate_via_api_key(API_KEY)
     self.assertEqual([
         'INFO - Users service auth api key',
         """ERROR - Database query for API key "0123456789abcdef0123456789abcdef" failed""",
     ], self.logger.lines)
Ejemplo n.º 5
0
def auth_filter():
    log = logging.getLogger(__name__)
    request = flask.request

    if request.method == 'OPTIONS':
        log.debug('Allowing preflight request to endpoint `%s`', request.path)
        return

    # Check session
    api_key = flask.session.get('api_key')

    # Check Authorization header
    if not api_key and request.authorization:
        api_key = request.authorization['username']

    if not api_key:
        if _is_public_endpoint(request.path):
            log.debug('Allowing access to public endpoint `%s`', request.path)
            return
        return 'Cannot authenticate request: API key is missing', 401

    try:
        log.debug('Attaching user to request context')
        request.user = users.authenticate_via_api_key(api_key)
    except users.Unauthorized as err:
        return str(err), 401
    except users.MalformedAPIKey:
        return 'Cannot authenticate request: API key is malformed', 401
    except users.Error:
        return 'Cannot authenticate request: an internal error prevents API key verification', 500
Ejemplo n.º 6
0
 def test_throws_when_api_key_is_malformed(self):
     self.mock_select_user_by_api_key.return_value.fetchone.return_value = None
     with self.assertRaises(users.MalformedAPIKey):
         users.authenticate_via_api_key('definitely not correctly formed')
Ejemplo n.º 7
0
 def test_throws_when_api_key_is_unauthorized(self):
     self.mock_select_user_by_api_key.return_value.fetchone.return_value = None
     with self.assertRaises(users.Unauthorized):
         users.authenticate_via_api_key(API_KEY)
Ejemplo n.º 8
0
 def test_throws_when_database_query_fails(self):
     self.mock_select_user_by_api_key.side_effect = helpers.create_database_error(
     )
     with self.assertRaises(DatabaseError):
         users.authenticate_via_api_key(API_KEY)
Ejemplo n.º 9
0
 def test_assigns_correct_api_key(self):
     self.mock_select_user_by_api_key.return_value.fetchone.return_value = create_user_db_record(
     )
     new_user = users.authenticate_via_api_key(API_KEY)
     self.assertEqual(API_KEY, new_user.api_key)
Ejemplo n.º 10
0
 def test_returns_a_user(self):
     self.mock_select_user_by_api_key.return_value.fetchone.return_value = create_user_db_record(
     )
     user = users.authenticate_via_api_key(API_KEY)
     self.assertIsInstance(user, users.User)