Skip to content

Commit

Permalink
Merge pull request #2312 from BobrImperator/2308-adaptive-store-DI
Browse files Browse the repository at this point in the history
[2308] Refactor Adaptive store to use DI
  • Loading branch information
BobrImperator authored Aug 13, 2021
2 parents ae243db + d976a4d commit e797c0a
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 219 deletions.
41 changes: 27 additions & 14 deletions packages/ember-simple-auth/addon/session-stores/adaptive.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { computed } from '@ember/object';
import { inject as service } from '@ember/service';
import { getOwner } from '@ember/application';
import Base from 'ember-simple-auth/session-stores/base';
import LocalStorage from 'ember-simple-auth/session-stores/local-storage';
import Cookie from 'ember-simple-auth/session-stores/cookie';

const LOCAL_STORAGE_TEST_KEY = '_ember_simple_auth_test_key';

Expand Down Expand Up @@ -102,8 +100,21 @@ export default Base.extend({
_cookieExpirationTime: null,
cookieExpirationTime: proxyToInternalStore(),

_sameSite: null,
sameSite: proxyToInternalStore(),

_cookies: service('cookies'),

_isLocalStorageAvailable: computed({
get() {
return testLocalStorageAvailable();
},

set(key, value) {
return value;
}
}),

init() {
this._super(...arguments);

Expand All @@ -112,28 +123,30 @@ export default Base.extend({
this._fastboot = owner.lookup('service:fastboot');
}

this._isLocalStorageAvailable = this.hasOwnProperty('_isLocalStorageAvailable') ? this._isLocalStorageAvailable : testLocalStorageAvailable();

let store;
if (this.get('_isLocalStorageAvailable')) {
const localStorage = owner.lookup('session-store:local-storage');
const options = { key: this.get('localStorageKey') };

options._isFastBoot = false;
store = this._createStore(LocalStorage, options);
localStorage.setProperties(options);

store = localStorage;
} else {
const options = this.getProperties('cookieDomain', 'cookieName', 'cookieExpirationTime', 'cookiePath');
options._fastboot = this.get('_fastboot');
options._cookies = this.get('_cookies');
const cookieStorage = owner.lookup('session-store:cookie');
const options = this.getProperties('sameSite', 'cookieDomain', 'cookieName', 'cookieExpirationTime', 'cookiePath');

cookieStorage._initialize(options);
this.set('cookieExpirationTime', cookieStorage.get('cookieExpirationTime'));

store = this._createStore(Cookie, options);
this.set('cookieExpirationTime', store.get('cookieExpirationTime'));
store = cookieStorage;
}

this.set('_store', store);
this._setupStoreEvents(store);
},

_createStore(storeType, options) {
let owner = getOwner(this);
const store = storeType.create(owner.ownerInjection(), options);

_setupStoreEvents(store) {
store.on('sessionDataUpdated', (data) => {
this.trigger('sessionDataUpdated', data);
});
Expand Down
11 changes: 10 additions & 1 deletion packages/ember-simple-auth/addon/session-stores/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,20 @@ export default BaseStore.extend({

init() {
this._super(...arguments);

let owner = getOwner(this);
if (owner && !this.hasOwnProperty('_fastboot')) {
this._fastboot = owner.lookup('service:fastboot');
}
},

_initialize(options) {
const clonedOptions = Object.assign({}, options);
if (clonedOptions.cookieName) {
this.set('_cookieName', clonedOptions.cookieName);
}

delete clonedOptions.cookieName;
this.setProperties(clonedOptions);

let cachedExpirationTime = this._read(`${this.get('cookieName')}-expiration_time`);
if (cachedExpirationTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import Configuration from 'ember-simple-auth/configuration';
import setupSession from 'ember-simple-auth/initializers/setup-session';
import setupSessionService from 'ember-simple-auth/initializers/setup-session-service';
import setupSessionRestoration from 'ember-simple-auth/initializers/setup-session-restoration';
import Adaptive from 'ember-simple-auth/session-stores/adaptive';
import LocalStorage from 'ember-simple-auth/session-stores/local-storage';
import Cookie from 'ember-simple-auth/session-stores/cookie';

export default {
name: 'ember-simple-auth',
Expand All @@ -12,6 +15,10 @@ export default {
config.rootURL = ENV.rootURL || ENV.baseURL;
Configuration.load(config);

registry.register('session-store:adaptive', Adaptive);
registry.register('session-store:cookie', Cookie);
registry.register('session-store:local-storage', LocalStorage);

setupSession(registry);
setupSessionService(registry);
setupSessionRestoration(registry);
Expand Down
22 changes: 5 additions & 17 deletions packages/ember-simple-auth/tests/helpers/create-adaptive-store.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
import Adaptive from 'ember-simple-auth/session-stores/adaptive';
import createCookieStore from './create-cookie-store';
import { merge, assign as emberAssign } from '@ember/polyfills';

const assign = emberAssign || merge;

export default function createAdaptiveStore(
cookiesService,
options = {},
props = {}
owner
) {
let cookieStore = createCookieStore(
cookiesService,
assign({}, options, { _isFastboot: false })
);
props._createStore = function() {
cookieStore.on('sessionDataUpdated', data => {
this.trigger('sessionDataUpdated', data);
});

return cookieStore;
};
owner.register('session-store:adaptive', Adaptive.extend(Object.assign({
_isLocalStorageAvailable: false,
}, options)));

return Adaptive.extend(props).create(options);
return owner.lookup('session-store:adaptive');
}

This file was deleted.

129 changes: 66 additions & 63 deletions packages/ember-simple-auth/tests/unit/session-stores/adaptive-test.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,101 @@
import { run } from '@ember/runloop';
import {
describe,
beforeEach,
afterEach,
it
} from 'mocha';
import { describe, it } from 'mocha';
import { expect } from 'chai';
import sinonjs from 'sinon';
import Adaptive from 'ember-simple-auth/session-stores/adaptive';
import LocalStorage from 'ember-simple-auth/session-stores/local-storage';
import itBehavesLikeAStore from './shared/store-behavior';
import itBehavesLikeACookieStore from './shared/cookie-store-behavior';
import FakeCookieService from '../../helpers/fake-cookie-service';
import createAdaptiveStore from '../../helpers/create-adaptive-store';
import { setupTest } from 'ember-mocha';

describe('AdaptiveStore', () => {
let sinon;
let store;

beforeEach(function() {
sinon = sinonjs.createSandbox();
});

afterEach(function() {
store.clear();
sinon.restore();
});
setupTest();

describe('when localStorage is available', function() {
beforeEach(function() {
store = Adaptive.extend({
_createStore(storeType, options) {
return LocalStorage.create({ _isFastBoot: false }, options);
}
}).create({
_isLocalStorageAvailable: true
});
});

itBehavesLikeAStore({
store() {
store(sinon, owner) {
let store;
let cookieService;
owner.register('service:cookies', FakeCookieService);
cookieService = owner.lookup('service:cookies');
sinon.spy(cookieService, 'read');
sinon.spy(cookieService, 'write');
store = createAdaptiveStore(cookieService, {
_isLocalStorageAvailable: true,
}, owner);
return store;
}
});
});

describe('when localStorage is not available', function() {
let cookieService;
beforeEach(function() {
cookieService = FakeCookieService.create();
sinon.spy(cookieService, 'read');
sinon.spy(cookieService, 'write');
store = createAdaptiveStore(cookieService, {
_isLocal: false,
_cookies: cookieService,
});
});

itBehavesLikeAStore({
store() {
store(sinon, owner) {
let store;
let cookieService;
owner.register('service:cookies', FakeCookieService);
cookieService = owner.lookup('service:cookies');
sinon.spy(cookieService, 'read');
sinon.spy(cookieService, 'write');
store = createAdaptiveStore(cookieService, {
_isLocalStorageAvailable: false,
_cookieName: 'test:session',
}, owner);
return store;
}
});
});

itBehavesLikeACookieStore({
createStore(cookieService, options = {}) {
options._isLocalStorageAvailable = false;
return createAdaptiveStore(cookieService, options, {
_cookies: cookieService,
_fastboot: { isFastBoot: false },
});
},
renew(store, data) {
return store.get('_store')._renew(data);
},
sync(store) {
store.get('_store')._syncData();
},
spyRewriteCookieMethod(store) {
sinon.spy(store.get('_store'), 'rewriteCookie');
return store.get('_store').rewriteCookie;
}
describe('CookieStore', function() {
describe('Behaviour', function() {
itBehavesLikeACookieStore({
store(sinon, owner, storeOptions) {
owner.register('service:cookies', FakeCookieService);
let cookieService = owner.lookup('service:cookies');
sinon.spy(cookieService, 'read');
sinon.spy(cookieService, 'write');
let store = createAdaptiveStore(cookieService, Object.assign({
_isLocalStorageAvailable: false,
_cookieName: 'test:session',
}, storeOptions), owner);
return store;
},
renew(store, data) {
return store.get('_store')._renew(data);
},
sync(store) {
store.get('_store')._syncData();
},
spyRewriteCookieMethod(sinon, store) {
sinon.spy(store.get('_store'), 'rewriteCookie');
return store.get('_store').rewriteCookie;
}
});
});

it('persists to cookie when cookie attributes change', function() {
let now = new Date();
it('persists to cookie when cookie attributes change', async function() {
let sinon = sinonjs.createSandbox();
let store;
let cookieService;
let now;

this.owner.register('service:cookies', FakeCookieService);
cookieService = this.owner.lookup('service:cookies');
sinon.spy(cookieService, 'read');
sinon.spy(cookieService, 'write');
run(() => {
store.persist({ key: 'value' });
now = new Date();
store = createAdaptiveStore(cookieService, {
_isLocalStorageAvailable: false,
_cookieName: 'test:session',
}, this.owner);

store.setProperties({
cookieName: 'test:session',
cookieExpirationTime: 60
});
});
await store.persist({ key: 'value' });

expect(cookieService.write).to.have.been.calledWith(
'test:session-expiration_time',
Expand Down
Loading

0 comments on commit e797c0a

Please sign in to comment.