From 7e1e3c05cc725729228c5b890e5914ef117ee452 Mon Sep 17 00:00:00 2001 From: ryanml Date: Mon, 16 Jul 2018 14:56:51 -0700 Subject: [PATCH] Fixes #14758, deletes publisher-data.json when wallet is deleted --- app/browser/api/ledger.js | 27 ++- test/unit/app/browser/api/ledgerTest.js | 231 +++++++++++++++--------- 2 files changed, 162 insertions(+), 96 deletions(-) diff --git a/app/browser/api/ledger.js b/app/browser/api/ledger.js index afd2fed00d6..1d31c6008e3 100644 --- a/app/browser/api/ledger.js +++ b/app/browser/api/ledger.js @@ -3273,24 +3273,32 @@ const disablePayments = () => { const deleteWallet = (state) => { state = ledgerState.deleteSynopsis(state) state = state.setIn(['settings', settings.PAYMENTS_ENABLED], false) + state = updateState.setUpdateProp(state, 'verifiedPublishersTimestamp', null) client = null synopsis = null + module.exports.deletePaymentsFiles() + return state +} + +const deletePaymentsFiles = () => { const fs = require('fs') - fs.access(pathName(statePath), fs.constants.F_OK, (err) => { - if (err) { - return - } + const paths = [statePath, publisherInfoPath] - fs.unlink(pathName(statePath), (err) => { + paths.forEach((path) => { + fs.access(pathName(path), fs.constants.F_OK, (err) => { if (err) { - return console.error('read error: ' + err.toString()) + return } + + fs.unlink(pathName(path), (err) => { + if (err) { + return console.error('read error: ' + err.toString()) + } + }) }) }) - - return state } const clearPaymentHistory = (state) => { @@ -3385,7 +3393,8 @@ const getMethods = () => { getPublisherInfo, checkPublisherInfoUpdate, updatePublishersInfo, - runPublishersUpdate + runPublishersUpdate, + deletePaymentsFiles } let privateMethods = {} diff --git a/test/unit/app/browser/api/ledgerTest.js b/test/unit/app/browser/api/ledgerTest.js index 0d6b6bb36e0..dd76362c77e 100644 --- a/test/unit/app/browser/api/ledgerTest.js +++ b/test/unit/app/browser/api/ledgerTest.js @@ -3639,100 +3639,113 @@ describe('ledger api unit tests', function () { }) describe('deleteWallet', function () { - it('data is cleared', function () { - const state = defaultAppState - .setIn(['cache', 'ledgerVideos'], Immutable.fromJS({ - 'youtube_Ece3i74Wces': 'youtube#channel:radio1slovenia' - })) - .setIn(['settings', 'payments.enabled'], true) - .set('pageData', Immutable.fromJS({ - info: { - 'https://www.youtube.com/user/radio1slovenia/videos': { - faviconURL: 'https://s.ytimg.com/yts/img/favicon_32-vflOogEID.png', - key: 'https://www.youtube.com/user/radio1slovenia/videos', - protocol: 'https:', - publisher: 'youtube.com', - timestamp: 1526367684155, - url: 'https://www.youtube.com/user/radio1slovenia/videos' - } - }, - last: { - closedTabValue: { - audible: false, - width: 2560, - active: true - }, - info: '', - tabId: '7' + let deletePaymentsFilesSpy + + const state = defaultAppState + .setIn(['cache', 'ledgerVideos'], Immutable.fromJS({ + 'youtube_Ece3i74Wces': 'youtube#channel:radio1slovenia' + })) + .setIn(['settings', 'payments.enabled'], true) + .set('pageData', Immutable.fromJS({ + info: { + 'https://www.youtube.com/user/radio1slovenia/videos': { + faviconURL: 'https://s.ytimg.com/yts/img/favicon_32-vflOogEID.png', + key: 'https://www.youtube.com/user/radio1slovenia/videos', + protocol: 'https:', + publisher: 'youtube.com', + timestamp: 1526367684155, + url: 'https://www.youtube.com/user/radio1slovenia/videos' } - })) - .set('ledger', Immutable.fromJS({ - about: { - synopsis: [ - { - daysSpent: 0, - duration: 166431, - exclude: false, - faviconURL: 'data:image/jpeg;base64', - hoursSpent: 0, - minutesSpent: 2, - percentage: 38, - pinPercentage: undefined, - providerName: 'YouTube', - publisherKey: 'youtube#channel:radio1slovenia', - publisherURL: 'https://www.youtube.com/user/radio1slovenia/videos', - score: 14.588460435541956, - secondsSpent: 46, - siteName: 'radio1slovenia on YouTube', - verified: false, - views: 2, - weight: 38.244594657485045 - } - ], - synopsisOptions: { - _a: 7000, - _b: 1000, - scorekeeper: 'concave', - _d: 0.000033333333333333335 - } - }, - info: { - balance: 0, - paymentId: 'ladasda' + }, + last: { + closedTabValue: { + audible: false, + width: 2560, + active: true }, - locations: { - 'https://www.youtube.com/user/radio1slovenia/videos': { - publisher: 'youtube.com' + info: '', + tabId: '7' + } + })) + .set('ledger', Immutable.fromJS({ + about: { + synopsis: [ + { + daysSpent: 0, + duration: 166431, + exclude: false, + faviconURL: 'data:image/jpeg;base64', + hoursSpent: 0, + minutesSpent: 2, + percentage: 38, + pinPercentage: undefined, + providerName: 'YouTube', + publisherKey: 'youtube#channel:radio1slovenia', + publisherURL: 'https://www.youtube.com/user/radio1slovenia/videos', + score: 14.588460435541956, + secondsSpent: 46, + siteName: 'radio1slovenia on YouTube', + verified: false, + views: 2, + weight: 38.244594657485045 } + ], + synopsisOptions: { + _a: 7000, + _b: 1000, + scorekeeper: 'concave', + _d: 0.000033333333333333335 + } + }, + info: { + balance: 0, + paymentId: 'ladasda' + }, + locations: { + 'https://www.youtube.com/user/radio1slovenia/videos': { + publisher: 'youtube.com' + } + }, + synopsis: { + options: { + _a: 7000, + _b: 1000, + scorekeeper: 'concave', + _d: 0.000033333333333333335 }, - synopsis: { - options: { - _a: 7000, - _b: 1000, - scorekeeper: 'concave', - _d: 0.000033333333333333335 - }, - publishers: { - 'youtube#channel:radio1slovenia': { - duration: 166431, - options: { - exclude: false - }, - pinPercentage: 20, - scores: { - concave: 3.249426617127623, - visits: 2 - }, - views: 2, - weight: 20 - } + publishers: { + 'youtube#channel:radio1slovenia': { + duration: 166431, + options: { + exclude: false + }, + pinPercentage: 20, + scores: { + concave: 3.249426617127623, + visits: 2 + }, + views: 2, + weight: 20 } - }, - promotion: {} - })) + } + }, + promotion: {} + })) - const result = ledgerApi.deleteWallet(state) + before(function () { + deletePaymentsFilesSpy = sinon.spy(ledgerApi, 'deletePaymentsFiles') + }) + + afterEach(function () { + deletePaymentsFilesSpy.reset() + }) + + after(function () { + deletePaymentsFilesSpy.restore() + }) + it('data is cleared', function () { + const result = ledgerApi.deleteWallet(state) const expectedState = state .set('ledger', Immutable.fromJS({ about: { @@ -3753,11 +3766,55 @@ describe('ledger api unit tests', function () { .setIn(['pageData', 'last', 'tabId'], null) .setIn(['pageData', 'last', 'closedTabValue'], null) .setIn(['settings', 'payments.enabled'], false) + .setIn(['updates', 'verifiedPublishersTimestamp'], null) assert.deepEqual(result.toJS(), expectedState.toJS()) assert.equal(ledgerApi.getClient(), null) assert.equal(ledgerApi.getSynopsis(), null) }) + + it('calls function to delete payments files', function () { + ledgerApi.deleteWallet(state) + assert(deletePaymentsFilesSpy.calledOnce) + }) + }) + + describe('deletePaymentsFiles', function () { + let accessStub, unlinkStub + const statePath = 'ledger-state.json' + const publishersInfoPath = 'publisher-data.json' + + before(function () { + unlinkStub = sinon.stub(fs, 'unlink', function (path, callback) { + callback(null) + }) + accessStub = sinon.stub(fs, 'access', function (path, fOk, callback) { + fs.unlink(path, function (err) { + // Satisfies the linter + if (err) { + console.log('Something went wrong') + } + }) + }) + }) + + afterEach(function () { + accessStub.reset() + unlinkStub.reset() + }) + + after(function () { + accessStub.restore() + unlinkStub.restore() + }) + + it('accesses and deletes ledger state and publisher data files', function () { + ledgerApi.deletePaymentsFiles() + assert.equal(ledgerApi.pathName(statePath), accessStub.getCall(0).args[0]) + assert.equal(ledgerApi.pathName(statePath), unlinkStub.getCall(0).args[0]) + assert.equal(ledgerApi.pathName(publishersInfoPath), accessStub.getCall(1).args[0]) + assert.equal(ledgerApi.pathName(publishersInfoPath), unlinkStub.getCall(1).args[0]) + }) }) describe('clearPaymentHistory', function () {