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