def test_initWithCauses(self): 'The instance should have the expected attributes [with causes]' e = NspError('code', 'message', ['cause1']) self.assertEqual(e.code, 'code') self.assertEqual(e.message, 'message') self.assertEqual(e.causes, ['cause1']) self.assertIsInstance(e.timestamp, datetime.datetime)
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 getAndCheckThing(principal, uuid): thing = repository.getThing(uuid) if thing is None: raise NspError(NspError.THING_NOT_FOUND, 'Thing "{0}" not found'.format(uuid)) else: principal.checkVisibility(thing, 'Thing', NspError.THING_NOT_FOUND) return thing
def checkReadOnlyProperties(self, oldEntity, newEntity, readOnlyProperties, errorCode): if not self.isAdmin(): readOnlyProperties = readOnlyProperties.copy() readOnlyProperties.append('owner') errors = [] for property in readOnlyProperties: if not oldEntity[property] == newEntity[property]: errors.append('Cannot change read-only property "{0}" from "{1}" to "{2}"'.format( property, oldEntity[property], newEntity[property] )) if errors: raise NspError(errorCode, 'Cannot change read-only properties', errors)
def validateJSON(instance, name, schema, errorFactory): try: jsonschema.Draft4Validator.check_schema(schema) except Exception as schemaError: raise NspError(NspError.INTERNAL_SERVER_ERROR, 'Invalid {0} JSON schema'.format(name), [str(schemaError)]) validator = jsonschema.Draft4Validator( schema, format_checker=jsonschema.FormatChecker()) if not validator.is_valid(instance): error = errorFactory() error.causes = [e.message for e in validator.iter_errors(instance)] raise error
def createThing(self, principal, thing): logger.debug('createThing(): principal=%s, thing=%s', principal, thing) if thing.get('uuid') is not None: existing = repository.getThing(thing['uuid']) if existing is not None: raise NspError( NspError.THING_ALREADY_EXISTS, 'Thing "{0}" already exists'.format(thing['uuid'])) else: thing['uuid'] = str(uuid4()) thing['created'] = datetime.now() thing['lastModified'] = thing['created'] checkCreate(principal, thing) return repository.createThing(thing)
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 raiseForbiddenError(action): raise NspError(NspError.FORBIDDEN, 'Principal is not authorized to {0}'.format(action))
def checkVisibility(self, entity, entityName, errorCode): if not self.isAdmin() and entity.get('owner') != self.organizationId: raise NspError(errorCode, '{0} "{1}" not found'.format(entityName.capitalize(), entity['uuid']))
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')
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')