Esempio n. 1
0
def test_received_before_after(db, api_client, default_namespace):
    thread = add_fake_thread(db.session, default_namespace.id)
    message = add_fake_message(db.session, default_namespace.id, thread,
                               to_addr=[('Bob', '*****@*****.**')],
                               from_addr=[('Alice', '*****@*****.**')],
                               received_date=datetime.datetime(year=1999,
                                                               day=20,
                                                               month=03),
                               subject='some subject')

    thread2 = add_fake_thread(db.session, default_namespace.id)
    message2 = add_fake_message(db.session, default_namespace.id, thread,
                                to_addr=[('Bob', '*****@*****.**')],
                                from_addr=[('Alice', '*****@*****.**')],
                                received_date=datetime.datetime(year=2000,
                                                                day=20,
                                                                month=03),
                                subject='another subject')

    inbox = Category(namespace_id=message.namespace_id, name='inbox',
                     display_name='Inbox', type_='label')
    message.categories.add(inbox)
    thread.subject = message.subject

    message2.categories.add(inbox)
    thread2.subject = message2.subject

    db.session.commit()

    received_date = message.received_date
    t_epoch = dt_to_timestamp(datetime.datetime(year=1998, month=2, day=3))
    t_firstmsg = dt_to_timestamp(received_date)

    results = api_client.get_data('/messages?received_before={}'
                                  .format(t_epoch))
    assert len(results) == 0

    # received_before should be inclusive (i.e: match <=, not just <).
    results = api_client.get_data('/messages?received_before={}'
                                  .format(t_firstmsg))
    assert len(results) == 1

    t1 = dt_to_timestamp(received_date + datetime.timedelta(days=1))
    results = api_client.get_data('/messages?received_after={}'
                                  .format(t1))
    assert len(results) == 1

    results = api_client.get_data(
        '/messages?received_before={}&received_after={}'.format(t1,
                                                                t_firstmsg))
    assert len(results) == 0

    # bogus values
    results = api_client.get_data(
        '/messages?received_before={}&received_after={}'.format(t_epoch, t1))
    assert len(results) == 0
