Esempio n. 1
0
    def ztest_restore_10_emails(self):
        """
           Restore 10 emails
        """
        gsource = imap_utils.GIMAPFetcher('imap.gmail.com', 993, self.login,
                                          self.passwd)
        gdestination = imap_utils.GIMAPFetcher('imap.gmail.com', 993, self.gmvault_login, self.gmvault_passwd, \
                                             readonly_folder = False)

        gsource.connect()
        gdestination.connect()

        criteria = ['Before 1-Oct-2008']
        #criteria = ['ALL']
        ids = gsource.search(criteria)

        #get 30 emails
        for index in range(9, 20):

            print("email nb %d\n" % (index))

            the_id = ids[index]

            source_email = gsource.fetch(the_id, gsource.GET_ALL_INFO)

            existing_labels = source_email[the_id][gsource.GMAIL_LABELS]

            # get labels
            test_labels = []
            for elem in existing_labels:
                test_labels.append(elem)

            dest_id = gdestination.push_email(source_email[the_id][gsource.EMAIL_BODY], \
                                               source_email[the_id][gsource.IMAP_FLAGS] , \
                                               source_email[the_id][gsource.IMAP_INTERNALDATE], test_labels)

            #retrieve email from destination email account
            dest_email = gdestination.fetch(dest_id, gsource.GET_ALL_INFO)

            #check that it has the same
            # do the checkings
            self.assertEquals(dest_email[dest_id][gsource.IMAP_FLAGS],
                              source_email[the_id][gsource.IMAP_FLAGS])
            self.assertEquals(dest_email[dest_id][gsource.EMAIL_BODY],
                              source_email[the_id][gsource.EMAIL_BODY])

            dest_labels = []
            for elem in dest_email[dest_id][gsource.GMAIL_LABELS]:
                if not elem == '\\Important':
                    dest_labels.append(elem)

            src_labels = []
            for elem in source_email[the_id][gsource.GMAIL_LABELS]:
                if not elem == '\\Important':
                    src_labels.append(elem)

            self.assertEquals(dest_labels, src_labels)
Esempio n. 2
0
    def ztest_restore_one_email(self):
        """
           get one email from one account and restore it
        """
        gsource = imap_utils.GIMAPFetcher('imap.gmail.com', 993, self.login,
                                          self.passwd)
        gdestination = imap_utils.GIMAPFetcher('imap.gmail.com',
                                               993,
                                               self.gmvault_login,
                                               self.gmvault_passwd,
                                               readonly_folder=False)

        gsource.connect()
        gdestination.connect()

        criteria = ['Before 1-Oct-2006']
        #criteria = ['ALL']
        ids = gsource.search(criteria)

        the_id = ids[0]

        source_email = gsource.fetch(the_id, gsource.GET_ALL_INFO)

        existing_labels = source_email[the_id][gsource.GMAIL_LABELS]

        test_labels = []
        for elem in existing_labels:
            test_labels.append(elem)

        #source_email[the_id][gsource.IMAP_INTERNALDATE] = source_email[the_id][gsource.IMAP_INTERNALDATE].replace(tzinfo= gmvault_utils.UTC_TZ)

        dest_id = gdestination.push_email(source_email[the_id][gsource.EMAIL_BODY], \
                                           source_email[the_id][gsource.IMAP_FLAGS] , \
                                           source_email[the_id][gsource.IMAP_INTERNALDATE], test_labels)

        dest_email = gdestination.fetch(dest_id, gsource.GET_ALL_INFO)

        # do the checkings
        self.assertEquals(dest_email[dest_id][gsource.IMAP_FLAGS],
                          source_email[the_id][gsource.IMAP_FLAGS])
        self.assertEquals(dest_email[dest_id][gsource.EMAIL_BODY],
                          source_email[the_id][gsource.EMAIL_BODY])
        self.assertEquals(dest_email[dest_id][gsource.GMAIL_LABELS],
                          source_email[the_id][gsource.GMAIL_LABELS])

        #should be ok to be checked
        self.assertEquals(dest_email[dest_id][gsource.IMAP_INTERNALDATE],
                          source_email[the_id][gsource.IMAP_INTERNALDATE])
Esempio n. 3
0
    def ztest_create_gmail_labels(self):
        """
           validate the label creation at the imap fetcher level
        """
        gimap = imap_utils.GIMAPFetcher('imap.gmail.com', 993,
                                        self.gmvault_login,
                                        self.gmvault_passwd)

        gimap.connect()

        labels_to_create = ['a', 'b/c', 'e/f/g', 'b/c/d']

        gimap.create_gmail_labels(labels_to_create)

        #get existing directories (or label parts)
        folders = [directory for (_, _, directory) in gimap.get_all_folders()]

        for label in labels_to_create:
            self.assertTrue((label in folders))

        gimap.delete_gmail_labels(labels_to_create)

        #get existing directories (or label parts)
        folders = [directory for (_, _, directory) in gimap.get_all_folders()]

        for label in labels_to_create:  #check that they have been deleted
            self.assertFalse((label in folders))
