From efa7f986a4b6eaedaf504f341281b282ad8ee122 Mon Sep 17 00:00:00 2001 From: Benedict Aas Date: Wed, 10 May 2017 00:24:44 +0100 Subject: [PATCH] fix(GUI): bound flash progress percentage within 0-100 (#1258) We limit the Store state flashing percentage to be within 0-100, throwing errors otherwise. Comes with tests. Changelog-Type: Bound flash progress percentage within 0-100 range. Signed-off-by: Juan Cruz Viotti --- lib/gui/models/store.js | 3 ++- tests/gui/models/flash-state.spec.js | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/gui/models/store.js b/lib/gui/models/store.js index 2914407f..bb4193e0 100644 --- a/lib/gui/models/store.js +++ b/lib/gui/models/store.js @@ -26,6 +26,7 @@ const supportedFormats = require('../../shared/supported-formats'); const errors = require('../../shared/errors'); const release = require('../../shared/release'); const fileExtensions = require('../../shared/file-extensions'); +const utils = require('../../shared/utils'); const packageJSON = require('../../../package.json'); /** @@ -203,7 +204,7 @@ const storeReducer = (state = DEFAULT_STATE, action) => { }); } - if (!_.isNumber(action.data.percentage)) { + if (!utils.isValidPercentage(action.data.percentage)) { throw errors.createError({ title: `Invalid state percentage: ${action.data.percentage}` }); diff --git a/tests/gui/models/flash-state.spec.js b/tests/gui/models/flash-state.spec.js index 39df0340..43753924 100644 --- a/tests/gui/models/flash-state.spec.js +++ b/tests/gui/models/flash-state.spec.js @@ -143,6 +143,30 @@ describe('Browser: flashState', function() { }).to.throw('Invalid state percentage: 50'); }); + it('should throw if percentage is outside maximum bound', function() { + flashState.setFlashingFlag(); + m.chai.expect(function() { + flashState.setProgressState({ + type: 'write', + percentage: 101, + eta: 15, + speed: 0 + }); + }).to.throw('Invalid state percentage: 101'); + }); + + it('should throw if percentage is outside minimum bound', function() { + flashState.setFlashingFlag(); + m.chai.expect(function() { + flashState.setProgressState({ + type: 'write', + percentage: -1, + eta: 15, + speed: 0 + }); + }).to.throw('Invalid state percentage: -1'); + }); + it('should throw if eta is missing', function() { flashState.setFlashingFlag(); m.chai.expect(function() {