Skip to content

Commit 72f239c

Browse files
committed
[Issue #8997] Handle Privilege HTTPError without logging exception
1 parent be786cd commit 72f239c

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

api/src/legacy_soap_api/legacy_soap_api_routes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22

3+
from apiflask.exceptions import HTTPError
34
from flask import request
45

56
import src.adapters.db as db
@@ -98,6 +99,15 @@ def simpler_soap_api_route(
9899
return get_simpler_soap_response(
99100
soap_request, soap_proxy_response, db_session
100101
).to_flask_response()
102+
except HTTPError:
103+
msg = "soap_client_certificate: User did not have permission to access this application"
104+
logger.info(
105+
msg=msg,
106+
extra={
107+
"soap_api_event": LegacySoapApiEvent.ERROR_CALLING_SIMPLER,
108+
},
109+
)
110+
return soap_proxy_response.to_flask_response()
101111
except Exception:
102112
msg = "Unable to process Simpler SOAP proxy response"
103113
logger.exception(

api/tests/src/legacy_soap_api/test_legacy_soap_api_routes.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
import logging
12
from unittest import mock
23

34
from lxml import etree
45

5-
from src.legacy_soap_api.legacy_soap_api_auth import USE_SOAP_JWT_HEADER_KEY
6+
from src.constants.lookup_constants import Privilege
7+
from src.legacy_soap_api.legacy_soap_api_auth import (
8+
USE_SOAP_JWT_HEADER_KEY,
9+
SOAPAuth,
10+
SOAPClientCertificate,
11+
)
612
from src.legacy_soap_api.legacy_soap_api_utils import get_invalid_path_response
13+
from tests.lib.data_factories import setup_cert_user
14+
from tests.src.db.models.factories import (
15+
AgencyFactory,
16+
ApplicationFactory,
17+
ApplicationSubmissionFactory,
18+
CompetitionFactory,
19+
OpportunityFactory,
20+
)
721

822
NSMAP = {
923
"envelope": "http://schemas.xmlsoap.org/soap/envelope/",
@@ -15,6 +29,9 @@
1529
LEGACY_TRACKING_NUMBER = "GRANT00000008"
1630
GET_APPLICATION_PATH = f"{{{NSMAP['envelope']}}}Body/{{{NSMAP['application_request']}}}GetApplicationRequest/{{{NSMAP['tracking_number']}}}GrantsGovTrackingNumber"
1731
GET_APPLICATION_ZIP_PATH = f"{{{NSMAP['envelope']}}}Body/{{{NSMAP['application_request']}}}GetApplicationZipRequest/{{{NSMAP['tracking_number']}}}GrantsGovTrackingNumber"
32+
MOCK_FINGERPRINT = "123"
33+
MOCK_CERT = "456"
34+
MOCK_CERT_STR = "certstr"
1835

1936

2037
def test_successful_request(client, fixture_from_file, caplog) -> None:
@@ -244,3 +261,42 @@ def test_simpler_getapplicationzip_operation_returns_not_found_response_includes
244261
assert (
245262
response.headers["Set-Cookie"] == "JSESSIONID=xyz; Path=/grantsws-agency; Secure; HttpOnly"
246263
)
264+
265+
266+
def test_simpler_getapplicationzip_operation_raising_httperror_due_to_privileges_logs_info(
267+
client, fixture_from_file, enable_factory_create, caplog
268+
) -> None:
269+
caplog.set_level(logging.INFO)
270+
agency = AgencyFactory.create()
271+
opportunity = OpportunityFactory.create(agency_code=agency.agency_code)
272+
competition = CompetitionFactory(
273+
opportunity=opportunity,
274+
)
275+
WRONG_PRIVILEGES = {Privilege.READ_TEST_USER_TOKEN}
276+
user, role, soap_client_certificate = setup_cert_user(agency, WRONG_PRIVILEGES)
277+
application = ApplicationFactory.create(competition=competition)
278+
submission = ApplicationSubmissionFactory.create(application=application)
279+
full_path = "/grantsws-agency/services/v2/AgencyWebServicesSoapPort"
280+
fixture_path = "/legacy_soap_api/grantors/get_application_zip_request.xml"
281+
mock_data = fixture_from_file(fixture_path)
282+
envelope = etree.fromstring(mock_data)
283+
tracking_number = envelope.find(GET_APPLICATION_ZIP_PATH)
284+
tracking_number.text = f"GRANT{submission.legacy_tracking_number}"
285+
mock_client_cert = SOAPClientCertificate(
286+
cert=MOCK_CERT_STR,
287+
fingerprint=MOCK_FINGERPRINT,
288+
serial_number="1235",
289+
legacy_certificate=soap_client_certificate.legacy_certificate,
290+
)
291+
with mock.patch("src.legacy_soap_api.legacy_soap_api_routes.get_soap_auth") as mock_get_auth:
292+
mock_get_auth.return_value = SOAPAuth(certificate=mock_client_cert)
293+
response = client.post(
294+
full_path, data=etree.tostring(envelope), headers={"Use-Simpler-Override": "true"}
295+
)
296+
assert response.status_code == 500
297+
post_message = next(
298+
record
299+
for record in caplog.records
300+
if record.message == "User did not have permission to access this application"
301+
)
302+
assert post_message.message == "User did not have permission to access this application"

0 commit comments

Comments
 (0)