Esempio n. 2
0
def test_filtering(db, api_client):
    message = db.session.query(Message).filter_by(id=2).one()
    thread = message.thread
    t_start = dt_to_timestamp(thread.subjectdate)
    t_lastmsg = dt_to_timestamp(thread.recentdate)

    subject = message.subject
    to_addr = message.to_addr[0][1]
    from_addr = message.from_addr[0][1]

    received_date = message.received_date

    results = api_client.get_data('/threads?thread_id={}'
                                  .format('e6z26rjrxs2gu8at6gsa8svr1'))
    assert len(results) == 1

    results = api_client.get_data('/messages?thread_id={}'
                                  .format('e6z26rjrxs2gu8at6gsa8svr1'))
    assert len(results) == 1

    results = api_client.get_data('/threads?cc={}'
                                  .format(message.cc_addr))
    assert len(results) == 0

    results = api_client.get_data('/messages?cc={}'
                                  .format(message.cc_addr))
    assert len(results) == 0

    results = api_client.get_data('/threads?bcc={}'
                                  .format(message.bcc_addr))
    assert len(results) == 0

    results = api_client.get_data('/messages?bcc={}'
                                  .format(message.bcc_addr))
    assert len(results) == 0

    results = api_client.get_data('/threads?filename=test')
    assert len(results) == 0

    results = api_client.get_data('/messages?filename=test')
    assert len(results) == 0

    results = api_client.get_data('/threads?started_after={}'
                                  .format(t_start-1))
    assert len(results) == 1

    results = api_client.get_data('/messages?started_after={}'
                                  .format(t_start-1))
    assert len(results) == 1

    results = api_client.get_data('/messages?last_message_before={}&limit=1'
                                  .format(t_lastmsg+1))
    assert len(results) == 1

    results = api_client.get_data('/threads?last_message_before={}&limit=1'
                                  .format(t_lastmsg+1))
    assert len(results) == 1

    results = api_client.get_data('/threads?tag={}&limit=1'
                                  .format('inbox'))
    assert len(results) == 1

    results = api_client.get_data('/messages?tag={}&limit=1'
                                  .format('inbox'))
    assert len(results) == 1

    results = api_client.get_data('/messages?subject={}'.format(subject))
    assert len(results) == 1
    results = api_client.get_data('/threads?subject={}'.format(subject))
    assert len(results) == 1

    results = api_client.get_data('/messages?any_email={}'.
                                  format('*****@*****.**'))
    assert len(results) > 1
    results = api_client.get_data('/threads?any_email={}'.
                                  format('*****@*****.**'))
    assert len(results) > 1

    # Check that we canonicalize when searching.
    alternate_results = api_client.get_data('/threads?any_email={}'.
                                            format('*****@*****.**'))
    assert len(alternate_results) == len(results)

    results = api_client.get_data('/messages?from={}'.format(from_addr))
    assert len(results) == 1
    results = api_client.get_data('/threads?from={}'.format(from_addr))
    assert len(results) == 1

    early_time = received_date - datetime.timedelta(seconds=1)
    late_time = received_date + datetime.timedelta(seconds=1)
    early_ts = calendar.timegm(early_time.utctimetuple())
    late_ts = calendar.timegm(late_time.utctimetuple())

    results = api_client.get_data('/messages?subject={}&started_before={}'.
                                  format(subject, early_ts))
    assert len(results) == 0
    results = api_client.get_data('/threads?subject={}&started_before={}'.
                                  format(subject, early_ts))
    assert len(results) == 0

    results = api_client.get_data('/messages?subject={}&started_before={}'.
                                  format(subject, late_ts))
    assert len(results) == 1
    results = api_client.get_data('/threads?subject={}&started_before={}'.
                                  format(subject, late_ts))
    assert len(results) == 1

    results = api_client.get_data('/messages?subject={}&last_message_after={}'.
                                  format(subject, early_ts))
    assert len(results) == 1
    results = api_client.get_data('/threads?subject={}&last_message_after={}'.
                                  format(subject, early_ts))
    assert len(results) == 1

    results = api_client.get_data('/messages?subject={}&last_message_after={}'.
                                  format(subject, late_ts))
    assert len(results) == 0
    results = api_client.get_data('/threads?subject={}&last_message_after={}'.
                                  format(subject, late_ts))
    assert len(results) == 0

    results = api_client.get_data('/messages?subject={}&started_before={}'.
                                  format(subject, early_ts))
    assert len(results) == 0
    results = api_client.get_data('/threads?subject={}&started_before={}'.
                                  format(subject, early_ts))
    assert len(results) == 0

    results = api_client.get_data('/messages?subject={}&started_before={}'.
                                  format(subject, late_ts))
    assert len(results) == 1
    results = api_client.get_data('/threads?subject={}&started_before={}'.
                                  format(subject, late_ts))
    assert len(results) == 1

    results = api_client.get_data('/messages?from={}&to={}'.
                                  format(from_addr, to_addr))
    assert len(results) == 1

    results = api_client.get_data('/threads?from={}&to={}'.
                                  format(from_addr, to_addr))
    assert len(results) == 1

    results = api_client.get_data('/messages?to={}&limit={}&offset={}'.
                                  format('*****@*****.**', 2, 1))
    assert len(results) == 2

    results = api_client.get_data('/threads?to={}&limit={}'.
                                  format('*****@*****.**', 3))
    assert len(results) == 3

    results = api_client.get_data('/threads?view=count')

    assert results['count'] == 16

    results = api_client.get_data('/threads?view=ids&to={}&limit=3'.
                                  format('*****@*****.**', 3))

    assert len(results) == 3
    assert all(isinstance(r, basestring)
               for r in results), "Returns a list of string"
