French TIN validation bugs fixed
This commit is contained in:
parent
ffe216f630
commit
70f854a463
|
@ -1,12 +1,11 @@
|
|||
const app = require('../../../../../client/server/server');
|
||||
const catchErrors = require('../../../../../../services/utils/jasmineHelpers').catchErrors;
|
||||
const restoreFixtures = require('../../../../../../services/db/testing_fixtures');
|
||||
|
||||
describe('Client Create', () => {
|
||||
let sqlStatements = {deletes: `
|
||||
DELETE FROM vn.address WHERE nickname = "Wade";
|
||||
DELETE FROM vn.client WHERE name = "Wade";
|
||||
DELETE FROM account.user WHERE name = "Deadpool";
|
||||
DELETE FROM vn.address WHERE nickname = 'Wade';
|
||||
DELETE FROM vn.client WHERE name = 'Wade';
|
||||
DELETE FROM account.user WHERE name = 'Deadpool';
|
||||
`, inserts: ``, updates: ``};
|
||||
|
||||
beforeAll(() => {
|
||||
|
@ -17,70 +16,50 @@ describe('Client Create', () => {
|
|||
restoreFixtures(sqlStatements);
|
||||
});
|
||||
|
||||
let newAccountData = {
|
||||
active: true,
|
||||
name: 'Wade',
|
||||
let newAccount = {
|
||||
userName: 'Deadpool',
|
||||
email: 'Deadpool@marvel.com',
|
||||
fi: '16195279J',
|
||||
socialName: 'Deadpool Marvel',
|
||||
salesPersonFk: 1
|
||||
name: 'Wade',
|
||||
socialName: 'Deadpool Marvel'
|
||||
};
|
||||
|
||||
it('should find Charles Xavier', done => {
|
||||
app.models.Account.findOne({where: {name: 'CharlesXavier'}})
|
||||
.then(account => {
|
||||
expect(account.name).toEqual('CharlesXavier');
|
||||
done();
|
||||
});
|
||||
it(`should not find Deadpool as he's not created yet`, async() => {
|
||||
let account = await app.models.Account.findOne({where: {name: newAccount.userName}});
|
||||
let client = await app.models.Client.findOne({where: {name: newAccount.name}});
|
||||
|
||||
expect(account).toEqual(null);
|
||||
expect(client).toEqual(null);
|
||||
});
|
||||
|
||||
it(`should not find Deadpool as he's not created yet`, done => {
|
||||
app.models.Account.findOne({where: {name: newAccountData.userName}})
|
||||
.then(account => {
|
||||
expect(account).toEqual(null);
|
||||
app.models.Client.findOne({where: {name: newAccountData.name}})
|
||||
.then(client => {
|
||||
expect(client).toEqual(null);
|
||||
done();
|
||||
});
|
||||
})
|
||||
.catch(catchErrors(done));
|
||||
it('should create a new account', async() => {
|
||||
let client = await app.models.Client.createWithUser(newAccount);
|
||||
let account = await app.models.Account.findOne({where: {name: newAccount.userName}});
|
||||
|
||||
expect(account.name).toEqual(newAccount.userName);
|
||||
|
||||
client = await app.models.Client.findOne({where: {name: newAccount.name}});
|
||||
|
||||
expect(client.id).toEqual(account.id);
|
||||
expect(client.name).toEqual(newAccount.name);
|
||||
expect(client.email).toEqual(newAccount.email);
|
||||
expect(client.fi).toEqual(newAccount.fi);
|
||||
expect(client.socialName).toEqual(newAccount.socialName);
|
||||
});
|
||||
|
||||
it('should find an existing account', async() => {
|
||||
let account = await app.models.Account.findOne({where: {name: newAccount.userName}});
|
||||
|
||||
expect(account.name).toEqual(newAccount.userName);
|
||||
});
|
||||
|
||||
it('should not be able to create a user if exists', async() => {
|
||||
let client = await app.models.Client.findOne({where: {name: 'Charles Xavier'}});
|
||||
let account = await app.models.Account.findOne({where: {id: client.id}});
|
||||
|
||||
let formerAccountData = {
|
||||
name: client.name,
|
||||
userName: account.name,
|
||||
email: client.email,
|
||||
fi: client.fi,
|
||||
socialName: client.socialName
|
||||
};
|
||||
|
||||
try {
|
||||
let client = await app.models.Client.createWithUser(formerAccountData);
|
||||
let client = await app.models.Client.createWithUser(newAccount);
|
||||
|
||||
expect(client).toBeNull();
|
||||
} catch (err) {
|
||||
expect(err.details.codes.name[0]).toEqual('uniqueness');
|
||||
}
|
||||
});
|
||||
|
||||
it('should create a new account', async() => {
|
||||
let client = await app.models.Client.createWithUser(newAccountData);
|
||||
let account = await app.models.Account.findOne({where: {name: newAccountData.userName}});
|
||||
|
||||
expect(account.name).toEqual(newAccountData.userName);
|
||||
|
||||
client = await app.models.Client.findOne({where: {name: newAccountData.name}});
|
||||
|
||||
expect(client.id).toEqual(account.id);
|
||||
expect(client.name).toEqual(newAccountData.name);
|
||||
expect(client.email).toEqual(newAccountData.email);
|
||||
expect(client.fi).toEqual(newAccountData.fi);
|
||||
expect(client.socialName).toEqual(newAccountData.socialName);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -44,11 +44,11 @@ module.exports = function(Self) {
|
|||
allowBlank: true
|
||||
});
|
||||
|
||||
Self.validateAsync('fi', fiIsValid, {
|
||||
Self.validateAsync('fi', tinIsValid, {
|
||||
message: 'DNI Incorrecto'
|
||||
});
|
||||
let validateDni = require('../validations/validateDni');
|
||||
async function fiIsValid(err, done) {
|
||||
let validateTin = require('../validations/validateTin');
|
||||
async function tinIsValid(err, done) {
|
||||
let filter = {
|
||||
fields: ['code'],
|
||||
where: {id: this.countryFk}
|
||||
|
@ -56,7 +56,7 @@ module.exports = function(Self) {
|
|||
let country = await Self.app.models.Country.findOne(filter);
|
||||
let code = country ? country.code.toLowerCase() : null;
|
||||
|
||||
if (!validateDni(this.fi, code))
|
||||
if (!validateTin(this.fi, code))
|
||||
err();
|
||||
done();
|
||||
}
|
||||
|
|
|
@ -1,26 +1,26 @@
|
|||
const validateDni = require('../validateDni');
|
||||
const validateDni = require('../validateTin');
|
||||
|
||||
describe('DNI validation', () => {
|
||||
it('should return true for any DNI when no country is passed', () => {
|
||||
describe('TIN validation', () => {
|
||||
it('should return true for any TIN when no country is passed', () => {
|
||||
let isValid = validateDni('Pepinillos');
|
||||
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
|
||||
describe('Spanish', () => {
|
||||
it('should return true for valid spanish DNI', () => {
|
||||
it('should return true for valid spanish TIN', () => {
|
||||
let isValid = validateDni('20849756A', 'es');
|
||||
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return false for spanish DNI with exceeded digits', () => {
|
||||
it('should return false for spanish TIN with exceeded digits', () => {
|
||||
let isValid = validateDni('208497563239A', 'es');
|
||||
|
||||
expect(isValid).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return false for spanish DNI with invalid letter', () => {
|
||||
it('should return false for spanish TIN with invalid letter', () => {
|
||||
let isValid = validateDni('20243746E', 'es');
|
||||
|
||||
expect(isValid).toBeFalsy();
|
||||
|
@ -40,19 +40,19 @@ describe('DNI validation', () => {
|
|||
});
|
||||
|
||||
describe('French', () => {
|
||||
it('should return true for valid french DNI', () => {
|
||||
let isValid = validateDni('1B123456789', 'fr');
|
||||
it('should return true for valid french TIN', () => {
|
||||
let isValid = validateDni('012345678', 'fr');
|
||||
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return false for french DNI with exceeded digits', () => {
|
||||
let isValid = validateDni('1B12345678910', 'fr');
|
||||
it('should return false for french TIN with exceeded digits', () => {
|
||||
let isValid = validateDni('1B123456789101234', 'fr');
|
||||
|
||||
expect(isValid).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return false for french DNI with bad syntax', () => {
|
||||
it('should return false for french TIN with bad syntax', () => {
|
||||
let isValid = validateDni('1B12345678A', 'fr');
|
||||
|
||||
expect(isValid).toBeFalsy();
|
||||
|
@ -60,19 +60,19 @@ describe('DNI validation', () => {
|
|||
});
|
||||
|
||||
describe('Italian', () => {
|
||||
it('should return true for valid italian DNI', () => {
|
||||
it('should return true for valid italian TIN', () => {
|
||||
let isValid = validateDni('12345678911', 'it');
|
||||
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return false for italian DNI with exceeded digits', () => {
|
||||
it('should return false for italian TIN with exceeded digits', () => {
|
||||
let isValid = validateDni('123456789112', 'it');
|
||||
|
||||
expect(isValid).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return false for italian DNI with bad syntax', () => {
|
||||
it('should return false for italian TIN with bad syntax', () => {
|
||||
let isValid = validateDni('1234567891A', 'it');
|
||||
|
||||
expect(isValid).toBeFalsy();
|
||||
|
@ -80,19 +80,19 @@ describe('DNI validation', () => {
|
|||
});
|
||||
|
||||
describe('Portuguese', () => {
|
||||
it('should return true for valid portuguese DNI', () => {
|
||||
it('should return true for valid portuguese TIN', () => {
|
||||
let isValid = validateDni('123456789', 'pt');
|
||||
|
||||
expect(isValid).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return false for portuguese DNI with exceeded digits', () => {
|
||||
it('should return false for portuguese TIN with exceeded digits', () => {
|
||||
let isValid = validateDni('12345678910', 'pt');
|
||||
|
||||
expect(isValid).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return false for portuguese DNI with bad syntax', () => {
|
||||
it('should return false for portuguese TIN with bad syntax', () => {
|
||||
let isValid = validateDni('12345678A', 'pt');
|
||||
|
||||
expect(isValid).toBeFalsy();
|
|
@ -1,24 +1,24 @@
|
|||
module.exports = function(fi, country) {
|
||||
if (fi == null || country == null)
|
||||
module.exports = function(tin, country) {
|
||||
if (tin == null || country == null)
|
||||
return true;
|
||||
if (typeof fi != 'string' || typeof country != 'string')
|
||||
if (typeof tin != 'string' || typeof country != 'string')
|
||||
return false;
|
||||
|
||||
fi = fi.toUpperCase();
|
||||
tin = tin.toUpperCase();
|
||||
country = country.toLowerCase();
|
||||
|
||||
let len = fi.length;
|
||||
let len = tin.length;
|
||||
|
||||
let validators = {
|
||||
es: {
|
||||
regExp: /^[A-Z0-9]\d{7}[A-Z0-9]$/,
|
||||
validate: () => {
|
||||
let isCif = /[A-W]/.test(fi.charAt(0));
|
||||
let lastDigit = fi.charAt(len - 1);
|
||||
let isCif = /[A-W]/.test(tin.charAt(0));
|
||||
let lastDigit = tin.charAt(len - 1);
|
||||
let computedDigit;
|
||||
|
||||
if (isCif) {
|
||||
let numbers = fi.substring(1, 8)
|
||||
let numbers = tin.substring(1, 8)
|
||||
.split('')
|
||||
.map(x => parseInt(x));
|
||||
|
||||
|
@ -41,8 +41,8 @@ module.exports = function(fi, country) {
|
|||
computedDigit = index == -1 ? control.toString() : index;
|
||||
} else {
|
||||
// Foreign NIF
|
||||
let index = 'XYZ'.indexOf(fi.charAt(0));
|
||||
let nif = index == -1 ? fi : index.toString() + fi.substring(1);
|
||||
let index = 'XYZ'.indexOf(tin.charAt(0));
|
||||
let nif = index == -1 ? tin : index.toString() + tin.substring(1);
|
||||
|
||||
let rest = parseInt(nif.substring(0, 8)) % 23;
|
||||
computedDigit = 'TRWAGMYFPDXBNJZSQVHLCKE'.charAt(rest);
|
||||
|
@ -52,10 +52,10 @@ module.exports = function(fi, country) {
|
|||
}
|
||||
},
|
||||
fr: {
|
||||
regExp: /^[A-Z0-9]{2}\d{9}$/
|
||||
regExp: /^\d{1,13}$/
|
||||
},
|
||||
it: {
|
||||
regExp: /^\d{11}$/
|
||||
regExp: /^(\d{11}|[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z])$/
|
||||
},
|
||||
pt: {
|
||||
regExp: /^\d{9}$/
|
||||
|
@ -67,6 +67,6 @@ module.exports = function(fi, country) {
|
|||
if (!validator)
|
||||
return true;
|
||||
|
||||
return validator.regExp.test(fi)
|
||||
return validator.regExp.test(tin)
|
||||
&& (!validator.validate || validator.validate());
|
||||
};
|
Loading…
Reference in New Issue