Skip to content
Snippets Groups Projects
Commit ac329860 authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

fix: onDelete account retrieve

parent c694b19b
No related branches found
No related tags found
1 merge request!17fix/US846-onDelete
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)
})
})
const { cozyClient } = require('cozy-konnector-libs') 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() const mockUpdateOrCreate = jest.fn()
...@@ -36,6 +40,39 @@ describe('getAccount', () => { ...@@ -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', () => { describe('saveAccountData', () => {
jest.mock('cozy-konnector-libs', () => ({ jest.mock('cozy-konnector-libs', () => ({
......
{ {
"name": "enedis-sge-konnector", "name": "enedissgekonnector",
"version": "1.0.0", "version": "1.0.1",
"description": "", "description": "",
"repository": { "repository": {
"type": "https", "type": "https",
......
...@@ -12,6 +12,7 @@ function getAccountId() { ...@@ -12,6 +12,7 @@ function getAccountId() {
function getAccountRev() { function getAccountRev() {
log('info', `getAccountRev`) log('info', `getAccountRev`)
log('info', `getAccountRev: ${JSON.stringify(process.env.COZY_FIELDS)}`)
try { try {
return isLocal() return isLocal()
? 'fakeAccountRev' ? 'fakeAccountRev'
...@@ -27,6 +28,7 @@ function getAccountRev() { ...@@ -27,6 +28,7 @@ function getAccountRev() {
* @returns {Fields} * @returns {Fields}
*/ */
function getAccountSecret() { function getAccountSecret() {
log('info', `getAccountSecret`)
try { try {
return isLocal() return isLocal()
? JSON.parse(process.env.COZY_FIELDS) ? JSON.parse(process.env.COZY_FIELDS)
......
...@@ -6,4 +6,12 @@ function isLocal() { ...@@ -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 }
// @ts-check // @ts-check
const { log, errors } = require('cozy-konnector-libs') 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 { getBoConsent, deleteBoConsent } = require('./requests/bo')
const { terminateContract } = require('./core/contractTermination') const { terminateContract } = require('./core/contractTermination')
const { getAccount } = require('./requests/cozy') const { getAccountForDelete } = require('./requests/cozy')
const moment = require('moment') const moment = require('moment')
require('moment-timezone') require('moment-timezone')
moment.locale('fr') // set the language moment.locale('fr') // set the language
moment.tz.setDefault('Europe/Paris') // set the timezone moment.tz.setDefault('Europe/Paris') // set the timezone
const { isLocal } = require('./helpers/env') const { isLocal, isAlpha } = require('./helpers/env')
const ACCOUNT_ID = isLocal() ? 'default_account_id' : 'enedis-sge-grandlyon' // const ACCOUNT_ID = isLocal() ? 'default_account_id' : 'enedis-sge-grandlyon'
async function onDeleteAccount() { async function onDeleteAccount() {
log('info', 'Deleting account ...') log('info', 'Deleting account ...')
log('info', 'Getting secrets ...') log('info', 'Getting secrets ...')
const ACCOUNT_ID = getAccountId()
const accountRev = getAccountRev() const accountRev = getAccountRev()
if (accountRev) { if (accountRev) {
log('info', 'Account rev exist') log('info', 'Account rev exist')
const accountData = await getAccount(ACCOUNT_ID) const accountData = await getAccountForDelete(ACCOUNT_ID, accountRev)
// Parse local info for deletion test // Parse local info for deletion test
if (isLocal()) { if (isLocal()) {
log('warn', 'Local run')
const fields = JSON.parse( const fields = JSON.parse(
process.env.COZY_FIELDS ? process.env.COZY_FIELDS : '{}' process.env.COZY_FIELDS ? process.env.COZY_FIELDS : '{}'
) )
...@@ -37,6 +42,8 @@ async function onDeleteAccount() { ...@@ -37,6 +42,8 @@ async function onDeleteAccount() {
accountData.data.consentId accountData.data.consentId
) )
log('info', `isAlpha: ${isAlpha()}`)
log('info', `userConsent: ${JSON.stringify(userConsent)}`)
if (userConsent.ID && userConsent.pointID) { if (userConsent.ID && userConsent.pointID) {
log('log', `Consent ${userConsent.ID} found for user`) log('log', `Consent ${userConsent.ID} found for user`)
if (userConsent.serviceID) { if (userConsent.serviceID) {
...@@ -45,14 +52,17 @@ async function onDeleteAccount() { ...@@ -45,14 +52,17 @@ async function onDeleteAccount() {
secrets.boToken, secrets.boToken,
userConsent.ID userConsent.ID
) )
await terminateContract( // Verify if it's dev env to prevent delete of real data
secrets.wso2BaseUrl, if (!isAlpha()) {
secrets.apiToken, await terminateContract(
secrets.sgeLogin, secrets.wso2BaseUrl,
secrets.contractId, secrets.apiToken,
userConsent.pointID, secrets.sgeLogin,
userConsent.serviceID secrets.contractId,
) userConsent.pointID,
userConsent.serviceID
)
}
} else { } else {
log('error', `No service id retrieved from BO`) log('error', `No service id retrieved from BO`)
throw errors.VENDOR_DOWN throw errors.VENDOR_DOWN
......
...@@ -27,4 +27,15 @@ async function getAccount(accountId) { ...@@ -27,4 +27,15 @@ async function getAccount(accountId) {
)[0] )[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 }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment