diff --git a/addon/mixins/data-adapter-mixin.js b/addon/mixins/data-adapter-mixin.js index 73fe4ea5f..d26edd013 100644 --- a/addon/mixins/data-adapter-mixin.js +++ b/addon/mixins/data-adapter-mixin.js @@ -94,7 +94,9 @@ export default Ember.Mixin.create({ */ handleResponse(status) { if (status === 401) { - this.get('session').invalidate(); + if (this.get('session.isAuthenticated')) { + this.get('session').invalidate(); + } return true; } else { return this._super(...arguments); diff --git a/tests/unit/mixins/data-adapter-mixin-test.js b/tests/unit/mixins/data-adapter-mixin-test.js index 5037f2750..fe04a5236 100644 --- a/tests/unit/mixins/data-adapter-mixin-test.js +++ b/tests/unit/mixins/data-adapter-mixin-test.js @@ -13,10 +13,10 @@ describe('DataAdapterMixin', () => { beforeEach(() => { hash = {}; - sessionService = { + sessionService = Ember.Object.create({ authorize() {}, invalidate() {} - }; + }); const BaseAdapter = Ember.Object.extend({ ajaxOptions() { @@ -105,8 +105,9 @@ describe('DataAdapterMixin', () => { sinon.spy(sessionService, 'invalidate'); }); - describe('when the response status is 401', () => { + describe('when the response status is 401 and the session is authenticated', () => { it('invalidates the session', () => { + sessionService.set('isAuthenticated', true); adapter.handleResponse(401); expect(sessionService.invalidate).to.have.been.calledOnce; @@ -117,6 +118,19 @@ describe('DataAdapterMixin', () => { }); }); + describe('when the response status is 401 and the session is not authenticated', () => { + it('invalidates the session', () => { + sessionService.set('isAuthenticated', false); + adapter.handleResponse(401); + + expect(sessionService.invalidate).to.not.have.been.calledOnce; + }); + + it('returns true', () => { + expect(adapter.handleResponse(401)).to.be.true; + }); + }); + describe('when the response status is not 401', () => { it('does not invalidate the session', () => { adapter.handleResponse(200);