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)
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
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_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 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) })
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 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_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')