Esempio n. 3
0
def test_filtering(db, api_client, default_namespace):
    thread = add_fake_thread(db.session, default_namespace.id)
    message = add_fake_message(db.session,
                               default_namespace.id,
                               thread,
                               to_addr=[('Bob', '*****@*****.**')],
                               from_addr=[('Alice', '*****@*****.**')],
                               subject='some subject')
    message.categories.add(
        Category(namespace_id=message.namespace_id,
                 name='inbox',
                 display_name='Inbox',
                 type_='label'))
    thread.subject = message.subject
    db.session.commit()

    t_start = dt_to_timestamp(thread.subjectdate)
    t_lastmsg = dt_to_timestamp(thread.recentdate)
    subject = message.subject
    to_addr = message.to_addr[0][1]
    from_addr = message.from_addr[0][1]
    received_date = message.received_date
    unread = not message.is_read
    starred = message.is_starred

    results = api_client.get_data('/threads?thread_id={}'.format(
        thread.public_id))
    assert len(results) == 1

    results = api_client.get_data('/messages?thread_id={}'.format(
        thread.public_id))
    assert len(results) == 1

    results = api_client.get_data('/threads?cc={}'.format(message.cc_addr))
    assert len(results) == 0

    results = api_client.get_data('/messages?cc={}'.format(message.cc_addr))
    assert len(results) == 0

    results = api_client.get_data('/threads?bcc={}'.format(message.bcc_addr))
    assert len(results) == 0

    results = api_client.get_data('/messages?bcc={}'.format(message.bcc_addr))
    assert len(results) == 0

    results = api_client.get_data('/threads?filename=test')
    assert len(results) == 0

    results = api_client.get_data('/messages?filename=test')
    assert len(results) == 0

    results = api_client.get_data('/threads?started_after={}'.format(t_start -
                                                                     1))
    assert len(results) == 1

    results = api_client.get_data('/messages?started_after={}'.format(t_start -
                                                                      1))
    assert len(results) == 1

    results = api_client.get_data(
        '/messages?last_message_before={}&limit=1'.format(t_lastmsg + 1))
    assert len(results) == 1

    results = api_client.get_data(
        '/threads?last_message_before={}&limit=1'.format(t_lastmsg + 1))
    assert len(results) == 1

    results = api_client.get_data('/threads?in=inbox&limit=1')
    assert len(results) == 1

    results = api_client.get_data('/messages?in=inbox&limit=1')
    assert len(results) == 1

    results = api_client.get_data('/messages?in=banana%20rama')
    assert len(results) == 0

    results = api_client.get_data('/threads?subject={}'.format(subject))
    assert len(results) == 1

    results = api_client.get_data('/messages?subject={}'.format(subject))
    assert len(results) == 1

    results = api_client.get_data('/threads?unread={}'.format(unread))
    assert len(results) == 1

    results = api_client.get_data('/messages?unread={}'.format((not unread)))
    assert len(results) == 0

    results = api_client.get_data('/threads?starred={}'.format((not starred)))
    assert len(results) == 0

    results = api_client.get_data('/messages?starred={}'.format(starred))
    assert len(results) == 1

    for _ in range(3):
        add_fake_message(db.session,
                         default_namespace.id,
                         to_addr=[('', '*****@*****.**')],
                         thread=add_fake_thread(db.session,
                                                default_namespace.id))

    results = api_client.get_data(
        '/messages?any_email={}'.format('*****@*****.**'))
    assert len(results) > 1

    # Test multiple any_email params
    multiple_results = api_client.get_data(
        '/messages?any_email={},{},{}'.format('*****@*****.**',
                                              '*****@*****.**',
                                              '*****@*****.**'))
    assert len(multiple_results) > len(results)

    # Check that we canonicalize when searching.
    alternate_results = api_client.get_data(
        '/threads?any_email={}'.format('*****@*****.**'))
    assert len(alternate_results) == len(results)

    results = api_client.get_data('/messages?from={}'.format(from_addr))
    assert len(results) == 1
    results = api_client.get_data('/threads?from={}'.format(from_addr))
    assert len(results) == 1

    early_time = received_date - datetime.timedelta(seconds=1)
    late_time = received_date + datetime.timedelta(seconds=1)
    early_ts = calendar.timegm(early_time.utctimetuple())
    late_ts = calendar.timegm(late_time.utctimetuple())

    results = api_client.get_data(
        '/messages?subject={}&started_before={}'.format(subject, early_ts))
    assert len(results) == 0
    results = api_client.get_data(
        '/threads?subject={}&started_before={}'.format(subject, early_ts))
    assert len(results) == 0

    results = api_client.get_data(
        '/messages?subject={}&started_before={}'.format(subject, late_ts))
    assert len(results) == 1
    results = api_client.get_data(
        '/threads?subject={}&started_before={}'.format(subject, late_ts))
    assert len(results) == 1

    results = api_client.get_data(
        '/messages?subject={}&last_message_after={}'.format(subject, early_ts))
    assert len(results) == 1
    results = api_client.get_data(
        '/threads?subject={}&last_message_after={}'.format(subject, early_ts))
    assert len(results) == 1

    results = api_client.get_data(
        '/messages?subject={}&last_message_after={}'.format(subject, late_ts))
    assert len(results) == 0
    results = api_client.get_data(
        '/threads?subject={}&last_message_after={}'.format(subject, late_ts))
    assert len(results) == 0

    results = api_client.get_data(
        '/messages?subject={}&started_before={}'.format(subject, early_ts))
    assert len(results) == 0
    results = api_client.get_data(
        '/threads?subject={}&started_before={}'.format(subject, early_ts))
    assert len(results) == 0

    results = api_client.get_data(
        '/messages?subject={}&started_before={}'.format(subject, late_ts))
    assert len(results) == 1
    results = api_client.get_data(
        '/threads?subject={}&started_before={}'.format(subject, late_ts))
    assert len(results) == 1

    results = api_client.get_data('/messages?from={}&to={}'.format(
        from_addr, to_addr))
    assert len(results) == 1

    results = api_client.get_data('/threads?from={}&to={}'.format(
        from_addr, to_addr))
    assert len(results) == 1

    results = api_client.get_data('/messages?to={}&limit={}&offset={}'.format(
        '*****@*****.**', 2, 1))
    assert len(results) == 2

    results = api_client.get_data('/threads?to={}&limit={}'.format(
        '*****@*****.**', 3))
    assert len(results) == 3

    results = api_client.get_data('/threads?view=count')

    assert results['count'] == 4

    results = api_client.get_data('/threads?view=ids&to={}&limit=3'.format(
        '*****@*****.**', 3))

    assert len(results) == 3
    assert all(isinstance(r, basestring)
               for r in results), "Returns a list of string"
