-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cookie based tokens for entry/success + test protection
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
Showing
5 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ tmp/* | |
log/*.log | ||
public/static/ | ||
.idea | ||
.bundle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
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.
Sorry, something went wrong.
robyoung
Contributor
|
||
}; | ||
}(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GDS dropped cookies should now be prefixed with 'GDS_'. This also applies to the earlier one.