Commit fb337543 authored by Antoine's avatar Antoine
Browse files

Change 'enabled=false' status to 'password=null'. Fixes #808.

parent cba3f09a
......@@ -127,7 +127,7 @@ export async function fetchOperations(req, res) {
let access = req.preloaded.access;
let bankVendor = bankVendorByUuid(access.bank);
if (!access.enabled || bankVendor.deprecated) {
if (!access.isEnabled() || bankVendor.deprecated) {
let errcode = getErrorCode('DISABLED_ACCESS');
throw new KError('disabled access', 403, errcode);
}
......@@ -154,7 +154,7 @@ export async function fetchAccounts(req, res) {
let access = req.preloaded.access;
let bankVendor = bankVendorByUuid(access.bank);
if (!access.enabled || bankVendor.deprecated) {
if (!access.isEnabled() || bankVendor.deprecated) {
let errcode = getErrorCode('DISABLED_ACCESS');
throw new KError('disabled access', 403, errcode);
}
......
......@@ -33,8 +33,15 @@ async function getAllData(userId, isExport = false, cleanPassword = true) {
ret.accounts = await Accounts.all(userId);
ret.accesses = await Accesses.all(userId);
if (cleanPassword) {
ret.accesses.forEach(access => delete access.password);
for (let access of ret.accesses) {
// Process enabled status only for the /all request.
if (!isExport) {
access.enabled = access.isEnabled();
}
if (cleanPassword) {
delete access.password;
}
}
ret.categories = await Categories.all(userId);
......@@ -226,11 +233,6 @@ export async function import_(req, res) {
let accessId = access.id;
delete access.id;
// An access without password should be disabled by default.
if (!access.password) {
access.enabled = false;
}
let created = await Accesses.create(userId, access);
accessMap[accessId] = created.id;
......
......@@ -84,18 +84,13 @@ export async function fullPoll(userId) {
// Update the repos only once.
needUpdate = false;
await accountManager.retrieveOperationsByAccess(userId, access);
} else if (!access.isEnabled()) {
log.info(`Won't poll, access from bank ${bank} with login ${login} is disabled.`);
} else {
let { enabled } = access;
if (!enabled) {
log.info(
`Won't poll, access from bank ${bank} with login ${login} is disabled.`
);
} else {
let error = access.fetchStatus;
log.info(
`Won't poll, access from bank ${bank} with login ${login} last fetch raised: ${error}.`
);
}
let error = access.fetchStatus;
log.info(
`Won't poll, access from bank ${bank} with login ${login} last fetch raised: ${error}.`
);
}
} catch (err) {
log.error(`Error when polling accounts: ${err.message}\n`, err);
......
......@@ -24,12 +24,6 @@ let Access = cozydb.getModel('bankaccess', {
default: 'OK'
},
// Boolean indicating if the access is enabled or not.
enabled: {
type: Boolean,
default: true
},
// Text label set by the user.
customLabel: {
type: String,
......@@ -39,7 +33,8 @@ let Access = cozydb.getModel('bankaccess', {
// ************************************************************************
// DEPRECATED.
// ************************************************************************
website: String
website: String,
enabled: Boolean
});
Access = promisifyModel(Access);
......@@ -103,10 +98,15 @@ Access.prototype.hasPassword = function() {
return typeof this.password === 'string' && this.password.length > 0;
};
// Is the access enabled
Access.prototype.isEnabled = function() {
return this.password !== null;
};
// Can the access be polled
Access.prototype.canBePolled = function() {
return (
this.enabled &&
this.isEnabled() &&
this.fetchStatus !== 'INVALID_PASSWORD' &&
this.fetchStatus !== 'EXPIRED_PASSWORD' &&
this.fetchStatus !== 'INVALID_PARAMETERS' &&
......
......@@ -800,7 +800,26 @@ let migrations = [
},
// m23: rename Accounts.initialAmount to initialBalance.
makeRenameField(Accounts, 'initialAmount', 'initialBalance')
makeRenameField(Accounts, 'initialAmount', 'initialBalance'),
async function m24(userId) {
try {
log.info("Deleting 'enabled' flag for accesses.");
let accesses = await Accesses.all(userId);
for (let access of accesses) {
let newFields = {};
if (typeof access.enabled !== 'undefined' && !access.enabled) {
newFields.password = null;
}
newFields.enabled = null;
await Accesses.update(userId, access.id, newFields);
}
return true;
} catch (e) {
log.error('Error while deleting enabled flag for accesses:', e.toString());
return false;
}
}
];
export const testing = { migrations };
......
......@@ -1510,3 +1510,63 @@ describe('Test migration 23', async function() {
should.not.exist(result.initialAmount);
});
});
describe('Test migration 24', async function() {
before(async function() {
await clear(Accesses);
});
const enabledAccess = {
login: 'login',
password: 'password',
enabled: true
};
const disabledAccess = {
login: 'login2',
password: 'password2',
enabled: false
};
const enabledAccessWithoutStatus = {
login: 'login3',
password: 'password3'
};
let enabledAccessId, disabledAccessId, enabledAccessWithoutStatusId;
it('The accesses should be added in the database.', async function() {
enabledAccessId = (await Accesses.create(0, enabledAccess)).id;
disabledAccessId = (await Accesses.create(0, disabledAccess)).id;
enabledAccessWithoutStatusId = (await Accesses.create(0, enabledAccessWithoutStatus)).id;
(await Accesses.all(0)).should.containDeep([
enabledAccess,
disabledAccess,
enabledAccessWithoutStatus
]);
});
it('should run migration m24 properly', async function() {
let m24 = MIGRATIONS[24];
let result = await m24(0);
result.should.equal(true);
});
it('The enabled status should be set to null', async function() {
let newEnabledAccess = await Accesses.find(0, enabledAccessId);
should.equal(newEnabledAccess.enabled, null);
newEnabledAccess.login.should.equal(enabledAccess.login);
newEnabledAccess.password.should.equal(enabledAccess.password);
let newDisabledAccess = await Accesses.find(0, disabledAccessId);
should.equal(newDisabledAccess.enabled, null);
newDisabledAccess.login.should.equal(disabledAccess.login);
should.equal(newDisabledAccess.password, null);
let newEnabledAccessWithoutStatus = await Accesses.find(0, enabledAccessWithoutStatusId);
should.equal(newEnabledAccessWithoutStatus.enabled, null);
newEnabledAccessWithoutStatus.login.should.equal(enabledAccessWithoutStatus.login);
newEnabledAccessWithoutStatus.password.should.equal(enabledAccessWithoutStatus.password);
});
});
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment