Skip to content

Commit

Permalink
change cookie default key names to be rfc2616 compliant (mainmatter#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzrdtales authored and Arjan Singh committed Jul 27, 2016
1 parent 98ed600 commit d5733ac
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
8 changes: 4 additions & 4 deletions addon/session-stores/adaptive.js
Expand Up @@ -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.,
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions addon/session-stores/cookie.js
Expand Up @@ -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
Expand Down Expand Up @@ -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;
},
Expand Down
4 changes: 2 additions & 2 deletions addon/session-stores/local-storage.js
Expand Up @@ -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);
Expand Down
24 changes: 11 additions & 13 deletions tests/unit/session-stores/shared/cookie-store-behavior.js
Expand Up @@ -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', () => {
Expand All @@ -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(() => {
Expand All @@ -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(() => {
Expand Down

0 comments on commit d5733ac

Please sign in to comment.