Exemplo n.º 1
0
 def getAndValidatePrincipal(self):
     principal = getAndValidateJSON(
         self.eventGet('requestContext.authorizer.principalId'),
         'principal', principalSchema,
         lambda: HttpError(HttpError.UNAUTHORIZED, 'Missing principal'),
         lambda: HttpError(HttpError.UNAUTHORIZED,
                           'Malformed principal JSON'),
         lambda: HttpError(HttpError.UNAUTHORIZED, 'Invalid principal'))
     principal['roles'] = set(principal['roles'])
     return Principal(principal)
Exemplo n.º 2
0
 def getAndValidateEntity(self, schema, name):
     contentType = self.eventGet('headers.Content-Type')
     if contentType and not APPLICATION_JSON_MATCHER.match(contentType):
         raise HttpError(HttpError.UNSUPPORTED_MEDIA_TYPE,
                         'Expected application/json Content-Type')
     entity = getAndValidateJSON(
         self.eventGet('body'), name, schema, lambda: HttpError(
             HttpError.BAD_REQUEST, 'Missing {0}'.format(name)),
         lambda: HttpError(HttpError.BAD_REQUEST, 'Malformed {0} JSON'.
                           format(name)),
         lambda: HttpError(HttpError.UNPROCESSABLE_ENTITY, 'Invalid {0}'.
                           format(name)))
     return entity
Exemplo n.º 3
0
 def getParameter(self, origin, basePath, name, required, validator):
     param = self.eventGet('{0}.{1}'.format(basePath, name))
     if required and param is None:
         raise HttpError(HttpError.BAD_REQUEST,
                         'Missing {0} "{1}"'.format(origin, name))
     if (validator):
         try:
             validator(param)
         except Exception as error:
             raise HttpError(HttpError.BAD_REQUEST,
                             'Invalid {0} "{1}"'.format(origin, name),
                             [error.__str__()])
     return param
 def test_wrapNspError(self):
     'It should wrap the NspError with the expected attributes'
     nspError = NspError(NspError.THING_NOT_FOUND, 'message', ['causes'])
     e = HttpError.wrap(nspError)
     self.assertEqual(e.message, nspError.message)
     self.assertEqual(e.causes, nspError.causes)
     self.assertEqual(e.timestamp, nspError.timestamp)
 def test_init3(self):
     'The instance should have the expected attributes [without causes]'
     e = HttpError(HttpError.NOT_FOUND, 'message', timestamp='dummy')
     self.assertEqual(e.statusCode, 404)
     self.assertEqual(e.message, 'message')
     self.assertEqual(e.causes, [])
     self.assertEqual(e.timestamp, 'dummy')
 def test_init2(self):
     'The instance should have the expected attributes [without timestamp]'
     e = HttpError(HttpError.NOT_FOUND, 'message', ['cause'])
     self.assertEqual(e.statusCode, 404)
     self.assertEqual(e.message, 'message')
     self.assertEqual(e.causes, ['cause'])
     self.assertIsInstance(e.timestamp, datetime.datetime)
 def test_wrapException(self):
     'It should wrap an Exception with the expected attributes'
     try:
         raise (Exception('hello'))
     except Exception as exception:
         e = HttpError.wrap(exception)
         self.assertEqual(e.statusCode, HttpError.INTERNAL_SERVER_ERROR)
         self.assertEqual(
             e.message,
             repr(exception) + ':\n' +
             ''.join(traceback.format_tb(sys.exc_info()[2])))
         self.assertEqual(e.causes, [])
         self.assertIsInstance(e.timestamp, datetime.datetime)
 def testWithHttpError(self):
     '''APIGateway.createErrorResponse() should create a response with the statusCode of the passed HttpError, the
     Access-Control-Allow-Origin header and the conversion to json of the __dict__ of the passed HttpError as
     body'''
     event = {}
     sut = APIGateway(mockLoggerFactory, event)
     error = HttpError(HttpError.NOT_FOUND, 'message')
     response = sut.createErrorResponse(error)
     self.assertEqual(response, {
         'statusCode': error.statusCode,
         'headers': {'Access-Control-Allow-Origin': '*'},
         'body': json.dumps(error.__dict__, default=jsonutils.dumpdefault)
     })
