class RequestRouterWithCRLValidatorTestCase(TestCase): def setUp(self) -> None: super().setUp() self.sas_url = 'https://fake.sas.url' self.rc_ingest_url = 'https://fake.rc.url' self.crl_validator_mock = mock.MagicMock(spec=CRLValidator) self.router = RequestRouter( sas_url=self.sas_url, rc_ingest_url=self.rc_ingest_url, cert_path='fake/cert/path', ssl_key_path='fake/key/path', request_mapping=request_mapping, ssl_verify=False, crl_validator=self.crl_validator_mock, ) @mocket.mocketize def test_request_router_uses_crl_validator(self, mocker: requests_mock.Mocker) -> None: # Given mocker.register_uri('POST', f'{self.sas_url}/registration') # When self.router.post_to_sas(request_dict={"registrationRequest": [{"foo": "bar"}]}) # Then self.crl_validator_mock.is_valid.assert_called_once_with(url=self.sas_url)
def process_requests( consumer: RequestDBConsumer, processor: ResponseDBProcessor, router: RequestRouter, session_manager: SessionManager, fluentd_client: FluentdClient, ) -> Optional[requests.Response]: """ Process SAS requests """ with session_manager.session_scope() as session: requests_map = consumer.get_pending_requests(session) requests_type = next(iter(requests_map)) requests_list = requests_map[requests_type] if not requests_list: logger.debug(f"Received no {requests_type} requests.") return None no_of_requests = len(requests_list) logger.info( f'Processing {no_of_requests} {requests_type} requests', ) bulked_sas_requests = merge_requests(requests_map) _log_requests_map(requests_map, fluentd_client) try: sas_response = router.post_to_sas(bulked_sas_requests) logger.info( f"Sent {bulked_sas_requests} to SAS and got the following response: {sas_response.content}", ) except RequestRouterError as e: logging.error(f"Error posting request to SAS: {e}") return None logger.info(f"About to process responses {sas_response=}") processor.process_response(requests_list, sas_response, session) session.commit() return sas_response
class RequestRouterTestCase(TestCase): def setUp(self): super().setUp() self.sas_url = 'https://fake.sas.url' self.rc_ingest_url = 'https://fake.rc.url' self.router = RequestRouter( sas_url=self.sas_url, rc_ingest_url=self.rc_ingest_url, cert_path='fake/cert/path', ssl_key_path='fake/key/path', request_mapping=request_mapping, ssl_verify=False, ) def test_router_forwarding_existing_sas_methods(self, mocker): # Given self._register_test_post_endpoints( mocker, f'{self.sas_url}/registration', ) # When resp = self.router.post_to_sas( request_dict={"registrationRequest": [{ "foo": "bar" }]}, ) # Then self.assertEqual(200, resp.status_code) def test_sas_response_is_forwarded_to_rc(self, mocker): # Given self._register_test_post_endpoints( mocker, f'{self.sas_url}/registration', ) self._register_test_post_endpoints(mocker, self.rc_ingest_url) # When sas_resp = self.router.post_to_sas( request_dict={"registrationRequest": [{ "foo": "bar" }]}, ) rc_resp = self.router.redirect_sas_response_to_radio_controller( sas_resp, ) # Then self.assertEqual(200, rc_resp.status_code) def test_router_raises_exception_for_non_existing_sas_method(self, mocker): # Given self._register_test_post_endpoints(mocker, f'{self.sas_url}/test_post') # When / Then with self.assertRaises(RequestRouterError): self.router.post_to_sas( request_dict={"nonExistingSasMethod": [{}]}, ) def _register_test_post_endpoints(self, mocker, url): mocker.register_uri('POST', url, json=self._response_callback) @staticmethod def _get_from_fixture(fixture_rel_path): return os.path.join(os.path.dirname(__file__), "fixtures", fixture_rel_path) @staticmethod def _response_callback(request, context): context.status_code = 200 return request.text