def test_draft_updates(db, default_account, mock_imapclient): # Set up folder list mock_imapclient._data['Drafts'] = {} mock_imapclient._data['Trash'] = {} mock_imapclient.list_folders = lambda: [ (('\\HasNoChildren', '\\Drafts'), '/', 'Drafts'), (('\\HasNoChildren', '\\Trash'), '/', 'Trash') ] pool = writable_connection_pool(default_account.id) draft = create_message_from_json({'subject': 'Test draft'}, default_account.namespace, db.session, True) draft.is_draft = True draft.version = 0 db.session.commit() save_draft(default_account.id, draft.id, {'version': 0}) with pool.get() as conn: conn.select_folder('Drafts', lambda *args: True) assert len(conn.all_uids()) == 1 # Check that draft is not resaved if already synced. update_draft(default_account.id, draft.id, {'version': 0}) with pool.get() as conn: conn.select_folder('Drafts', lambda *args: True) assert len(conn.all_uids()) == 1 # Check that an older version is deleted draft.version = 4 sendmail_update_draft(db.session, default_account, draft, from_addr=draft.from_addr, subject='New subject', blocks=[]) db.session.commit() update_draft(default_account.id, draft.id, {'version': 5}) with pool.get() as conn: conn.select_folder('Drafts', lambda *args: True) all_uids = conn.all_uids() assert len(all_uids) == 1 data = conn.uids(all_uids)[0] parsed = mime.from_string(data.body) expected_message_id = '<{}-{}@mailer.nylas.com>'.format( draft.public_id, draft.version) assert parsed.headers.get('Message-Id') == expected_message_id delete_draft( default_account.id, draft.id, { 'message_id_header': draft.message_id_header, 'inbox_uid': draft.inbox_uid, 'version': 5 }) with pool.get() as conn: conn.select_folder('Drafts', lambda *args: True) all_uids = conn.all_uids() assert len(all_uids) == 0
def test_draft_updates(db, default_account, mock_imapclient): # Set up folder list mock_imapclient._data['Drafts'] = {} mock_imapclient._data['Trash'] = {} mock_imapclient.list_folders = lambda: [ (('\\HasNoChildren', '\\Drafts'), '/', 'Drafts'), (('\\HasNoChildren', '\\Trash'), '/', 'Trash') ] pool = writable_connection_pool(default_account.id) draft = create_message_from_json({'subject': 'Test draft'}, default_account.namespace, db.session, True) draft.is_draft = True draft.version = 0 db.session.commit() save_draft(default_account.id, draft.id, {'version': 0}) with pool.get() as conn: conn.select_folder('Drafts', lambda *args: True) assert len(conn.all_uids()) == 1 # Check that draft is not resaved if already synced. update_draft(default_account.id, draft.id, {'version': 0}) with pool.get() as conn: conn.select_folder('Drafts', lambda *args: True) assert len(conn.all_uids()) == 1 # Check that an older version is deleted draft.version = 4 sendmail_update_draft(db.session, default_account, draft, from_addr=draft.from_addr, subject='New subject', blocks=[]) db.session.commit() update_draft(default_account.id, draft.id, {'version': 5}) with pool.get() as conn: conn.select_folder('Drafts', lambda *args: True) all_uids = conn.all_uids() assert len(all_uids) == 1 data = conn.uids(all_uids)[0] parsed = mime.from_string(data.body) expected_message_id = '<{}-{}@mailer.nylas.com>'.format( draft.public_id, draft.version) assert parsed.headers.get('Message-Id') == expected_message_id delete_draft(default_account.id, draft.id, {'message_id_header': draft.message_id_header, 'inbox_uid': draft.inbox_uid, 'version': 5}) with pool.get() as conn: conn.select_folder('Drafts', lambda *args: True) all_uids = conn.all_uids() assert len(all_uids) == 0
def test_save_update_bad_recipient_draft(db, patch_remote_save_draft, default_account, example_bad_recipient_drafts): # You should be able to save a draft, even if # the recipient's email is invalid. from inbox.sendmail.base import create_message_from_json from inbox.actions.base import save_draft for example_draft in example_bad_recipient_drafts: draft = create_message_from_json(example_draft, default_account.namespace, db.session, is_draft=True) save_draft(default_account.id, draft.id, {"version": draft.version}) assert len(patch_remote_save_draft) == 2
def test_save_update_bad_recipient_draft( db, patch_remote_save_draft, default_account, example_bad_recipient_drafts ): # You should be able to save a draft, even if # the recipient's email is invalid. from inbox.actions.base import save_draft from inbox.sendmail.base import create_message_from_json for example_draft in example_bad_recipient_drafts: draft = create_message_from_json( example_draft, default_account.namespace, db.session, is_draft=True ) save_draft(default_account.id, draft.id, {"version": draft.version}) assert len(patch_remote_save_draft) == 2
def test_draft_updates(db, default_account, mock_imapclient): # Set up folder list mock_imapclient._data["Drafts"] = {} mock_imapclient._data["Trash"] = {} mock_imapclient._data["Sent Mail"] = {} mock_imapclient.list_folders = lambda: [ (("\\HasNoChildren", "\\Drafts"), "/", "Drafts"), (("\\HasNoChildren", "\\Trash"), "/", "Trash"), (("\\HasNoChildren", "\\Sent"), "/", "Sent Mail"), ] pool = writable_connection_pool(default_account.id) draft = create_message_from_json({"subject": "Test draft"}, default_account.namespace, db.session, True) draft.is_draft = True draft.version = 0 db.session.commit() with pool.get() as conn: save_draft(conn, default_account.id, draft.id, {"version": 0}) conn.select_folder("Drafts", lambda *args: True) assert len(conn.all_uids()) == 1 # Check that draft is not resaved if already synced. update_draft(conn, default_account.id, draft.id, {"version": 0}) conn.select_folder("Drafts", lambda *args: True) assert len(conn.all_uids()) == 1 # Check that an older version is deleted draft.version = 4 sendmail_update_draft( db.session, default_account, draft, from_addr=draft.from_addr, subject="New subject", blocks=[], ) db.session.commit() update_draft(conn, default_account.id, draft.id, {"version": 5}) conn.select_folder("Drafts", lambda *args: True) all_uids = conn.all_uids() assert len(all_uids) == 1 data = conn.uids(all_uids)[0] parsed = mime.from_string(data.body) expected_message_id = "<{}-{}@mailer.nylas.com>".format( draft.public_id, draft.version) assert parsed.headers.get("Message-Id") == expected_message_id # We're testing the draft deletion with Gmail here. However, # because of a race condition in Gmail's reconciliation algorithm, # we need to check if the sent mail has been created in the sent # folder. Since we're mocking everything, we have to create it # ourselves. mock_imapclient.append("Sent Mail", data.body, None, None, x_gm_msgid=4323) delete_draft( conn, default_account.id, draft.id, { "message_id_header": draft.message_id_header, "nylas_uid": draft.nylas_uid, "version": 5, }, ) conn.select_folder("Drafts", lambda *args: True) all_uids = conn.all_uids() assert len(all_uids) == 0
def test_draft_updates(db, default_account, mock_imapclient): # Set up folder list mock_imapclient._data['Drafts'] = {} mock_imapclient._data['Trash'] = {} mock_imapclient._data['Sent Mail'] = {} mock_imapclient.list_folders = lambda: [ (('\\HasNoChildren', '\\Drafts'), '/', 'Drafts'), (('\\HasNoChildren', '\\Trash'), '/', 'Trash'), (('\\HasNoChildren', '\\Sent'), '/', 'Sent Mail'), ] pool = writable_connection_pool(default_account.id) draft = create_message_from_json({'subject': 'Test draft'}, default_account.namespace, db.session, True) draft.is_draft = True draft.version = 0 db.session.commit() with pool.get() as conn: save_draft(conn, default_account.id, draft.id, {'version': 0}) conn.select_folder('Drafts', lambda *args: True) assert len(conn.all_uids()) == 1 # Check that draft is not resaved if already synced. update_draft(conn, default_account.id, draft.id, {'version': 0}) conn.select_folder('Drafts', lambda *args: True) assert len(conn.all_uids()) == 1 # Check that an older version is deleted draft.version = 4 sendmail_update_draft(db.session, default_account, draft, from_addr=draft.from_addr, subject='New subject', blocks=[]) db.session.commit() update_draft(conn, default_account.id, draft.id, {'version': 5}) conn.select_folder('Drafts', lambda *args: True) all_uids = conn.all_uids() assert len(all_uids) == 1 data = conn.uids(all_uids)[0] parsed = mime.from_string(data.body) expected_message_id = '<{}-{}@mailer.nylas.com>'.format( draft.public_id, draft.version) assert parsed.headers.get('Message-Id') == expected_message_id # We're testing the draft deletion with Gmail here. However, # because of a race condition in Gmail's reconciliation algorithm, # we need to check if the sent mail has been created in the sent # folder. Since we're mocking everything, we have to create it # ourselves. mock_imapclient.append('Sent Mail', data.body, None, None, x_gm_msgid=4323) delete_draft( conn, default_account.id, draft.id, { 'message_id_header': draft.message_id_header, 'nylas_uid': draft.nylas_uid, 'version': 5 }) conn.select_folder('Drafts', lambda *args: True) all_uids = conn.all_uids() assert len(all_uids) == 0
def test_draft_updates(db, default_account, mock_imapclient): # Set up folder list mock_imapclient._data['Drafts'] = {} mock_imapclient._data['Trash'] = {} mock_imapclient._data['Sent Mail'] = {} mock_imapclient.list_folders = lambda: [ (('\\HasNoChildren', '\\Drafts'), '/', 'Drafts'), (('\\HasNoChildren', '\\Trash'), '/', 'Trash'), (('\\HasNoChildren', '\\Sent'), '/', 'Sent Mail'), ] pool = writable_connection_pool(default_account.id) draft = create_message_from_json({'subject': 'Test draft'}, default_account.namespace, db.session, True) draft.is_draft = True draft.version = 0 db.session.commit() with pool.get() as conn: save_draft(conn, default_account.id, draft.id, {'version': 0}) conn.select_folder('Drafts', lambda *args: True) assert len(conn.all_uids()) == 1 # Check that draft is not resaved if already synced. update_draft(conn, default_account.id, draft.id, {'version': 0}) conn.select_folder('Drafts', lambda *args: True) assert len(conn.all_uids()) == 1 # Check that an older version is deleted draft.version = 4 sendmail_update_draft(db.session, default_account, draft, from_addr=draft.from_addr, subject='New subject', blocks=[]) db.session.commit() update_draft(conn, default_account.id, draft.id, {'version': 5}) conn.select_folder('Drafts', lambda *args: True) all_uids = conn.all_uids() assert len(all_uids) == 1 data = conn.uids(all_uids)[0] parsed = mime.from_string(data.body) expected_message_id = '<{}-{}@mailer.nylas.com>'.format( draft.public_id, draft.version) assert parsed.headers.get('Message-Id') == expected_message_id # We're testing the draft deletion with Gmail here. However, # because of a race condition in Gmail's reconciliation algorithm, # we need to check if the sent mail has been created in the sent # folder. Since we're mocking everything, we have to create it # ourselves. mock_imapclient.append('Sent Mail', data.body, None, None, x_gm_msgid=4323) delete_draft(conn, default_account.id, draft.id, {'message_id_header': draft.message_id_header, 'nylas_uid': draft.nylas_uid, 'version': 5}) conn.select_folder('Drafts', lambda *args: True) all_uids = conn.all_uids() assert len(all_uids) == 0