]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix enforced share expiration date to be based on share time
authorVincent Petry <pvince81@owncloud.com>
Mon, 14 Jul 2014 14:05:46 +0000 (16:05 +0200)
committerVincent Petry <pvince81@owncloud.com>
Mon, 21 Jul 2014 13:01:20 +0000 (15:01 +0200)
core/js/js.js
core/js/share.js
core/js/tests/specs/coreSpec.js
core/js/tests/specs/shareSpec.js

index 4a9a5ce82ffbeef3fed65de46632aca94dca18d1..5c9d7bf0141ec2da40c28a308f6639c872a669d6 100644 (file)
@@ -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());
        }
 };
 
index e8d486055b0a0966d26ab2d3354de42f9b3516ac..14abdf18ade180c2896726127d525d61504614d1 100644 (file)
@@ -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);
        }
 };
 
index dd9d4a79277d3a3cfeb9d2b9584344cbd6de0736..166210d0312296ee45c6c82e33b1d7d812a9364c 100644 (file)
@@ -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));
+                       });
+               });
        });
 });
 
index 00a88ba36ef4f55c626694a897fb3a473741f57a..32fecf82b659f87dacc33a4442ac9c5620f0a80d 100644 (file)
@@ -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;