Esempio n. 4
0
    def ztest_retrieve_gmail_ids(self):
        """
           Get all uid before Sep 2004
           Retrieve all GMAIL IDs 
        """
        gimap = imap_utils.GIMAPFetcher('imap.gmail.com', 993, self.login,
                                        self.passwd)

        gimap.connect()

        criteria = ['Before 1-Oct-2004']
        #criteria = ['ALL']
        ids = gimap.search(criteria)

        res = gimap.fetch(ids, [gimap.GMAIL_ID])

        self.assertEquals(
            res, {
                27362: {
                    'X-GM-MSGID': 1147537963432096749L,
                    'SEQ': 14535
                },
                27363: {
                    'X-GM-MSGID': 1147537994018957026L,
                    'SEQ': 14536
                }
            })
Esempio n. 5
0
    def zztest_create_gmail_labels_android(self):
        """
           Handle labels with [Imap]
        """
        gs_credential = {'type': 'passwd', 'value': self.gmvault_passwd}
        gimap = imap_utils.GIMAPFetcher('imap.gmail.com', 993,
                                        self.gmvault_login, gs_credential)

        gimap.connect()

        print("\nCreate labels.\n")

        labels_to_create = [u'[IMAP]/Trash', u'[IMAP]/Sent']

        existing_folders = set()

        existing_folders = gimap.create_gmail_labels(labels_to_create,
                                                     existing_folders)

        #get existing directories (or label parts)
        #print("xlist folders = %s\n" % (gimap.get_all_folders()) )

        #folders = [ directory.lower() for (flags, delim, directory) in gimap.server.list_folders() ]
        folders = [directory.lower() for directory in existing_folders]

        print("folders = %s\n" % (folders))
        for label in labels_to_create:
            self.assertTrue((label.lower() in folders))

        # second creation
        labels_to_create = [
            u'[RETEST]', u'[RETEST]/test', u'[RETEST]/Trash', u'[IMAP]/Trash',
            u'[IMAP]/Draft', u'[IMAP]/Sent', u'[IMAP]'
        ]

        existing_folders = gimap.create_gmail_labels(labels_to_create,
                                                     existing_folders)

        folders = [directory.lower() for directory in existing_folders]

        print("folders = %s" % (folders))
        for label in labels_to_create:
            self.assertTrue((label.lower() in folders))

        #it isn't possible to delete the [IMAP]/Sent, [IMAP]/Draft [IMAP]/Trash labels
        # I give up and do not delete them in the test
        labels_to_delete = [u'[RETEST]', u'[RETEST]/test', u'[RETEST]/Trash']

        print("Delete labels\n")

        # delete them
        gimap.delete_gmail_labels(labels_to_delete)

        #get existing directories (or label parts)
        folders = [
            directory.lower() for (_, _, directory) in gimap.get_all_folders()
        ]

        for label in labels_to_delete:  #check that they have been deleted
            self.assertFalse((label.lower() in folders))
Esempio n. 6
0
    def ztest_retrieve_all_params(self):
        """
           Get all params for a uid
           Retrieve all parts for one email
        """
        gimap = imap_utils.GIMAPFetcher('imap.gmail.com', 993, self.login,
                                        self.passwd)

        gimap.connect()

        criteria = ['Before 1-Oct-2004']
        #criteria = ['ALL']
        ids = gimap.search(criteria)

        self.assertEquals(len(ids), 2)

        res = gimap.fetch(ids[0], [
            gimap.GMAIL_ID, gimap.EMAIL_BODY, gimap.GMAIL_THREAD_ID,
            gimap.GMAIL_LABELS
        ])

        self.assertEquals(res[ids[0]][gimap.GMAIL_ID], 1147537963432096749L)

        self.assertEquals(res[ids[0]][gimap.EMAIL_BODY], \
                          'Message-ID: <*****@*****.**>\r\nDate: Sun, 5 Sep 2004 09:44:43 +0000 (GMT)\r\nFrom: [email protected]\r\nReply-To: [email protected]\r\nTo: [email protected]\r\nSubject: Fwd: [Flickr] Guillaume Aubert wants you to see their photos\r\nMime-Version: 1.0\r\nContent-Type: text/plain; charset=us-ascii\r\nContent-Transfer-Encoding: 7bit\r\nX-Mailer: jwma\r\nStatus: RO\r\nX-Status: \r\nX-Keywords:                 \r\nX-UID: 1\r\n\r\n\r\n') #pylint:disable-msg=C0301
