Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2302] put internal-session into DI system #2315

Merged
merged 1 commit into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import Ember from 'ember';
import InternalSession from '../internal-session';
import Ephemeral from '../session-stores/ephemeral';
import inject from '../utils/inject';

export default function setupSession(registry) {
registry.register('session:main', InternalSession);

let store = 'session-store:application';
if (Ember.testing) {
store = 'session-store:test';
registry.register(store, Ephemeral);
registry.register('session-store:test', Ephemeral);
}

inject(registry, 'session:main', 'store', store);
}
7 changes: 7 additions & 0 deletions packages/ember-simple-auth/addon/internal-session.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Ember from 'ember';
import RSVP from 'rsvp';
import { isEmpty, isNone } from '@ember/utils';
import ObjectProxy from '@ember/object/proxy';
Expand All @@ -17,6 +18,12 @@ export default ObjectProxy.extend(Evented, {
init() {
this._super(...arguments);
this.set('content', { authenticated: {} });
let storeFactory = 'session-store:application';
if (Ember.testing) {
storeFactory = 'session-store:test';
}

this.set('store', getOwner(this).lookup(storeFactory));
this._busy = false;
this._bindToStoreEvents();
},
Expand Down
3 changes: 3 additions & 0 deletions packages/ember-simple-auth/addon/services/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,11 @@ export default Service.extend(Evented, {
*/
attemptedTransition: alias('session.attemptedTransition'),

session: null,

init() {
this._super(...arguments);
this.set('session', getOwner(this).lookup('session:main'));
this._forwardSessionEvents();
},

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ENV from '../config/environment';
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';
Expand All @@ -20,7 +19,6 @@ export default {
registry.register('session-store:local-storage', LocalStorage);

setupSession(registry);
setupSessionService(registry);
setupSessionRestoration(registry);
}
};
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import { describe, beforeEach, it } from 'mocha';
import { expect } from 'chai';
import sinonjs from 'sinon';
import setupSessionService from 'ember-simple-auth/initializers/setup-session-service';
import { setupTest } from 'ember-mocha';

describe('setupSessionService', () => {
setupTest();
let sinon;
let registry;

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

afterEach(function() {
sinon.restore();
});

it('injects the session into the session service', function() {
sinon.spy(registry, 'injection');
setupSessionService(registry);

expect(registry.injection).to.have.been.calledWith('service:session', 'session', 'session:main');
expect(this.owner.lookup('service:session').session).eq(this.owner.lookup('session:main'));
});
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Ember from 'ember';
import { describe, beforeEach, it } from 'mocha';
import { setupApplicationTest } from 'ember-mocha';
import { expect } from 'chai';
import sinonjs from 'sinon';

describe('InternalSession store injection', () => {
setupApplicationTest();

let sinon;
let session;

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

afterEach(function() {
sinon.restore();
});

describe('session store injection', function() {
afterEach(function() {
Ember.testing = true; // eslint-disable-line ember/no-ember-testing-in-module-scope
});

it('looks up the test session store when Ember.testing true', function() {
Ember.testing = true; // eslint-disable-line ember/no-ember-testing-in-module-scope

session = this.owner.lookup('session:main');
expect(session.get('store')).to.eql(this.owner.lookup('session-store:test'));
});

it('looks up the application session store when Ember.testing false', function() {
Ember.testing = false; // eslint-disable-line ember/no-ember-testing-in-module-scope

session = this.owner.lookup('session:main');
expect(session.get('store')).to.eql(this.owner.lookup('session-store:application'));
});
});
});
20 changes: 4 additions & 16 deletions packages/ember-simple-auth/tests/unit/internal-session-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import RSVP from 'rsvp';
import { next } from '@ember/runloop';
import { setOwner } from '@ember/application';
import { describe, beforeEach, it } from 'mocha';
import { setupTest } from 'ember-mocha';
import { expect } from 'chai';
import sinonjs from 'sinon';
import InternalSession from 'ember-simple-auth/internal-session';
import EphemeralStore from 'ember-simple-auth/session-stores/ephemeral';
import Authenticator from 'ember-simple-auth/authenticators/base';

describe('InternalSession', () => {
Expand All @@ -20,12 +17,10 @@ describe('InternalSession', () => {
beforeEach(function() {
sinon = sinonjs.createSandbox();

store = EphemeralStore.create();
authenticator = Authenticator.create();
this.owner.register('authenticator:test', authenticator, { instantiate: false });

session = InternalSession.create({ store });
setOwner(session, this.owner);
this.owner.register('authenticator:test', Authenticator);
authenticator = this.owner.lookup('authenticator:test');
session = this.owner.lookup('session:main');
store = session.get('store');
});

afterEach(function() {
Expand Down Expand Up @@ -808,11 +803,4 @@ describe('InternalSession', () => {
});
});
});

it('does not share the content object between multiple instances', function() {
Copy link
Collaborator Author

@BobrImperator BobrImperator Aug 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't make sense to me since there's only 1 session:main in the app anyway? Which is even more true with us only looking it up once in session service.

let session2 = InternalSession.create({ store });
setOwner(session2, this.owner);

expect(session2.get('content')).to.not.equal(session.get('content'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { setupTest } from 'ember-mocha';
import { expect } from 'chai';
import sinonjs from 'sinon';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
import InternalSession from 'ember-simple-auth/internal-session';
import EphemeralStore from 'ember-simple-auth/session-stores/ephemeral';
import * as LocationUtil from 'ember-simple-auth/utils/location';

describe('ApplicationRouteMixin', () => {
Expand All @@ -24,8 +22,7 @@ describe('ApplicationRouteMixin', () => {
beforeEach(function() {
sinon = sinonjs.createSandbox();

session = InternalSession.create({ store: EphemeralStore.create() });
this.owner.register('session:main', session, { instantiate: false });
session = this.owner.lookup('session:main');
sessionService = this.owner.lookup('service:session');

this.owner.register('service:cookies', Service.extend({
Expand Down
24 changes: 6 additions & 18 deletions packages/ember-simple-auth/tests/unit/services/session-test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import Ember from 'ember';
import ObjectProxy from '@ember/object/proxy';
import Service from '@ember/service';
import { setOwner } from '@ember/application';
import Evented from '@ember/object/evented';
import { next } from '@ember/runloop';
import EmberObject, { set } from '@ember/object';
import { registerDeprecationHandler } from '@ember/debug';
import { describe, beforeEach, it } from 'mocha';
import { setupTest } from 'ember-mocha';
import { expect } from 'chai';
import sinonjs from 'sinon';
import Session from 'ember-simple-auth/services/session';
import * as LocationUtil from 'ember-simple-auth/utils/location';

describe('SessionService', () => {
Expand All @@ -22,20 +18,12 @@ describe('SessionService', () => {

beforeEach(function() {
sinon = sinonjs.createSandbox();
session = ObjectProxy.extend(Evented, {
init() {
this._super(...arguments);
this.content = {};
}
}).create();

this.owner.register('authorizer:custom', EmberObject.extend({
authorize() {}
}));

sessionService = Session.create({ session });
setOwner(sessionService, this.owner);
this.owner.register('service:session', sessionService, { instantiate: false });
sessionService = this.owner.lookup('service:session');
session = sessionService.get('session');
});

afterEach(function() {
Expand Down Expand Up @@ -147,19 +135,19 @@ describe('SessionService', () => {
it("is read from the session's content", function() {
session.set('some', 'data');

expect(sessionService.get('data')).to.eql({ some: 'data' });
expect(sessionService.get('data')).to.eql({ some: 'data', authenticated: {} });
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a valid change IMO since the internal-session initializes it's content to { authenticated: {} } while the test session proxy initialized an empty object instead.

});

it("is written back to the session's content", function() {
sessionService.set('data.some', { other: 'data' });

expect(session.content).to.eql({ some: { other: 'data' } });
expect(session.content).to.eql({ some: { other: 'data' }, authenticated: {} });
});

it('can be set with Ember.set', function() {
set(sessionService, 'data.emberSet', 'ember-set-data');

expect(session.content).to.eql({ emberSet: 'ember-set-data' });
expect(session.content).to.eql({ emberSet: 'ember-set-data', authenticated: {} });
});

it('is read-only', function() {
Expand Down Expand Up @@ -325,7 +313,7 @@ describe('SessionService', () => {
it("does not set the session's 'attemptedTransition' property", function() {
sessionService.requireAuthentication(null, 'login');

expect(sessionService.get('attemptedTransition')).to.be.undefined;
expect(sessionService.get('attemptedTransition')).to.be.null;
});

it('does not set the redirectTarget cookie in fastboot', function() {
Expand Down