mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-11-14 05:41:05 +08:00
531118ac5c
Summary: Fixes T4291 If I made a final edit to a pre-existing draft and sent, we'd queue a `SyncbackDraftTask` before a `SendDraftTask`. This is important because since we have a valid draft `server_id`, the `SendDraftTask` will send by server_id, not by POSTing the whole body. If the `SyncbackDraftTask` fails, then we had a very serious issue whereby the `SendDraftTask` would keep on sending. Unfortunately the server never got the latest changes and sent the wrong version of the draft. This incorrect version would show up later when the `/send` endpoint returned the message that got actually sent. The solution was to make any queued `SendDraftTask` fail if a dependent `SyncbackDraftTask` failed. This meant we needed to make the requirements for `shouldWaitForTask` stricter, and block if tasks failed. Unfortunatley there was no infrastructure in place to do this. The first change was to change `shouldWaitForTask` to `isDependentTask`. If we're going to fail when a dependent task fails, I wanted the method name to reflect this. Now, if a dependent task fails, we recursively check the dependency tree (and check for cycles) and `dequeue` anything that needed that to succeed. I chose `dequeue` as the default action because it seemed as though all current uses of `shouldWaitForTask` really should bail if their dependencies fail. It's possible you don't want your task dequeued in this dependency case. You can return the special `Task.DO_NOT_DEQUEUE_ME` constant from the `onDependentTaskError` method. When a task gets dequeued because of the reason above, the `onDependentTaskError` callback gets fired. This gives tasks like the `SendDraftTask` a chance to notify the user that it bailed. Not all tasks need to notify. The next big issue was a better way to determine if a task truely errored to the point that we need to dequeue dependencies. In the Developer Status area we were showing tasks that had errored as "Green" because we caught the error and resolved with `Task.Status.Finished`. This used to be fine since nothing life-or-death cared if a task errored or not. Now that it might cause abortions down the line, we needed a more robust method then this. For one I changed `Task.Status.Finished` to a variety of finish types including `Task.Status.Success`. The way you "error" out is to `throw` or `Promise.reject` an `Error` object from the `performRemote` method. This allows us to propagate API errors up, and acts as a safety net that can catch any malformed code or unexpected responses. The developer bar now shows a much richer set of statuses instead of a binary one, which was REALLY helpful in debugging this. We also record when a Task got dequeued because of the conditions introduced here. Once all this was working we still had an issue of sending old drafts. If after a `SyncbackDraftTask` failed, now we'd block the send and notify the users as such. However, if we tried to send again, there was a separate issue whereby we wouldn't queue another `SyncbackDraftTask` to update the server with the latest information. Since our changes were persisted to the DB, we thought we had no changes, and therefore didn't need to queue a `SyncbackDraftTask`. The fix to this is to always force the creation of a `SyncbackDraftTask` before send regardless of the state of the `DraftStoreProxy`. Test Plan: new tests. Lots of manual testing Reviewers: bengotow Reviewed By: bengotow Subscribers: mg Maniphest Tasks: T4291 Differential Revision: https://phab.nylas.com/D2156
296 lines
11 KiB
CoffeeScript
296 lines
11 KiB
CoffeeScript
NylasAPI = require '../../src/flux/nylas-api'
|
|
Actions = require '../../src/flux/actions'
|
|
SyncbackDraftTask = require '../../src/flux/tasks/syncback-draft'
|
|
FileUploadTask = require '../../src/flux/tasks/file-upload-task'
|
|
SendDraftTask = require '../../src/flux/tasks/send-draft'
|
|
DatabaseStore = require '../../src/flux/stores/database-store'
|
|
{APIError} = require '../../src/flux/errors'
|
|
Message = require '../../src/flux/models/message'
|
|
TaskQueue = require '../../src/flux/stores/task-queue'
|
|
SoundRegistry = require '../../src/sound-registry'
|
|
_ = require 'underscore'
|
|
|
|
describe "SendDraftTask", ->
|
|
describe "isDependentTask", ->
|
|
it "should return true if there are SyncbackDraftTasks for the same draft", ->
|
|
@draftA = new Message
|
|
version: '1'
|
|
id: '1233123AEDF1'
|
|
accountId: 'A12ADE'
|
|
subject: 'New Draft'
|
|
draft: true
|
|
to:
|
|
name: 'Dummy'
|
|
email: 'dummy@nylas.com'
|
|
|
|
@draftB = new Message
|
|
version: '1'
|
|
id: '1233OTHERDRAFT'
|
|
accountId: 'A12ADE'
|
|
subject: 'New Draft'
|
|
draft: true
|
|
to:
|
|
name: 'Dummy'
|
|
email: 'dummy@nylas.com'
|
|
|
|
@saveA = new SyncbackDraftTask('localid-A')
|
|
@saveB = new SyncbackDraftTask('localid-B')
|
|
@sendA = new SendDraftTask('localid-A')
|
|
|
|
expect(@sendA.isDependentTask(@saveA)).toBe(true)
|
|
|
|
describe "performLocal", ->
|
|
it "should throw an exception if the first parameter is not a clientId", ->
|
|
badTasks = [new SendDraftTask()]
|
|
goodTasks = [new SendDraftTask('localid-a')]
|
|
caught = []
|
|
succeeded = []
|
|
|
|
runs ->
|
|
[].concat(badTasks, goodTasks).forEach (task) ->
|
|
task.performLocal()
|
|
.then -> succeeded.push(task)
|
|
.catch (err) -> caught.push(task)
|
|
|
|
waitsFor ->
|
|
succeeded.length + caught.length == badTasks.length + goodTasks.length
|
|
|
|
runs ->
|
|
expect(caught).toEqual(badTasks)
|
|
expect(succeeded).toEqual(goodTasks)
|
|
|
|
describe "performRemote", ->
|
|
beforeEach ->
|
|
@draftClientId = "local-123"
|
|
@serverMessageId = '1233123AEDF1'
|
|
@draft = new Message
|
|
version: 1
|
|
clientId: @draftClientId
|
|
accountId: 'A12ADE'
|
|
subject: 'New Draft'
|
|
draft: true
|
|
body: 'hello world'
|
|
to:
|
|
name: 'Dummy'
|
|
email: 'dummy@nylas.com'
|
|
|
|
response =
|
|
version: 2
|
|
id: @serverMessageId
|
|
account_id: 'A12ADE'
|
|
subject: 'New Draft'
|
|
body: 'hello world'
|
|
to:
|
|
name: 'Dummy'
|
|
email: 'dummy@nylas.com'
|
|
|
|
@task = new SendDraftTask(@draftClientId)
|
|
|
|
|
|
spyOn(NylasAPI, 'makeRequest').andCallFake (options) =>
|
|
options.success?(response)
|
|
Promise.resolve(response)
|
|
spyOn(DatabaseStore, 'run').andCallFake (klass, id) =>
|
|
Promise.resolve(@draft)
|
|
spyOn(DatabaseStore, 'unpersistModel').andCallFake (draft) ->
|
|
Promise.resolve()
|
|
spyOn(DatabaseStore, 'persistModel').andCallFake (draft) ->
|
|
Promise.resolve()
|
|
spyOn(SoundRegistry, "playSound")
|
|
spyOn(Actions, "postNotification")
|
|
spyOn(Actions, "sendDraftSuccess")
|
|
|
|
it "should notify the draft was sent", ->
|
|
waitsForPromise => @task.performRemote().then =>
|
|
args = Actions.sendDraftSuccess.calls[0].args[0]
|
|
expect(args.draftClientId).toBe @draftClientId
|
|
|
|
it "get an object back on success", ->
|
|
waitsForPromise => @task.performRemote().then =>
|
|
args = Actions.sendDraftSuccess.calls[0].args[0]
|
|
expect(args.newMessage.id).toBe @serverMessageId
|
|
|
|
it "should play a sound", ->
|
|
spyOn(atom.config, "get").andReturn true
|
|
waitsForPromise => @task.performRemote().then ->
|
|
expect(atom.config.get).toHaveBeenCalledWith("core.sending.sounds")
|
|
expect(SoundRegistry.playSound).toHaveBeenCalledWith("send")
|
|
|
|
it "shouldn't play a sound if the config is disabled", ->
|
|
spyOn(atom.config, "get").andReturn false
|
|
waitsForPromise => @task.performRemote().then ->
|
|
expect(atom.config.get).toHaveBeenCalledWith("core.sending.sounds")
|
|
expect(SoundRegistry.playSound).not.toHaveBeenCalled()
|
|
|
|
it "should start an API request to /send", ->
|
|
waitsForPromise =>
|
|
@task.performRemote().then =>
|
|
expect(NylasAPI.makeRequest.calls.length).toBe(1)
|
|
options = NylasAPI.makeRequest.mostRecentCall.args[0]
|
|
expect(options.path).toBe("/send")
|
|
expect(options.accountId).toBe(@draft.accountId)
|
|
expect(options.method).toBe('POST')
|
|
|
|
it "should locally convert the draft to a message on send", ->
|
|
expect(@draft.clientId).toBe @draftClientId
|
|
expect(@draft.serverId).toBeUndefined()
|
|
waitsForPromise =>
|
|
@task.performRemote().then =>
|
|
expect(DatabaseStore.persistModel).toHaveBeenCalled()
|
|
model = DatabaseStore.persistModel.calls[0].args[0]
|
|
expect(model.clientId).toBe @draftClientId
|
|
expect(model.serverId).toBe @serverMessageId
|
|
expect(model.draft).toBe false
|
|
|
|
describe "when the draft has been saved", ->
|
|
it "should send the draft ID and version", ->
|
|
waitsForPromise =>
|
|
@task.performRemote().then =>
|
|
expect(NylasAPI.makeRequest.calls.length).toBe(1)
|
|
options = NylasAPI.makeRequest.mostRecentCall.args[0]
|
|
expect(options.body.version/1).toBe(1)
|
|
expect(options.body.draft_id).toBe(@draft.serverId)
|
|
|
|
describe "when the draft has not been saved", ->
|
|
beforeEach ->
|
|
@draft = new Message
|
|
serverId: null
|
|
clientId: @draftClientId
|
|
accountId: 'A12ADE'
|
|
subject: 'New Draft'
|
|
draft: true
|
|
body: 'hello world'
|
|
to:
|
|
name: 'Dummy'
|
|
email: 'dummy@nylas.com'
|
|
@task = new SendDraftTask(@draftClientId)
|
|
|
|
it "should send the draft JSON", ->
|
|
waitsForPromise =>
|
|
expectedJSON = @draft.toJSON()
|
|
@task.performRemote().then =>
|
|
expect(NylasAPI.makeRequest.calls.length).toBe(1)
|
|
options = NylasAPI.makeRequest.mostRecentCall.args[0]
|
|
expect(options.body).toEqual(expectedJSON)
|
|
|
|
it "should always send the draft body in the request body (joined attribute check)", ->
|
|
waitsForPromise =>
|
|
@task.performRemote().then =>
|
|
expect(NylasAPI.makeRequest.calls.length).toBe(1)
|
|
options = NylasAPI.makeRequest.mostRecentCall.args[0]
|
|
expect(options.body.body).toBe('hello world')
|
|
|
|
it "should pass returnsModel:false", ->
|
|
waitsForPromise =>
|
|
@task.performRemote().then ->
|
|
expect(NylasAPI.makeRequest.calls.length).toBe(1)
|
|
options = NylasAPI.makeRequest.mostRecentCall.args[0]
|
|
expect(options.returnsModel).toBe(false)
|
|
|
|
it "should write the saved message to the database with the same client ID", ->
|
|
waitsForPromise =>
|
|
@task.performRemote().then =>
|
|
expect(DatabaseStore.persistModel).toHaveBeenCalled()
|
|
expect(DatabaseStore.persistModel.mostRecentCall.args[0].clientId).toEqual(@draftClientId)
|
|
expect(DatabaseStore.persistModel.mostRecentCall.args[0].serverId).toEqual('1233123AEDF1')
|
|
expect(DatabaseStore.persistModel.mostRecentCall.args[0].draft).toEqual(false)
|
|
|
|
describe "failing performRemote", ->
|
|
beforeEach ->
|
|
@draft = new Message
|
|
version: '1'
|
|
clientId: @draftClientId
|
|
accountId: 'A12ADE'
|
|
threadId: 'threadId'
|
|
replyToMessageId: 'replyToMessageId'
|
|
subject: 'New Draft'
|
|
body: 'body'
|
|
draft: true
|
|
to:
|
|
name: 'Dummy'
|
|
email: 'dummy@nylas.com'
|
|
@task = new SendDraftTask("local-1234")
|
|
spyOn(Actions, "dequeueTask")
|
|
spyOn(DatabaseStore, 'unpersistModel').andCallFake (draft) ->
|
|
Promise.resolve()
|
|
spyOn(DatabaseStore, 'persistModel').andCallFake (draft) ->
|
|
Promise.resolve()
|
|
|
|
describe "when the server responds with `Invalid message public ID`", ->
|
|
it "should resend the draft without the reply_to_message_id key set", ->
|
|
spyOn(DatabaseStore, 'run').andCallFake =>
|
|
Promise.resolve(@draft)
|
|
spyOn(NylasAPI, 'makeRequest').andCallFake ({body, success, error}) =>
|
|
if body.reply_to_message_id
|
|
err = new APIError(body: "Invalid message public id", statusCode: 400)
|
|
error?(err)
|
|
return Promise.reject(err)
|
|
else
|
|
success?(body)
|
|
return Promise.resolve(body)
|
|
|
|
waitsForPromise =>
|
|
@task.performRemote().then =>
|
|
expect(NylasAPI.makeRequest.calls.length).toBe(2)
|
|
expect(NylasAPI.makeRequest.calls[1].args[0].body.thread_id).toBe('threadId')
|
|
expect(NylasAPI.makeRequest.calls[1].args[0].body.reply_to_message_id).toBe(null)
|
|
|
|
describe "when the server responds with `Invalid thread ID`", ->
|
|
it "should resend the draft without the thread_id or reply_to_message_id keys set", ->
|
|
spyOn(DatabaseStore, 'run').andCallFake => Promise.resolve(@draft)
|
|
spyOn(NylasAPI, 'makeRequest').andCallFake ({body, success, error}) =>
|
|
new Promise (resolve, reject) =>
|
|
if body.thread_id
|
|
err = new APIError(body: "Invalid thread public id", statusCode: 400)
|
|
error?(err)
|
|
reject(err)
|
|
else
|
|
success?(body)
|
|
resolve(body)
|
|
|
|
waitsForPromise =>
|
|
@task.performRemote().then =>
|
|
expect(NylasAPI.makeRequest.calls.length).toBe(2)
|
|
expect(NylasAPI.makeRequest.calls[1].args[0].body.thread_id).toBe(null)
|
|
expect(NylasAPI.makeRequest.calls[1].args[0].body.reply_to_message_id).toBe(null)
|
|
.catch (err) =>
|
|
console.log(err.trace)
|
|
|
|
it "throws an error if the draft can't be found", ->
|
|
spyOn(DatabaseStore, 'run').andCallFake (klass, clientId) ->
|
|
Promise.resolve()
|
|
waitsForPromise =>
|
|
@task.performRemote().catch (error) ->
|
|
expect(error.message).toBeDefined()
|
|
|
|
it "throws an error if the draft isn't saved", ->
|
|
spyOn(DatabaseStore, 'run').andCallFake (klass, clientId) ->
|
|
Promise.resolve(serverId: null)
|
|
waitsForPromise =>
|
|
@task.performRemote().catch (error) ->
|
|
expect(error.message).toBeDefined()
|
|
|
|
it "throws an error if the DB store has issues", ->
|
|
spyOn(DatabaseStore, 'run').andCallFake (klass, clientId) ->
|
|
Promise.reject("DB error")
|
|
waitsForPromise =>
|
|
@task.performRemote().catch (error) ->
|
|
expect(error).toBe "DB error"
|
|
|
|
describe "failing dependent task", ->
|
|
it "notifies the user that the required draft save failed", ->
|
|
task = new SendDraftTask("local-1234")
|
|
syncback = new SyncbackDraftTask('local-1234')
|
|
spyOn(task, "_notifyUserOfError")
|
|
task.onDependentTaskError(syncback, new Error("Oh no"))
|
|
expect(task._notifyUserOfError).toHaveBeenCalled()
|
|
expect(task._notifyUserOfError.calls.length).toBe 1
|
|
|
|
it "notifies the user that the required file upload failed", ->
|
|
task = new SendDraftTask("local-1234")
|
|
fileUploadTask = new FileUploadTask('/dev/null', 'local-1234')
|
|
spyOn(task, "_notifyUserOfError")
|
|
task.onDependentTaskError(fileUploadTask, new Error("Oh no"))
|
|
expect(task._notifyUserOfError).toHaveBeenCalled()
|
|
expect(task._notifyUserOfError.calls.length).toBe 1
|
|
|