Skip to content

Commit

Permalink
👋 Goodbye $.cookie (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmelberg-okta committed May 31, 2018
1 parent 23124e1 commit 5dd795f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 80 deletions.
27 changes: 0 additions & 27 deletions THIRD-PARTY-NOTICES
Expand Up @@ -575,33 +575,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


jQuery.cookie
Version (if any): 1.4.0
Brief Description: A simple, lightweight jQuery plugin for reading,
writing and deleting cookies.
License MIT

Copyright 2014 Klaus Hartl

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


jQuery.custominput
Version (if any):
Brief Description: Accessible, custom designed checkbox and radio button
Expand Down
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -97,7 +97,6 @@
"handlebars": "4.0.5",
"jquery": "1.12.1",
"jquery-placeholder": "2.0.7",
"jquery.cookie": "1.4.1",
"q": "1.4.1",
"u2f-api-polyfill": "0.4.1",
"underscore": "1.8.3"
Expand Down
25 changes: 8 additions & 17 deletions src/util/CookieUtil.js
Expand Up @@ -10,35 +10,26 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

define(['okta', 'jquery.cookie'], function (Okta) {

var $ = Okta.$;
define(['okta'], function (Okta) {
var Cookie = Okta.internal.util.Cookie;
var LAST_USERNAME_COOKIE_NAME = 'ln';
var DAYS_SAVE_REMEMBER = 365;

function removeCookie (name) {
$.removeCookie(name, { path: '/' });
}

function setCookie (name, value) {
$.cookie(name, value, {
expires: DAYS_SAVE_REMEMBER,
path: '/'
});
}

var fn = {};

fn.getCookieUsername = function () {
return $.cookie(LAST_USERNAME_COOKIE_NAME);
return Cookie.getCookie(LAST_USERNAME_COOKIE_NAME);
};

fn.setUsernameCookie = function (username) {
setCookie(LAST_USERNAME_COOKIE_NAME, username);
Cookie.setCookie(LAST_USERNAME_COOKIE_NAME, username, {
expires: DAYS_SAVE_REMEMBER,
path: '/'
});
};

fn.removeUsernameCookie = function () {
removeCookie(LAST_USERNAME_COOKIE_NAME);
Cookie.removeCookie(LAST_USERNAME_COOKIE_NAME, { path: '/' });
};

return fn;
Expand Down
23 changes: 14 additions & 9 deletions test/unit/helpers/mocks/Util.js
@@ -1,12 +1,13 @@
/* eslint no-global-assign: 0, max-statements: [2, 32] */
/* eslint no-global-assign: 0, max-statements: [2, 33] */
define([
'okta/jquery',
'okta/underscore',
'backbone',
'vendor/lib/q',
'duo'
'duo',
'shared/util/Cookie'
],
function ($, _, Backbone, Q, Duo) {
function ($, _, Backbone, Q, Duo, Cookie) {

var fn = {};

Expand All @@ -15,12 +16,16 @@ function ($, _, Backbone, Q, Duo) {
'ut tempor eros gravida egestas. Curabitur tempus dignissim justo et pellentesque. ' +
'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.';

// Can mock both set and get
fn.mockCookie = function (name, value) {
spyOn($, 'cookie').and.callFake(function (nameGiven) {
fn.mockGetCookie = function (name, value) {
spyOn(Cookie, 'getCookie').and.callFake(function (nameGiven) {
return name === nameGiven ? value : undefined;
});
return $.cookie;
return Cookie.getCookie;
};

fn.mockSetCookie = function () {
spyOn(Cookie, 'setCookie');
return Cookie.setCookie;
};

fn.mockSDKCookie = function (authClient, key, value) {
Expand All @@ -30,8 +35,8 @@ function ($, _, Backbone, Q, Duo) {
};

fn.mockRemoveCookie = function () {
spyOn($, 'removeCookie');
return $.removeCookie;
spyOn(Cookie, 'removeCookie');
return Cookie.removeCookie;
};

fn.mockRouterNavigate = function (router, start) {
Expand Down
20 changes: 10 additions & 10 deletions test/unit/spec/IDPDiscovery_spec.js
Expand Up @@ -744,7 +744,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, IDPDiscoveryF
});
});
itp('updates security beacon immediately if rememberMe is available', function () {
Util.mockCookie('ln', 'testuser@clouditude.net');
Util.mockGetCookie('ln', 'testuser@clouditude.net');
var options = {
features: {
rememberMe: true,
Expand Down Expand Up @@ -781,7 +781,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, IDPDiscoveryF
});
});
itp('has username in field if rememberMe is available', function () {
Util.mockCookie('ln', 'testuser@clouditude.net');
Util.mockGetCookie('ln', 'testuser@clouditude.net');
var options = {
'features.rememberMe': true
};
Expand All @@ -790,7 +790,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, IDPDiscoveryF
});
});
itp('has rememberMe checked if rememberMe is available', function () {
Util.mockCookie('ln', 'testuser@clouditude.net');
Util.mockGetCookie('ln', 'testuser@clouditude.net');
var options = {
'features.rememberMe': true
};
Expand All @@ -799,7 +799,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, IDPDiscoveryF
});
});
itp('unchecks rememberMe if username is changed', function () {
Util.mockCookie('ln', 'testuser@clouditude.net');
Util.mockGetCookie('ln', 'testuser@clouditude.net');
var options = {
'features.rememberMe': true
};
Expand All @@ -810,7 +810,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, IDPDiscoveryF
});
});
itp('does not re-render rememberMe checkbox on changes', function () {
Util.mockCookie('ln', 'testuser@clouditude.net');
Util.mockGetCookie('ln', 'testuser@clouditude.net');
var options = {
'features.rememberMe': true
};
Expand Down Expand Up @@ -840,7 +840,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, IDPDiscoveryF
});
});
itp('ignores lastUsername and hides rememberMe if features.rememberMe is false and cookie is set', function () {
Util.mockCookie('ln', 'testuser@ABC.com');
Util.mockGetCookie('ln', 'testuser@ABC.com');
var options = {
'features.rememberMe': false
};
Expand All @@ -851,7 +851,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, IDPDiscoveryF
});
});
itp('unchecks rememberMe if username is populated and lastUsername is different from username', function () {
Util.mockCookie('ln', 'testuser@clouditude.net');
Util.mockGetCookie('ln', 'testuser@clouditude.net');
var options = {
'features.rememberMe': true,
'username': 'testuser@ABC.com'
Expand All @@ -862,7 +862,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, IDPDiscoveryF
});
});
itp('checks rememberMe if username is populated and lastUsername is same as username', function () {
Util.mockCookie('ln', 'testuser@ABC.com');
Util.mockGetCookie('ln', 'testuser@ABC.com');
var options = {
'features.rememberMe': true,
'username': 'testuser@ABC.com'
Expand Down Expand Up @@ -963,7 +963,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, IDPDiscoveryF
});
});
itp('sets rememberMe cookie if rememberMe is enabled and checked on submit', function () {
var cookieSpy = Util.mockCookie();
var cookieSpy = Util.mockSetCookie();
return setup({ 'features.rememberMe': true })
.then(function (test) {
test.form.setUsername('testuser@clouditude.net');
Expand All @@ -980,7 +980,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, IDPDiscoveryF
});
});
itp('removes rememberMe cookie if called with existing username and unchecked', function () {
Util.mockCookie('ln', 'testuser@clouditude.net');
Util.mockGetCookie('ln', 'testuser@clouditude.net');
var removeCookieSpy = Util.mockRemoveCookie();
return setup({ 'features.rememberMe': true }).then(function (test) {
test.form.setUsername('testuser@clouditude.net');
Expand Down
20 changes: 10 additions & 10 deletions test/unit/spec/PrimaryAuth_spec.js
Expand Up @@ -1245,7 +1245,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, PrimaryAuthFo
});
});
itp('updates security beacon immediately if rememberMe is available', function () {
Util.mockCookie('ln', 'testuser');
Util.mockGetCookie('ln', 'testuser');
var options = {
features: {
rememberMe: true,
Expand Down Expand Up @@ -1282,7 +1282,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, PrimaryAuthFo
});
});
itp('has username in field if rememberMe is available', function () {
Util.mockCookie('ln', 'testuser');
Util.mockGetCookie('ln', 'testuser');
var options = {
'features.rememberMe': true
};
Expand All @@ -1291,7 +1291,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, PrimaryAuthFo
});
});
itp('has rememberMe checked if rememberMe is available', function () {
Util.mockCookie('ln', 'testuser');
Util.mockGetCookie('ln', 'testuser');
var options = {
'features.rememberMe': true
};
Expand All @@ -1300,7 +1300,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, PrimaryAuthFo
});
});
itp('unchecks rememberMe if username is changed', function () {
Util.mockCookie('ln', 'testuser');
Util.mockGetCookie('ln', 'testuser');
var options = {
'features.rememberMe': true
};
Expand All @@ -1311,7 +1311,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, PrimaryAuthFo
});
});
itp('does not re-render rememberMe checkbox on changes', function () {
Util.mockCookie('ln', 'testuser');
Util.mockGetCookie('ln', 'testuser');
var options = {
'features.rememberMe': true
};
Expand Down Expand Up @@ -1341,7 +1341,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, PrimaryAuthFo
});
});
itp('ignores lastUsername and hides rememberMe if features.rememberMe is false and cookie is set', function () {
Util.mockCookie('ln', 'testuser@ABC.com');
Util.mockGetCookie('ln', 'testuser@ABC.com');
var options = {
'features.rememberMe': false
};
Expand All @@ -1352,7 +1352,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, PrimaryAuthFo
});
});
itp('unchecks rememberMe if username is populated and lastUsername is different from username', function () {
Util.mockCookie('ln', 'testuser');
Util.mockGetCookie('ln', 'testuser');
var options = {
'features.rememberMe': true,
'username': 'testuser@ABC.com'
Expand All @@ -1363,7 +1363,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, PrimaryAuthFo
});
});
itp('checks rememberMe if username is populated and lastUsername is same as username', function () {
Util.mockCookie('ln', 'testuser@ABC.com');
Util.mockGetCookie('ln', 'testuser@ABC.com');
var options = {
'features.rememberMe': true,
'username': 'testuser@ABC.com'
Expand Down Expand Up @@ -1596,7 +1596,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, PrimaryAuthFo
});
});
itp('sets rememberMe cookie if rememberMe is enabled and checked on submit', function () {
var cookieSpy = Util.mockCookie();
var cookieSpy = Util.mockSetCookie();
return setup({ 'features.rememberMe': true })
.then(function (test) {
test.form.setUsername('testuser');
Expand All @@ -1614,7 +1614,7 @@ function (_, $, Q, OktaAuth, LoginUtil, Okta, Util, AuthContainer, PrimaryAuthFo
});
});
itp('removes rememberMe cookie if called with existing username and unchecked', function () {
Util.mockCookie('ln', 'testuser');
Util.mockGetCookie('ln', 'testuser');
var removeCookieSpy = Util.mockRemoveCookie();
return setup({ 'features.rememberMe': true }).then(function (test) {
test.form.setUsername('testuser');
Expand Down
1 change: 0 additions & 1 deletion webpack.release.config.js
Expand Up @@ -29,7 +29,6 @@ entryConfig.externals = {
'amd': 'jquery',
'root': 'jQuery'
},
'jquery.cookie': true,
'handlebars': {
'commonjs': 'handlebars/dist/handlebars',
'commonjs2': 'handlebars/dist/handlebars',
Expand Down

0 comments on commit 5dd795f

Please sign in to comment.