2022-03-01 09:44:04 +00:00
|
|
|
const models = require('vn-loopback/server/server').models;
|
2021-04-01 16:38:40 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
|
|
|
|
|
|
|
describe('loopback model Supplier-account', () => {
|
|
|
|
describe('create', () => {
|
|
|
|
const supplierId = 1;
|
|
|
|
const bankEntityId = 2100;
|
2022-03-01 09:44:04 +00:00
|
|
|
const activeCtx = {
|
|
|
|
accessToken: {userId: 5},
|
|
|
|
http: {
|
|
|
|
req: {
|
|
|
|
headers: {origin: 'http://localhost'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
activeCtx.http.req.__ = value => {
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
2021-04-01 16:38:40 +00:00
|
|
|
it('should throw an error when attempting to set an invalid iban account', async() => {
|
|
|
|
let error;
|
|
|
|
const expectedError = 'The IBAN does not have the correct format';
|
|
|
|
const iban = 'incorrect format';
|
2021-04-16 16:35:03 +00:00
|
|
|
try {
|
2022-03-01 09:44:04 +00:00
|
|
|
await models.SupplierAccount.create(
|
2021-04-16 16:35:03 +00:00
|
|
|
{
|
|
|
|
supplierFk: supplierId,
|
|
|
|
bankEntityFk: bankEntityId,
|
|
|
|
iban: iban
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2021-04-01 16:38:40 +00:00
|
|
|
error = e;
|
|
|
|
|
|
|
|
expect(error.message).toContain(expectedError);
|
2021-04-16 16:35:03 +00:00
|
|
|
}
|
2021-04-01 16:38:40 +00:00
|
|
|
|
|
|
|
expect(error).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create a valid supplier account', async() => {
|
2022-03-01 09:44:04 +00:00
|
|
|
const tx = await models.SupplierAccount.beginTransaction({});
|
2021-04-01 16:38:40 +00:00
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
const iban = 'ES91 2100 0418 4502 0005 1332';
|
|
|
|
|
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
|
|
active: activeCtx
|
|
|
|
});
|
2022-03-01 09:44:04 +00:00
|
|
|
|
|
|
|
const createdSupplierAccount = await models.SupplierAccount.create({
|
2021-04-16 16:35:03 +00:00
|
|
|
supplierFk: supplierId,
|
|
|
|
bankEntityFk: bankEntityId,
|
|
|
|
iban: iban
|
|
|
|
},
|
|
|
|
options);
|
2021-04-01 16:38:40 +00:00
|
|
|
|
|
|
|
expect(createdSupplierAccount.iban).toBe(iban);
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
2022-03-01 09:44:04 +00:00
|
|
|
|
|
|
|
it('should change isPayMethodChecked to false', async() => {
|
|
|
|
const tx = await models.SupplierAccount.beginTransaction({});
|
|
|
|
try {
|
|
|
|
const options = {transaction: tx};
|
|
|
|
const iban = 'ES91 2100 0418 4502 0005 1332';
|
|
|
|
|
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
|
|
active: activeCtx
|
|
|
|
});
|
|
|
|
|
|
|
|
const supplierBefore = await models.Supplier.findById(supplierId, null, options);
|
|
|
|
|
|
|
|
await models.SupplierAccount.create({
|
|
|
|
supplierFk: supplierId,
|
|
|
|
bankEntityFk: bankEntityId,
|
|
|
|
iban: iban
|
|
|
|
},
|
|
|
|
options);
|
|
|
|
|
|
|
|
const supplierAfter = await models.Supplier.findById(supplierId, null, options);
|
|
|
|
|
|
|
|
expect(supplierBefore.isPayMethodChecked).toBeTruthy();
|
|
|
|
expect(supplierAfter.isPayMethodChecked).toBeFalsy();
|
|
|
|
await tx.rollback();
|
|
|
|
} catch (e) {
|
|
|
|
await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
});
|
2021-04-01 16:38:40 +00:00
|
|
|
});
|
|
|
|
});
|