From d5733acf18be9d715973e095fff9191f1903924a Mon Sep 17 00:00:00 2001 From: Tobias Gurtzick Date: Fri, 13 May 2016 10:56:19 +0200 Subject: [PATCH] change cookie default key names to be rfc2616 compliant (#978) fixes #977 --- addon/session-stores/adaptive.js | 8 +++---- addon/session-stores/cookie.js | 6 ++--- addon/session-stores/local-storage.js | 4 ++-- .../shared/cookie-store-behavior.js | 24 +++++++++---------- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/addon/session-stores/adaptive.js b/addon/session-stores/adaptive.js index 73585b4cd..5f219a6ae 100644 --- a/addon/session-stores/adaptive.js +++ b/addon/session-stores/adaptive.js @@ -28,10 +28,10 @@ export default Base.extend({ @property localStorageKey @type String - @default 'ember_simple_auth:session' + @default 'ember_simple_auth-session' @public */ - localStorageKey: 'ember_simple_auth:session', + localStorageKey: 'ember_simple_auth-session', /** The domain to use for the cookie if `localStorage` is not available, e.g., @@ -51,10 +51,10 @@ export default Base.extend({ @property cookieName @type String - @default ember_simple_auth:session + @default ember_simple_auth-session @public */ - cookieName: 'ember_simple_auth:session', + cookieName: 'ember_simple_auth-session', /** The expiration time for the cookie in seconds if `localStorage` is not diff --git a/addon/session-stores/cookie.js b/addon/session-stores/cookie.js index 7116e19cf..ea505be5c 100644 --- a/addon/session-stores/cookie.js +++ b/addon/session-stores/cookie.js @@ -60,10 +60,10 @@ export default BaseStore.extend({ @property cookieName @type String - @default ember_simple_auth:session + @default ember_simple_auth-session @public */ - cookieName: 'ember_simple_auth:session', + cookieName: 'ember_simple_auth-session', /** The expiration time for the cookie in seconds. A value of `null` will make @@ -174,7 +174,7 @@ export default BaseStore.extend({ }, _calculateExpirationTime() { - let cachedExpirationTime = this._read(`${this.cookieName}:expiration_time`); + let cachedExpirationTime = this._read(`${this.cookieName}-expiration_time`); cachedExpirationTime = !!cachedExpirationTime ? new Date().getTime() + cachedExpirationTime * 1000 : null; return !!this.cookieExpirationTime ? new Date().getTime() + this.cookieExpirationTime * 1000 : cachedExpirationTime; }, diff --git a/addon/session-stores/local-storage.js b/addon/session-stores/local-storage.js index 68c2e6395..c856cc53c 100644 --- a/addon/session-stores/local-storage.js +++ b/addon/session-stores/local-storage.js @@ -25,10 +25,10 @@ export default BaseStore.extend({ @property key @type String - @default 'ember_simple_auth:session' + @default 'ember_simple_auth-session' @public */ - key: 'ember_simple_auth:session', + key: 'ember_simple_auth-session', init() { this._super(...arguments); diff --git a/tests/unit/session-stores/shared/cookie-store-behavior.js b/tests/unit/session-stores/shared/cookie-store-behavior.js index e38049290..6e9832d5a 100644 --- a/tests/unit/session-stores/shared/cookie-store-behavior.js +++ b/tests/unit/session-stores/shared/cookie-store-behavior.js @@ -31,33 +31,32 @@ export default function(options) { describe('#persist', function() { it('respects the configured cookieName', () => { - store = createStore(cookieService, { cookieName: 'test:session' }); + store = createStore({ cookieName: 'test-session' }); store.persist({ key: 'value' }); - expect(cookieService.write).to.have.been.calledWith('test:session', JSON.stringify({ key: 'value' }), { domain: null, expires: null, path: '/', secure: false }); + expect(document.cookie).to.contain('test-session=%7B%22key%22%3A%22value%22%7D'); }); it('respects the configured cookieDomain', () => { store = createStore(cookieService, { cookieDomain: 'example.com' }); store.persist({ key: 'value' }); - expect(cookieService.write).to.have.been.calledWith('ember_simple_auth:session', JSON.stringify({ key: 'value' }), { domain: 'example.com', expires: null, path: '/', secure: false }); + expect(document.cookie).to.not.contain('test-session=%7B%22key%22%3A%22value%22%7D'); }); }); describe('#renew', () => { - beforeEach((done) => { - store = createStore(cookieService, { - cookieName: 'test:session', + beforeEach(() => { + store = createStore({ + cookieName: 'test-session', cookieExpirationTime: 60, expires: new Date().getTime() + store.cookieExpirationTime * 1000 }); - store.persist({ key: 'value' }); - renew(store).then(done); }); - // TODO: the "…:expiration_time" never actually seems to get written - it('stores the expiration time in a cookie named "test:session:expiration_time"'); + it('stores the expiration time in a cookie named "test-session-expiration_time"', () => { + expect(document.cookie).to.contain(`${store.cookieName}-expiration_time=60`); + }); }); describe('the "sessionDataUpdated" event', () => { @@ -72,7 +71,7 @@ export default function(options) { }); it('is not triggered when the cookie has not actually changed', (done) => { - document.cookie = 'ember_simple_auth:session=%7B%22key%22%3A%22value%22%7D;path=/;'; + document.cookie = 'ember_simple_auth-session=%7B%22key%22%3A%22value%22%7D;path=/;'; sync(store); Ember.run.next(() => { @@ -82,8 +81,7 @@ export default function(options) { }); it('is triggered when the cookie changed', (done) => { - const cookiesService = store.get('_cookies') || store.get('_store._cookies'); - cookiesService._content['ember_simple_auth:session'] = '%7B%22key%22%3A%22other%20value%22%7D'; + document.cookie = 'ember_simple_auth-session=%7B%22key%22%3A%22other%20value%22%7D;path=/;'; sync(store); Ember.run.next(() => {