Ejemplo n.º 1
0
    def test_extended_properties(self):
        # Test extended properties on folders and folder roots. This extended prop gets the size (in bytes) of a folder
        class FolderSize(ExtendedProperty):
            property_tag = 0x0e08
            property_type = 'Integer'

        try:
            Folder.register('size', FolderSize)
            self.account.inbox.refresh()
            self.assertGreater(self.account.inbox.size, 0)
        finally:
            Folder.deregister('size')

        try:
            RootOfHierarchy.register('size', FolderSize)
            self.account.root.refresh()
            self.assertGreater(self.account.root.size, 0)
        finally:
            RootOfHierarchy.deregister('size')

        # Register is only allowed on Folder and RootOfHierarchy classes
        with self.assertRaises(TypeError):
            self.account.calendar.register(FolderSize)
        with self.assertRaises(TypeError):
            self.account.root.register(FolderSize)
Ejemplo n.º 2
0
    def test_generic_folder(self):
        f = Folder(parent=self.account.inbox, name=get_random_string(16))
        f.save()
        f.name = get_random_string(16)
        f.save()
        f.delete()

        self.assertEqual(Folder().has_distinguished_name, None)
        self.assertEqual(Inbox(name="XXX").has_distinguished_name, False)
        self.assertEqual(Inbox(name="Inbox").has_distinguished_name, True)
        self.assertEqual(Inbox(is_distinguished=False).is_deletable, True)
        self.assertEqual(Inbox(is_distinguished=True).is_deletable, False)
Ejemplo n.º 3
0
    def test_extended_properties(self):
        # Extended properties also work with folders. Here's an example of getting the size (in bytes) of a folder:
        class FolderSize(ExtendedProperty):
            property_tag = 0x0e08
            property_type = 'Integer'

        try:
            Folder.register('size', FolderSize)
            self.account.inbox.refresh()
            self.assertGreater(self.account.inbox.size, 0)
        finally:
            Folder.deregister('size')
Ejemplo n.º 4
0
 def test_folder_failure(self):
     # Folders must have an ID
     with self.assertRaises(ValueError):
         self.account.root.get_folder(Folder())
     with self.assertRaises(ValueError):
         self.account.root.add_folder(Folder())
     with self.assertRaises(ValueError):
         self.account.root.update_folder(Folder())
     with self.assertRaises(ValueError):
         self.account.root.remove_folder(Folder())
     # Removing a non-existent folder is allowed
     self.account.root.remove_folder(Folder(id='XXX'))
     # Must be called on a distinguished folder class
     with self.assertRaises(ValueError):
         RootOfHierarchy.get_distinguished(self.account)
     with self.assertRaises(ValueError):
         self.account.root.get_default_folder(Folder)
Ejemplo n.º 5
0
    def test_folder_query_set(self):
        # Create a folder hierarchy and test a folder queryset
        #
        # -f0
        #  - f1
        #  - f2
        #    - f21
        #    - f22
        f0 = Folder(parent=self.account.inbox, name=get_random_string(16)).save()
        f1 = Folder(parent=f0, name=get_random_string(16)).save()
        f2 = Folder(parent=f0, name=get_random_string(16)).save()
        f21 = Folder(parent=f2, name=get_random_string(16)).save()
        f22 = Folder(parent=f2, name=get_random_string(16)).save()
        folder_qs = SingleFolderQuerySet(account=self.account, folder=f0)
        try:
            # Test all()
            self.assertSetEqual({f.name for f in folder_qs.all()}, {f.name for f in (f1, f2, f21, f22)})

            # Test only()
            self.assertSetEqual({f.name for f in folder_qs.only("name").all()}, {f.name for f in (f1, f2, f21, f22)})
            self.assertSetEqual({f.child_folder_count for f in folder_qs.only("name").all()}, {None})
            # Test depth()
            self.assertSetEqual({f.name for f in folder_qs.depth(SHALLOW).all()}, {f.name for f in (f1, f2)})

            # Test filter()
            self.assertSetEqual({f.name for f in folder_qs.filter(name=f1.name)}, {f.name for f in (f1,)})
            self.assertSetEqual(
                {f.name for f in folder_qs.filter(name__in=[f1.name, f2.name])}, {f.name for f in (f1, f2)}
            )

            # Test get()
            self.assertEqual(folder_qs.get(id=f2.id).name, f2.name)
            self.assertEqual(folder_qs.get(id=f2.id, changekey=f2.changekey).name, f2.name)
            self.assertEqual(folder_qs.get(name=f2.name).child_folder_count, 2)
            self.assertEqual(folder_qs.filter(name=f2.name).get().child_folder_count, 2)
            self.assertEqual(folder_qs.only("name").get(name=f2.name).name, f2.name)
            self.assertEqual(folder_qs.only("name").get(name=f2.name).child_folder_count, None)
            with self.assertRaises(DoesNotExist):
                folder_qs.get(name=get_random_string(16))
            with self.assertRaises(MultipleObjectsReturned):
                folder_qs.get()
        finally:
            f0.wipe()
            f0.delete()
