Commit 206d1540 authored by Alexander Pace's avatar Alexander Pace

Exception fixes for ligo-gracedb-2.7.1

parent b68da6ee
Pipeline #162389 passed with stages
in 15 minutes and 52 seconds
%define name ligo-gracedb
%define version 2.7.0
%define unmangled_version 2.7.0
%define version 2.7.1
%define unmangled_version 2.7.1
%define release 1
Summary: Gravity Wave Candidate Event Database
......
......@@ -90,8 +90,14 @@ def test_main_with_bad_response(main_tester, capsys, response_msg):
code=status, reason=reason)
# Define an error response object:
err_response = Response()
err_response.status_code = 400
err_response.reason = 'Bad Request'
err_response._content = b'test'
EXCEPTION_DATA = [Exception('test'),
HTTPError(400, 'Bad Request', 'test')]
HTTPError(response=err_response)]
@pytest.mark.parametrize("exc", EXCEPTION_DATA) # noqa: E302
def test_main_with_exception_in_CLI_call(main_tester, capsys, exc):
......
......@@ -26,7 +26,7 @@ from .extern.safe_netrc import netrc
from os import getuid
from .version import __version__
from .adapter import GraceDbCertAdapter
from .utils import hook_response
from .utils import hook_response, raise_status_exception
# To remove later: python2 compatibility fix:
if sys.version_info[0] > 2:
......@@ -119,7 +119,7 @@ class GraceDBClient(Session):
self.headers.update(self._update_headers())
# Adjust the response via a session hook:
self.hooks = {'response': hook_response}
self.hooks = {'response': [hook_response, raise_status_exception]}
if reload_certificate and self.auth_type == 'x509':
self.mount('https://', GraceDbCertAdapter(
......
# Custom exceptions
class HTTPError(Exception):
def __init__(self,
status_code,
reason,
text):
self.status = status_code
self.status_code = status_code
self.reason = reason
self.message = text
self.text = text
Exception.__init__(self,
status_code,
reason,
text)
import requests.exceptions
class HTTPError(requests.exceptions.HTTPError):
def __init__(self, response):
self.response = response
self.status_code = response.status_code
self.status = response.status_code
self.reason = response.reason
self.message = response.text
self.text = response.text
Exception.__init__(self, response)
......@@ -281,17 +281,6 @@ Longer strings will be truncated.""" % {
# Compile dict of kwargs for instantiating the client class
client_args = {}
# Process proxy args
proxyport = None
if proxy and proxy.find(':') > 0:
try:
proxy, proxyport = proxy.split(':')
proxyport = int(proxyport)
except:
op.error("Malformed proxy: '%s'" % proxy)
client_args['proxy_host'] = proxy
client_args['proxy_port'] = proxyport
# Auth args
client_args['force_noauth'] = options.force_noauth
client_args['username'] = options.username
......
......@@ -184,9 +184,7 @@ class GraceDb(GraceDBClient):
raise e
else:
if r.status_code != 200:
raise HTTPError(r.status_code,
r.reason,
r.text)
raise HTTPError(r)
self._service_info = r.json()
return self._service_info
......
......@@ -4,6 +4,7 @@ import pytest
import shlex
from ligo.gracedb.cli.client import CommandLineInterface
from ligo.gracedb.exceptions import HTTPError
# Apply module-level marks
pytestmark = [pytest.mark.cli, pytest.mark.integration]
......@@ -499,15 +500,16 @@ def test_update_grbevent():
cmd = ('update grbevent {gid} --redshift={rs} --designation="{des}" '
'--ra={ra}').format(gid=gid, rs=redshift, des=designation,
ra=ra)
response = run_CLI_test(cmd)
new_data = response.json()
try:
response = run_CLI_test(cmd)
new_data = response.json()
# Even though they're test GRB events, unprivileged users
# can't update GRB events. So try to catch a 200 error
# in the case of an admin or grb user, and 403 otherwise.
# This came up with setting up the gitlab integration instance.
# Even though they're test GRB events, unprivileged users
# can't update GRB events. So try to catch a 200 status
# in the case of an admin or grb user, and 403 otherwise.
# This came up with setting up the gitlab integration instance.
if response.status_code == 200:
assert response.status_code == 200
# Compare results
initial_grb_params = initial_data['extra_attributes']['GRB']
new_grb_params = new_data['extra_attributes']['GRB']
......@@ -517,12 +519,8 @@ def test_update_grbevent():
assert new_grb_params['ra'] != initial_grb_params['ra']
assert new_grb_params['redshift'] != initial_grb_params['redshift']
elif response.status_code == 403:
# Make sure the permissions error is returned:
assert 'You do not have permission' in new_data.get('detail')
else:
assert response.status_code <= 400
except HTTPError as e:
assert e.status_code == 403
def test_confirm_as_gw():
......@@ -604,37 +602,35 @@ def test_permissions():
# Expose superevent
cmd = 'expose {sid}'.format(sid=sid)
response = run_CLI_test(cmd)
out = response.json()
try:
response = run_CLI_test(cmd)
out = response.json()
# Check response-- this will be 200 for admins and
# em_advocates, or 403 for everyone else, like th
# gitlab runner.
if response.status_code == 200:
# Check response-- this will be 200 for admins and
# em_advocates, or 403 for everyone else, like th
# gitlab runner.
assert response.status_code == 200
assert isinstance(out, list)
assert len(out) == 3
elif response.status_code == 403:
assert "You are not allowed" in out.get('detail')
else:
assert response.status_code <= 400
except HTTPError as e:
assert e.status_code == 403
# Hide superevent
cmd = 'hide {sid}'.format(sid=sid)
response = run_CLI_test(cmd)
out = response.json()
try:
response = run_CLI_test(cmd)
out = response.json()
# Check response-- same rules apply for exposing:
# Note that unauthorized users will get the 403 response
# at the permissions check even if the superevent is not
# exposed. So if the previous test 200'ed, then in theory
# this one will too if everything worked.
if response.status_code == 200:
# Check response-- same rules apply for exposing:
# Note that unauthorized users will get the 403 response
# at the permissions check even if the superevent is not
# exposed. So if the previous test 200'ed, then in theory
# this one will too if everything worked.
assert response.status_code == 200
assert isinstance(out, list)
assert len(out) == 0
elif response.status_code == 403:
assert "You are not allowed" in out.get('detail')
else:
assert response.status_code <= 400
except HTTPError as e:
assert e.status_code == 403
def test_voevents():
......@@ -787,11 +783,12 @@ def test_signoffs():
# Create signoff ----------------------------------------------------------
cmd = 'create signoff {sid} ADV OK "looks good"'.format(sid=sid)
response = run_CLI_test(cmd)
out = response.json()
try:
response = run_CLI_test(cmd)
out = response.json()
# Check response
if response.status_code == 201:
# Check response
assert response.status_code == 201
assert out['status'] == 'OK'
assert out['comment'] == 'looks good'
assert out['instrument'] == ''
......@@ -803,11 +800,9 @@ def test_signoffs():
# clever way of doing it.
authorized_user = True
elif response.status_code == 403:
assert 'You do not have permission' in out.get('detail')
except HTTPError as e:
assert e.status_code == 403
authorized_user = False
else:
assert response.status_code <= 400
if authorized_user:
# Get signoff ------------------------------------------------------
......
import pytest
from ligo.gracedb.exceptions import HTTPError
# Apply module-level mark
pytestmark = pytest.mark.integration
......@@ -38,9 +39,8 @@ def test_creation_with_already_used_preferred_event(client, create_event):
# Try to create new superevent with an existing
# preferred event:
response = client.createSuperevent(1, 2, 3, event, category='T')
assert response.status_code == 400
assert response.reason == 'Bad Request'
with pytest.raises(HTTPError):
response = client.createSuperevent(1, 2, 3, event, category='T')
def test_creation_with_events(client, create_event):
......@@ -163,18 +163,16 @@ def test_event_addition_and_removal(client, create_event, create_superevent):
assert new_event['graceid'] not in data['gw_events']
# Try remove event again, should get error
response = client.removeEventFromSuperevent(
superevent['superevent_id'], new_event['graceid']
)
assert response.status_code == 404
assert response.reason == 'Not Found'
with pytest.raises(HTTPError):
client.removeEventFromSuperevent(
superevent['superevent_id'], new_event['graceid']
)
# Try remove preferred event, should get error
response = client.removeEventFromSuperevent(
superevent['superevent_id'], superevent['preferred_event']
)
assert response.status_code == 400
assert response.reason == 'Bad Request'
with pytest.raises(HTTPError):
client.removeEventFromSuperevent(
superevent['superevent_id'], superevent['preferred_event']
)
def test_confirm_as_gw(client, create_superevent):
......@@ -194,9 +192,8 @@ def test_confirm_as_gw(client, create_superevent):
assert gw_id.startswith('TGW') or 'GW' in gw_id[:3]
# Try to update again
response = client.confirm_superevent_as_gw(superevent['superevent_id'])
assert response.status_code == 400
assert response.reason == 'Bad Request'
with pytest.raises(HTTPError):
client.confirm_superevent_as_gw(superevent['superevent_id'])
# Test getting data using both IDs
response = client.superevent(superevent['superevent_id'])
......
import os
import pytest
from ligo.gracedb.exceptions import HTTPError
# Apply module-level marks
pytestmark = pytest.mark.integration
......@@ -19,20 +19,21 @@ def test_update_grbevent(client, test_data_dir):
redshift = 3.4
designation = 'very good'
ra = 12.34
response = client.update_grbevent(
gid,
redshift=redshift,
designation=designation,
ra=ra
)
new_data = response.json()
# Even though they're test GRB events, unprivileged users
# can't update GRB events. So try to catch a 200 error
# in the case of an admin or grb user, and 403 otherwise.
# This came up with setting up the gitlab integration instance.
if response.status_code == 200:
try:
response = client.update_grbevent(
gid,
redshift=redshift,
designation=designation,
ra=ra
)
new_data = response.json()
# Even though they're test GRB events, unprivileged users
# can't update GRB events. So try to catch a 200 error
# in the case of an admin or grb user, and 403 otherwise.
# This came up with setting up the gitlab integration instance.
assert response.status_code == 200
# Compare results
initial_grb_params = initial_data['extra_attributes']['GRB']
new_grb_params = new_data['extra_attributes']['GRB']
......@@ -42,9 +43,5 @@ def test_update_grbevent(client, test_data_dir):
assert new_grb_params['ra'] != initial_grb_params['ra']
assert new_grb_params['redshift'] != initial_grb_params['redshift']
elif response.status_code == 403:
# Make sure the permissions error is returned:
assert 'You do not have permission' in new_data.get('detail')
else:
assert response.status_code <= 400
except HTTPError as e:
assert e.status_code == 403
import re
import six
import mimetypes
import requests
from functools import wraps
from .exceptions import HTTPError
EVENT_PREFIXES = ['G', 'E', 'H', 'M', 'T']
event_prefix_regex = re.compile(r'^({prefixes})\d+'.format(
......@@ -65,3 +67,12 @@ def get_mimetype(file_to_send):
# https://requests.readthedocs.io/en/master/user/advanced/#event-hooks
def hook_response(r, *args, **kwargs):
r.status = r.status_code
# Another hook: return the raise_for_status behavior which was previously
# enabled in the adjustResponse function.
def raise_status_exception(r, *args, **kwargs):
try:
r.raise_for_status()
except requests.HTTPError as e:
raise HTTPError(response=e.response)
__version__ = '2.7.0'
__version__ = '2.7.1'
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment