From 0b4552a8e71f1773363297a5974fecf87b1325e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 4 Dec 2015 10:32:23 +0100 Subject: [PATCH] fix(timerange): fix handling of invalid dates in from/to url parameters, fixes #3345 --- public/app/features/dashboard/timeSrv.js | 6 ++++-- public/test/mocks/dashboard-mock.js | 2 +- public/test/specs/time_srv_specs.js | 8 ++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/public/app/features/dashboard/timeSrv.js b/public/app/features/dashboard/timeSrv.js index 691bfd07904..3fa0abf508e 100644 --- a/public/app/features/dashboard/timeSrv.js +++ b/public/app/features/dashboard/timeSrv.js @@ -21,6 +21,7 @@ define([ this._initTimeFromUrl(); this._parseTime(); + console.log(dashboard.time); if(this.dashboard.refresh) { this.setAutoRefresh(this.dashboard.refresh); @@ -47,8 +48,9 @@ define([ if (value.length === 15) { return moment.utc(value, 'YYYYMMDDTHHmmss'); } - var epoch = parseInt(value); - if (!_.isNaN(epoch)) { + + if (!isNaN(value)) { + var epoch = parseInt(value); return moment(epoch); } diff --git a/public/test/mocks/dashboard-mock.js b/public/test/mocks/dashboard-mock.js index 6367093bd36..9b61108728e 100644 --- a/public/test/mocks/dashboard-mock.js +++ b/public/test/mocks/dashboard-mock.js @@ -15,7 +15,7 @@ define([], rows: [], pulldowns: [ { type: 'templating' }, { type: 'annotations' } ], nav: [ { type: 'timepicker' } ], - time: {from: '1h', to: 'now'}, + time: {from: 'now-6h', to: 'now'}, templating: { list: [] }, diff --git a/public/test/specs/time_srv_specs.js b/public/test/specs/time_srv_specs.js index 9943aae6cc3..60bdd1a4c5b 100644 --- a/public/test/specs/time_srv_specs.js +++ b/public/test/specs/time_srv_specs.js @@ -75,6 +75,14 @@ define([ expect(time.to.valueOf()).to.equal(1410337665699); }); + it('should handle bad dates', function() { + ctx.$routeParams.from = '20151126T00010%3C%2Fp%3E%3Cspan%20class'; + ctx.$routeParams.to = 'now'; + _dashboard.time.from = 'now-6h'; + ctx.service.init(_dashboard); + expect(ctx.service.time.from).to.equal('now-6h'); + expect(ctx.service.time.to).to.equal('now'); + }); }); describe('setTime', function() {