Ejemplo n.º 6
0
    def test_parent(self):
        self.assertEqual(self.account.calendar.parent.name, "Top of Information Store")
        self.assertEqual(self.account.calendar.parent.parent.name, "root")
        # Setters
        parent = self.account.calendar.parent
        with self.assertRaises(TypeError) as e:
            self.account.calendar.parent = "XXX"
        self.assertEqual(
            e.exception.args[0], "'value' 'XXX' must be of type <class 'exchangelib.folders.base.BaseFolder'>"
        )
        self.account.calendar.parent = None
        self.account.calendar.parent = parent

        # Test self-referencing folder
        self.assertIsNone(Folder(id=self.account.inbox.id, parent=self.account.inbox).parent)
Ejemplo n.º 7
0
    def test_invalid_bulk_args(self):
        # Test bulk_create
        with self.assertRaises(ValueError):
            # Folder must belong to account
            self.account.bulk_create(folder=Folder(root=None), items=[1])
        with self.assertRaises(AttributeError):
            # Must have folder on save
            self.account.bulk_create(folder=None, items=[1], message_disposition=SAVE_ONLY)
        # Test that we can send_and_save with a default folder
        self.account.bulk_create(folder=None, items=[], message_disposition=SEND_AND_SAVE_COPY)
        with self.assertRaises(AttributeError):
            # Must not have folder on send-only
            self.account.bulk_create(folder=self.test_folder, items=[1], message_disposition=SEND_ONLY)

        # Test bulk_update
        with self.assertRaises(ValueError):
            # Cannot update in send-only mode
            self.account.bulk_update(items=[1], message_disposition=SEND_ONLY)
Ejemplo n.º 8
0
    def test_refresh(self):
        # Test that we can refresh folders
        for f in self.account.root.walk():
            with self.subTest(f=f):
                if isinstance(f, System):
                    # Can't refresh the 'System' folder for some reason
                    continue
                old_values = {}
                for field in f.FIELDS:
                    old_values[field.name] = getattr(f, field.name)
                    if field.name in ('account', 'id', 'changekey', 'parent_folder_id'):
                        # These are needed for a successful refresh()
                        continue
                    if field.is_read_only:
                        continue
                    setattr(f, field.name, self.random_val(field))
                f.refresh()
                for field in f.FIELDS:
                    if field.name == 'changekey':
                        # folders may change while we're testing
                        continue
                    if field.is_read_only:
                        # count values may change during the test
                        continue
                    self.assertEqual(getattr(f, field.name), old_values[field.name], (f, field.name))

        # Test refresh of root
        all_folders = sorted(f.name for f in self.account.root.walk())
        self.account.root.refresh()
        self.assertIsNone(self.account.root._subfolders)
        self.assertEqual(
            sorted(f.name for f in self.account.root.walk()),
            all_folders
        )

        folder = Folder()
        with self.assertRaises(ValueError):
            folder.refresh()  # Must have root folder
        folder.root = self.account.root
        with self.assertRaises(ValueError):
            folder.refresh()  # Must have an id