def test_filtering(db, api_client, default_namespace):
    thread = add_fake_thread(db.session, default_namespace.id)
    thread.tags.add(default_namespace.tags['inbox'])
    message = add_fake_message(db.session, default_namespace.id, thread,
                               to_addr=[('Bob', '*****@*****.**')],
                               from_addr=[('Alice', '*****@*****.**')],
                               subject='some subject')
    thread.subject = message.subject
    db.session.commit()
    t_start = dt_to_timestamp(thread.subjectdate)
    t_lastmsg = dt_to_timestamp(thread.recentdate)

    subject = message.subject
    to_addr = message.to_addr[0][1]
    from_addr = message.from_addr[0][1]

    received_date = message.received_date

    results = api_client.get_data('/threads?thread_id={}'
                                  .format(thread.public_id))
    assert len(results) == 1

    results = api_client.get_data('/messages?thread_id={}'
                                  .format(thread.public_id))
    assert len(results) == 1

    results = api_client.get_data('/threads?cc={}'
                                  .format(message.cc_addr))
    assert len(results) == 0

    results = api_client.get_data('/messages?cc={}'
                                  .format(message.cc_addr))
    assert len(results) == 0

    results = api_client.get_data('/threads?bcc={}'
                                  .format(message.bcc_addr))
    assert len(results) == 0

    results = api_client.get_data('/messages?bcc={}'
                                  .format(message.bcc_addr))
    assert len(results) == 0

    results = api_client.get_data('/threads?filename=test')
    assert len(results) == 0

    results = api_client.get_data('/messages?filename=test')
    assert len(results) == 0

    results = api_client.get_data('/threads?started_after={}'
                                  .format(t_start-1))
    assert len(results) == 1

    results = api_client.get_data('/messages?started_after={}'
                                  .format(t_start-1))
    assert len(results) == 1

    results = api_client.get_data('/messages?last_message_before={}&limit=1'
                                  .format(t_lastmsg+1))
    assert len(results) == 1

    results = api_client.get_data('/threads?last_message_before={}&limit=1'
                                  .format(t_lastmsg+1))
    assert len(results) == 1

    results = api_client.get_data('/threads?tag={}&limit=1'
                                  .format('inbox'))
    assert len(results) == 1

    results = api_client.get_data('/messages?tag={}&limit=1'
                                  .format('inbox'))
    assert len(results) == 1

    results = api_client.get_data('/messages?subject={}'.format(subject))
    assert len(results) == 1
    results = api_client.get_data('/threads?subject={}'.format(subject))
    assert len(results) == 1

    for _ in range(3):
        add_fake_message(db.session, default_namespace.id,
                         to_addr=[('', '*****@*****.**')],
                         thread=add_fake_thread(db.session,
                                                default_namespace.id))

    results = api_client.get_data('/messages?any_email={}'.
                                  format('*****@*****.**'))
    assert len(results) > 1
    results = api_client.get_data('/threads?any_email={}'.
                                  format('*****@*****.**'))
    assert len(results) > 1

    # Check that we canonicalize when searching.
    alternate_results = api_client.get_data('/threads?any_email={}'.
                                            format('*****@*****.**'))
    assert len(alternate_results) == len(results)

    results = api_client.get_data('/messages?from={}'.format(from_addr))
    assert len(results) == 1
    results = api_client.get_data('/threads?from={}'.format(from_addr))
    assert len(results) == 1

    early_time = received_date - datetime.timedelta(seconds=1)
    late_time = received_date + datetime.timedelta(seconds=1)
    early_ts = calendar.timegm(early_time.utctimetuple())
    late_ts = calendar.timegm(late_time.utctimetuple())

    results = api_client.get_data('/messages?subject={}&started_before={}'.
                                  format(subject, early_ts))
    assert len(results) == 0
    results = api_client.get_data('/threads?subject={}&started_before={}'.
                                  format(subject, early_ts))
    assert len(results) == 0

    results = api_client.get_data('/messages?subject={}&started_before={}'.
                                  format(subject, late_ts))
    assert len(results) == 1
    results = api_client.get_data('/threads?subject={}&started_before={}'.
                                  format(subject, late_ts))
    assert len(results) == 1

    results = api_client.get_data('/messages?subject={}&last_message_after={}'.
                                  format(subject, early_ts))
    assert len(results) == 1
    results = api_client.get_data('/threads?subject={}&last_message_after={}'.
                                  format(subject, early_ts))
    assert len(results) == 1

    results = api_client.get_data('/messages?subject={}&last_message_after={}'.
                                  format(subject, late_ts))
    assert len(results) == 0
    results = api_client.get_data('/threads?subject={}&last_message_after={}'.
                                  format(subject, late_ts))
    assert len(results) == 0

    results = api_client.get_data('/messages?subject={}&started_before={}'.
                                  format(subject, early_ts))
    assert len(results) == 0
    results = api_client.get_data('/threads?subject={}&started_before={}'.
                                  format(subject, early_ts))
    assert len(results) == 0

    results = api_client.get_data('/messages?subject={}&started_before={}'.
                                  format(subject, late_ts))
    assert len(results) == 1
    results = api_client.get_data('/threads?subject={}&started_before={}'.
                                  format(subject, late_ts))
    assert len(results) == 1

    results = api_client.get_data('/messages?from={}&to={}'.
                                  format(from_addr, to_addr))
    assert len(results) == 1

    results = api_client.get_data('/threads?from={}&to={}'.
                                  format(from_addr, to_addr))
    assert len(results) == 1

    results = api_client.get_data('/messages?to={}&limit={}&offset={}'.
                                  format('*****@*****.**', 2, 1))
    assert len(results) == 2

    results = api_client.get_data('/threads?to={}&limit={}'.
                                  format('*****@*****.**', 3))
    assert len(results) == 3

    results = api_client.get_data('/threads?view=count')

    assert results['count'] == 4

    results = api_client.get_data('/threads?view=ids&to={}&limit=3'.
                                  format('*****@*****.**', 3))

    assert len(results) == 3
    assert all(isinstance(r, basestring)
               for r in results), "Returns a list of string"
