Merge branch 'dev' of https://git.verdnatura.es/salix into dev

This commit is contained in:
SAMBA\bernat 2018-01-03 12:01:31 +01:00
commit 492902e637
10 changed files with 231 additions and 33 deletions

View File

@ -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 = `<input type="name" ng-model="user.name" vn-validation="user"/>`;
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 = `<input type="name" ng-model="user.name" vn-validation="user.name"/>`;
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 = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
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 = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
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 = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
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 = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
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 = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
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 = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
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 = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
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 = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
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 = `<form><input type="text" ng-model="user.phone" vn-validation="user.phone"/></form>`;
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 = `<form><input type="text" ng-model="user.phone" vn-validation="user.phone"/></form>`;
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 = `<form><input type="text" ng-model="user.phone" vn-validation="user.phone"/></form>`;
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 = `<form><input type="text" ng-model="user.phone" vn-validation="user.phone"/></form>`;
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 = `<form><input type="text" ng-model="user.email" vn-validation="user.email"/></form>`;
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');
});
});
});

View File

@ -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) {

View File

@ -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);

View File

@ -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`);
}

View File

@ -47,8 +47,7 @@ module.exports = {
this.request.user = {
id: token.userId,
token: this.getToken()
}
};
this.next();
});
},

View File

@ -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;
module.exports = config;

View File

@ -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",

View File

@ -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');
}
});
},
/**

View File

@ -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');

View File

@ -7,9 +7,9 @@
"mysql": {
"host": "localhost",
"port": 3306,
"user": "reports",
"user": "root",
"password": "",
"database": ""
"database": "vn"
},
"pdf": {
"footer": {