Exemplo n.º 9
0
 def wasModifiedSince(self, entity):
     ifModifiedSince = self.eventGet('headers.If-Modified-Since')
     if ifModifiedSince is None:
         return True
     try:
         ifModifiedSince = int(
             dateutil.parser.parse(ifModifiedSince).timestamp())
         lastModified = int(entity['lastModified'].timestamp())
         return lastModified > ifModifiedSince
     except Exception as e:
         raise HttpError(
             HttpError.BAD_REQUEST,
             'Invalid If-Modified-Since header: "{0}"'.format(
                 ifModifiedSince), [repr(e)])
 def testWithNspError(self):
     '''APIGateway.createErrorResponse() should wrap the passed NspError in an HttpError and then create an error
     response with it'''
     event = {}
     sut = APIGateway(mockLoggerFactory, event)
     error = NspError(NspError.THING_NOT_FOUND, 'message')
     httpError = HttpError.wrap(error)
     httpError.method = sut.getHttpMethod()
     httpError.resource = sut.getHttpResource()
     response = sut.createErrorResponse(error)
     self.assertEqual(response, {
         'statusCode': httpError.statusCode,
         'headers': {'Access-Control-Allow-Origin': '*'},
         'body': json.dumps(httpError.__dict__, default=jsonutils.dumpdefault)
     })
 def testWithException(self):
     '''APIGateway.createErrorResponse() should wrap the passed Exception in an HttpError and the create an error
     response with it'''
     event = {}
     sut = APIGateway(mockLoggerFactory, event)
     error = KeyError('unknown')
     httpError = HttpError.wrap(error)
     httpError.method = sut.getHttpMethod()
     httpError.resource = sut.getHttpResource()
     response = sut.createErrorResponse(error)
     self.assertEqual(response['statusCode'], httpError.statusCode)
     self.assertEqual(response['headers'], {'Access-Control-Allow-Origin': '*'})
     body = json.loads(response['body'])
     self.assertEqual(body['message'], httpError.message)
     self.assertEqual(body['method'], httpError.method)
     self.assertEqual(body['resource'], httpError.resource)
     self.assertEqual(body['causes'], [])
 def test_UNPROCESSABLE_ENTITY(self):
     'The UNPROCESSABLE_ENTITY instance should have the expected attributes'
     e = HttpError(HttpError.UNPROCESSABLE_ENTITY, 'message')
     self.assertEqual(e.statusCode, 422)
     self.assertEqual(e.statusReason, 'Unprocessable entity')
 def test_UNAUTHORIZED(self):
     'The UNAUTHORIZED instance should have the expected attributes'
     e = HttpError(HttpError.UNAUTHORIZED, 'message')
     self.assertEqual(e.statusCode, 401)
     self.assertEqual(e.statusReason, 'Unauthorized')
 def test_NOT_FOUND(self):
     'The NOT_FOUND instance should have the expected attributes'
     e = HttpError(HttpError.NOT_FOUND, 'message')
     self.assertEqual(e.statusCode, 404)
     self.assertEqual(e.statusReason, 'Not found')
 def test_INTERNAL_SERVER_ERROR(self):
     'The INTERNAL_SERVER_ERROR instance should have the expected attributes'
     e = HttpError(HttpError.INTERNAL_SERVER_ERROR, 'message')
     self.assertEqual(e.statusCode, 500)
     self.assertEqual(e.statusReason, 'Internal server error')
 def test_FORBIDDEN(self):
     'The FORBIDDEN instance should have the expected attributes'
     e = HttpError(HttpError.FORBIDDEN, 'message')
     self.assertEqual(e.statusCode, 403)
     self.assertEqual(e.statusReason, 'Forbidden')
 def test_BAD_REQUEST(self):
     'The BAD_REQUEST instance should have the expected attributes'
     e = HttpError(HttpError.BAD_REQUEST, 'message')
     self.assertEqual(e.statusCode, 400)
     self.assertEqual(e.statusReason, 'Bad request')
 def test_UNSUPPORTED_MEDIA_TYPE(self):
     'The UNSUPPORTED_MEDIA_TYPE instance should have the expected attributes'
     e = HttpError(HttpError.UNSUPPORTED_MEDIA_TYPE, 'message')
     self.assertEqual(e.statusCode, 415)
     self.assertEqual(e.statusReason, 'Unsupported media type')
 def test_wrap_THING_NOT_FOUND(self):
     'It should wrap the THING_NOT_FOUND NspError'
     e = HttpError.wrap(NspError(NspError.THING_NOT_FOUND, 'message'))
     self.assertEqual(e.statusCode, 404)
     self.assertEqual(e.message, 'message')
 def test_wrap_INTERNAL_SERVER_ERROR(self):
     'It should wrap the INTERNAL_SERVER_ERROR NspError'
     e = HttpError.wrap(NspError(NspError.INTERNAL_SERVER_ERROR, 'message'))
     self.assertEqual(e.statusCode, 500)
     self.assertEqual(e.message, 'message')
 def test_wrap_FORBIDDEN(self):
     'It should wrap the FORBIDDEN NspError'
     e = HttpError.wrap(NspError(NspError.FORBIDDEN, 'message'))
     self.assertEqual(e.statusCode, 403)
     self.assertEqual(e.message, 'message')
 def test_wrap_THING_UNPROCESSABLE(self):
     'It should wrap the THING_UNPROCESSABLE NspError'
     e = HttpError.wrap(NspError(NspError.THING_UNPROCESSABLE, 'message'))
     self.assertEqual(e.statusCode, 422)
     self.assertEqual(e.message, 'message')
Exemplo n.º 23
0
 def createErrorResponse(self, error):
     if not isinstance(error, HttpError):
         error = HttpError.wrap(error)
     error.method = self.getHttpMethod()
     error.resource = self.getHttpResource()
     return createResponse(error.statusCode, {}, error.__dict__)