Ejemplo n.º 9
0
 def test_generic_folder(self):
     f = Folder(parent=self.account.inbox, name=get_random_string(16))
     f.save()
     f.name = get_random_string(16)
     f.save()
     f.delete()
Ejemplo n.º 10
0
    def test_counts(self):
        # Test count values on a folder
        f = Folder(parent=self.account.inbox, name=get_random_string(16)).save()
        f.refresh()

        self.assertEqual(f.total_count, 0)
        self.assertEqual(f.unread_count, 0)
        self.assertEqual(f.child_folder_count, 0)
        # Create some items
        items = []
        for i in range(3):
            subject = 'Test Subject %s' % i
            item = Message(account=self.account, folder=f, is_read=False, subject=subject, categories=self.categories)
            item.save()
            items.append(item)
        # Refresh values and see that total_count and unread_count changes
        f.refresh()
        self.assertEqual(f.total_count, 3)
        self.assertEqual(f.unread_count, 3)
        self.assertEqual(f.child_folder_count, 0)
        for i in items:
            i.is_read = True
            i.save()
        # Refresh values and see that unread_count changes
        f.refresh()
        self.assertEqual(f.total_count, 3)
        self.assertEqual(f.unread_count, 0)
        self.assertEqual(f.child_folder_count, 0)
        self.bulk_delete(items)
        # Refresh values and see that total_count changes
        f.refresh()
        self.assertEqual(f.total_count, 0)
        self.assertEqual(f.unread_count, 0)
        self.assertEqual(f.child_folder_count, 0)
        # Create some subfolders
        subfolders = []
        for i in range(3):
            subfolders.append(Folder(parent=f, name=get_random_string(16)).save())
        # Refresh values and see that child_folder_count changes
        f.refresh()
        self.assertEqual(f.total_count, 0)
        self.assertEqual(f.unread_count, 0)
        self.assertEqual(f.child_folder_count, 3)
        for sub_f in subfolders:
            sub_f.delete()
        # Refresh values and see that child_folder_count changes
        f.refresh()
        self.assertEqual(f.total_count, 0)
        self.assertEqual(f.unread_count, 0)
        self.assertEqual(f.child_folder_count, 0)
        f.delete()
Ejemplo n.º 11
0
    def test_move(self):
        f1 = Folder(parent=self.account.inbox,
                    name=get_random_string(16)).save()
        f2 = Folder(parent=self.account.inbox,
                    name=get_random_string(16)).save()

        f1_id, f1_changekey, f1_parent = f1.id, f1.changekey, f1.parent
        f1.move(f2)
        self.assertEqual(f1.id, f1_id)
        self.assertNotEqual(f1.changekey, f1_changekey)
        self.assertEqual(f1.parent, f2)
        self.assertNotEqual(f1.changekey, f1_parent)

        f1_id, f1_changekey, f1_parent = f1.id, f1.changekey, f1.parent
        f1.refresh()
        self.assertEqual(f1.id, f1_id)
        self.assertEqual(f1.parent, f2)
        self.assertNotEqual(f1.changekey, f1_parent)

        f1.delete()
        f2.delete()
