def test_list_type_value_right_type(self): prototype = PrototypeHandler({'foo': 1}) response = CRUDResponse() item = {'foo': 2} result = prototype.check(item, 'create', response) self.assertTrue(result) self.assertEqual(response.status, 'success')
def test_int_value_default(self): response = CRUDResponse() prototype = PrototypeHandler({'foo': 1}) item = {} result = prototype.check(item, 'create', response) self.assertTrue(result) self.assertEqual(response.status, 'success') self.assertEqual(item['foo'], 1)
def test_list_type_value_wrong_type(self): response = CRUDResponse() prototype = PrototypeHandler({'foo': []}) item = {'foo': 1} result = prototype.check(item, 'create', response) self.assertFalse(result) self.assertEqual(response.status, 'error') self.assertEqual(response.error_type, 'InvalidType')
def test_timestamp_value_create(self): response = CRUDResponse() prototype = PrototypeHandler({'foo': '<on-create:timestamp>'}) item = {'bar': 2} result = prototype.check(item, 'create', response) self.assertTrue(result) self.assertEqual(response.status, 'success') self.assertEqual(item['bar'], 2) self.assertTrue(isinstance(item['foo'], int))
def test_uuid_value_create(self): response = CRUDResponse() prototype = PrototypeHandler({'foo': '<on-create:uuid>'}) item = {'bar': 2} result = prototype.check(item, 'create', response) self.assertTrue(result) self.assertEqual(response.status, 'success') self.assertEqual(item['bar'], 2) self.assertTrue(self.uuid_re.match(item['foo']))
def __init__(self, **kwargs): """ Create a new CRUD handler. The CRUD handler accepts the following parameters: * table_name - name of the backing DynamoDB table (required) * profile_name - name of the AWS credential profile to use when creating the boto3 Session * region_name - name of the AWS region to use when creating the boto3 Session * prototype - a dictionary of name/value pairs that will be used to initialize newly created items * supported_ops - a list of operations supported by the CRUD handler (choices are list, get, create, update, delete, search, increment_counter, describe, help, ping) * encrypted_attributes - a list of tuples where the first item in the tuple is the name of the attribute that should be encrypted and the second item in the tuple is the KMS master key ID to use for encrypting/decrypting the value * debug - if not False this will cause the raw_response to be left in the response dictionary """ self.table_name = kwargs['table_name'] profile_name = kwargs.get('profile_name') region_name = kwargs.get('region_name') placebo = kwargs.get('placebo') placebo_dir = kwargs.get('placebo_dir') placebo_mode = kwargs.get('placebo_mode', 'record') self.prototype = kwargs.get('prototype', dict()) self._prototype_handler = PrototypeHandler(self.prototype) self.supported_ops = kwargs.get('supported_ops', self.SupportedOps) self.supported_ops.append('describe') self.encrypted_attributes = kwargs.get('encrypted_attributes', list()) session = boto3.Session(profile_name=profile_name, region_name=region_name) if placebo and placebo_dir: self.pill = placebo.attach(session, placebo_dir, debug=True) if placebo_mode == 'record': self.pill.record() else: self.pill.playback() else: self.pill = None ddb_resource = session.resource('dynamodb') self.table = ddb_resource.Table(self.table_name) self._indexes = {} self._analyze_table() self._debug = kwargs.get('debug', False) if self.encrypted_attributes: self._kms_client = session.client('kms') else: self._kms_client = None
def test_no_prototype(self): response = CRUDResponse() prototype = PrototypeHandler({}) item = {'foo': 'bar'} result = prototype.check(item, 'create', response) self.assertTrue(result)