Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
fix(Notifications): set syncPubKeys setting when updating notificatio…
Browse files Browse the repository at this point in the history
…ns settings
  • Loading branch information
Thore3 committed Jul 26, 2016
1 parent 78c57fd commit 9d58da3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 61 deletions.
8 changes: 7 additions & 1 deletion src/blockchain-settings-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,17 @@ function updateNotificationsType (types) {
return acc | n;
});

var success = function (result) {
WalletStore.setSyncPubKeys(payload !== 0);
MyWallet.syncWallet();
return result;
};

return API.securePost('wallet', {
method: 'update-notifications-type',
length: String(payload).length,
payload: payload
});
}).then(success);
}

function updateNotificationsOn (on) {
Expand Down
24 changes: 2 additions & 22 deletions src/blockchain-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,33 +633,13 @@ Wallet.prototype.scanBip44 = function (secondPassword, progress) {
Wallet.prototype.enableNotifications = function (success, error) {
assert(success, 'Success callback required');
assert(error, 'Error callback required');

BlockchainSettingsAPI.enableEmailReceiveNotifications(
function () {
WalletStore.setSyncPubKeys(true);
MyWallet.syncWallet();
success();
},
function () {
error();
}
);
BlockchainSettingsAPI.enableEmailReceiveNotifications(success, error);
};

Wallet.prototype.disableNotifications = function (success, error) {
assert(success, 'Success callback required');
assert(error, 'Error callback required');

BlockchainSettingsAPI.disableAllNotifications(
function () {
WalletStore.setSyncPubKeys(false);
MyWallet.syncWallet();
success();
},
function () {
error();
}
);
BlockchainSettingsAPI.disableAllNotifications(success, error);
};

// creating a new wallet object
Expand Down
47 changes: 39 additions & 8 deletions tests/blockchain_settings_api_spec.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@ MyWallet =
accountInfo:
email: "a@b.com"
isEmailVerified: false
syncWallet: ->

API =
callFailWithResponseText: false
callFailWithoutResponseText: false
securePost: (endpoint, data) ->
then: (success, error) ->
if API.callFailWithoutResponseText
error('call failed')
else if API.callFailWithResponseText
error({responseText: 'call failed'})
else
success('call succeeded')
catch: () ->
promise =
then: (success, error) ->
if API.callFailWithoutResponseText
error('call failed')
else if API.callFailWithResponseText
error({responseText: 'call failed'})
else
success('call succeeded')
catch: ->
if data.method == 'update-notifications-type'
then: (success) ->
if !API.callFailWithoutResponseText && !API.callFailWithResponseText
success()
promise
else
return promise
securePostCallbacks: (endpoint, data, success, error) ->
if API.callFailWithoutResponseText
error('call failed')
Expand All @@ -33,6 +42,7 @@ WalletStore =
sendEvent: () ->
getPassword: () -> "password"
setRealAuthType: () ->
setSyncPubKeys: ->

AccountInfo = {
email: "a@b.com"
Expand Down Expand Up @@ -342,11 +352,32 @@ describe "SettingsAPI", ->
payload: 33
}]

beforeEach ->
spyOn(MyWallet, 'syncWallet')
spyOn(WalletStore, 'setSyncPubKeys')

vectors.forEach (v) ->
it "should send #{v.payload} when email is #{v.email} and sms is #{v.sms}", ->
SettingsAPI.updateNotificationsType.apply(null, v.args)
expect(API.securePost).toHaveBeenCalledWith('wallet', { method: method, length: v.length, payload: v.payload })

it "should call syncWallet if successful", ->
SettingsAPI.updateNotificationsType({})
expect(MyWallet.syncWallet).toHaveBeenCalled()

it "should not call syncWallet if unsuccessful", ->
API.callFailWithoutResponseText = true
SettingsAPI.updateNotificationsType({})
expect(MyWallet.syncWallet).not.toHaveBeenCalled()

it "should set syncPubKeys to true if enabling notifications", ->
SettingsAPI.updateNotificationsType({ email: true })
expect(WalletStore.setSyncPubKeys).toHaveBeenCalledWith(true)

it "should set syncPubKeys to false if disabling notifications", ->
SettingsAPI.updateNotificationsType({})
expect(WalletStore.setSyncPubKeys).toHaveBeenCalledWith(false)

describe "updateNotificationsOn", ->
method = 'update-notifications-on'

Expand Down
30 changes: 0 additions & 30 deletions tests/blockchain_wallet_spec.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -768,39 +768,9 @@ describe "Blockchain-Wallet", ->
expect(() -> wallet.enableNotifications(cb.success)).toThrow()
expect(() -> wallet.enableNotifications(cb.success, cb.error)).not.toThrow()

it "should call setSyncPubKeys and syncWallet if successful", ->
wallet.enableNotifications(cb.success, cb.error)
expect(MyWallet.syncWallet).toHaveBeenCalled()
expect(WalletStore.setSyncPubKeys).toHaveBeenCalledWith(true)
expect(cb.success).toHaveBeenCalled()
expect(cb.error).not.toHaveBeenCalled()

it "should not call setSyncPubKeys and syncWallet if successful", ->
BlockchainSettingsAPI.shouldFail = true
wallet.enableNotifications(cb.success, cb.error)
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
expect(WalletStore.setSyncPubKeys).not.toHaveBeenCalled()
expect(cb.success).not.toHaveBeenCalled()
expect(cb.error).toHaveBeenCalled()

describe ".disableNotifications", ->

it "should require success and error callbacks", ->
expect(() -> wallet.disableNotifications()).toThrow()
expect(() -> wallet.disableNotifications(cb.success)).toThrow()
expect(() -> wallet.disableNotifications(cb.success, cb.error)).not.toThrow()

it "should call setSyncPubKeys and syncWallet if successful", ->
wallet.disableNotifications(cb.success, cb.error)
expect(MyWallet.syncWallet).toHaveBeenCalled()
expect(WalletStore.setSyncPubKeys).toHaveBeenCalledWith(false)
expect(cb.success).toHaveBeenCalled()
expect(cb.error).not.toHaveBeenCalled()

it "should not call setSyncPubKeys and syncWallet if successful", ->
BlockchainSettingsAPI.shouldFail = true
wallet.disableNotifications(cb.success, cb.error)
expect(MyWallet.syncWallet).not.toHaveBeenCalled()
expect(WalletStore.setSyncPubKeys).not.toHaveBeenCalled()
expect(cb.success).not.toHaveBeenCalled()
expect(cb.error).toHaveBeenCalled()

0 comments on commit 9d58da3

Please sign in to comment.