Esempio n. 7
0
    def ztest_gmvault_check_gmailness(self):
        """
           Test simple retrieval
        """
        gimap = imap_utils.GIMAPFetcher('imap.gmail.com', 993, self.login,
                                        self.passwd)

        gimap.connect()

        self.assertEquals(True, gimap.check_gmailness())
Esempio n. 8
0
    def ztest_gmvault_simple_search(self):
        """
           search all emails before 01.01.2005
        """
        gimap = imap_utils.GIMAPFetcher('imap.gmail.com', 993, self.login,
                                        self.passwd)

        gimap.connect()

        criteria = ['Before 1-Jan-2011']
        ids = gimap.search(criteria)

        self.assertEquals(len(ids), 33577)
Esempio n. 9
0
    def zztest_create_gmail_labels_upper_case(self):
        """
           validate the label creation at the imap fetcher level.
           Use upper case
        """
        gs_credential = {'type': 'passwd', 'value': self.gmvault_passwd}
        gimap = imap_utils.GIMAPFetcher('imap.gmail.com', 993,
                                        self.gmvault_login, gs_credential)

        gimap.connect()

        print("\nCreate labels.\n")

        labels_to_create = ['0', 'A', 'a', 'B/C', 'B/C/d', 'B/C/d/e', 'c/d']

        existing_folders = set()

        existing_folders = gimap.create_gmail_labels(labels_to_create,
                                                     existing_folders)

        print("folders = %s\n" % (existing_folders))
        for label in labels_to_create:
            self.assertTrue((label.lower() in existing_folders))

        labels_to_create = [
            '0', 'A', 'a', 'B/C', 'B/C/d', 'B/C/d/e', 'c/d', 'diablo3',
            'blizzard', 'blizzard/diablo'
        ]
        #labels_to_create = ['B/c', u'[Imap]/Trash', u'[Imap]/Sent', 'a', 'A', 'e/f/g', 'b/c/d', ]

        existing_folders = set()

        existing_folders = gimap.create_gmail_labels(labels_to_create,
                                                     existing_folders)

        print("folders = %s\n" % (existing_folders))
        for label in labels_to_create:
            self.assertTrue((label.lower() in existing_folders))

        print("Delete labels\n")

        gimap.delete_gmail_labels(labels_to_create)

        #get existing directories (or label parts)
        folders = [
            directory.lower() for (_, _, directory) in gimap.get_all_folders()
        ]

        for label in labels_to_create:  #check that they have been deleted
            self.assertFalse((label.lower() in folders))
Esempio n. 10
0
    def ztest_gmvault_get_capabilities(self):
        """
           Test simple retrieval
        """
        gimap = imap_utils.GIMAPFetcher('imap.gmail.com', 993, self.login,
                                        self.passwd)

        gimap.connect()

        self.assertEquals(('IMAP4REV1', 'UNSELECT', \
                           'IDLE', 'NAMESPACE', \
                           'QUOTA', 'ID', 'XLIST', \
                           'CHILDREN', 'X-GM-EXT-1', \
                           'XYZZY', 'SASL-IR', 'AUTH=XOAUTH') , gimap.get_capabilities())
Esempio n. 11
0
    def ztest_gmvault_connect_error(self):
        """
           Test connect error (connect to a wrong port). Too long to check
        """

        gimap = imap_utils.GIMAPFetcher('imap.gmafil.com', 80, "badlogin",
                                        "badpassword")

        try:
            gimap.connect()
        except ssl.SSLError, err:

            msg = str(err)

            if not msg.startswith('[Errno 8] _ssl.c:') or not msg.endswith(
                    'EOF occurred in violation of protocol'):
                self.fail('received %s. Bad error message' % (msg))
Esempio n. 12
0
def clean_mailbox(login, credential):
    """
       Delete all emails, destroy all labels
    """
    gimap = imap_utils.GIMAPFetcher('imap.gmail.com',
                                    993,
                                    login,
                                    credential,
                                    readonly_folder=False)

    print("login = %s" % (login))

    assert_login_is_protected(login)

    gimap.connect()

    gimap.erase_mailbox()
Esempio n. 13
0
    def ztest_gmvault_compression(self):
        """
           Test simple retrieval
        """
        gimap = imap_utils.GIMAPFetcher('imap.gmail.com', 993, self.login,
                                        self.passwd)

        gimap.connect()

        gimap.enable_compression()

        self.assertEquals(True, gimap.check_gmailness())

        criteria = ['Before 1-Jan-2011']
        ids = gimap.search(criteria)

        self.assertEquals(len(ids), 33577)
Esempio n. 14
0
    def ztest_fix_bug_search_broken_gm_id_and_quarantine(self):
        """
           Search with a gm_id and quarantine it
        """
        db_dir = '/tmp/gmail_bk'

        #clean db dir
        delete_db_dir(db_dir)

        credential = {'type': 'passwd', 'value': self.passwd}
        gs_credential = {'type': 'passwd', 'value': self.gmvault_passwd}
        gstorer = gmvault.GmailStorer(db_dir)
        gimap = imap_utils.GIMAPFetcher('imap.gmail.com', 993, self.login,
                                        credential)

        gimap.connect()

        criteria = {
            'type': 'imap',
            'req': ['X-GM-MSGID 1254269417797093924']
        }  #broken one
        #criteria = ['X-GM-MSGID 1254267782370534098']
        #criteria = ['ALL']
        ids = gimap.search(criteria)

        for the_id in ids:
            res = gimap.fetch(the_id, gimap.GET_ALL_INFO)

            gm_id = gstorer.bury_email(res[the_id], compress=True)

            syncer = gmvault.GMVaulter(db_dir, 'imap.gmail.com', 993,
                                       self.gmvault_login, gs_credential)

            syncer.restore()

        #check that the file has been quarantine
        quarantine_dir = '%s/quarantine' % (db_dir)

        self.assertTrue(
            os.path.exists('%s/1254269417797093924.eml.gz' % (quarantine_dir)))
        self.assertTrue(
            os.path.exists('%s/1254269417797093924.meta' % (quarantine_dir)))
Esempio n. 15
0
    def test_check_flags(self):
        """
           Check flags 
        """
        credential = {'type': 'passwd', 'value': self.passwd}
        #print("credential %s\n" % (credential))
        gimap = imap_utils.GIMAPFetcher('imap.gmail.com', 993, self.login,
                                        credential)

        gimap.connect()

        imap_ids = [155182]
        gmail_id = 1405877259414135030

        imap_ids = [155070]

        #res = gimap.fetch(imap_ids, [gimap.GMAIL_ID, gimap.IMAP_FLAGS])
        res = gimap.fetch(imap_ids, gimap.GET_ALL_BUT_DATA)

        print(res)
Esempio n. 16
0
    def ztest_gmvault_store_gzip_email_and_read(
            self):  #pylint:disable-msg=C0103
        """
           Retrieve emails store them it on disk and read it
        """
        storage_dir = '/tmp/gmail_bk'
        gmvault_utils.delete_all_under(storage_dir)
        gimap = imap_utils.GIMAPFetcher('imap.gmail.com', 993, self.login,
                                        self.passwd)

        gstorer = gmvault.GmailStorer(storage_dir)

        gimap.connect()

        criteria = ['Before 1-Oct-2006']
        #criteria = ['ALL']
        ids = gimap.search(criteria)

        #get 30 emails
        for index in range(9, 20):

            print("retrieve email index %d\n" % (index))
            the_id = ids[index]

            res = gimap.fetch(the_id, gimap.GET_ALL_INFO)

            gm_id = gstorer.bury_email(res[the_id], compress=True)

            print("restore email index %d\n" % (index))
            metadata, data = gstorer.unbury_email(gm_id)

            self.assertEquals(res[the_id][gimap.GMAIL_ID], metadata['gm_id'])
            self.assertEquals(res[the_id][gimap.EMAIL_BODY], data)
            self.assertEquals(res[the_id][gimap.GMAIL_THREAD_ID],
                              metadata['thread_ids'])

            labels = []
            for label in res[the_id][gimap.GMAIL_LABELS]:
                labels.append(label)

            self.assertEquals(labels, metadata['labels'])
Esempio n. 17
0
    def ztest_gmvault_compress_retrieve_email_store_and_read(
            self):  #pylint:disable-msg=C0103
        """
           Activate compression and retrieve an email store it on disk and read it
        """
        storage_dir = '/tmp/gmail_bk'
        gmvault_utils.delete_all_under(storage_dir)

        gimap = imap_utils.GIMAPFetcher('imap.gmail.com', 993, self.login,
                                        self.passwd)

        gstorer = gmvault.GmailStorer(storage_dir)

        gimap.connect()

        gimap.enable_compression()

        criteria = ['Before 1-Oct-2006']
        #criteria = ['ALL']
        ids = gimap.search(criteria)

        the_id = ids[124]

        res = gimap.fetch(the_id, gimap.GET_ALL_INFO)

        gm_id = gstorer.bury_email(res[the_id])

        metadata, data = gstorer.unbury_email(gm_id)

        self.assertEquals(res[the_id][gimap.GMAIL_ID], metadata['gm_id'])
        self.assertEquals(res[the_id][gimap.EMAIL_BODY], data)
        self.assertEquals(res[the_id][gimap.GMAIL_THREAD_ID],
                          metadata['thread_ids'])

        labels = []
        for label in res[the_id][gimap.GMAIL_LABELS]:
            labels.append(label)

        self.assertEquals(labels, metadata['labels'])