mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-03-09 12:52:56 -04:00
delete cookie stuff to even out js converage
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
(function (window) {
|
||||
"use strict";
|
||||
|
||||
function hasConsentFor (cookieCategory, consentCookie) {
|
||||
if (consentCookie === undefined) { consentCookie = window.GOVUK.getConsentCookie(); }
|
||||
|
||||
if (consentCookie === null) { return false; }
|
||||
|
||||
if (!(cookieCategory in consentCookie)) { return false; }
|
||||
|
||||
return consentCookie[cookieCategory];
|
||||
}
|
||||
|
||||
window.GOVUK.hasConsentFor = hasConsentFor;
|
||||
})(window);
|
||||
@@ -1,104 +0,0 @@
|
||||
window.GOVUK = window.GOVUK || {};
|
||||
window.GOVUK.Modules = window.GOVUK.Modules || {};
|
||||
|
||||
(function (Modules) {
|
||||
function CookieBanner () { }
|
||||
|
||||
CookieBanner.clearOldCookies = function (consent) {
|
||||
var gaCookies = ['_ga', '_gid'];
|
||||
|
||||
// clear old cookie set by our previous JS, set on the www domain
|
||||
if (window.GOVUK.cookie('seen_cookie_message')) {
|
||||
document.cookie = 'seen_cookie_message=;expires=' + new Date().toGMTString() + ';path=/';
|
||||
}
|
||||
|
||||
if (consent === null) {
|
||||
for (var i = 0; i < gaCookies.length; i++) {
|
||||
if (window.GOVUK.cookie(gaCookies[i])) {
|
||||
// GA cookies are set on the base domain so need the www stripping
|
||||
var cookieString = gaCookies[i] + '=;expires=' + new Date().toGMTString() + ';domain=' + window.location.hostname.replace(/^www\./, '.') + ';path=/';
|
||||
document.cookie = cookieString;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
CookieBanner.prototype.start = function ($module) {
|
||||
this.$module = $module[0];
|
||||
this.$module.hideCookieMessage = this.hideCookieMessage.bind(this);
|
||||
this.$module.showConfirmationMessage = this.showConfirmationMessage.bind(this);
|
||||
this.$module.setCookieConsent = this.setCookieConsent.bind(this);
|
||||
|
||||
this.$module.cookieBanner = document.querySelector('.notify-cookie-banner');
|
||||
this.$module.cookieBannerConfirmationMessage = this.$module.querySelector('.notify-cookie-banner__confirmation');
|
||||
|
||||
this.setupCookieMessage();
|
||||
};
|
||||
|
||||
CookieBanner.prototype.setupCookieMessage = function () {
|
||||
this.$hideLink = this.$module.querySelector('button[data-hide-cookie-banner]');
|
||||
if (this.$hideLink) {
|
||||
this.$hideLink.addEventListener('click', this.$module.hideCookieMessage);
|
||||
}
|
||||
|
||||
this.$acceptCookiesLink = this.$module.querySelector('button[data-accept-cookies=true]');
|
||||
if (this.$acceptCookiesLink) {
|
||||
this.$acceptCookiesLink.addEventListener('click', () => this.$module.setCookieConsent(true));
|
||||
}
|
||||
|
||||
this.$rejectCookiesLink = this.$module.querySelector('button[data-accept-cookies=false]');
|
||||
if (this.$rejectCookiesLink) {
|
||||
this.$rejectCookiesLink.addEventListener('click', () => this.$module.setCookieConsent(false));
|
||||
}
|
||||
|
||||
this.showCookieMessage();
|
||||
};
|
||||
|
||||
CookieBanner.prototype.showCookieMessage = function () {
|
||||
// Show the cookie banner if not in the cookie settings page
|
||||
if (!this.isInCookiesPage()) {
|
||||
var hasCookiesPolicy = window.GOVUK.cookie('cookies_policy');
|
||||
|
||||
if (this.$module && !hasCookiesPolicy) {
|
||||
this.$module.style.display = 'block';
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
CookieBanner.prototype.hideCookieMessage = function (event) {
|
||||
if (this.$module) {
|
||||
this.$module.style.display = 'none';
|
||||
}
|
||||
|
||||
if (event.target) {
|
||||
event.preventDefault();
|
||||
}
|
||||
};
|
||||
|
||||
CookieBanner.prototype.setCookieConsent = function (analyticsConsent) {
|
||||
window.GOVUK.setConsentCookie({ 'analytics': analyticsConsent });
|
||||
|
||||
this.$module.showConfirmationMessage(analyticsConsent);
|
||||
this.$module.cookieBannerConfirmationMessage.focus();
|
||||
|
||||
if (analyticsConsent) { window.GOVUK.initAnalytics(); }
|
||||
};
|
||||
|
||||
CookieBanner.prototype.showConfirmationMessage = function (analyticsConsent) {
|
||||
var messagePrefix = analyticsConsent ? 'You’ve accepted analytics cookies.' : 'You told us not to use analytics cookies.';
|
||||
|
||||
this.$cookieBannerMainContent = document.querySelector('.notify-cookie-banner__wrapper');
|
||||
this.$cookieBannerConfirmationMessage = document.querySelector('.notify-cookie-banner__confirmation-message');
|
||||
|
||||
this.$cookieBannerConfirmationMessage.insertAdjacentText('afterbegin', messagePrefix);
|
||||
this.$cookieBannerMainContent.style.display = 'none';
|
||||
this.$module.cookieBannerConfirmationMessage.style.display = 'block';
|
||||
};
|
||||
|
||||
CookieBanner.prototype.isInCookiesPage = function () {
|
||||
return window.location.pathname === '/cookies';
|
||||
};
|
||||
|
||||
Modules.CookieBanner = CookieBanner;
|
||||
})(window.GOVUK.Modules);
|
||||
|
||||
@@ -1,163 +0,0 @@
|
||||
// used by the cookie banner component
|
||||
|
||||
(function (root) {
|
||||
'use strict';
|
||||
window.GOVUK = window.GOVUK || {};
|
||||
|
||||
var DEFAULT_COOKIE_CONSENT = {
|
||||
'analytics': false
|
||||
};
|
||||
|
||||
var COOKIE_CATEGORIES = {
|
||||
'_ga': 'analytics',
|
||||
'_gid': 'analytics'
|
||||
};
|
||||
|
||||
/*
|
||||
Cookie methods
|
||||
==============
|
||||
|
||||
Usage:
|
||||
|
||||
Setting a cookie:
|
||||
GOVUK.cookie('hobnob', 'tasty', { days: 30 });
|
||||
|
||||
Reading a cookie:
|
||||
GOVUK.cookie('hobnob');
|
||||
|
||||
Deleting a cookie:
|
||||
GOVUK.cookie('hobnob', null);
|
||||
*/
|
||||
window.GOVUK.cookie = function (name, value, options) {
|
||||
if (typeof value !== 'undefined') {
|
||||
if (value === false || value === null) {
|
||||
return window.GOVUK.setCookie(name, '', { days: -1 });
|
||||
} else {
|
||||
// Default expiry date of 30 days
|
||||
if (typeof options === 'undefined') {
|
||||
options = { days: 30 };
|
||||
}
|
||||
return window.GOVUK.setCookie(name, value, options);
|
||||
}
|
||||
} else {
|
||||
return window.GOVUK.getCookie(name);
|
||||
}
|
||||
};
|
||||
|
||||
window.GOVUK.getConsentCookie = function () {
|
||||
var consentCookie = window.GOVUK.cookie('cookies_policy');
|
||||
var consentCookieObj;
|
||||
|
||||
if (consentCookie) {
|
||||
try {
|
||||
consentCookieObj = JSON.parse(consentCookie);
|
||||
} catch (err) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof consentCookieObj !== 'object' && consentCookieObj !== null) {
|
||||
consentCookieObj = JSON.parse(consentCookieObj);
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
return consentCookieObj;
|
||||
};
|
||||
|
||||
window.GOVUK.setConsentCookie = function (options) {
|
||||
var cookieConsent = window.GOVUK.getConsentCookie();
|
||||
|
||||
if (!cookieConsent) {
|
||||
cookieConsent = JSON.parse(JSON.stringify(DEFAULT_COOKIE_CONSENT));
|
||||
}
|
||||
|
||||
for (var cookieType in options) {
|
||||
cookieConsent[cookieType] = options[cookieType];
|
||||
|
||||
// Delete cookies of that type if consent being set to false
|
||||
if (!options[cookieType]) {
|
||||
for (var cookie in COOKIE_CATEGORIES) {
|
||||
if (COOKIE_CATEGORIES[cookie] === cookieType) {
|
||||
window.GOVUK.cookie(cookie, null);
|
||||
|
||||
if (window.GOVUK.cookie(cookie)) {
|
||||
document.cookie = cookie + '=;expires=' + new Date() + ';domain=' + window.location.hostname.replace(/^www\./, '.') + ';path=/';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
window.GOVUK.setCookie('cookies_policy', JSON.stringify(cookieConsent), { days: 365 });
|
||||
};
|
||||
|
||||
window.GOVUK.checkConsentCookieCategory = function (cookieName, cookieCategory) {
|
||||
var currentConsentCookie = window.GOVUK.getConsentCookie();
|
||||
|
||||
// If the consent cookie doesn't exist, but the cookie is in our known list, return true
|
||||
if (!currentConsentCookie && COOKIE_CATEGORIES[cookieName]) {
|
||||
return true;
|
||||
}
|
||||
|
||||
currentConsentCookie = window.GOVUK.getConsentCookie();
|
||||
|
||||
// Sometimes currentConsentCookie is malformed in some of the tests, so we need to handle these
|
||||
try {
|
||||
return currentConsentCookie[cookieCategory];
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
window.GOVUK.checkConsentCookie = function (cookieName, cookieValue) {
|
||||
// If we're setting the consent cookie OR deleting a cookie, allow by default
|
||||
if (cookieName === 'cookies_policy' || (cookieValue === null || cookieValue === false)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (COOKIE_CATEGORIES[cookieName]) {
|
||||
var cookieCategory = COOKIE_CATEGORIES[cookieName];
|
||||
|
||||
return window.GOVUK.checkConsentCookieCategory(cookieName, cookieCategory);
|
||||
} else {
|
||||
// Deny the cookie if it is not known to us
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
window.GOVUK.setCookie = function (name, value, options) {
|
||||
if (window.GOVUK.checkConsentCookie(name, value)) {
|
||||
if (typeof options === 'undefined') {
|
||||
options = {};
|
||||
}
|
||||
var cookieString = name + '=' + value + '; path=/';
|
||||
if (options.days) {
|
||||
var date = new Date();
|
||||
date.setTime(date.getTime() + (options.days * 24 * 60 * 60 * 1000));
|
||||
cookieString = cookieString + '; expires=' + date.toGMTString();
|
||||
}
|
||||
if (document.location.protocol === 'https:') {
|
||||
cookieString = cookieString + '; Secure';
|
||||
}
|
||||
document.cookie = cookieString;
|
||||
}
|
||||
};
|
||||
|
||||
window.GOVUK.getCookie = function (name) {
|
||||
var nameEQ = name + '=';
|
||||
var cookies = document.cookie.split(';');
|
||||
for (var i = 0, len = cookies.length; i < len; i++) {
|
||||
var cookie = cookies[i];
|
||||
while (cookie.charAt(0) === ' ') {
|
||||
cookie = cookie.substring(1, cookie.length);
|
||||
}
|
||||
if (cookie.indexOf(nameEQ) === 0) {
|
||||
return decodeURIComponent(cookie.substring(nameEQ.length));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
}(window));
|
||||
|
||||
@@ -1,151 +0,0 @@
|
||||
{% extends "withoutnav_template.html" %}
|
||||
{% from "components/banner.html" import banner %}
|
||||
|
||||
{% block per_page_title %}
|
||||
Cookies
|
||||
{% endblock %}
|
||||
|
||||
{% block cookie_message %}{% endblock %}
|
||||
|
||||
{% block maincolumn_content %}
|
||||
|
||||
<div class="grid-row">
|
||||
<div class="grid-col-8">
|
||||
<div class="cookie-settings__confirmation banner banner-with-tick" data-cookie-confirmation="true" role="group" tabindex="-1">
|
||||
<h2 class="banner-title">Your cookie settings were saved</h2>
|
||||
<a class="govuk_link cookie-settings__prev-page" href="#" data-module="track-click" data-track-category="cookieSettings" data-track-action="Back to previous page">
|
||||
Go back to the page you were looking at
|
||||
</a>
|
||||
</div>
|
||||
<h1 class="font-body-2xl margin-bottom-3">Cookies</h1>
|
||||
<p class="summary">
|
||||
Cookies are small files saved on your phone, tablet, or computer when you visit a website.
|
||||
</p>
|
||||
<p>We use cookies to make Notify.gov work and collect information about how you use our service.</p>
|
||||
|
||||
<h2 class="font-body-lg" id="essential-cookies">Essential cookies</h2>
|
||||
<p>
|
||||
Essential cookies keep your information secure while you use Notify. We do not need to ask permission to use them.
|
||||
</p>
|
||||
<table>
|
||||
<caption class="usa-sr-only">Essential cookies</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Purpose</th>
|
||||
<th>Expires</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>
|
||||
notify_admin_session
|
||||
</td>
|
||||
<td width="50%">
|
||||
Used to keep you signed in
|
||||
</td>
|
||||
<td>
|
||||
20 hours
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
cookie_policy
|
||||
</td>
|
||||
<td width="50%">
|
||||
Saves your cookie consent settings
|
||||
</td>
|
||||
<td>
|
||||
1 year
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2 class="font-body-lg" id="analytics-cookies">Analytics cookies (optional)</h2>
|
||||
<p>
|
||||
With your permission, we use Google Analytics to collect data about how you use Notify. This information helps us to improve our service.
|
||||
</p>
|
||||
<p>
|
||||
Google is not allowed to use or share our analytics data with anyone.
|
||||
</p>
|
||||
<p>
|
||||
Google Analytics stores anonymized information about:
|
||||
</p>
|
||||
<ul class="usa-list usa-list--bullet">
|
||||
<li>how you got to Notify.gov</li>
|
||||
<li>the pages you visit on Notify and how long you spend on them</li>
|
||||
<li>any errors you see while using Notify</li>
|
||||
</ul>
|
||||
<table>
|
||||
<caption class="usa-sr-only">Google Analytics cookies</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Purpose</th>
|
||||
<th>Expires</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>
|
||||
_ga
|
||||
</td>
|
||||
<td width="50%">
|
||||
Checks if you’ve visited Notify before. This helps us count how many people visit our site.
|
||||
</td>
|
||||
<td>
|
||||
2 years
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
_gid
|
||||
</td>
|
||||
<td width="50%">
|
||||
Checks if you’ve visited Notify before. This helps us count how many people visit our site.
|
||||
</td>
|
||||
<td>
|
||||
24 hours
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="cookie-settings__no-js">
|
||||
<h2 class="govuk-heading-s">Do you want to accept analytics cookies?</h2>
|
||||
<p>We use Javascript to set our analytics cookies. Unfortunately Javascript is not running on your browser, so you cannot change your settings. You can try:</p>
|
||||
<ul class="usa-list usa-list--bullet">
|
||||
<li>reloading the page</li>
|
||||
<li>turning on Javascript in your browser</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="cookie-settings__form-wrapper">
|
||||
<form data-module="cookie-settings">
|
||||
<div class="usa-form-group">
|
||||
<fieldset class="usa-fieldset" aria-describedby="changed-name-hint">
|
||||
<legend class="usa-fieldset__legend usa-fieldset__legend--s">
|
||||
Do you want to accept analytics cookies?
|
||||
</legend>
|
||||
<div class="usa-radios usa-radios--inline">
|
||||
<div class="usa-radio">
|
||||
<input class="usa-radio__input" id="cookies-analytics-yes" name="cookies-analytics" type="radio" value="on">
|
||||
<label class="usa-radio__label" for="cookies-analytics-yes">
|
||||
Yes
|
||||
</label>
|
||||
</div>
|
||||
<div class="usa-radio">
|
||||
<input class="usa-radio__input" id="cookies-analytics-no" name="cookies-analytics" type="radio" value="off">
|
||||
<label class="usa-radio__label" for="cookies-analytics-no">
|
||||
No
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
</div>
|
||||
<button class="govuk-button" type="submit">Save cookie settings</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
@@ -1,55 +0,0 @@
|
||||
const helpers = require('./support/helpers');
|
||||
|
||||
beforeAll(() => {
|
||||
|
||||
require('../../app/assets/javascripts/govuk/cookie-functions.js');
|
||||
require('../../app/assets/javascripts/consent.js');
|
||||
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
|
||||
require('./support/teardown.js');
|
||||
|
||||
});
|
||||
|
||||
describe("Cookie consent", () => {
|
||||
|
||||
describe("hasConsentFor", () => {
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
// remove cookie set by tests
|
||||
helpers.deleteCookie('cookies_policy');
|
||||
|
||||
});
|
||||
|
||||
test("If there is no consent cookie, return false", () => {
|
||||
|
||||
expect(window.GOVUK.hasConsentFor('analytics')).toBe(false);
|
||||
|
||||
});
|
||||
|
||||
describe("If a consent cookie is set", () => {
|
||||
|
||||
test("If the category is not saved in the cookie, return false", () => {
|
||||
|
||||
window.GOVUK.setConsentCookie({ 'usage': true });
|
||||
|
||||
expect(window.GOVUK.hasConsentFor('analytics')).toBe(false);
|
||||
|
||||
});
|
||||
|
||||
test("If the category is saved in the cookie, return its value", () => {
|
||||
|
||||
window.GOVUK.setConsentCookie({ 'analytics': true });
|
||||
|
||||
expect(window.GOVUK.hasConsentFor('analytics')).toBe(true);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@@ -1,7 +1,6 @@
|
||||
const globals = require('./helpers/globals.js');
|
||||
const events = require('./helpers/events.js');
|
||||
const domInterfaces = require('./helpers/dom_interfaces.js');
|
||||
const cookies = require('./helpers/cookies.js');
|
||||
const html = require('./helpers/html.js');
|
||||
const elements = require('./helpers/elements.js');
|
||||
const rendering = require('./helpers/rendering.js');
|
||||
@@ -15,8 +14,6 @@ exports.moveSelectionToRadio = events.moveSelectionToRadio;
|
||||
exports.activateRadioWithSpace = events.activateRadioWithSpace;
|
||||
exports.RangeMock = domInterfaces.RangeMock;
|
||||
exports.SelectionMock = domInterfaces.SelectionMock;
|
||||
exports.deleteCookie = cookies.deleteCookie;
|
||||
exports.setCookie = cookies.setCookie;
|
||||
exports.getRadioGroup = html.getRadioGroup;
|
||||
exports.getRadios = html.getRadios;
|
||||
exports.templatesAndFoldersCheckboxes = html.templatesAndFoldersCheckboxes;
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
// Helper for deleting a cookie
|
||||
function deleteCookie (cookieName, options) {
|
||||
if (typeof options === 'undefined') {
|
||||
options = {};
|
||||
}
|
||||
if (!options.domain) { options.domain = window.location.hostname; }
|
||||
document.cookie = cookieName + '=; path=/; domain=' + options.domain + '; expires=' + (new Date());
|
||||
};
|
||||
|
||||
function setCookie (name, value, options) {
|
||||
if (typeof options === 'undefined') {
|
||||
options = {};
|
||||
}
|
||||
if (!options.domain) { options.domain = window.location.hostname; }
|
||||
var cookieString = name + '=' + value + '; path=/; domain=' + options.domain;
|
||||
if (options.days) {
|
||||
var date = new Date();
|
||||
date.setTime(date.getTime() + (options.days * 24 * 60 * 60 * 1000));
|
||||
cookieString = cookieString + '; expires=' + date.toGMTString();
|
||||
}
|
||||
document.cookie = cookieString;
|
||||
};
|
||||
|
||||
exports.deleteCookie = deleteCookie;
|
||||
exports.setCookie = setCookie;
|
||||
Reference in New Issue
Block a user