diff --git a/client/core/src/directives/specs/validation.spec.js b/client/core/src/directives/specs/validation.spec.js index e69de29bb..f13f0024f 100644 --- a/client/core/src/directives/specs/validation.spec.js +++ b/client/core/src/directives/specs/validation.spec.js @@ -0,0 +1,186 @@ +describe('Directive validation', () => { + let scope; + let element; + let compile; + + beforeEach(() => { + angular.mock.module('client'); + }); + + compile = (_element, validations, value) => { + inject(($compile, $rootScope, aclService, _$timeout_, $window) => { + $window.validations = validations; + scope = $rootScope.$new(); + scope.user = {name: value}; + element = angular.element(_element); + $compile(element)(scope); + scope.$digest(); + }); + }; + + it(`should throw an error if the vnValidation doesn't have the right syntax`, () => { + let html = ``; + + expect(() => { + compile(html, {}); + }).toThrow(new Error(`vnValidation: Attribute must have this syntax: [entity].[field]`)); + }); + + it('should throw an error if the window.validations aint defined', () => { + let html = ``; + + expect(() => { + compile(html, {}); + }).toThrow(new Error(`vnValidation: Entity 'User' doesn't exist`)); + }); + + describe('Validator presence()', () => { + it('should not validate the user name as it is an empty string', () => { + let html = `
`; + let validations = {User: {validations: {name: [{validation: 'presence'}]}}}; + compile(html, validations, 'Spiderman'); + scope.user.name = ''; + scope.$digest(); + + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + }); + + describe('Validator absence()', () => { + it('should not validate the entity as it should be an empty string', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'absence'}]}}}; + compile(html, validations, 'Spiderman'); + scope.$digest(); + + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + + it('should validate the entity as it is an empty string', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'absence'}]}}}; + compile(html, validations, ''); + scope.$digest(); + + expect(element[0].classList).toContain('ng-valid'); + expect(element[0].classList).not.toContain('ng-invalid'); + }); + }); + + describe('Validator length()', () => { + it('should not validate the user name as it should have min length of 15', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'length', min: 10, max: 50, is: 15}]}}}; + compile(html, validations, 'fifteen!'); + scope.$digest(); + + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + + it('should validate the user name as it has length of 15', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'length', min: 10, max: 50, is: 15}]}}}; + compile(html, validations, 'fifteen length!'); + scope.$digest(); + + expect(element[0].classList).toContain('ng-valid'); + expect(element[0].classList).not.toContain('ng-invalid'); + }); + + it('should not validate the user name as it should have min length of 10', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'length', min: 10}]}}}; + compile(html, validations, 'shortname'); + scope.$digest(); + + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + + it('should validate the user name as its length is greater then the minimum', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'length', min: 10}]}}}; + compile(html, validations, 'verylongname'); + scope.$digest(); + + expect(element[0].classList).toContain('ng-valid'); + expect(element[0].classList).not.toContain('ng-invalid'); + }); + + it('should not validate the user name as its length is greater then the maximum', () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'length', max: 10}]}}}; + compile(html, validations, 'toolongname'); + scope.$digest(); + + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + }); + + describe('Validator numericality()', () => { + it('should not validate the phone number as it should a integer', () => { + let html = ``; + let validations = {User: {validations: {phone: [{validation: 'numericality', is: 'what is this?'}]}}}; + compile(html, validations, 'spiderman'); + scope.user.phone = 'this is not a phone number!'; + scope.$digest(); + + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + + it('should validate the phone number as it an integer', () => { + let html = ``; + let validations = {User: {validations: {phone: [{validation: 'numericality', is: 'what is this?'}]}}}; + compile(html, validations, 'spiderman'); + scope.user.phone = '555555555'; + scope.$digest(); + + expect(element[0].classList).toContain('ng-valid'); + expect(element[0].classList).not.toContain('ng-invalid'); + }); + }); + + describe('Validator inclusion()', () => { + it('should not validate the phone number as it is not an integer', () => { + let html = ``; + let validations = {User: {validations: {phone: [{validation: 'inclusion'}]}}}; + compile(html, validations, 'spiderman'); + scope.user.phone = 'this is not a phone number!'; + scope.$digest(); + + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + }); + + describe('Validator exclusion()', () => { + it('should validate the phone number as it is an integer', () => { + let html = ``; + let validations = {User: {validations: {phone: [{validation: 'exclusion'}]}}}; + compile(html, validations, 'spiderman'); + scope.user.phone = '555555555'; + scope.$digest(); + + expect(element[0].classList).toContain('ng-valid'); + expect(element[0].classList).not.toContain('ng-invalid'); + }); + }); + + describe('Validator format()', () => { + it('should not validate the email number as it doesnt contain @', () => { + let html = ``; + let validations = {User: {validations: {email: [{validation: 'format', with: '@'}]}}}; + compile(html, validations, 'spiderman'); + scope.user.email = 'userverdnatura.es'; + scope.$digest(); + + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + }); +}); diff --git a/client/core/src/directives/validation.js b/client/core/src/directives/validation.js index cd3878d9e..10669e738 100644 --- a/client/core/src/directives/validation.js +++ b/client/core/src/directives/validation.js @@ -40,7 +40,7 @@ export function directive(interpolate, compile, $window) { let errorShown = false; input.$options.$$options.allowInvalid = true; - input.$validators.entity = function(value) { + input.$validators.entity = value => { try { validateAll(value, validations); return true; @@ -51,9 +51,9 @@ export function directive(interpolate, compile, $window) { } }; - scope.$watch(function() { + scope.$watch(() => { return (form.$submitted || input.$dirty) && input.$invalid; - }, function(value) { + }, value => { let parent = element.parent(); if (value) { diff --git a/client/core/src/lib/app.js b/client/core/src/lib/app.js index 5851357d8..3b58309b4 100644 --- a/client/core/src/lib/app.js +++ b/client/core/src/lib/app.js @@ -12,7 +12,7 @@ export default class App { this.$rootScope = $rootScope; } show(message) { - if (this.snackbar) this.snackbar.show({message: message, timeout: 400}); + if (this.snackbar) this.snackbar.show({message: message}); } showMessage(message) { this.show(message); diff --git a/client/core/src/lib/validator.js b/client/core/src/lib/validator.js index d31fa9e36..c29142653 100644 --- a/client/core/src/lib/validator.js +++ b/client/core/src/lib/validator.js @@ -1,19 +1,20 @@ import {validator} from 'vendor'; export const validators = { - presence: function(value, conf) { + presence: value => { if (validator.isEmpty(value)) throw new Error(`Value can't be empty`); }, - absence: function(value, conf) { + absence: value => { if (!validator.isEmpty(value)) throw new Error(`Value should be empty`); }, - length: function(value, conf) { + length: (value, conf) => { let options = { min: conf.min || conf.is, max: conf.max || conf.is }; + if (!validator.isLength(value, options)) { if (conf.is) { throw new Error(`Value should be ${conf.is} characters long`); @@ -26,26 +27,26 @@ export const validators = { } } }, - numericality: function(value, conf) { + numericality: (value, conf) => { if (conf.int) { if (!validator.isInt(value)) throw new Error(`Value should be integer`); } else if (!validator.isNumeric(value)) throw new Error(`Value should be a number`); }, - inclusion: function(value, conf) { + inclusion: (value, conf) => { if (!validator.isIn(value, conf.in)) throw new Error(`Invalid value`); }, - exclusion: function(value, conf) { + exclusion: (value, conf) => { if (validator.isIn(value, conf.in)) throw new Error(`Invalid value`); }, - format: function(value, conf) { + format: (value, conf) => { if (!validator.matches(value, conf.with)) throw new Error(`Invalid value`); }, - custom: function(value, conf) { + custom: (value, conf) => { if (!conf.bindedFunction(value)) throw new Error(`Invalid value`); } diff --git a/services/mailer/application/auth.js b/services/mailer/application/auth.js index 7e9457537..dc84cd911 100644 --- a/services/mailer/application/auth.js +++ b/services/mailer/application/auth.js @@ -47,8 +47,7 @@ module.exports = { this.request.user = { id: token.userId, token: this.getToken() - } - + }; this.next(); }); }, diff --git a/services/mailer/application/config.js b/services/mailer/application/config.js index 1442e85ed..f4e90f14a 100644 --- a/services/mailer/application/config.js +++ b/services/mailer/application/config.js @@ -1,18 +1,23 @@ var path = require('path'); -var fs = require('fs'); -var config = {}; +let defaultFile = 'datasources.json'; -let devConfigPath = path.join(__dirname, '/config/datasources.development.json'); -let configPath = path.join(__dirname, '/config/datasources.json'); +function getFile(fileName) { + return require(path.join(__dirname, `/config/${fileName}`)); +} try { - config = Object.assign(require(configPath), require(devConfigPath)); + let envFile = 'datasources.development.json'; + + if (process.env.NODE_ENV === 'test') + envFile = 'datasources.test.json'; + + config = getFile(envFile); } catch (e) { if (e.code == 'MODULE_NOT_FOUND') - config = require(configPath); + config = getFile(defaultFile); } config.proxy = require('../../nginx/config.json'); config.package = require('../package.json'); -module.exports = config; \ No newline at end of file +module.exports = config; diff --git a/services/mailer/application/config/datasources.test.json b/services/mailer/application/config/datasources.test.json index 69943439d..d3a2d8b33 100644 --- a/services/mailer/application/config/datasources.test.json +++ b/services/mailer/application/config/datasources.test.json @@ -4,14 +4,14 @@ "debug": false, "defaultLanguage": "es", "senderMail": "noreply@localhost", - "senderName": "" + "senderName": "VerdNatura" }, "mysql": { "host": "localhost", "port": 3306, - "user": "reports", + "user": "root", "password": "", - "database": "" + "database": "vn" }, "smtp": { "host": "localhost", diff --git a/services/mailer/application/mail.js b/services/mailer/application/mail.js index 3980e5128..04af6b007 100644 --- a/services/mailer/application/mail.js +++ b/services/mailer/application/mail.js @@ -12,15 +12,17 @@ module.exports = { * Load mail config. */ init: function() { + this.transporter = nodemailer.createTransport(config.smtp); this.transporter.verify(function(error, success) { if (error) { - throw new Error(error); + console.error(error); } else if (config.app.debug) { console.log('SMTP connection stablished'); } }); + }, /** diff --git a/services/print/application/config.js b/services/print/application/config.js index 1442e85ed..c46f236f8 100644 --- a/services/print/application/config.js +++ b/services/print/application/config.js @@ -1,15 +1,20 @@ var path = require('path'); -var fs = require('fs'); -var config = {}; +let defaultFile = 'datasources.json'; -let devConfigPath = path.join(__dirname, '/config/datasources.development.json'); -let configPath = path.join(__dirname, '/config/datasources.json'); +function getFile(fileName) { + return require(path.join(__dirname, `/config/${fileName}`)); +} try { - config = Object.assign(require(configPath), require(devConfigPath)); + let envFile = 'datasources.development.json'; + + if (process.env.NODE_ENV === 'test') + envFile = 'datasources.test.json'; + + config = getFile(envFile); } catch (e) { if (e.code == 'MODULE_NOT_FOUND') - config = require(configPath); + config = getFile(defaultFile); } config.proxy = require('../../nginx/config.json'); diff --git a/services/print/application/config/datasources.test.json b/services/print/application/config/datasources.test.json index 92965aa5a..d51ae1fa2 100644 --- a/services/print/application/config/datasources.test.json +++ b/services/print/application/config/datasources.test.json @@ -7,9 +7,9 @@ "mysql": { "host": "localhost", "port": 3306, - "user": "reports", + "user": "root", "password": "", - "database": "" + "database": "vn" }, "pdf": { "footer": {