[Pkg-owncloud-commits] [owncloud] 24/61: Fix enforced share expiration date to be based on share time

David Prévot taffit at moszumanska.debian.org
Thu Jul 31 03:51:43 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud.

commit 8757a99f789614a9d05ec98a0e347f2f056b0710
Author: Vincent Petry <pvince81 at owncloud.com>
Date:   Mon Jul 14 16:05:46 2014 +0200

    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 4a9a5ce..5c9d7bf 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 e8d4860..14abdf1 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 dd9d4a7..166210d 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 00a88ba..32fecf8 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;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list