Skip to content

Commit

Permalink
Cookie based tokens for entry/success + test protection
Browse files Browse the repository at this point in the history
The jasmine tests will now whine at you
if you attempt to run them outside of
firefox... this seems okay as we don't
seem to be using phantom for ci.
  • Loading branch information
Gareth Rogers committed Oct 9, 2012
1 parent 2f8aecb commit 3e5fdb4
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ tmp/*
log/*.log
public/static/
.idea
.bundle
48 changes: 41 additions & 7 deletions app/assets/javascripts/analytics/events-cookie-handler.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var GOVUK = GOVUK || {};
GOVUK.Analytics = GOVUK.Analytics || {};

GOVUK.Analytics.internalSiteEvents = function() {
GOVUK.Analytics.internalSiteEvents = function () {

var eventQueue = [];

var loadCookie = function() {
var loadCookie = function () {
var value = Alphagov.read_cookie("successEvents");
if (value) {
value = jQuery.parseJSON(jQuery.base64Decode(value));
Expand All @@ -15,22 +15,56 @@ GOVUK.Analytics.internalSiteEvents = function() {
eventQueue = value;
};

var sendCookieEvents = function() {
var sendCookieEvents = function () {
loadCookie();
$(eventQueue).each(function() {
$(eventQueue).each(function () {
GOVUK.sendToAnalytics(this);
});
eventQueue = [];
Alphagov.delete_cookie("successEvents");
};

var pushCookieEvent = function(event) {
var pushCookieEvent = function (event) {
eventQueue.push(event);
Alphagov.write_cookie("successEvents", jQuery.base64Encode(JSON.stringify(eventQueue)));
};

return {
push: pushCookieEvent,
sendAll: sendCookieEvents
push:pushCookieEvent,
sendAll:sendCookieEvents
};
}();

GOVUK.Analytics.entryTokens = function () {

var COOKIE_NAME = 'analyticsTokens';

This comment has been minimized.

Copy link
@robyoung

robyoung Oct 10, 2012

Contributor

GDS dropped cookies should now be prefixed with 'GDS_'. This also applies to the earlier one.

This comment has been minimized.

Copy link
@pbadenski

pbadenski Oct 10, 2012

Contributor

Fixed


var valueIsInArray = function (value, arr) {
return $.inArray(value, arr) === -1;
};

var assignToken = function () {
var tokens = JSON.parse(Alphagov.read_cookie(COOKIE_NAME));
if (!tokens) tokens = [];
if (valueIsInArray(GOVUK.Analytics.NeedID, tokens))
{
tokens.push(GOVUK.Analytics.NeedID);
Alphagov.write_cookie(COOKIE_NAME, JSON.stringify(tokens));
}
};

var revokeToken = function () {
var tokens = JSON.parse(Alphagov.read_cookie(COOKIE_NAME));
var positionOfToken = $.inArray(GOVUK.Analytics.NeedID,tokens);
if (positionOfToken !== -1) {
tokens.splice(positionOfToken,1);
Alphagov.write_cookie(COOKIE_NAME,JSON.stringify(tokens));
}
};

return {
assignToken:assignToken,
revokeToken:revokeToken,
COOKIE_NAME:COOKIE_NAME

This comment has been minimized.

Copy link
@robyoung

robyoung Oct 10, 2012

Contributor

Does this need to be exposed? As far as I can see it's only exposed for the tests.

This comment has been minimized.

Copy link
@pbadenski

pbadenski Oct 10, 2012

Contributor

Fixed

};
}();
70 changes: 70 additions & 0 deletions spec/javascripts/AnalyticsTokenSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
describe("analytics cookie tokens", function () {

var cookieName = GOVUK.Analytics.entryTokens.COOKIE_NAME;

beforeEach(function () {
Alphagov.delete_cookie(cookieName)

});

it("should write a token into a cookie array", function () {
GOVUK.Analytics.NeedID = 12;
GOVUK.Analytics.entryTokens.assignToken();

expect(Alphagov.read_cookie(cookieName)).toBe("[12]");
});

it("should write multiple tokens to the array", function () {
GOVUK.Analytics.NeedID = 12;
GOVUK.Analytics.entryTokens.assignToken();
GOVUK.Analytics.NeedID = 42;
GOVUK.Analytics.entryTokens.assignToken();

expect(Alphagov.read_cookie(cookieName)).toBe("[12,42]");
});

it("should not write the same token multiple times", function () {
GOVUK.Analytics.NeedID = 15;
GOVUK.Analytics.entryTokens.assignToken();
GOVUK.Analytics.entryTokens.assignToken();

expect(Alphagov.read_cookie(cookieName)).toBe("[15]");
});

it("should remove a token from the cookie array", function () {
GOVUK.Analytics.NeedID = 20;
GOVUK.Analytics.entryTokens.assignToken();

GOVUK.Analytics.entryTokens.revokeToken();

expect(Alphagov.read_cookie(cookieName)).toBe("[]");
});

it("should preserve the remaining tokens when one is removed", function () {
GOVUK.Analytics.NeedID = 16;
GOVUK.Analytics.entryTokens.assignToken();
GOVUK.Analytics.NeedID = 19;
GOVUK.Analytics.entryTokens.assignToken();

GOVUK.Analytics.entryTokens.revokeToken();

expect(Alphagov.read_cookie(cookieName)).toBe("[16]");
});

it("should not blow up if a token is revoked when the cookie array is empty", function () {
GOVUK.Analytics.NeedID = 33;
GOVUK.Analytics.entryTokens.revokeToken();

// should not throw any error
});

it("should not blow up if a nonexistent token is revoked", function () {
GOVUK.Analytics.NeedID = 21;
GOVUK.Analytics.entryTokens.assignToken();
GOVUK.Analytics.NeedID = 42;

GOVUK.Analytics.entryTokens.revokeToken();

expect(Alphagov.read_cookie(cookieName)).toBe("[21]");
});
});
11 changes: 11 additions & 0 deletions spec/javascripts/ShouldOnlyRunTestsOnFirefox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
describe("the tests", function () {

it("should only be run on firefox - TESTS WILL FAIL ON CHROME DUE TO NOT BEING ABLE TO WRITE COOKIES TO LOCALHOST, PLEASE RE-RUN IN FIREFOX!!!!", function () {
if (!$.browser.mozilla) {
alert("your tests are failing because you are not running them on firefox!!!")
}

expect($.browser.mozilla).toBeTruthy();
});

});
1 change: 1 addition & 0 deletions spec/javascripts/support/jasmine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ helpers:
# - **/*[sS]pec.js
#
spec_files:
- 'ShouldOnlyRunTestsOnFirefox.js'
- '**/*[sS]pec.js'

# src_dir
Expand Down

0 comments on commit 3e5fdb4

Please sign in to comment.