Esempio n. 5
0
def test_filtering(db, api_client, default_namespace):
    thread = add_fake_thread(db.session, default_namespace.id)
    message = add_fake_message(
        db.session,
        default_namespace.id,
        thread,
        to_addr=[("Bob", "*****@*****.**")],
        from_addr=[("Alice", "*****@*****.**")],
        subject="some subject",
    )
    message.categories.add(
        Category(namespace_id=message.namespace_id, name="inbox", display_name="Inbox", type_="label")
    )
    thread.subject = message.subject
    db.session.commit()

    t_start = dt_to_timestamp(thread.subjectdate)
    t_lastmsg = dt_to_timestamp(thread.recentdate)
    subject = message.subject
    to_addr = message.to_addr[0][1]
    from_addr = message.from_addr[0][1]
    received_date = message.received_date
    unread = not message.is_read
    starred = message.is_starred

    results = api_client.get_data("/threads?thread_id={}".format(thread.public_id))
    assert len(results) == 1

    results = api_client.get_data("/messages?thread_id={}".format(thread.public_id))
    assert len(results) == 1

    results = api_client.get_data("/threads?cc={}".format(message.cc_addr))
    assert len(results) == 0

    results = api_client.get_data("/messages?cc={}".format(message.cc_addr))
    assert len(results) == 0

    results = api_client.get_data("/threads?bcc={}".format(message.bcc_addr))
    assert len(results) == 0

    results = api_client.get_data("/messages?bcc={}".format(message.bcc_addr))
    assert len(results) == 0

    results = api_client.get_data("/threads?filename=test")
    assert len(results) == 0

    results = api_client.get_data("/messages?filename=test")
    assert len(results) == 0

    results = api_client.get_data("/threads?started_after={}".format(t_start - 1))
    assert len(results) == 1

    results = api_client.get_data("/messages?started_after={}".format(t_start - 1))
    assert len(results) == 1

    results = api_client.get_data("/messages?last_message_before={}&limit=1".format(t_lastmsg + 1))
    assert len(results) == 1

    results = api_client.get_data("/threads?last_message_before={}&limit=1".format(t_lastmsg + 1))
    assert len(results) == 1

    results = api_client.get_data("/threads?in=inbox&limit=1")
    assert len(results) == 1

    results = api_client.get_data("/messages?in=inbox&limit=1")
    assert len(results) == 1

    results = api_client.get_data("/messages?in=banana%20rama")
    assert len(results) == 0

    results = api_client.get_data("/threads?subject={}".format(subject))
    assert len(results) == 1

    results = api_client.get_data("/messages?subject={}".format(subject))
    assert len(results) == 1

    results = api_client.get_data("/threads?unread={}".format(unread))
    assert len(results) == 1

    results = api_client.get_data("/messages?unread={}".format((not unread)))
    assert len(results) == 0

    results = api_client.get_data("/threads?starred={}".format((not starred)))
    assert len(results) == 0

    results = api_client.get_data("/messages?starred={}".format(starred))
    assert len(results) == 1

    for _ in range(3):
        add_fake_message(
            db.session,
            default_namespace.id,
            to_addr=[("", "*****@*****.**")],
            thread=add_fake_thread(db.session, default_namespace.id),
        )

    results = api_client.get_data("/messages?any_email={}".format("*****@*****.**"))
    assert len(results) > 1

    # Test multiple any_email params
    multiple_results = api_client.get_data(
        "/messages?any_email={},{},{}".format("*****@*****.**", "*****@*****.**", "*****@*****.**")
    )
    assert len(multiple_results) > len(results)

    # Check that we canonicalize when searching.
    alternate_results = api_client.get_data("/threads?any_email={}".format("*****@*****.**"))
    assert len(alternate_results) == len(results)

    results = api_client.get_data("/messages?from={}".format(from_addr))
    assert len(results) == 1
    results = api_client.get_data("/threads?from={}".format(from_addr))
    assert len(results) == 1

    early_time = received_date - datetime.timedelta(seconds=1)
    late_time = received_date + datetime.timedelta(seconds=1)
    early_ts = calendar.timegm(early_time.utctimetuple())
    late_ts = calendar.timegm(late_time.utctimetuple())

    results = api_client.get_data("/messages?subject={}&started_before={}".format(subject, early_ts))
    assert len(results) == 0
    results = api_client.get_data("/threads?subject={}&started_before={}".format(subject, early_ts))
    assert len(results) == 0

    results = api_client.get_data("/messages?subject={}&started_before={}".format(subject, late_ts))
    assert len(results) == 1
    results = api_client.get_data("/threads?subject={}&started_before={}".format(subject, late_ts))
    assert len(results) == 1

    results = api_client.get_data("/messages?subject={}&last_message_after={}".format(subject, early_ts))
    assert len(results) == 1
    results = api_client.get_data("/threads?subject={}&last_message_after={}".format(subject, early_ts))
    assert len(results) == 1

    results = api_client.get_data("/messages?subject={}&last_message_after={}".format(subject, late_ts))
    assert len(results) == 0
    results = api_client.get_data("/threads?subject={}&last_message_after={}".format(subject, late_ts))
    assert len(results) == 0

    results = api_client.get_data("/messages?subject={}&started_before={}".format(subject, early_ts))
    assert len(results) == 0
    results = api_client.get_data("/threads?subject={}&started_before={}".format(subject, early_ts))
    assert len(results) == 0

    results = api_client.get_data("/messages?subject={}&started_before={}".format(subject, late_ts))
    assert len(results) == 1
    results = api_client.get_data("/threads?subject={}&started_before={}".format(subject, late_ts))
    assert len(results) == 1

    results = api_client.get_data("/messages?from={}&to={}".format(from_addr, to_addr))
    assert len(results) == 1

    results = api_client.get_data("/threads?from={}&to={}".format(from_addr, to_addr))
    assert len(results) == 1

    results = api_client.get_data("/messages?to={}&limit={}&offset={}".format("*****@*****.**", 2, 1))
    assert len(results) == 2

    results = api_client.get_data("/threads?to={}&limit={}".format("*****@*****.**", 3))
    assert len(results) == 3

    results = api_client.get_data("/threads?view=count")

    assert results["count"] == 4

    results = api_client.get_data("/threads?view=ids&to={}&limit=3".format("*****@*****.**", 3))

    assert len(results) == 3
    assert all(isinstance(r, basestring) for r in results), "Returns a list of string"
