From 70f854a463aab470ae328dac8ea9dfb9b7cc92ad Mon Sep 17 00:00:00 2001 From: Juan Date: Wed, 21 Mar 2018 12:57:23 +0100 Subject: [PATCH] French TIN validation bugs fixed --- .../client/specs/createWithUser.spec.js | 85 +++++++------------ services/loopback/common/models/client.js | 8 +- ...alidateDni.spec.js => validateTin.spec.js} | 34 ++++---- .../{validateDni.js => validateTin.js} | 26 +++--- 4 files changed, 66 insertions(+), 87 deletions(-) rename services/loopback/common/validations/specs/{validateDni.spec.js => validateTin.spec.js} (66%) rename services/loopback/common/validations/{validateDni.js => validateTin.js} (72%) diff --git a/services/loopback/common/methods/client/specs/createWithUser.spec.js b/services/loopback/common/methods/client/specs/createWithUser.spec.js index 25f6789fb..e6a2589ef 100644 --- a/services/loopback/common/methods/client/specs/createWithUser.spec.js +++ b/services/loopback/common/methods/client/specs/createWithUser.spec.js @@ -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); - }); }); diff --git a/services/loopback/common/models/client.js b/services/loopback/common/models/client.js index 928fe43d5..eee710035 100644 --- a/services/loopback/common/models/client.js +++ b/services/loopback/common/models/client.js @@ -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(); } diff --git a/services/loopback/common/validations/specs/validateDni.spec.js b/services/loopback/common/validations/specs/validateTin.spec.js similarity index 66% rename from services/loopback/common/validations/specs/validateDni.spec.js rename to services/loopback/common/validations/specs/validateTin.spec.js index e61ab23c8..8a6ad04b4 100644 --- a/services/loopback/common/validations/specs/validateDni.spec.js +++ b/services/loopback/common/validations/specs/validateTin.spec.js @@ -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(); diff --git a/services/loopback/common/validations/validateDni.js b/services/loopback/common/validations/validateTin.js similarity index 72% rename from services/loopback/common/validations/validateDni.js rename to services/loopback/common/validations/validateTin.js index b169b6100..ae1c38cee 100644 --- a/services/loopback/common/validations/validateDni.js +++ b/services/loopback/common/validations/validateTin.js @@ -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()); };