Skip to content

Commit

Permalink
Merge pull request #722 from wireframe/patch-1
Browse files Browse the repository at this point in the history
Only invalidate session if authenticated
  • Loading branch information
marcoow committed Oct 19, 2015
2 parents 94faf88 + 6a09e70 commit f0e939e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 3 additions & 1 deletion addon/mixins/data-adapter-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 17 additions & 3 deletions tests/unit/mixins/data-adapter-mixin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ describe('DataAdapterMixin', () => {

beforeEach(() => {
hash = {};
sessionService = {
sessionService = Ember.Object.create({
authorize() {},
invalidate() {}
};
});

const BaseAdapter = Ember.Object.extend({
ajaxOptions() {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit f0e939e

Please sign in to comment.