#6744 fix worker setPassword #2027

Merged
jorgep merged 18 commits from 6744-fixWorkerSetPassword into dev 2024-03-15 09:48:14 +00:00
3 changed files with 66 additions and 31 deletions
Showing only changes of commit 9f2768c131 - Show all commits

View File

@ -33,11 +33,11 @@ module.exports = Self => {
}
try {
jorgep marked this conversation as resolved Outdated
Outdated
Review

Lanzar ForbiddenError indicando en el mensaje que no es subordinado.

Lanzar ForbiddenError indicando en el mensaje que no es subordinado.
const ishimself = userId === workerId;
const isHimself = userId === workerId;
jorgep marked this conversation as resolved Outdated

isHimself

isHimself
Outdated
Review

No pondría aquí isHimself, para cambiarse la contraseña uno mismo que se utilice el método tradicional que yahace las comprobaciones de seguridad correspondientes

No pondría aquí `isHimself`, para cambiarse la contraseña uno mismo que se utilice el método tradicional que yahace las comprobaciones de seguridad correspondientes
const isSubordinate = await Self.isSubordinate(ctx, workerId, myOptions);
jorgep marked this conversation as resolved
Review

no podemos poner aquí el contenido de setUnverifiedPassword?
es necesario crear ese método?

no podemos poner aquí el contenido de setUnverifiedPassword? es necesario crear ese método?
Review

me lo pidio exprasemente @juan

me lo pidio exprasemente @juan
const {emailVerified} = await models.VnUser.findById(workerId, {fields: ['emailVerified']}, myOptions);
if (ishimself || (isSubordinate && !emailVerified)) {
if (isHimself || (isSubordinate && !emailVerified)) {
await models.VnUser.setPassword(workerId, newPass, myOptions);
await models.VnUser.updateAll({id: workerId}, {emailVerified: true}, myOptions);
} else

View File

@ -1,31 +1,30 @@
const UserError = require('vn-loopback/util/user-error');
const models = require('vn-loopback/server/server').models;
const {models} = require('vn-loopback/server/server');
describe('worker setPassword()', () => {
let ctx;
const newPass = 'H3rn4d3z#';
const employeeId = 1;
const managerId = 20;
const administrativeId = 5;
beforeAll(() => {
ctx = {
req: {
accessToken: {},
accessToken: {userId: managerId},
headers: {origin: 'http://localhost'}
},
args: {workerFk: 9}
};
});
beforeEach(() => {
ctx.req.accessToken.userId = 20;
ctx.args.newPass = 'H3rn4d3z#';
});
it('should change the password', async() => {
it('should change the password if it is a subordinate and the email is not verified', async() => {
const tx = await models.Worker.beginTransaction({});
try {
const options = {transaction: tx};
await models.Worker.setPassword(ctx, options);
await models.Worker.setPassword(ctx, employeeId, newPass, options);
const isNewPass = await passHasBeenChanged(employeeId, newPass, options);
expect(isNewPass).toBeTrue();
await tx.rollback();
} catch (e) {
await tx.rollback();
@ -33,29 +32,64 @@ describe('worker setPassword()', () => {
}
});
it('should throw an error: Password does not meet requirements', async() => {
const tx = await models.Collection.beginTransaction({});
ctx.args.newPass = 'Hi';
it('should not change the password if it is a subordinate and the email is verified', async() => {
const tx = await models.Worker.beginTransaction({});
try {
const options = {transaction: tx};
await models.Worker.setPassword(ctx, options);
await models.VnUser.updateAll({id: employeeId}, {emailVerified: true}, options);
await models.Worker.setPassword(ctx, employeeId, newPass, options);
await tx.rollback();
} catch (e) {
expect(e.message).toEqual(`You don't have enough privileges.`);
await tx.rollback();
}
});
it('should change the password if it is himself', async() => {
const tx = await models.Worker.beginTransaction({});
try {
const options = {transaction: tx};
await models.VnUser.updateAll({id: managerId}, {emailVerified: true}, options);
await models.Worker.setPassword(ctx, managerId, newPass, options);
const isNewPass = await passHasBeenChanged(managerId, newPass, options);
expect(isNewPass).toBeTrue();
await tx.rollback();
} catch (e) {
await tx.rollback();
}
});
it('should not change the password if it is not a subordinate', async() => {
const tx = await models.Worker.beginTransaction({});
try {
const options = {transaction: tx};
await models.Worker.setPassword(ctx, administrativeId, newPass, options);
await tx.rollback();
} catch (e) {
expect(e.message).toEqual(`You don't have enough privileges.`);
await tx.rollback();
}
});
it('should throw an error: Password does not meet requirements', async() => {
const tx = await models.Worker.beginTransaction({});
const newPass = 'Hi';
try {
const options = {transaction: tx};
await models.Worker.setPassword(ctx, employeeId, newPass, options);
await tx.rollback();
} catch (e) {
expect(e.sqlMessage).toEqual('Password does not meet requirements');
await tx.rollback();
}
});
it('should throw an error: You don\'t have enough privileges.', async() => {
ctx.req.accessToken.userId = 5;
const tx = await models.Collection.beginTransaction({});
try {
const options = {transaction: tx};
await models.Worker.setPassword(ctx, options);
await tx.rollback();
} catch (e) {
expect(e).toEqual(new UserError(`You don't have enough privileges.`));
await tx.rollback();
}
});
});
const passHasBeenChanged = async(userId, pass, options) => {
const user = await models.VnUser.findById(userId, null, options);
return user.hasPassword(pass);
};

View File

@ -16,6 +16,7 @@ describe('vnWorkerDescriptor', () => {
const id = 1;
const response = 'foo';
$httpBackend.whenGET('UserConfigs/getUserConfig').respond({});
$httpBackend.expectRoute('GET', `Workers/${id}`).respond(response);
controller.id = id;
$httpBackend.flush();