Exemplo n.º 1
0
class TestMailbox(unittest.TestCase):

    def setUp(self):
        self.mbt = MailboxTool()
        self.log = LogCapture()

    def zztest_send_get_message(self):
        site, from_, to, msg, thread_id, msg_id, _, subject, payload, now = get_data()
        self.mbt.send_message(site, from_, to, msg)

        raw_msg, msg = self.mbt.get_message(site, from_, 'sent', thread_id, msg_id)

        self.assertEquals(raw_msg.message_id, msg_id)
        self.assertEquals(raw_msg.flags, [])

        self.assertEquals(msg['Message-Id'], msg_id)
        self.assertEquals(msg['Subject'], subject)
        self.assertEquals(msg['From'], from_)
        self.assertEquals(msg['To'], ', '.join(to))
        self.assertEquals(msg['Date'],  now)
        self.assertEquals(msg['X-oc-thread-id'], thread_id)
        
        transaction.commit()

    def zztest_delete_message(self):
        site, from_, to, msg, thread_id, msg_id, _, subject, payload, now = get_data()
        self.mbt.send_message(site, from_, to, msg)
        self.mbt.delete_message(site, from_, 'sent', thread_id, msg_id)

        try:
            self.mbt.get_message(site, from_, 'sent', thread_id, msg_id)
        except NoSuchThreadException:
            pass
        else:
            raise
        
        transaction.commit()

    def zztest_set_message_flags(self):
        site, from_, to, msg, thread_id, msg_id, flags, subject, payload, now = get_data()
        self.mbt.send_message(site, from_, to, msg)
        self.mbt.set_message_flags(site, from_, 'sent', thread_id, msg_id, flags)

        raw_msg, msg = self.mbt.get_message(site, from_, 'sent', thread_id, msg_id)
        self.assertEquals(raw_msg.flags, flags)

        raw_msg, msg = self.mbt.get_message(site, from_, 'sent', thread_id, msg_id)
        
        transaction.commit()

    def zztest_get_queues(self):
        site, from_, to, msg, thread_id, msg_id, _, subject, payload, now = get_data()
        self.mbt.send_message(site, from_, to, msg)
        queues = self.mbt.get_queues(site, from_, 'sent')

        for queue in queues:
            self.assertTrue(isinstance(queue, Queue))
            
        transaction.commit()
Exemplo n.º 2
0
def show_mbox(context, request):
    user = authenticated_userid(request)

    page = request.params.get('page', 1)
    mbox_type = _get_mbox_type(request)

    if not page:
        page = 1

    try:
        page = int(page)
    except ValueError:
        page = 1

    if page < 1:
        page = 1

    mbt = MailboxTool()
    mbox_queues = mbt.get_queues(context, user, mbox_type)
    alternate_mbox_type = 'sent' if mbox_type == 'inbox' else 'inbox'
    unread = mbt.get_unread(context, user, 'inbox')

    pagination = Pagination(page, PER_PAGE, len(mbox_queues))

    # We always count lists from 0, unlike what people do.
    actual_page = page - 1

    # First page ..
    if actual_page == 0:
        start_idx = 0
        end_idx = PER_PAGE

    # .. last page ..
    if not pagination.has_next:
        start_idx = actual_page * PER_PAGE
        end_idx = pagination.total_count

    # .. anything in between
    else:
        start_idx = actual_page * PER_PAGE
        end_idx = (actual_page + 1) * PER_PAGE

    queues = []
    for mbox_q in mbox_queues[start_idx:end_idx]:
        queue = {}
        queue['id'] = mbox_q.id
        queue['name'] = mbox_q.name
        messages = list(mbox_q)
        if mbt.has_queue(context, user, alternate_mbox_type, queue['id']):
            alternate_queue, _, _ = mbt.get_queue_data(context, user,
                    alternate_mbox_type, queue['id'])
            messages += list(alternate_queue)

        sender_names = get_sender_names(request, user, messages)

        queue['total_messages'] = len(messages)
        queue['thread_from'] = ', '.join(sender_names)
        last_message = messages[queue['total_messages'] - 1]
        queue['thread_last_date'] = _format_date(context, last_message['Date'])
        queue['unread'] = mbox_q.get_unread()


        queues.append(queue)

    return_data = {}
    return_data['profile'] = _get_profile_details(context, request, user)
    return_data['queues'] = queues
    return_data['api'] = request.api
    return_data['mbox_type'] = mbox_type
    return_data['unread'] = unread
    return_data['page'] = page

    return return_data