diff --git a/client/core/src/directives/specs/validation.spec.js b/client/core/src/directives/specs/validation.spec.js index e69de29bb..3ff608a68 100644 --- a/client/core/src/directives/specs/validation.spec.js +++ b/client/core/src/directives/specs/validation.spec.js @@ -0,0 +1,81 @@ +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 are not defined', () => { + let html = ``; + + expect(() => { + compile(html, {}); + }).toThrow(new Error(`vnValidation: Entity 'User' doesn't exist`)); + }); + + // should adapt the test to whathver needs to test the user name instead of mail validation as it should be in mail-validation.js + 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 = ''; + + expect(element[0].classList).toContain('ng-valid'); + expect(element[0].classList).not.toContain('ng-invalid'); + scope.$digest(); + + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + }); + + it(`should not validate the name as it's format is incorrect`, () => { + let html = ``; + let validations = {User: {validations: {name: [{validation: 'presence'}]}}}; + compile(html, validations, 'spiderman'); + scope.user.email = '1234?'; + + expect(element[0].classList).toContain('ng-valid'); + expect(element[0].classList).not.toContain('ng-invalid'); + scope.$digest(); + + expect(element[0].classList).toContain('ng-invalid'); + expect(element[0].classList).not.toContain('ng-valid'); + }); + + it(`should validate the email as it's format is correct`, () => { + let html = ``; + let validations = {User: {validations: {email: [{validation: 'presence'}]}}}; + compile(html, validations, 'user@verdnatura.es'); + scope.user.email = 'user@verdnatura.es'; + + expect(element[0].classList).toContain('ng-valid'); + expect(element[0].classList).not.toContain('ng-invalid'); + scope.$digest(); + + expect(element[0].classList).toContain('ng-valid'); + expect(element[0].classList).not.toContain('ng-invalid'); + }); +}); diff --git a/client/core/src/directives/validation.js b/client/core/src/directives/validation.js index cd3878d9e..db34fe12b 100644 --- a/client/core/src/directives/validation.js +++ b/client/core/src/directives/validation.js @@ -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/validator.js b/client/core/src/lib/validator.js index d31fa9e36..ce6dc989b 100644 --- a/client/core/src/lib/validator.js +++ b/client/core/src/lib/validator.js @@ -1,7 +1,7 @@ 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`); }, 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..ab30f576b 100644 --- a/services/mailer/application/config.js +++ b/services/mailer/application/config.js @@ -15,4 +15,4 @@ try { config.proxy = require('../../nginx/config.json'); config.package = require('../package.json'); -module.exports = config; \ No newline at end of file +module.exports = config;