def test_user_configuration(self): """Test that we can do CRUD operations on user configuration data.""" # Create a test folder that we delete afterwards f = Messages(parent=self.account.inbox, name=get_random_string(16)).save() # The name must be fewer than 237 characters, can contain only the characters "A-Z", "a-z", "0-9", and ".", # and must not start with "IPM.Configuration" name = get_random_string(16, spaces=False, special=False) # Should not exist yet with self.assertRaises(ErrorItemNotFound): f.get_user_configuration(name=name) # Create a config dictionary = { get_random_bool(): get_random_str_tuple(10, 2), get_random_int(): get_random_bool(), get_random_byte(): get_random_int(), get_random_bytes(16): get_random_byte(), get_random_string(8): get_random_bytes(16), get_random_datetime(tz=self.account.default_timezone): get_random_string(8), get_random_str_tuple(4, 4): get_random_datetime(tz=self.account.default_timezone), } xml_data = b'<foo>%s</foo>' % get_random_string(16).encode('utf-8') binary_data = get_random_bytes(100) f.create_user_configuration(name=name, dictionary=dictionary, xml_data=xml_data, binary_data=binary_data) # Fetch and compare values config = f.get_user_configuration(name=name) self.assertEqual(config.dictionary, dictionary) self.assertEqual(config.xml_data, xml_data) self.assertEqual(config.binary_data, binary_data) # Cannot create one more with the same name with self.assertRaises(ErrorItemSave): f.create_user_configuration(name=name) # Does not exist on a different folder with self.assertRaises(ErrorItemNotFound): self.account.inbox.get_user_configuration(name=name) # Update the config f.update_user_configuration( name=name, dictionary={'bar': 'foo', 456: 'a', 'b': True}, xml_data=b'<foo>baz</foo>', binary_data=b'YYY' ) # Fetch again and compare values config = f.get_user_configuration(name=name) self.assertEqual(config.dictionary, {'bar': 'foo', 456: 'a', 'b': True}) self.assertEqual(config.xml_data, b'<foo>baz</foo>') self.assertEqual(config.binary_data, b'YYY') # Delete the config f.delete_user_configuration(name=name) # We already deleted this config with self.assertRaises(ErrorItemNotFound): f.get_user_configuration(name=name) f.delete()
def test_create_update_empty_delete(self): name = get_random_string(16) f = Messages(parent=self.account.inbox, name=name).save() with self.assertRaises(ErrorFolderExists): Messages(parent=self.account.inbox, name=name).save() self.assertIsNotNone(f.id) self.assertIsNotNone(f.changekey) new_name = get_random_string(16) f.name = new_name f.save() f.refresh() self.assertEqual(f.name, new_name) with self.assertRaises(ErrorObjectTypeChanged): # FolderClass may not be changed f.folder_class = get_random_string(16) f.save(update_fields=["folder_class"]) # Create a subfolder Messages(parent=f, name=get_random_string(16)).save() self.assertEqual(len(list(f.children)), 1) f.empty() self.assertEqual(len(list(f.children)), 1) f.empty(delete_sub_folders=True) self.assertEqual(len(list(f.children)), 0) # Create a subfolder again, and delete it by wiping Messages(parent=f, name=get_random_string(16)).save() self.assertEqual(len(list(f.children)), 1) f.wipe() self.assertEqual(len(list(f.children)), 0) item_id, changekey = f.id, f.changekey f.delete() with self.assertRaises(ValueError): # No longer has an ID f.refresh() with self.assertRaises(ErrorItemNotFound): f.id, f.changekey = item_id, changekey # Invalid ID f.save() with self.assertRaises(ErrorDeleteDistinguishedFolder): self.account.inbox.delete()
def test_user_configuration(self): """Test that we can do CRUD operations on user configuration data.""" # Create a test folder that we delete afterwards f = Messages(parent=self.account.inbox, name=get_random_string(16)).save() # The name must be fewer than 237 characters, can contain only the characters "A-Z", "a-z", "0-9", and ".", # and must not start with "IPM.Configuration" name = get_random_string(16, spaces=False, special=False) # Bad property with self.assertRaises(ValueError) as e: f.get_user_configuration(name=name, properties="XXX") self.assertEqual( e.exception.args[0], "'properties' 'XXX' must be one of ['All', 'BinaryData', 'Dictionary', 'Id', 'XmlData']", ) # Should not exist yet with self.assertRaises(ErrorItemNotFound): f.get_user_configuration(name=name) # Create a config dictionary = { get_random_bool(): get_random_str_tuple(10, 2), get_random_int(): get_random_bool(), get_random_byte(): get_random_int(), get_random_bytes(16): get_random_byte(), get_random_string(8): get_random_bytes(16), get_random_datetime(tz=self.account.default_timezone): get_random_string(8), get_random_str_tuple(4, 4): get_random_datetime(tz=self.account.default_timezone), } xml_data = f"<foo>{get_random_string(16)}</foo>".encode("utf-8") binary_data = get_random_bytes(100) f.create_user_configuration(name=name, dictionary=dictionary, xml_data=xml_data, binary_data=binary_data) # Fetch and compare values config = f.get_user_configuration(name=name) self.assertEqual(config.dictionary, dictionary) self.assertEqual(config.xml_data, xml_data) self.assertEqual(config.binary_data, binary_data) # Cannot create one more with the same name with self.assertRaises(ErrorItemSave): f.create_user_configuration(name=name) # Does not exist on a different folder with self.assertRaises(ErrorItemNotFound): self.account.inbox.get_user_configuration(name=name) # Update the config f.update_user_configuration( name=name, dictionary={"bar": "foo", 456: "a", "b": True}, xml_data=b"<foo>baz</foo>", binary_data=b"YYY" ) # Fetch again and compare values config = f.get_user_configuration(name=name) self.assertEqual(config.dictionary, {"bar": "foo", 456: "a", "b": True}) self.assertEqual(config.xml_data, b"<foo>baz</foo>") self.assertEqual(config.binary_data, b"YYY") # Fetch again but only one property type config = f.get_user_configuration(name=name, properties="XmlData") self.assertEqual(config.dictionary, None) self.assertEqual(config.xml_data, b"<foo>baz</foo>") self.assertEqual(config.binary_data, None) # Delete the config f.delete_user_configuration(name=name) # We already deleted this config with self.assertRaises(ErrorItemNotFound): f.get_user_configuration(name=name) f.delete()