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_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