test(salix): refs #7648 #7648 entry.filter

This commit is contained in:
Javier Segarra 2024-07-02 09:52:18 +02:00
parent f41c2489d9
commit 89a394a10d
2 changed files with 65 additions and 19 deletions

View File

@ -149,7 +149,7 @@ module.exports = Self => {
const isSupplier = await Self.app.models.Supplier.findById(userId, options); const isSupplier = await Self.app.models.Supplier.findById(userId, options);
if (isSupplier) { if (isSupplier) {
if (!filter.where) filter.where = {}; if (!filter.where) filter.where = {};
filter.where.supplierFk = ctx.req.accessToken.userId; filter.where[`e.supplierFk`] = ctx.req.accessToken.userId;
} }
const stmts = []; const stmts = [];
let stmt; let stmt;

View File

@ -1,6 +1,6 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
describe('Entry filter()', () => { fdescribe('Entry filter()', () => {
it('should return the entry matching "search"', async() => { it('should return the entry matching "search"', async() => {
const tx = await models.Entry.beginTransaction({}); const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};
@ -48,27 +48,73 @@ describe('Entry filter()', () => {
} }
}); });
it('should return the entry matching the supplier', async() => { describe('should return the entry matching the supplier', () => {
const tx = await models.Entry.beginTransaction({}); it('when userId is supplier ', async() => {
const options = {transaction: tx}; const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
try { try {
const ctx = { const ctx = {
args: { args: {},
supplierFk: 2 req: {accessToken: {userId: 2}}
}, };
req: {accessToken: {userId: 9}}
};
const result = await models.Entry.filter(ctx, options); const result = await models.Entry.filter(ctx, options);
expect(result.length).toEqual(6); expect(result.length).toEqual(6);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
await tx.rollback(); await tx.rollback();
throw e; throw e;
} }
});
it('when userId is supplier fetching other supplier', async() => {
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
try {
const ctx = {
args: {
supplierFk: 1
},
req: {accessToken: {userId: 2}}
};
const result = await models.Entry.filter(ctx, options);
expect(result.length).toEqual(6);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('when userId is not supplier', async() => {
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
try {
const ctx = {
args: {
supplierFk: 2
},
req: {accessToken: {userId: 9}}
};
const result = await models.Entry.filter(ctx, options);
expect(result.length).toEqual(6);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
}); });
it('should return the entry matching the company', async() => { it('should return the entry matching the company', async() => {