Esempio n. 6
0
def test_filtering(db, api_client):
    message = db.session.query(Message).filter_by(id=2).one()
    thread = message.thread
    t_start = dt_to_timestamp(thread.subjectdate)
    t_lastmsg = dt_to_timestamp(thread.recentdate)

    subject = message.subject
    to_addr = message.to_addr[0][1]
    from_addr = message.from_addr[0][1]

    received_date = message.received_date

    results = api_client.get_data('/threads?thread_id={}'
                                  .format('e6z26rjrxs2gu8at6gsa8svr1'))
    assert len(results) == 1

    results = api_client.get_data('/messages?thread_id={}'
                                  .format('e6z26rjrxs2gu8at6gsa8svr1'))
    assert len(results) == 1

    results = api_client.get_data('/threads?cc={}'
                                  .format(message.cc_addr))
    assert len(results) == 0

    results = api_client.get_data('/messages?cc={}'
                                  .format(message.cc_addr))
    assert len(results) == 0

    results = api_client.get_data('/threads?bcc={}'
                                  .format(message.bcc_addr))
    assert len(results) == 0

    results = api_client.get_data('/messages?bcc={}'
                                  .format(message.bcc_addr))
    assert len(results) == 0

    results = api_client.get_data('/threads?filename=test')
    assert len(results) == 0

    results = api_client.get_data('/messages?filename=test')
    assert len(results) == 0

    results = api_client.get_data('/threads?started_after={}'
                                  .format(t_start-1))
    assert len(results) == 1

    results = api_client.get_data('/messages?started_after={}'
                                  .format(t_start-1))
    assert len(results) == 1

    results = api_client.get_data('/messages?last_message_before={}&limit=1'
                                  .format(t_lastmsg+1))
    assert len(results) == 1

    results = api_client.get_data('/threads?last_message_before={}&limit=1'
                                  .format(t_lastmsg+1))
    assert len(results) == 1

    results = api_client.get_data('/threads?tag={}&limit=1'
                                  .format('inbox'))
    assert len(results) == 1

    results = api_client.get_data('/messages?tag={}&limit=1'
                                  .format('inbox'))
    assert len(results) == 1

    results = api_client.get_data('/messages?subject={}'.format(subject))
    assert len(results) == 1
    results = api_client.get_data('/threads?subject={}'.format(subject))
    assert len(results) == 1

    results = api_client.get_data('/messages?any_email={}'.
                                  format('*****@*****.**'))
    assert len(results) > 1
    results = api_client.get_data('/threads?any_email={}'.
                                  format('*****@*****.**'))
    assert len(results) > 1

    # Check that we canonicalize when searching.
    alternate_results = api_client.get_data('/threads?any_email={}'.
                                            format('*****@*****.**'))
    assert len(alternate_results) == len(results)

    results = api_client.get_data('/messages?from={}'.format(from_addr))
    assert len(results) == 1
    results = api_client.get_data('/threads?from={}'.format(from_addr))
    assert len(results) == 1

    early_time = received_date - datetime.timedelta(seconds=1)
    late_time = received_date + datetime.timedelta(seconds=1)
    early_ts = calendar.timegm(early_time.utctimetuple())
    late_ts = calendar.timegm(late_time.utctimetuple())

    results = api_client.get_data('/messages?subject={}&started_before={}'.
                                  format(subject, early_ts))
    assert len(results) == 0
    results = api_client.get_data('/threads?subject={}&started_before={}'.
                                  format(subject, early_ts))
    assert len(results) == 0

    results = api_client.get_data('/messages?subject={}&started_before={}'.
                                  format(subject, late_ts))
    assert len(results) == 1
    results = api_client.get_data('/threads?subject={}&started_before={}'.
                                  format(subject, late_ts))
    assert len(results) == 1

    results = api_client.get_data('/messages?subject={}&last_message_after={}'.
                                  format(subject, early_ts))
    assert len(results) == 1
    results = api_client.get_data('/threads?subject={}&last_message_after={}'.
                                  format(subject, early_ts))
    assert len(results) == 1

    results = api_client.get_data('/messages?subject={}&last_message_after={}'.
                                  format(subject, late_ts))
    assert len(results) == 0
    results = api_client.get_data('/threads?subject={}&last_message_after={}'.
                                  format(subject, late_ts))
    assert len(results) == 0

    results = api_client.get_data('/messages?subject={}&started_before={}'.
                                  format(subject, early_ts))
    assert len(results) == 0
    results = api_client.get_data('/threads?subject={}&started_before={}'.
                                  format(subject, early_ts))
    assert len(results) == 0

    results = api_client.get_data('/messages?subject={}&started_before={}'.
                                  format(subject, late_ts))
    assert len(results) == 1
    results = api_client.get_data('/threads?subject={}&started_before={}'.
                                  format(subject, late_ts))
    assert len(results) == 1

    results = api_client.get_data('/messages?from={}&to={}'.
                                  format(from_addr, to_addr))
    assert len(results) == 1

    results = api_client.get_data('/threads?from={}&to={}'.
                                  format(from_addr, to_addr))
    assert len(results) == 1

    results = api_client.get_data('/messages?to={}&limit={}&offset={}'.
                                  format('*****@*****.**', 2, 1))
    assert len(results) == 2

    results = api_client.get_data('/threads?to={}&limit={}'.
                                  format('*****@*****.**', 3))
    assert len(results) == 3

    results = api_client.get_data('/threads?view=count')

    assert results['count'] == 16

    results = api_client.get_data('/threads?view=ids&to={}&limit=3'.
                                  format('*****@*****.**', 3))

    assert len(results) == 3
    assert all(isinstance(r, basestring)
               for r in results), "Returns a list of string"
