diff --git a/__tests__/helpers/env.spec.js b/__tests__/helpers/env.spec.js new file mode 100644 index 0000000000000000000000000000000000000000..97161504b404dce04798658f2c5c8b26ce8b63e7 --- /dev/null +++ b/__tests__/helpers/env.spec.js @@ -0,0 +1,32 @@ +const { isAlpha } = require('../../src/helpers/env') +describe('isAlpha', () => { + const OLD_ENV = process.env + + beforeEach(() => { + jest.resetModules() // Most important - it clears the cache + process.env = { ...OLD_ENV } // Make a copy + }) + + afterAll(() => { + process.env = OLD_ENV // Restore old environment + }) + + it('should return false for local', () => { + // Set the variables + process.env.COZY_URL = 'http://cozy.tools:8080' + const reply = isAlpha() + expect(reply).toBe(false) + }) + it('should return false for prod URL', () => { + // Set the variables + process.env.COZY_URL = 'https://pouet-ecolyo.cozygrandlyon.cloud/' + const reply = isAlpha() + expect(reply).toBe(false) + }) + it('should return true for alpha', () => { + // Set the variables + process.env.COZY_URL = 'https://pouet.cozy.self-data.alpha.grandlyon.com/' + const reply = isAlpha() + expect(reply).toBe(true) + }) +}) diff --git a/__tests__/requests/cozy.spec.js b/__tests__/requests/cozy.spec.js index ab0b4aa303cbe7bdd973508cc49b19b5a5b9be82..22109364ac9f00b37b87fdc026f3cc3381475aaa 100644 --- a/__tests__/requests/cozy.spec.js +++ b/__tests__/requests/cozy.spec.js @@ -1,5 +1,9 @@ const { cozyClient } = require('cozy-konnector-libs') -const { getAccount, saveAccountData } = require('../../src/requests/cozy') +const { + getAccount, + saveAccountData, + getAccountForDelete, +} = require('../../src/requests/cozy') const mockUpdateOrCreate = jest.fn() @@ -36,6 +40,39 @@ describe('getAccount', () => { }) }) }) +describe('getAccountForDelete', () => { + it('should find account with provided ID', async () => { + const spy = jest.spyOn(cozyClient, 'fetchJSON') + spy.mockResolvedValueOnce( + { + _id: '123456', + account_type: '123456', + auth: { + address: '12 rue du pouet', + city: 'Lyon', + firstname: 'Jean', + lastname: 'POUET', + pointId: '1234567891234567', + postalCode: '69007', + }, + }, + { _id: '1111111', account_type: '1111111' } + ) + const account = await getAccountForDelete('123456', '789456') + expect(account).toEqual({ + _id: '123456', + account_type: '123456', + auth: { + address: '12 rue du pouet', + city: 'Lyon', + firstname: 'Jean', + lastname: 'POUET', + pointId: '1234567891234567', + postalCode: '69007', + }, + }) + }) +}) describe('saveAccountData', () => { jest.mock('cozy-konnector-libs', () => ({ diff --git a/src/helpers/account.js b/src/helpers/account.js index 0982c1373da24af56717ca08408695c41138f80e..f92e1294aa0a4729e6b3a4bdf7ccc43d784034d6 100644 --- a/src/helpers/account.js +++ b/src/helpers/account.js @@ -12,6 +12,7 @@ function getAccountId() { function getAccountRev() { log('info', `getAccountRev`) + log('info', `getAccountRev: ${JSON.stringify(process.env.COZY_FIELDS)}`) try { return isLocal() ? 'fakeAccountRev' @@ -27,6 +28,7 @@ function getAccountRev() { * @returns {Fields} */ function getAccountSecret() { + log('info', `getAccountSecret`) try { return isLocal() ? JSON.parse(process.env.COZY_FIELDS) diff --git a/src/helpers/env.js b/src/helpers/env.js index 07df4c667829ca46efa56b9dfb23e1f1b93b3b53..9e801c4fbf1aeae7160ec1f1389eada5d4247f2e 100644 --- a/src/helpers/env.js +++ b/src/helpers/env.js @@ -6,4 +6,12 @@ function isLocal() { ) } -module.exports = { isLocal } +/** + * Verify if it's an alpha URL + * @returns {boolean} + */ +function isAlpha() { + return process.env.COZY_URL.includes('alpha') +} + +module.exports = { isLocal, isAlpha } diff --git a/src/onDeleteAccount.js b/src/onDeleteAccount.js index dd4acbef25935179e4578670b85abc6f90b7350b..b0233ee4b828da66cd5c8d2e10d29d9c73909604 100644 --- a/src/onDeleteAccount.js +++ b/src/onDeleteAccount.js @@ -1,27 +1,32 @@ // @ts-check const { log, errors } = require('cozy-konnector-libs') -const { getAccountRev, getAccountSecret } = require('./helpers/account') +const { + getAccountRev, + getAccountSecret, + getAccountId, +} = require('./helpers/account') const { getBoConsent, deleteBoConsent } = require('./requests/bo') const { terminateContract } = require('./core/contractTermination') -const { getAccount } = require('./requests/cozy') +const { getAccountForDelete } = require('./requests/cozy') const moment = require('moment') require('moment-timezone') moment.locale('fr') // set the language moment.tz.setDefault('Europe/Paris') // set the timezone -const { isLocal } = require('./helpers/env') -const ACCOUNT_ID = isLocal() ? 'default_account_id' : 'enedis-sge-grandlyon' +const { isLocal, isAlpha } = require('./helpers/env') +// const ACCOUNT_ID = isLocal() ? 'default_account_id' : 'enedis-sge-grandlyon' async function onDeleteAccount() { log('info', 'Deleting account ...') log('info', 'Getting secrets ...') - + const ACCOUNT_ID = getAccountId() const accountRev = getAccountRev() if (accountRev) { log('info', 'Account rev exist') - const accountData = await getAccount(ACCOUNT_ID) + const accountData = await getAccountForDelete(ACCOUNT_ID, accountRev) // Parse local info for deletion test if (isLocal()) { + log('warn', 'Local run') const fields = JSON.parse( process.env.COZY_FIELDS ? process.env.COZY_FIELDS : '{}' ) @@ -37,6 +42,8 @@ async function onDeleteAccount() { accountData.data.consentId ) + log('info', `isAlpha: ${isAlpha()}`) + log('info', `userConsent: ${JSON.stringify(userConsent)}`) if (userConsent.ID && userConsent.pointID) { log('log', `Consent ${userConsent.ID} found for user`) if (userConsent.serviceID) { @@ -45,14 +52,17 @@ async function onDeleteAccount() { secrets.boToken, userConsent.ID ) - await terminateContract( - secrets.wso2BaseUrl, - secrets.apiToken, - secrets.sgeLogin, - secrets.contractId, - userConsent.pointID, - userConsent.serviceID - ) + // Verify if it's dev env to prevent delete of real data + if (!isAlpha()) { + await terminateContract( + secrets.wso2BaseUrl, + secrets.apiToken, + secrets.sgeLogin, + secrets.contractId, + userConsent.pointID, + userConsent.serviceID + ) + } } else { log('error', `No service id retrieved from BO`) throw errors.VENDOR_DOWN diff --git a/src/requests/cozy.js b/src/requests/cozy.js index 138c822a8de790c029b1879ae59bbdca053d151d..563dc48eee9e3115504bc3667f20c73a5a35be6c 100644 --- a/src/requests/cozy.js +++ b/src/requests/cozy.js @@ -22,4 +22,15 @@ async function getAccount(accountId) { )[0] } -module.exports = { getAccount, saveAccountData } +async function getAccountForDelete(accountId, accountRev) { + log('info', `getAccountForDelete: ${accountId} ${accountRev}`) + const body = await cozyClient.fetchJSON( + 'GET', + `/data/io.cozy.accounts/${accountId}?rev=${accountRev}` + ) + + log('debug', `getAccountForDelete: ${body}`) + return body +} + +module.exports = { getAccount, saveAccountData, getAccountForDelete }