Ejemplo n.º 12
0
    def test_move(self):
        f1 = Folder(parent=self.account.inbox, name=get_random_string(16)).save()
        f2 = Folder(parent=self.account.inbox, name=get_random_string(16)).save()

        f1_id, f1_changekey, f1_parent = f1.id, f1.changekey, f1.parent
        with self.assertRaises(TypeError) as e:
            f1.move(to_folder="XXX")  # Must be folder instance
        self.assertEqual(
            e.exception.args[0],
            "'to_folder' 'XXX' must be of type (<class 'exchangelib.folders.base.BaseFolder'>, "
            "<class 'exchangelib.properties.FolderId'>)",
        )
        f1.move(f2)
        self.assertEqual(f1.id, f1_id)
        self.assertNotEqual(f1.changekey, f1_changekey)
        self.assertEqual(f1.parent, f2)
        self.assertNotEqual(f1.changekey, f1_parent)

        f1_id, f1_changekey, f1_parent = f1.id, f1.changekey, f1.parent
        f1.refresh()
        self.assertEqual(f1.id, f1_id)
        self.assertEqual(f1.parent, f2)
        self.assertNotEqual(f1.changekey, f1_parent)

        f1.delete()
        f2.delete()
Ejemplo n.º 13
0
    def test_refresh(self):
        # Test that we can refresh folders
        f = Folder(parent=self.account.inbox, name=get_random_string(16)).save()
        f.refresh()
        old_values = {}
        for field in f.FIELDS:
            old_values[field.name] = getattr(f, field.name)
            if field.name in ("account", "id", "changekey", "parent_folder_id"):
                # These are needed for a successful refresh()
                continue
            if field.is_read_only:
                continue
            setattr(f, field.name, self.random_val(field))
        f.refresh()
        for field in f.FIELDS:
            if field.name == "changekey":
                # folders may change while we're testing
                continue
            if field.is_read_only:
                # count values may change during the test
                continue
            self.assertEqual(getattr(f, field.name), old_values[field.name], (f, field.name))

        # Test refresh of root
        orig_name = self.account.root.name
        self.account.root.name = "xxx"
        self.account.root.refresh()
        self.assertEqual(self.account.root.name, orig_name)

        folder = Folder()
        with self.assertRaises(ValueError):
            folder.refresh()  # Must have root folder
        folder.root = self.account.root
        with self.assertRaises(ValueError):
            folder.refresh()  # Must have an id
Ejemplo n.º 14
0
    def test_folder_failure(self):
        # Folders must have an ID
        with self.assertRaises(ValueError):
            self.account.root.get_folder(Folder())
        with self.assertRaises(ValueError):
            self.account.root.add_folder(Folder())
        with self.assertRaises(ValueError):
            self.account.root.update_folder(Folder())
        with self.assertRaises(ValueError):
            self.account.root.remove_folder(Folder())
        # Removing a non-existent folder is allowed
        self.account.root.remove_folder(Folder(id="XXX"))
        # Must be called on a distinguished folder class
        with self.assertRaises(ValueError):
            RootOfHierarchy.get_distinguished(self.account)
        with self.assertRaises(ValueError):
            self.account.root.get_default_folder(Folder)

        with self.assertRaises(ValueError) as e:
            Folder(root=self.account.public_folders_root, parent=self.account.inbox)
        self.assertEqual(e.exception.args[0], "'parent.root' must match 'root'")
        with self.assertRaises(ValueError) as e:
            Folder(parent=self.account.inbox, parent_folder_id="XXX")
        self.assertEqual(e.exception.args[0], "'parent_folder_id' must match 'parent' ID")
        with self.assertRaises(TypeError) as e:
            Folder(root="XXX").clean()
        self.assertEqual(
            e.exception.args[0], "'root' 'XXX' must be of type <class 'exchangelib.folders.roots.RootOfHierarchy'>"
        )
        with self.assertRaises(ValueError) as e:
            Folder().save(update_fields=["name"])
        self.assertEqual(e.exception.args[0], "'update_fields' is only valid for updates")
        with self.assertRaises(ValueError) as e:
            Messages().validate_item_field("XXX", version=self.account.version)
        self.assertIn("'XXX' is not a valid field on", e.exception.args[0])
        with self.assertRaises(ValueError) as e:
            Folder.item_model_from_tag("XXX")
        self.assertEqual(e.exception.args[0], "Item type XXX was unexpected in a Folder folder")