def get(self, _): error_response = ErrorLCResponse(status_code=mock_status_code, errors=[mock_formatted_error]) error_response.add_error(mock_formatted_error) raise error_response
def exception_handler(exc, context=None) -> ErrorLCResponse: if isinstance(exc, ErrorLCResponse): return exc elif isinstance(exc, NotAuthenticated): formatted_error = FormattedError( code=ErrorResponseCodes.not_authenticated, source='NotAuthenticated', detail=exc.detail, status=exc.status_code, ) return ErrorLCResponse( status_code=exc.status_code, errors=[formatted_error], ) else: logger.exception(exc) status_code = status.HTTP_500_INTERNAL_SERVER_ERROR formatted_error = FormattedError( code=ErrorResponseCodes.unhandled_exception, source='exception_handler', detail=str(exc), status=status_code, ) return ErrorLCResponse( status_code=status_code, errors=[formatted_error], )
def test_error_response_shorthand(): response = ErrorLCResponse( status_code=mock_status_code, errors=[formatted_error], ) response.add_error(formatted_error) num_errors_expected = 2 assert response.data['errors'] == [formatted_error] * num_errors_expected assert response.status_code == mock_status_code
def test_required_headers_via_get(http_method, endpoint, endpoint_kwargs): mocked_lms_base_url = 'http://jjjjjjjj' headers = { header_key: header_val for header_key, header_val, in fixtures.get_mocked_headers( mocked_lms_base_url).items() if header_key == 'HTTP_LMS_TYPE' } client = Client() resp = getattr(client, http_method)( reverse( endpoint, kwargs=endpoint_kwargs, ), content_type='application/json', **headers, ) expected_errors = [] for required_header in sakai.DEFAULT_REQUIRED_HEADERS: expected_errors.append( FormattedError( source=helpers.HEADERS_PROCESSOR, code=ErrorResponseCodes.missing_required_header, detail=helpers.internal_header_to_external(required_header))) expected_error_response = ErrorLCResponse( status_code=status.HTTP_400_BAD_REQUEST, errors=expected_errors, ) assert resp.data == expected_error_response.data
def test_required_get_auth_headers(): """ Test error response from missing auth headers. """ mock_authorize_url = 'http://host/oauth-tool/authorize/' data = { 'request_token_url': 'http://host/oauth-tool/request_tokén', 'authorize_url': mock_authorize_url, 'callback_url': "http://this.doesnt.ma/tter", } headers = { header_key: header_val for header_key, header_val, in fixtures.get_mocked_headers( 'http://somebaseurl').items() if header_key == 'HTTP_LMS_TYPE' } client = Client() resp = client.get( reverse('auth_url'), content_type='application/json', data=data, **headers, ) expected_errors = [] for required_header in sakai.AUTH_REQUIRED_HEADERS: expected_errors.append( FormattedError( source=helpers.HEADERS_PROCESSOR, code=ErrorResponseCodes.missing_required_header, detail=helpers.internal_header_to_external(required_header))) expected_error_response = ErrorLCResponse( status_code=status.HTTP_400_BAD_REQUEST, errors=expected_errors, ) assert resp.data == expected_error_response.data
def incoming_request_headers(self) -> Dict: if self._incoming_request_headers is not None: return self._incoming_request_headers else: # This would be a programming error, a user should not be able # to cause this. formatted_error = FormattedError( source='incoming request headers property', code=ErrorResponseCodes.headers_not_set, detail='headers were accessed without being set.') raise ErrorLCResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, errors=[formatted_error])
def _raise_thirdparty_error_on_error(cls, url): try: yield except Exception as e: logger.info(traceback.format_exc()) error = FormattedError( source=url, code=ErrorResponseCodes.bad_thirdparty_request, detail=str(e), ) raise ErrorLCResponse( status_code=status.HTTP_400_BAD_REQUEST, errors=[error], )
def get_connector( lms_type: str, lms_base_url: str, ) -> 'AbstractLMSConnector': if lms_type == 'sakai': from lms_connector.connectors.sakai import SakaiConnector connector = SakaiConnector else: formatted_error = FormattedError( source='get_connector', code=ErrorResponseCodes.unsupported_lms, detail=ErrorResponseDetails.lms_not_supported(str(lms_type)), ) raise ErrorLCResponse( status_code=status.HTTP_400_BAD_REQUEST, errors=[formatted_error], ) return connector(lms_base_url=lms_base_url, )
def raise_for_missing_headers( incoming_headers: Dict, required_headers: List[str], ): errors = [] for required_header in required_headers: if required_header not in incoming_headers: errors.append( FormattedError( source=HEADERS_PROCESSOR, code=ErrorResponseCodes.missing_required_header, detail=internal_header_to_external(required_header), )) if errors: raise ErrorLCResponse( status_code=status.HTTP_400_BAD_REQUEST, errors=errors, )
def test_inheritance(): """ Test inheritance of response classes. Inheriting from DRFResponse is particularly important because, for example, it is what causes self.data to be rendered correctly in the response. """ response = LCResponse(status_code=mock_status_code) assert isinstance(response, DRFResponse) single_response = SingleLCResponse(status_code=mock_status_code, result={}) assert isinstance(single_response, LCResponse) multi_response = MultiLCResponse(status_code=mock_status_code) assert isinstance(multi_response, LCResponse) error_response = ErrorLCResponse(status_code=mock_status_code) assert isinstance(error_response, LCResponse)
def test_error_response(): response = ErrorLCResponse(status_code=mock_status_code) response.add_error(formatted_error) assert response.data['errors'] == [formatted_error] assert response.status_code == mock_status_code