aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/api/mocks/UserTokensMock.ts
blob: f0f9f523a9e5d39748cd15a201c9adf79811c272 (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
/*
 * 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 { cloneDeep, last } from 'lodash';
import { mockUserToken } from '../../helpers/mocks/token';
import { NewUserToken, TokenType, UserToken } from '../../types/token';
import { generateToken, getTokens, revokeToken } from '../user-tokens';

const defaultTokens = [
  mockUserToken({
    name: 'local-scanner',
    createdAt: '2022-03-07T09:02:59+0000',
    lastConnectionDate: '2022-04-07T09:51:48+0000',
  }),
  mockUserToken({
    name: 'test',
    createdAt: '2020-01-23T19:25:19+0000',
  }),
];

export default class UserTokensMock {
  tokens: Array<Partial<NewUserToken> & UserToken>;
  failGeneration = false;

  constructor() {
    this.tokens = cloneDeep(defaultTokens);

    (getTokens as jest.Mock).mockImplementation(this.handleGetTokens);
    (generateToken as jest.Mock).mockImplementation(this.handleGenerateToken);
    (revokeToken as jest.Mock).mockImplementation(this.handleRevokeToken);
  }

  handleGetTokens = () => {
    return Promise.resolve(cloneDeep(this.tokens));
  };

  handleGenerateToken = ({
    name,
    login,
    type,
    projectKey,
    expirationDate,
  }: {
    name: string;
    login?: string;
    type: TokenType;
    projectKey: string;
    expirationDate?: string;
  }) => {
    if (this.failGeneration) {
      this.failGeneration = false;
      return Promise.reject('x_x');
    }

    if (this.tokens.some((t) => t.name === name)) {
      return Promise.reject('This name is already used');
    }

    const token = {
      name,
      login,
      type,
      projectKey,
      isExpired: false,
      token: `generatedtoken${this.tokens.length}`,
      createdAt: '2022-04-04T04:04:04+0000',
      expirationDate,
    };

    this.tokens.push(token);

    return Promise.resolve(token);
  };

  handleRevokeToken = ({ name }: { name: string; login?: string }) => {
    this.tokens = this.tokens.filter((t) => t.name !== name);

    return Promise.resolve();
  };

  failNextTokenGeneration = () => {
    this.failGeneration = true;
  };

  getTokens = () => {
    return cloneDeep(this.tokens);
  };

  getLastToken = () => {
    return last(this.getTokens());
  };

  reset = () => {
    this.tokens = cloneDeep(defaultTokens);
  };
}