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

Only invalidate session if authenticated #722

Merged
merged 1 commit into from
Oct 19, 2015
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
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