2015-05-22 09:08:29 +08:00
|
|
|
_ = require 'underscore'
|
2016-03-15 06:14:08 +08:00
|
|
|
keytar = require 'keytar'
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
AccountStore = require '../../src/flux/stores/account-store'
|
2015-09-25 05:51:15 +08:00
|
|
|
Account = require '../../src/flux/models/account'
|
2016-01-12 06:11:25 +08:00
|
|
|
Actions = require '../../src/flux/actions'
|
2015-05-22 09:08:29 +08:00
|
|
|
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
describe "AccountStore", ->
|
2015-05-22 09:08:29 +08:00
|
|
|
beforeEach ->
|
2015-08-06 00:44:34 +08:00
|
|
|
@instance = null
|
feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.
This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.
When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.
Search bar doesn't need to do full refresh on clear if it never committed
Allow drafts to be switched to a different account when not in reply to an existing thread
Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal
Show many dots for many accounts in long polling status bar
add/remove accounts from prefs
Spec fixes!
Test Plan: Run tests, none broken!
Reviewers: evan, dillon
Reviewed By: evan, dillon
Differential Revision: https://phab.nylas.com/D1928
2015-08-22 06:29:58 +08:00
|
|
|
@constructor = AccountStore.constructor
|
2016-03-15 06:14:08 +08:00
|
|
|
@keys = {}
|
|
|
|
spyOn(keytar, 'getPassword').andCallFake (service, account) =>
|
|
|
|
@keys[account]
|
|
|
|
spyOn(keytar, 'deletePassword').andCallFake (service, account) =>
|
|
|
|
delete @keys[account]
|
|
|
|
spyOn(keytar, 'replacePassword').andCallFake (service, account, pass) =>
|
|
|
|
@keys[account] = pass
|
2015-05-22 09:08:29 +08:00
|
|
|
|
2015-08-06 00:44:34 +08:00
|
|
|
afterEach ->
|
|
|
|
@instance.stopListeningToAll()
|
|
|
|
|
2016-03-15 06:14:08 +08:00
|
|
|
describe "initialization", ->
|
|
|
|
beforeEach ->
|
|
|
|
@configTokens = null
|
|
|
|
@configVersion = 1
|
|
|
|
@configAccounts =
|
|
|
|
[{
|
|
|
|
"id": "A",
|
|
|
|
"client_id" : 'local-4f9d476a-c173',
|
|
|
|
"server_id" : 'A',
|
|
|
|
"email_address":"bengotow@gmail.com",
|
|
|
|
"object":"account"
|
|
|
|
"organization_unit": "label"
|
|
|
|
},{
|
|
|
|
"id": "B",
|
|
|
|
"client_id" : 'local-4f9d476a-c175',
|
|
|
|
"server_id" : 'B',
|
|
|
|
"email_address":"ben@nylas.com",
|
|
|
|
"object":"account"
|
|
|
|
"organization_unit": "label"
|
|
|
|
}]
|
2015-05-22 09:08:29 +08:00
|
|
|
|
2016-03-15 06:14:08 +08:00
|
|
|
spyOn(NylasEnv.config, 'set')
|
|
|
|
spyOn(NylasEnv.config, 'get').andCallFake (key) =>
|
|
|
|
return @configAccounts if key is 'nylas.accounts'
|
|
|
|
return @configVersion if key is 'nylas.accountsVersion'
|
|
|
|
return @configTokens if key is 'nylas.accountTokens'
|
|
|
|
return null
|
2015-09-25 05:51:15 +08:00
|
|
|
|
2016-03-15 06:14:08 +08:00
|
|
|
it "should initialize the accounts and version from config", ->
|
|
|
|
@instance = new @constructor
|
|
|
|
expect(@instance._version).toEqual(@configVersion)
|
|
|
|
expect(@instance.accounts()).toEqual([
|
|
|
|
(new Account).fromJSON(@configAccounts[0]),
|
|
|
|
(new Account).fromJSON(@configAccounts[1])
|
|
|
|
])
|
|
|
|
|
2016-03-24 10:02:51 +08:00
|
|
|
it "should initialize tokens from config, if present, and save them to keytar", ->
|
2016-03-15 06:14:08 +08:00
|
|
|
@configTokens = {'A': 'A-TOKEN'}
|
|
|
|
@instance = new @constructor
|
|
|
|
expect(@instance.tokenForAccountId('A')).toEqual('A-TOKEN')
|
|
|
|
expect(@instance.tokenForAccountId('B')).toEqual(undefined)
|
|
|
|
expect(keytar.replacePassword).toHaveBeenCalledWith('Nylas', 'bengotow@gmail.com', 'A-TOKEN')
|
|
|
|
|
|
|
|
it "should initialize tokens from keytar", ->
|
|
|
|
@configTokens = null
|
|
|
|
jasmine.unspy(keytar, 'getPassword')
|
|
|
|
spyOn(keytar, 'getPassword').andCallFake (service, account) =>
|
|
|
|
return 'A-TOKEN' if account is 'bengotow@gmail.com'
|
|
|
|
return 'B-TOKEN' if account is 'ben@nylas.com'
|
|
|
|
return null
|
|
|
|
@instance = new @constructor
|
|
|
|
expect(@instance.tokenForAccountId('A')).toEqual('A-TOKEN')
|
|
|
|
expect(@instance.tokenForAccountId('B')).toEqual('B-TOKEN')
|
2015-10-01 01:12:12 +08:00
|
|
|
|
2016-02-07 16:39:11 +08:00
|
|
|
describe "accountForEmail", ->
|
|
|
|
beforeEach ->
|
|
|
|
@instance = new @constructor
|
|
|
|
@ac1 = new Account emailAddress: 'juan@nylas.com', aliases: []
|
|
|
|
@ac2 = new Account emailAddress: 'juan@gmail.com', aliases: ['Juan <juanchis@gmail.com>']
|
|
|
|
@ac3 = new Account emailAddress: 'jackie@columbia.edu', aliases: ['Jackie Luo <jacqueline.luo@columbia.edu>']
|
|
|
|
@instance._accounts = [@ac1, @ac2, @ac3]
|
|
|
|
|
|
|
|
it 'returns correct account when no alises present', ->
|
|
|
|
expect(@instance.accountForEmail('juan@nylas.com')).toEqual @ac1
|
|
|
|
|
|
|
|
it 'returns correct account when alias is used', ->
|
|
|
|
expect(@instance.accountForEmail('juanchis@gmail.com')).toEqual @ac2
|
|
|
|
expect(@instance.accountForEmail('jacqueline.luo@columbia.edu')).toEqual @ac3
|
|
|
|
|
2015-10-01 01:12:12 +08:00
|
|
|
describe "adding account from json", ->
|
|
|
|
beforeEach ->
|
2015-11-12 02:25:11 +08:00
|
|
|
spyOn(NylasEnv.config, "set")
|
2015-10-01 01:12:12 +08:00
|
|
|
@json =
|
2016-03-15 06:14:08 +08:00
|
|
|
"id": "B",
|
2015-10-01 01:12:12 +08:00
|
|
|
"client_id" : 'local-4f9d476a-c175',
|
2016-03-15 06:14:08 +08:00
|
|
|
"server_id" : 'B',
|
2015-10-01 01:12:12 +08:00
|
|
|
"email_address":"ben@nylas.com",
|
2015-10-06 07:22:22 +08:00
|
|
|
"provider":"gmail",
|
2015-10-01 01:12:12 +08:00
|
|
|
"object":"account"
|
2016-03-15 06:14:08 +08:00
|
|
|
"auth_token": "B-NEW-TOKEN"
|
2015-10-01 01:12:12 +08:00
|
|
|
"organization_unit": "label"
|
|
|
|
@instance = new @constructor
|
2016-01-20 15:42:50 +08:00
|
|
|
spyOn(Actions, 'focusDefaultMailboxPerspectiveForAccounts')
|
2015-10-01 01:12:12 +08:00
|
|
|
spyOn(@instance, "trigger")
|
|
|
|
@instance.addAccountFromJSON(@json)
|
|
|
|
|
2016-03-15 06:14:08 +08:00
|
|
|
it "saves the token to keytar and to the loaded tokens cache", ->
|
|
|
|
expect(@instance._tokens["B"]).toBe("B-NEW-TOKEN")
|
|
|
|
expect(keytar.replacePassword).toHaveBeenCalledWith("Nylas", "ben@nylas.com", "B-NEW-TOKEN")
|
2015-10-01 01:12:12 +08:00
|
|
|
|
2016-03-15 06:14:08 +08:00
|
|
|
it "saves the account to the accounts cache and saves", ->
|
2015-10-01 01:12:12 +08:00
|
|
|
account = (new Account).fromJSON(@json)
|
|
|
|
expect(@instance._accounts.length).toBe 1
|
|
|
|
expect(@instance._accounts[0]).toEqual account
|
2016-03-24 09:10:15 +08:00
|
|
|
expect(NylasEnv.config.set.calls.length).toBe 3
|
|
|
|
expect(NylasEnv.config.set.calls[2].args).toEqual(['nylas.accountTokens', null])
|
2015-11-12 02:25:11 +08:00
|
|
|
expect(NylasEnv.config.save).toHaveBeenCalled()
|
2015-10-01 01:12:12 +08:00
|
|
|
|
|
|
|
it "selects the account", ->
|
2016-03-15 06:14:08 +08:00
|
|
|
expect(Actions.focusDefaultMailboxPerspectiveForAccounts).toHaveBeenCalledWith(["B"])
|
2015-10-01 01:12:12 +08:00
|
|
|
|
|
|
|
it "triggers", ->
|
|
|
|
expect(@instance.trigger).toHaveBeenCalled()
|