aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/helpers/__tests__/sonarlint-test.ts
blob: e9bfddfcef3004feadaeb8c2dfbeaa6be0328627 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * SonarQube
 * Copyright (C) 2009-2023 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { TokenType } from '../../types/token';
import { HttpStatus } from '../request';
import {
  buildPortRange,
  openHotspot,
  portIsValid,
  probeSonarLintServers,
  sendUserToken,
} from '../sonarlint';

describe('buildPortRange', () => {
  it('should build a port range of size <size> starting at port <port>', () => {
    expect(buildPortRange(10000, 5)).toStrictEqual([10000, 10001, 10002, 10003, 10004]);
  });
});

describe('probeSonarLintServers', () => {
  const sonarLintResponse = { ideName: 'BlueJ IDE', description: 'Hello World' };

  window.fetch = jest.fn((input: RequestInfo) => {
    const calledPort = new URL(input.toString()).port;
    if (calledPort === '64120') {
      const resp = new Response();
      resp.json = () => Promise.resolve(sonarLintResponse);
      return Promise.resolve(resp);
    } else {
      return Promise.reject('oops');
    }
  });

  it('should probe all ports in range', async () => {
    const results = await probeSonarLintServers();
    expect(results).toStrictEqual([{ port: 64120, ...sonarLintResponse }]);
  });
});

describe('openHotspot', () => {
  it('should send request to IDE on the right port', async () => {
    const resp = new Response();
    window.fetch = jest.fn((input: RequestInfo) => {
      const calledUrl = new URL(input.toString());
      try {
        expect(calledUrl.searchParams.get('server')).toStrictEqual('http://localhost');
        expect(calledUrl.searchParams.get('project')).toStrictEqual('my-project:key');
        expect(calledUrl.searchParams.get('hotspot')).toStrictEqual('my-hotspot-key');
      } catch (error) {
        return Promise.reject(error);
      }
      return Promise.resolve(resp);
    });

    const result = await openHotspot(42000, 'my-project:key', 'my-hotspot-key');
    expect(result).toBe(resp);
  });
});

describe('portIsValid', () => {
  it.each([
    [64119, false],
    [64120, true],
    [64125, true],
    [64130, true],
    [64131, false],
  ])('should validate port %s is within the expected range', (port, expectation) => {
    expect(portIsValid(port)).toBe(expectation);
  });
});

describe('sendUserToken', () => {
  it('should send the token the right port', async () => {
    const token = {
      login: 'Takeshi',
      name: 'sonarlint-vscode-1',
      createdAt: '12-12-2018',
      expirationDate: '17-02-2019',
      token: '78gfh78d6gf8h',
      type: TokenType.User,
    };

    const resp = new Response();
    window.fetch = jest.fn((_url, { body }: RequestInit) => {
      try {
        const data = JSON.parse(body?.toString() ?? '{}');

        expect(data).toEqual(token);
      } catch (error) {
        return Promise.reject(error);
      }
      return Promise.resolve(resp);
    });

    const result = await sendUserToken(64122, { ...token, isExpired: false });
    expect(result).toBeUndefined();
  });

  it('should handle errors', async () => {
    const token = {
      login: 'Takeshi',
      name: 'sonarlint-vscode-1',
      createdAt: '12-12-2018',
      expirationDate: '17-02-2019',
      token: '78gfh78d6gf8h',
      type: TokenType.User,
    };

    const resp = new Response('Meh', { status: HttpStatus.BadRequest, statusText: 'I no likez' });
    window.fetch = jest.fn(() => {
      return Promise.resolve(resp);
    });

    await expect(async () => {
      await sendUserToken(64122, { ...token, isExpired: false });
    }).rejects.toThrow('400 I no likez. Meh');
  });
});