def test_streaming_search_results(db, imap_api_client, generic_account,
                                  imap_folder, monkeypatch, sorted_imap_messages,
                                  different_imap_messages, endpoint):
    # Check that the streaming search returns results from different
    # folders.

    class MultiFolderMockImapConnection(MockImapConnection):
        def __init__(self):
            self._responses = list(reversed([
                [2000, 2001, 2002],
                [5000, 5001],
            ]))

        def search(self, criteria, charset=None):
            self.search_args = (criteria, charset)
            return self._responses.pop()

    conn = MultiFolderMockImapConnection()
    monkeypatch.setattr(
        'inbox.auth.generic.GenericAuthHandler.connect_account',
        lambda *_, **__: conn)

    search_client = get_search_client(generic_account)
    assert isinstance(search_client, IMAPSearchClient)

    url = '/{}/search/streaming?q=fantastic'.format(endpoint)
    raw_data = imap_api_client.get_raw(url).data
    assert len(conn._responses) == 0, "Search should go through both folders"

    # The API returns JSON lists separated by '\n'
    responses = raw_data.split('\n')
    assert len(responses) == 3 and responses[2] == ''
    assert len(json.loads(responses[0])) == 3
    assert len(json.loads(responses[1])) == 2
Exemple #2
0
def test_streaming_search_results(db, imap_api_client, generic_account,
                                  imap_folder, monkeypatch, sorted_imap_messages,
                                  different_imap_messages, endpoint):
    # Check that the streaming search returns results from different
    # folders.

    class MultiFolderMockImapConnection(MockImapConnection):
        def __init__(self):
            self._responses = list(reversed([
                [2000, 2001, 2002],
                [5000, 5001],
            ]))

        def search(self, criteria, charset=None):
            self.search_args = (criteria, charset)
            return self._responses.pop()

    conn = MultiFolderMockImapConnection()
    monkeypatch.setattr(
        'inbox.auth.generic.GenericAuthHandler.connect_account',
        lambda *_, **__: conn)

    search_client = get_search_client(generic_account)
    assert isinstance(search_client, IMAPSearchClient)

    url = '/{}/search/streaming?q=fantastic'.format(endpoint)
    raw_data = imap_api_client.get_raw(url).data
    assert len(conn._responses) == 0, "Search should go through both folders"

    # The API returns JSON lists separated by '\n'
    responses = raw_data.split('\n')
    assert len(responses) == 3 and responses[2] == ''
    assert len(json.loads(responses[0])) == 3
    assert len(json.loads(responses[1])) == 2
def test_invalid_imap_account_search(db, imap_api_client, generic_account,
                                     invalid_imap_connection, imap_folder,
                                     sorted_imap_messages, is_streaming):

    if is_streaming:
        # Because of the way streaming search work, it will return a
        # 200 response even though we can't access the account.
        response = imap_api_client.get_raw('/messages/search/streaming?'
                                           'q=blah%20blah%20blah')
        assert response.status_code == 200
    else:
        response = imap_api_client.get_raw('/messages/search?'
                                           'q=blah%20blah%20blah')

        assert response.status_code == 403
        assert "This search can\'t be performed because the account\'s "\
            "credentials are out of date." in json.loads(response.data)['message']
def test_invalid_imap_account_search(db, imap_api_client, generic_account,
                                     invalid_imap_connection,
                                     imap_folder,
                                     sorted_imap_messages, is_streaming):

    if is_streaming:
        # Because of the way streaming search work, it will return a
        # 200 response even though we can't access the account.
        response = imap_api_client.get_raw('/messages/search/streaming?'
                                           'q=blah%20blah%20blah')
        assert response.status_code == 200
    else:
        response = imap_api_client.get_raw('/messages/search?'
                                           'q=blah%20blah%20blah')

        assert response.status_code == 403
        assert "This search can\'t be performed because the account\'s "\
            "credentials are out of date." in json.loads(response.data)['message']