Skip to content

Commit 7b7514e

Browse files
bjoernricksgreenbonebot
authored andcommitted
Change: Use new /login URL for the login
Requires gsad with greenbone/gsad#403
1 parent 2d7be5a commit 7b7514e

3 files changed

Lines changed: 81 additions & 29 deletions

File tree

src/gmp/__tests__/gmp.test.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,32 @@ describe('Gmp tests', () => {
3535

3636
test('should login user', async () => {
3737
const token = 'foo';
38-
const http = createHttp(createResponse({token}));
38+
const http = createHttp(createResponse({token}), {
39+
apiServer: 'localhost',
40+
apiProtocol: 'http:',
41+
});
3942

4043
const storage = createStorage();
4144
const session = createSession();
4245
const settings = new Settings(storage);
4346
const gmp = new Gmp({settings, http, session});
4447

4548
await gmp.login('foo', 'bar');
46-
expect(http.request).toHaveBeenCalledWith(
47-
'post',
48-
expect.objectContaining({
49-
data: {
50-
cmd: 'login',
51-
login: 'foo',
52-
password: 'bar',
53-
},
54-
}),
55-
);
49+
expect(http.request).toHaveBeenCalledWith('post', {
50+
data: {
51+
login: 'foo',
52+
password: 'bar',
53+
},
54+
url: 'http://localhost/login',
55+
});
5656
expect(session.login).toHaveBeenCalledWith({token, username: 'foo'});
5757
});
5858

5959
test('should not login if request fails', async () => {
60-
const http = createHttpError(new Error('An error'));
60+
const http = createHttpError(new Error('An error'), {
61+
apiServer: 'localhost',
62+
apiProtocol: 'http:',
63+
});
6164
const storage = createStorage();
6265
const session = createSession();
6366
const settings = new Settings(storage);
@@ -66,16 +69,13 @@ describe('Gmp tests', () => {
6669
try {
6770
return await gmp.login('foo', 'bar');
6871
} catch (error) {
69-
expect(http.request).toHaveBeenCalledWith(
70-
'post',
71-
expect.objectContaining({
72-
data: {
73-
cmd: 'login',
74-
login: 'foo',
75-
password: 'bar',
76-
},
77-
}),
78-
);
72+
expect(http.request).toHaveBeenCalledWith('post', {
73+
data: {
74+
login: 'foo',
75+
password: 'bar',
76+
},
77+
url: 'http://localhost/login',
78+
});
7979
expect((error as Error).message).toEqual('An error');
8080
expect(session.login).not.toHaveBeenCalled();
8181
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* SPDX-FileCopyrightText: 2024 Greenbone AG
2+
*
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import {describe, test, expect, testing} from '@gsa/testing';
7+
import LoginCommand from 'gmp/commands/login';
8+
import {createResponse, createHttp} from 'gmp/commands/testing';
9+
import {type Envelope} from 'gmp/http/transform/fast-xml';
10+
import date from 'gmp/models/date';
11+
12+
describe('LoginCommand tests', () => {
13+
test('should return Login model on successful login', async () => {
14+
testing.useFakeTimers();
15+
const response = createResponse<Envelope>({
16+
token: 'abc123',
17+
timezone: 'UTC',
18+
i18n: 'en',
19+
session: 3600,
20+
jwt: 'jwt_token',
21+
client_address: '127.0.0.123',
22+
});
23+
const fakeHttp = createHttp(response, {
24+
apiProtocol: 'https',
25+
apiServer: 'example.com',
26+
});
27+
const cmd = new LoginCommand(fakeHttp);
28+
const loginModel = await cmd.login('user', 'pass');
29+
expect(fakeHttp.request).toHaveBeenCalledWith('post', {
30+
data: {
31+
login: 'user',
32+
password: 'pass',
33+
},
34+
url: 'https://example.com/login',
35+
});
36+
expect(loginModel.token).toEqual('abc123');
37+
expect(loginModel.timezone).toEqual('UTC');
38+
expect(loginModel.locale).toEqual('en');
39+
expect(loginModel.sessionTimeout).toEqual(date('1970-01-01T01:00:00.000Z'));
40+
expect(loginModel.jwt).toEqual('jwt_token');
41+
42+
testing.useRealTimers();
43+
});
44+
});

src/gmp/commands/login.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,30 @@
66
import HttpCommand from 'gmp/commands/http';
77
import type Http from 'gmp/http/http';
88
import {ResponseRejection} from 'gmp/http/rejection';
9+
import {buildServerUrl} from 'gmp/http/utils';
910
import _ from 'gmp/locale';
1011
import Login from 'gmp/models/login';
1112

1213
class LoginCommand extends HttpCommand {
1314
constructor(http: Http) {
14-
super(http, {
15-
cmd: 'login',
16-
});
15+
super(http);
1716
}
1817

1918
async login(username: string, password: string) {
2019
try {
21-
const response = await this.httpPostWithTransform({
22-
login: username,
23-
password,
24-
});
20+
const response = await this.httpPostWithTransform(
21+
{
22+
login: username,
23+
password,
24+
},
25+
{
26+
url: buildServerUrl(
27+
this.http.apiServer,
28+
'login',
29+
this.http.apiProtocol,
30+
),
31+
},
32+
);
2533
return new Login(response);
2634
} catch (rej) {
2735
if (rej instanceof ResponseRejection) {

0 commit comments

Comments
 (0)