2022-10-03 13:11:29 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
|
|
|
|
2023-01-23 14:24:00 +00:00
|
|
|
describe('VnUser privileges()', () => {
|
2022-10-03 13:11:29 +00:00
|
|
|
const employeeId = 1;
|
|
|
|
const developerId = 9;
|
|
|
|
const sysadminId = 66;
|
2022-10-28 06:09:47 +00:00
|
|
|
const itBossId = 104;
|
|
|
|
const rootId = 100;
|
2022-10-26 06:27:28 +00:00
|
|
|
const clarkKent = 1103;
|
2022-10-03 13:11:29 +00:00
|
|
|
|
|
|
|
it('should throw an error when user not has privileges', async() => {
|
|
|
|
const ctx = {req: {accessToken: {userId: developerId}}};
|
2023-01-23 14:24:00 +00:00
|
|
|
const tx = await models.VnUser.beginTransaction({});
|
2022-10-03 13:11:29 +00:00
|
|
|
|
|
|
|
let error;
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
2023-01-23 14:24:00 +00:00
|
|
|
await models.VnUser.privileges(ctx, employeeId, null, true, options);
|
2022-10-03 13:11:29 +00:00
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
await tx.rollback();
|
|
|
|
}
|
|
|
|
|
2022-10-26 06:27:28 +00:00
|
|
|
expect(error.message).toContain(`You don't have grant privilege`);
|
2022-10-03 13:11:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when user has privileges but not has the role', async() => {
|
|
|
|
const ctx = {req: {accessToken: {userId: sysadminId}}};
|
2023-01-23 14:24:00 +00:00
|
|
|
const tx = await models.VnUser.beginTransaction({});
|
2022-10-03 13:11:29 +00:00
|
|
|
|
|
|
|
let error;
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
2023-01-23 14:24:00 +00:00
|
|
|
await models.VnUser.privileges(ctx, employeeId, rootId, null, options);
|
2022-10-28 06:09:47 +00:00
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
await tx.rollback();
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error.message).toContain(`You don't own the role and you can't assign it to another user`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when user has privileges but not has the role from user', async() => {
|
|
|
|
const ctx = {req: {accessToken: {userId: sysadminId}}};
|
2023-01-23 14:24:00 +00:00
|
|
|
const tx = await models.VnUser.beginTransaction({});
|
2022-10-28 06:09:47 +00:00
|
|
|
|
|
|
|
let error;
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
|
2023-01-23 14:24:00 +00:00
|
|
|
await models.VnUser.privileges(ctx, itBossId, developerId, null, options);
|
2022-10-03 13:11:29 +00:00
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
await tx.rollback();
|
|
|
|
}
|
|
|
|
|
2022-10-26 06:27:28 +00:00
|
|
|
expect(error.message).toContain(`You don't own the role and you can't assign it to another user`);
|
2022-10-03 13:11:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should change role', async() => {
|
|
|
|
const ctx = {req: {accessToken: {userId: sysadminId}}};
|
2023-01-23 14:24:00 +00:00
|
|
|
const tx = await models.VnUser.beginTransaction({});
|
2022-10-03 13:11:29 +00:00
|
|
|
|
|
|
|
const options = {transaction: tx};
|
2023-12-05 06:16:12 +00:00
|
|
|
const agency = await models.VnRole.findOne({
|
2022-10-03 13:11:29 +00:00
|
|
|
where: {
|
|
|
|
name: 'agency'
|
|
|
|
}
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
let error;
|
|
|
|
let result;
|
|
|
|
try {
|
2023-01-23 14:24:00 +00:00
|
|
|
await models.VnUser.privileges(ctx, clarkKent, agency.id, null, options);
|
|
|
|
result = await models.VnUser.findById(clarkKent, null, options);
|
2022-10-03 13:11:29 +00:00
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
await tx.rollback();
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error).not.toBeDefined();
|
|
|
|
expect(result.roleFk).toEqual(agency.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should change hasGrant', async() => {
|
|
|
|
const ctx = {req: {accessToken: {userId: sysadminId}}};
|
2023-01-23 14:24:00 +00:00
|
|
|
const tx = await models.VnUser.beginTransaction({});
|
2022-10-03 13:11:29 +00:00
|
|
|
|
|
|
|
let error;
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
2023-01-23 14:24:00 +00:00
|
|
|
await models.VnUser.privileges(ctx, clarkKent, null, true, options);
|
|
|
|
result = await models.VnUser.findById(clarkKent, null, options);
|
2022-10-03 13:11:29 +00:00
|
|
|
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
|
|
|
await tx.rollback();
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(error).not.toBeDefined();
|
|
|
|
expect(result.hasGrant).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|