Esempio n. 7
0
def test_received_before_after(db, api_client, default_namespace):
    thread = add_fake_thread(db.session, default_namespace.id)
    message = add_fake_message(db.session,
                               default_namespace.id,
                               thread,
                               to_addr=[('Bob', '*****@*****.**')],
                               from_addr=[('Alice', '*****@*****.**')],
                               received_date=datetime.datetime(year=1999,
                                                               day=20,
                                                               month=03),
                               subject='some subject')

    thread2 = add_fake_thread(db.session, default_namespace.id)
    message2 = add_fake_message(db.session,
                                default_namespace.id,
                                thread,
                                to_addr=[('Bob', '*****@*****.**')],
                                from_addr=[('Alice', '*****@*****.**')],
                                received_date=datetime.datetime(year=2000,
                                                                day=20,
                                                                month=03),
                                subject='another subject')

    inbox = Category(namespace_id=message.namespace_id,
                     name='inbox',
                     display_name='Inbox',
                     type_='label')
    message.categories.add(inbox)
    thread.subject = message.subject

    message2.categories.add(inbox)
    thread2.subject = message2.subject

    db.session.commit()

    received_date = message.received_date
    t_epoch = dt_to_timestamp(datetime.datetime(year=1998, month=2, day=3))
    t_firstmsg = dt_to_timestamp(received_date)

    results = api_client.get_data(
        '/messages?received_before={}'.format(t_epoch))
    assert len(results) == 0

    # received_before should be inclusive (i.e: match <=, not just <).
    results = api_client.get_data(
        '/messages?received_before={}'.format(t_firstmsg))
    assert len(results) == 1

    t1 = dt_to_timestamp(received_date + datetime.timedelta(days=1))
    results = api_client.get_data('/messages?received_after={}'.format(t1))
    assert len(results) == 1

    results = api_client.get_data(
        '/messages?received_before={}&received_after={}'.format(
            t1, t_firstmsg))
    assert len(results) == 0

    # bogus values
    results = api_client.get_data(
        '/messages?received_before={}&received_after={}'.format(t_epoch, t1))
    assert len(results) == 0