Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use local storage instead of cookies for settings #1023

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
<link rel="shortcut icon" href="${resURL}/favicon.ico" type="image/vnd.microsoft.icon" />

<j:set var="yuiSuffix" value="${h.yuiSuffix}" />
<l:yui module="yahoo" />
<l:yui module="cookie" />

<st:adjunct includes="org.kohsuke.stapler.bind"/>

<j:forEach var="pd" items="${h.pageDecorators}">
Expand Down Expand Up @@ -199,8 +196,7 @@
proxyProvider.configureProxiesUsing(window.bindings);

cookieJarProvider.describe({
label: 'buildMonitor.' + hashCodeOf(document.body.dataset.displayName),
shelfLife: 365
label: 'buildMonitor.' + hashCodeOf(document.body.dataset.displayName)
});
});
</script>
Expand Down
29 changes: 8 additions & 21 deletions build-monitor-plugin/src/main/webapp/scripts/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
angular.
module('buildMonitor.services', ['ui.bootstrap.dialog', 'buildMonitor.templates', 'buildMonitor.cron', 'template/dialog/message.html']).

value('YahooCookie', YAHOO.util.Cookie).
value('persistentStorage', window.localStorage).

service('notifyUser',function ($dialog, $window) {
this.aboutInsufficientSupportOfCSS3 = function(feature) {
Expand Down Expand Up @@ -115,33 +115,21 @@ angular.
};
}]).

provider('cookieJar',function () {
provider('cookieJar', function () {
var defaultAttributes = {
label: '',
shelfLife: 0
label: ''
},
attributes = {};

this.describe = function (cookieJarAttributes) {
attributes = cookieJarAttributes;
}

this.$get = ['YahooCookie', function (YahooCookie) {
return new CookieJar(YahooCookie, angular.extend(defaultAttributes, attributes));
this.$get = ['persistentStorage', function (persistentStorage) {
return new CookieJar(persistentStorage, angular.extend(defaultAttributes, attributes));
}];


function CookieJar(YahooCookie, attributes) {

function expiryDetailsBasedOn(days) {
if (days <= 0) {
return {};
}

return {
expires: new Date(+new Date() + (days * 1000 * 3600 * 24))
}
}
function CookieJar(persistentStorage, attributes) {

function prefixed(name) {
return attributes.label
Expand All @@ -151,11 +139,10 @@ angular.

return {
put: function (name, value) {
YahooCookie.set(prefixed(name), value, expiryDetailsBasedOn(attributes.shelfLife));
persistentStorage.setItem(prefixed(name), value);
},
get: function (name, defaultValue) {
var value = YahooCookie.get(prefixed(name));

var value = persistentStorage.getItem(prefixed(name));
return (value !== null)
? value
: defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,30 @@ describe('buildMonitor', function () {
describe('cookieJar', function () {

var services,
YahooCookie = YAHOO.util.Cookie,
mockedCookie, // YAHOO.util.Cookie backend used by the cookieJar

fakeStorage,
NAME = 'numberOfColumns',
VALUE = 3,
DEFAULT_VALUE = 1;

beforeEach(function () {
services = angular.module('buildMonitor.services');

mockedCookie = sinon.mock(YahooCookie);

var items = {};
var calls = 0;
fakeStorage = {
getItem: function(key){
calls++;
return key in items ? items[key] : null;
},
setItem: function(key, val) {
calls++;
items[key] = "" + val;
},
verify: function(expectedCalls) {
expect(calls).toBe(expectedCalls);
}
};
module('buildMonitor.services', function ($provide) {
$provide.value('YahooCookie', YahooCookie);
$provide.value('persistentStorage', fakeStorage);
});
});

Expand All @@ -33,26 +43,10 @@ describe('buildMonitor', function () {
}));

it('allows for a value to be persisted and then retrieved', inject(function (cookieJar) {
mockedCookie.expects("set").withArgs(NAME, VALUE);
mockedCookie.expects("get").withArgs(NAME).returns(VALUE);


cookieJar.put(NAME, VALUE);
expect(cookieJar.get(NAME)).toBe(VALUE + "");

expect(cookieJar.get(NAME)).toBe(VALUE);

mockedCookie.verify()
}));

it('should use cookies that expire at the end of the session', inject(function (cookieJar) {
var noExpiryDateSpecified = {};

mockedCookie.expects("set").withArgs(NAME, VALUE, noExpiryDateSpecified);


cookieJar.put(NAME, VALUE);

mockedCookie.verify();
fakeStorage.verify(2)
}));
});

Expand All @@ -69,56 +63,20 @@ describe('buildMonitor', function () {
});

it('should prefix each cookie with a label', inject(function (cookieJar) {
mockedCookie.expects("set").withArgs(prefixed(NAME), VALUE);
mockedCookie.expects("get").withArgs(prefixed(NAME));


cookieJar.put(NAME, VALUE);
cookieJar.get(NAME);

mockedCookie.verify();
expect(fakeStorage.getItem(prefixed(NAME))).toBe(VALUE + "");
fakeStorage.verify(3);
}));

afterEach(function() {
mockedCookie.restore();
//storageSpy.restore();
});

function prefixed(name) {
return COOKIE_JAR_LABEL + '.' + name;
};
});

describe('custom shelf life', function() {

var SHELF_LIFE_IN_DAYS = 7;

beforeEach(function () {

services.config(function(cookieJarProvider) {
cookieJarProvider.describe({
shelfLife: SHELF_LIFE_IN_DAYS
});
});
});

it('should use cookies that expire when specified', inject(function (cookieJar) {
mockedCookie.expects("set").withArgs(NAME, VALUE, {
expires: dateIn(SHELF_LIFE_IN_DAYS)}
);

cookieJar.put(NAME, VALUE);

mockedCookie.verify();
}));

afterEach(function() {
mockedCookie.restore();
});

function dateIn(days) {
return new Date(+new Date() + (days * 24 * 3600 * 1000));
};
});
});
});
});
2 changes: 0 additions & 2 deletions build-monitor-plugin/src/test/resources/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ module.exports = function(config) {
'src/test/resources/vendor/angular-mocks-1.5.8.js',
'src/test/resources/vendor/sinon-1.17.7.js',
'src/test/resources/vendor/jasmine-sinon-0.4.0.js',
'src/test/resources/vendor/yahoo-2.9.0.min.js',
'src/test/resources/vendor/yahoo-cookie-2.9.0.min.js',
'src/main/webapp/scripts/**/*.js',
'src/test/javascript/**/*Spec.js', // todo: deprecate the "Spec" suffix in favour of ".spec.js"
'src/test/javascript/**/*.spec.js'
Expand Down

This file was deleted.

This file was deleted.

Loading