From 4fea52110265617b971f848475ed9e94ffa068d8 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Mon, 14 Jul 2014 16:05:46 +0200 Subject: [PATCH] Fix enforced share expiration date to be based on share time --- core/js/js.js | 12 +++ core/js/share.js | 28 +++++-- core/js/tests/specs/coreSpec.js | 6 ++ core/js/tests/specs/shareSpec.js | 139 ++++++++++++++++++++++++++++--- 4 files changed, 170 insertions(+), 15 deletions(-) diff --git a/core/js/js.js b/core/js/js.js index 4a9a5ce82ff..5c9d7bf0141 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -1353,6 +1353,18 @@ OC.Util = { } }); }); + }, + + /** + * Remove the time component from a given date + * + * @param {Date} date date + * @return {Date} date with stripped time + */ + stripTime: function(date) { + // FIXME: likely to break when crossing DST + // would be better to use a library like momentJS + return new Date(date.getFullYear(), date.getMonth(), date.getDate()); } }; diff --git a/core/js/share.js b/core/js/share.js index e8d486055b0..14abdf18ade 100644 --- a/core/js/share.js +++ b/core/js/share.js @@ -436,7 +436,7 @@ OC.Share={ } } if (share.expiration != null) { - OC.Share.showExpirationDate(share.expiration); + OC.Share.showExpirationDate(share.expiration, share.stime); } }); } @@ -716,7 +716,24 @@ OC.Share={ dirname:function(path) { return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, ''); }, - showExpirationDate:function(date) { + /** + * Displays the expiration date field + * + * @param {Date} date current expiration date + * @param {int} [shareTime] share timestamp in seconds, defaults to now + */ + showExpirationDate:function(date, shareTime) { + var now = new Date(); + var datePickerOptions = { + minDate: now, + maxDate: null + }; + if (_.isNumber(shareTime)) { + shareTime = new Date(shareTime * 1000); + } + if (!shareTime) { + shareTime = now; + } $('#expirationCheckbox').attr('checked', true); $('#expirationDate').val(date); $('#expirationDate').show('blind'); @@ -726,13 +743,14 @@ OC.Share={ }); if (oc_appconfig.core.defaultExpireDateEnforced) { $('#expirationCheckbox').attr('disabled', true); - $.datepicker.setDefaults({ - maxDate : new Date(date.replace(' 00:00:00', '')) - }); + shareTime = OC.Util.stripTime(shareTime).getTime(); + // max date is share date + X days + datePickerOptions.maxDate = new Date(shareTime + oc_appconfig.core.defaultExpireDate * 24 * 3600 * 1000); } if(oc_appconfig.core.defaultExpireDateEnabled) { $('#defaultExpireMessage').show('blind'); } + $.datepicker.setDefaults(datePickerOptions); } }; diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index dd9d4a79277..166210d0312 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -466,6 +466,12 @@ describe('Core base tests', function() { } }); }); + describe('stripTime', function() { + it('strips time from dates', function() { + expect(OC.Util.stripTime(new Date(2014, 2, 24, 15, 4, 45, 24))) + .toEqual(new Date(2014, 2, 24, 0, 0, 0, 0)); + }); + }); }); }); diff --git a/core/js/tests/specs/shareSpec.js b/core/js/tests/specs/shareSpec.js index 00a88ba36ef..32fecf82b65 100644 --- a/core/js/tests/specs/shareSpec.js +++ b/core/js/tests/specs/shareSpec.js @@ -83,16 +83,6 @@ describe('OC.Share tests', function() { expect($el.attr('data-item-source')).toEqual('123'); // TODO: expect that other parts are rendered correctly }); - it('shows default expiration date when set', function() { - oc_appconfig.core.defaultExpireDateEnabled = "yes"; - oc_appconfig.core.defaultExpireDate = ''; - // TODO: expect that default date was set - }); - it('shows default expiration date is set but disabled', function() { - oc_appconfig.core.defaultExpireDateEnabled = "no"; - oc_appconfig.core.defaultExpireDate = ''; - // TODO: expect that default date was NOT set - }); describe('Share with link', function() { // TODO: test ajax calls // TODO: test password field visibility (whenever enforced or not) @@ -265,6 +255,135 @@ describe('OC.Share tests', function() { OC.linkTo('', 'public.php')+'?service=files&t=anothertoken'; expect($('#dropdown #linkText').val()).toEqual(link); }); + describe('expiration date', function() { + var shareData; + var shareItem; + var clock; + + function showDropDown() { + OC.Share.showDropDown( + 'file', + 123, + $container, + 'http://localhost/dummylink', + 31, + 'folder' + ); + } + + beforeEach(function() { + // pick a fake date + clock = sinon.useFakeTimers(new Date(2014, 0, 20, 14, 0, 0).getTime()); + shareItem = { + displayname_owner: 'root', + expiration: null, + file_source: 123, + file_target: '/folder', + id: 20, + item_source: '123', + item_type: 'folder', + mail_send: '0', + parent: null, + path: '/folder', + permissions: OC.PERMISSION_READ, + share_type: OC.Share.SHARE_TYPE_LINK, + share_with: null, + stime: 1403884258, + storage: 1, + token: 'tehtoken', + uid_owner: 'root' + }; + shareData = { + reshare: [], + shares: [] + }; + loadItemStub.returns(shareData); + oc_appconfig.core.defaultExpireDate = 7; + oc_appconfig.core.defaultExpireDateEnabled = false; + oc_appconfig.core.defaultExpireDateEnforced = false; + }); + afterEach(function() { + clock.restore(); + }); + + it('does not check expiration date checkbox when no date was set', function() { + shareItem.expiration = null; + shareData.shares.push(shareItem); + showDropDown(); + expect($('#dropdown [name=expirationCheckbox]').prop('checked')).toEqual(false); + expect($('#dropdown #expirationDate').val()).toEqual(''); + }); + it('does not check expiration date checkbox for new share', function() { + showDropDown(); + expect($('#dropdown [name=expirationCheckbox]').prop('checked')).toEqual(false); + expect($('#dropdown #expirationDate').val()).toEqual(''); + }); + it('checks expiration date checkbox and populates field when expiration date was set', function() { + shareItem.expiration = 1234; + shareData.shares.push(shareItem); + showDropDown(); + expect($('#dropdown [name=expirationCheckbox]').prop('checked')).toEqual(true); + expect($('#dropdown #expirationDate').val()).toEqual('1234'); + }); + it('sets default date when default date setting is enabled', function() { + /* jshint camelcase:false */ + oc_appconfig.core.defaultExpireDateEnabled = true; + showDropDown(); + $('#dropdown [name=linkCheckbox]').click(); + // enabled by default + expect($('#dropdown [name=expirationCheckbox]').prop('checked')).toEqual(true); + // TODO: those zeros must go... + expect($('#dropdown #expirationDate').val()).toEqual('2014-1-27 00:00:00'); + + // disabling is allowed + $('#dropdown [name=expirationCheckbox]').click(); + expect($('#dropdown [name=expirationCheckbox]').prop('checked')).toEqual(false); + }); + it('enforces default date when enforced date setting is enabled', function() { + /* jshint camelcase:false */ + oc_appconfig.core.defaultExpireDateEnabled = true; + oc_appconfig.core.defaultExpireDateEnforced = true; + showDropDown(); + $('#dropdown [name=linkCheckbox]').click(); + expect($('#dropdown [name=expirationCheckbox]').prop('checked')).toEqual(true); + // TODO: those zeros must go... + expect($('#dropdown #expirationDate').val()).toEqual('2014-1-27 00:00:00'); + + // disabling is not allowed + expect($('#dropdown [name=expirationCheckbox]').prop('disabled')).toEqual(true); + $('#dropdown [name=expirationCheckbox]').click(); + expect($('#dropdown [name=expirationCheckbox]').prop('checked')).toEqual(true); + }); + it('sets picker minDate to today and no maxDate by default', function() { + showDropDown(); + $('#dropdown [name=linkCheckbox]').click(); + $('#dropdown [name=expirationCheckbox]').click(); + expect($.datepicker._defaults.minDate).toEqual(new Date()); + expect($.datepicker._defaults.maxDate).toEqual(null); + }); + it('limits the date range to X days after share time when enforced', function() { + /* jshint camelcase:false */ + oc_appconfig.core.defaultExpireDateEnabled = true; + oc_appconfig.core.defaultExpireDateEnforced = true; + showDropDown(); + $('#dropdown [name=linkCheckbox]').click(); + expect($.datepicker._defaults.minDate).toEqual(new Date()); + expect($.datepicker._defaults.maxDate).toEqual(new Date(2014, 0, 27, 0, 0, 0, 0)); + }); + it('limits the date range to X days after share time when enforced, even when redisplayed the next days', function() { + // item exists, was created two days ago + shareItem.expiration = '2014-1-27'; + // share time has time component but must be stripped later + shareItem.stime = new Date(2014, 0, 20, 11, 0, 25).getTime() / 1000; + shareData.shares.push(shareItem); + /* jshint camelcase:false */ + oc_appconfig.core.defaultExpireDateEnabled = true; + oc_appconfig.core.defaultExpireDateEnforced = true; + showDropDown(); + expect($.datepicker._defaults.minDate).toEqual(new Date()); + expect($.datepicker._defaults.maxDate).toEqual(new Date(2014, 0, 27, 0, 0, 0, 0)); + }); + }); }); describe('"sharesChanged" event', function() { var autocompleteOptions; -- 2.39.5