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

Improve logout #1366

Merged
merged 15 commits into from
May 7, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Cleanup get_report function in gsad [#1263](https://github.com/greenbone/gsa/pull/1263)

### Fixed
- Don't show error message after re-login [#1366](https://github.com/greenbone/gsa/pull/1366)
- Fix creating permissions in Roles dialog [#1365](https://github.com/greenbone/gsa/pull/1365)
- Fix cloning permission for Roles [#1361](https://github.com/greenbone/gsa/pull/1361)
- Use correct loaded filter in entities container [#1359](https://github.com/greenbone/gsa/pull/1359)
Expand Down
266 changes: 266 additions & 0 deletions gsa/src/gmp/__tests__/gmp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
/* Copyright (C) 2019 Greenbone Networks GmbH
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
import DefaultTransform from '../http/transform/default';

import Gmp from '../gmp';

describe('Gmp tests', () => {
describe('isLoggedIn tests', () => {
test('should return false if user has no token', () => {
const gmp = new Gmp();

expect(gmp.isLoggedIn()).toEqual(false);
});

test('should return false if user has empty token', () => {
const gmp = new Gmp({token: ''});

expect(gmp.isLoggedIn()).toEqual(false);
});

test('should return true if user has token', () => {
const gmp = new Gmp({token: 'foo'});

expect(gmp.isLoggedIn()).toEqual(true);
});
});

describe('login tests', () => {
test('should login user', () => {
const request = jest.fn().mockResolvedValue({
data: {
token: 'foo',
},
});
const settings = {};

const http = {
request,
};

const gmp = new Gmp(settings, http);

return gmp.login('foo', 'bar').then(() => {
expect(request).toHaveBeenCalledWith('post', {
data: {
cmd: 'login',
login: 'foo',
password: 'bar',
},
});
expect(gmp.isLoggedIn()).toEqual(true);
});
});

test('should not login if request fails', () => {
const request = jest.fn().mockRejectedValue({
message: 'An error',
});
const settings = {};

const http = {
request,
};

const gmp = new Gmp(settings, http);

return gmp.login('foo', 'bar').catch(error => {
expect(request).toHaveBeenCalledWith('post', {
data: {
cmd: 'login',
login: 'foo',
password: 'bar',
},
});
expect(error.message).toEqual('An error');
expect(gmp.isLoggedIn()).toEqual(false);
});
});
});

describe('clearToken tests', () => {
test('should reset token', () => {
const settings = {token: 'foo'};
const gmp = new Gmp(settings);

expect(gmp.isLoggedIn()).toEqual(true);

gmp.clearToken();

expect(gmp.isLoggedIn()).toEqual(false);
expect(settings.token).toBeUndefined();
});
});

describe('logout tests', () => {
test('should reset token', () => {
const settings = {token: 'foo'};
const gmp = new Gmp(settings);

expect(gmp.isLoggedIn()).toEqual(true);

gmp.logout();

expect(gmp.isLoggedIn()).toEqual(false);
expect(settings.token).toBeUndefined();
});

test('should call logout handlers if logged in', () => {
const settings = {token: 'foo'};
const gmp = new Gmp(settings);
const handler = jest.fn();
const unsub = gmp.subscribeToLogout(handler);

expect(gmp.isLoggedIn()).toEqual(true);

gmp.logout();

expect(gmp.isLoggedIn()).toEqual(false);
expect(handler).toHaveBeenCalled();

unsub();
});

test('should call logout handlers if logged out', () => {
const settings = {};
const gmp = new Gmp(settings);
const handler = jest.fn();
const unsub = gmp.subscribeToLogout(handler);

expect(gmp.isLoggedIn()).toEqual(false);

gmp.logout();

expect(gmp.isLoggedIn()).toEqual(false);
expect(handler).toHaveBeenCalled();

unsub();
});
});

describe('doLogout tests', () => {
test('should logout user', () => {
const request = jest.fn().mockResolvedValue({
data: {
token: 'foo',
},
});
const settings = {token: 'foo', server: 'localhost'};

const http = {
request,
};

const gmp = new Gmp(settings, http);

expect(gmp.isLoggedIn()).toEqual(true);

return gmp.doLogout().then(() => {
expect(request).toHaveBeenCalledWith('get', {
args: {
token: 'foo',
},
transform: DefaultTransform,
url: 'http://localhost/logout',
});
expect(gmp.isLoggedIn()).toEqual(false);
expect(settings.token).toBeUndefined();
});
});

test('should notify handler on logout success', () => {
const request = jest.fn().mockResolvedValue({
data: {},
});
const settings = {token: 'foo', server: 'localhost'};

const http = {
request,
};

const handler = jest.fn();

const gmp = new Gmp(settings, http);

gmp.subscribeToLogout(handler);

expect(gmp.isLoggedIn()).toEqual(true);

return gmp.doLogout().then(() => {
expect(gmp.isLoggedIn()).toEqual(false);
expect(settings.token).toBeUndefined();
expect(handler).toHaveBeenCalled();
});
});

test('should ignore logout api call failure', () => {
const request = jest.fn().mockRejectedValue({
message: 'foo',
});
const settings = {
loglevel: 'silent',
token: 'foo',
server: 'localhost',
};

const http = {
request,
};

const handler = jest.fn();

const gmp = new Gmp(settings, http);

gmp.subscribeToLogout(handler);

expect(gmp.isLoggedIn()).toEqual(true);

return gmp.doLogout().then(() => {
expect(gmp.isLoggedIn()).toEqual(false);
expect(settings.token).toBeUndefined();
expect(handler).toHaveBeenCalled();
});
});

test('should not do logout if not logged int', () => {
const request = jest.fn().mockResolvedValue({
data: {},
});
const settings = {server: 'localhost'};

const http = {
request,
};

const handler = jest.fn();

const gmp = new Gmp(settings, http);

gmp.subscribeToLogout(handler);

expect(gmp.isLoggedIn()).toEqual(false);

return gmp.doLogout().then(() => {
expect(gmp.isLoggedIn()).toEqual(false);
expect(settings.token).toBeUndefined();
expect(handler).not.toHaveBeenCalled();
});
});
});
});
27 changes: 14 additions & 13 deletions gsa/src/gmp/gmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ import {BROWSER_LANGUAGE} from './locale/languages';
const log = logger.getLogger('gmp');

class Gmp {
constructor(settings = {}) {
constructor(settings = {}, http) {
this.settings = settings;

logger.init(this.settings);
Expand All @@ -84,7 +84,7 @@ class Gmp {

this.log = logger;

this.http = new GmpHttp(this.settings);
this.http = isDefined(http) ? http : new GmpHttp(this.settings);

this._login = new LoginCommand(this.http);

Expand Down Expand Up @@ -124,7 +124,7 @@ class Gmp {
});
}

logout() {
doLogout() {
if (this.isLoggedIn()) {
const url = this.buildUrl('logout');
const args = {token: this.settings.token};
Expand All @@ -135,26 +135,27 @@ class Gmp {
args,
transform: DefaultTransform,
})
.then(xhr => {
this.clearToken();
log.debug('Logged out successfully');
return xhr;
})
.catch(err => {
this.clearToken();
log.error('Error on logout', err);
})
.then(() => {
this.logout();
});

for (const listener of this._logoutListeners) {
listener();
}

return promise;
}

return Promise.resolve();
}

logout() {
this.clearToken();

for (const listener of this._logoutListeners) {
listener();
}
}

isLoggedIn() {
return !isEmpty(this.settings.token);
}
Expand Down
Loading