def get(self, request, pk): """ Proxy to Export Wins API for GET requests for given company's match id is obtained from Company Matching Service. """ company = self._get_company(pk) try: match_ids = self._get_match_ids(company, request) export_wins_results = get_export_wins(match_ids, request) return JsonResponse(export_wins_results.json()) except ( CompanyMatchingServiceConnectionError, CompanyMatchingServiceTimeoutError, CompanyMatchingServiceHTTPError, ExportWinsAPIConnectionError, ExportWinsAPITimeoutError, ExportWinsAPIHTTPError, ) as exc: raise APIUpstreamException(str(exc)) else: return HttpResponse( status=status.HTTP_500_INTERNAL_SERVER_ERROR, content_type='application/json', )
def test_export_wins_api_error( self, requests_mock, response_status, ): """Test if the export wins api returns a status code that is not 200.""" match_id = 1 requests_mock.get( f'/wins/match?match_id={match_id}', status_code=response_status, ) with pytest.raises( ExportWinsAPIHTTPError, match=f'The Export Wins API returned an error status: {response_status}', ): get_export_wins([match_id])
def get(self, request, pk): """ Proxy to Export Wins API for GET requests for given company's match id is obtained from Company Matching Service. """ company = self._get_company(pk) try: match_ids = self._get_match_ids(company, request) except ( CompanyMatchingServiceConnectionError, CompanyMatchingServiceTimeoutError, CompanyMatchingServiceHTTPError, ) as exc: raise APIUpstreamException(str(exc)) if not match_ids: return JsonResponse( { 'count': 0, 'next': None, 'previous': None, 'results': [], }, ) try: export_wins_results = get_export_wins(match_ids, request) except ( ExportWinsAPIConnectionError, ExportWinsAPITimeoutError, ExportWinsAPIHTTPError, ) as exc: raise APIUpstreamException(str(exc)) return JsonResponse(export_wins_results.json())
def test_export_wins_api_request_error( self, requests_mock, request_exception, expected_exception, ): """ Test if there is an error connecting to export wins API the expected exception was thrown. """ match_id = 1234 requests_mock.get( f'/wins/match?match_id={match_id}', exc=request_exception, ) with pytest.raises(expected_exception): get_export_wins([match_id])
def test_get_call_invoked_multiple_match_ids( self, requests_mock, ): """ Check GET call will be made """ dynamic_response = HawkMockJSONResponse( api_id=settings.EXPORT_WINS_HAWK_ID, api_key=settings.EXPORT_WINS_HAWK_KEY, ) matcher = requests_mock.get( '/wins/match?match_id=1234,2345', status_code=status.HTTP_200_OK, text=dynamic_response, ) get_export_wins([1234, 2345]) assert matcher.called_once
def test_export_wins_api_missing_settings_error(self): """ Test when environment variables are not set an exception is thrown. """ with pytest.raises(ImproperlyConfigured): get_export_wins([1234])