From ac3298605596a7c731b901b2b0dcfeb77769d32b Mon Sep 17 00:00:00 2001 From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com> Date: Mon, 3 Oct 2022 06:54:18 +0000 Subject: [PATCH] fix: onDelete account retrieve --- __tests__/helpers/env.spec.js | 32 +++++++++++++++++++++++++++ __tests__/requests/cozy.spec.js | 39 ++++++++++++++++++++++++++++++++- package.json | 4 ++-- src/helpers/account.js | 2 ++ src/helpers/env.js | 10 ++++++++- src/onDeleteAccount.js | 38 ++++++++++++++++++++------------ src/requests/cozy.js | 13 ++++++++++- 7 files changed, 119 insertions(+), 19 deletions(-) create mode 100644 __tests__/helpers/env.spec.js diff --git a/__tests__/helpers/env.spec.js b/__tests__/helpers/env.spec.js new file mode 100644 index 0000000..9716150 --- /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 ab0b4aa..2210936 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/package.json b/package.json index f767edb..3bab086 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "enedis-sge-konnector", - "version": "1.0.0", + "name": "enedissgekonnector", + "version": "1.0.1", "description": "", "repository": { "type": "https", diff --git a/src/helpers/account.js b/src/helpers/account.js index 0982c13..f92e129 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 07df4c6..9e801c4 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 dd4acbe..b0233ee 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 5bbb86b..6e99ea0 100644 --- a/src/requests/cozy.js +++ b/src/requests/cozy.js @@ -27,4 +27,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 } -- GitLab