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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
/*
* SonarQube
* Copyright (C) 2009-2024 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, countBy, pick, trim } from 'lodash';
import { RuleDescriptionSections } from '../../apps/coding-rules/rule';
import {
mockCurrentUser,
mockQualityProfile,
mockRuleDetails,
mockRuleRepository,
} from '../../helpers/testMocks';
import { RuleRepository } from '../../types/coding-rules';
import { RawIssuesResponse } from '../../types/issues';
import { SearchRulesQuery } from '../../types/rules';
import { Rule, RuleActivation, RuleDetails, RulesUpdateRequest } from '../../types/types';
import { NoticeType } from '../../types/users';
import { getFacet } from '../issues';
import {
bulkActivateRules,
bulkDeactivateRules,
Profile,
searchQualityProfiles,
SearchQualityProfilesParameters,
SearchQualityProfilesResponse,
} from '../quality-profiles';
import { getRuleDetails, getRulesApp, searchRules, updateRule } from '../rules';
import { dismissNotice, getCurrentUser } from '../users';
interface FacetFilter {
languages?: string;
}
const FACET_RULE_MAP: { [key: string]: keyof Rule } = {
languages: 'lang',
types: 'type',
};
export default class CodingRulesMock {
defaultRules: RuleDetails[] = [];
rules: RuleDetails[] = [];
qualityProfile: Profile[] = [];
repositories: RuleRepository[] = [];
isAdmin = false;
applyWithWarning = false;
dismissedNoticesEP = false;
constructor() {
this.repositories = [
mockRuleRepository({ key: 'repo1' }),
mockRuleRepository({ key: 'repo2' }),
];
this.qualityProfile = [
mockQualityProfile({ key: 'p1', name: 'QP Foo', language: 'java', languageName: 'Java' }),
mockQualityProfile({ key: 'p2', name: 'QP Bar', language: 'js' }),
mockQualityProfile({ key: 'p3', name: 'QP FooBar', language: 'java', languageName: 'Java' }),
];
const resourceContent = 'Some link <a href="http://example.com">Awsome Reading</a>';
const introTitle = 'Introduction to this rule';
const rootCauseContent = 'This how to fix';
this.defaultRules = [
mockRuleDetails({
key: 'rule1',
type: 'BUG',
lang: 'java',
langName: 'Java',
name: 'Awsome java rule',
}),
mockRuleDetails({
key: 'rule2',
name: 'Hot hotspot',
type: 'SECURITY_HOTSPOT',
lang: 'js',
descriptionSections: [
{ key: RuleDescriptionSections.INTRODUCTION, content: introTitle },
{ key: RuleDescriptionSections.ROOT_CAUSE, content: rootCauseContent },
{ key: RuleDescriptionSections.ASSESS_THE_PROBLEM, content: 'Assess' },
{
key: RuleDescriptionSections.RESOURCES,
content: resourceContent,
},
],
langName: 'JavaScript',
}),
mockRuleDetails({ key: 'rule3', name: 'Unknown rule', lang: 'js', langName: 'JavaScript' }),
mockRuleDetails({
key: 'rule4',
type: 'BUG',
lang: 'c',
langName: 'C',
name: 'Awsome C rule',
}),
mockRuleDetails({
key: 'rule5',
type: 'VULNERABILITY',
lang: 'py',
langName: 'Python',
name: 'Awsome Python rule',
descriptionSections: [
{ key: RuleDescriptionSections.INTRODUCTION, content: introTitle },
{ key: RuleDescriptionSections.HOW_TO_FIX, content: rootCauseContent },
{
key: RuleDescriptionSections.RESOURCES,
content: resourceContent,
},
],
}),
mockRuleDetails({
key: 'rule6',
type: 'VULNERABILITY',
lang: 'py',
langName: 'Python',
name: 'Bad Python rule',
isExternal: true,
descriptionSections: undefined,
}),
mockRuleDetails({
key: 'rule7',
type: 'VULNERABILITY',
lang: 'py',
langName: 'Python',
name: 'Python rule with context',
descriptionSections: [
{
key: RuleDescriptionSections.INTRODUCTION,
content: 'Introduction to this rule with context',
},
{
key: RuleDescriptionSections.HOW_TO_FIX,
content: 'This is how to fix for spring',
context: { key: 'spring', displayName: 'Spring' },
},
{
key: RuleDescriptionSections.HOW_TO_FIX,
content: 'This is how to fix for spring boot',
context: { key: 'spring_boot', displayName: 'Spring boot' },
},
{
key: RuleDescriptionSections.RESOURCES,
content: resourceContent,
},
],
}),
mockRuleDetails({
key: 'rule8',
type: 'VULNERABILITY',
lang: 'py',
langName: 'Python',
name: 'Awesome Python rule with education principles',
descriptionSections: [
{ key: RuleDescriptionSections.INTRODUCTION, content: introTitle },
{ key: RuleDescriptionSections.HOW_TO_FIX, content: rootCauseContent },
{
key: RuleDescriptionSections.RESOURCES,
content: resourceContent,
},
],
educationPrinciples: ['defense_in_depth', 'never_trust_user_input'],
}),
];
(updateRule as jest.Mock).mockImplementation(this.handleUpdateRule);
(searchRules as jest.Mock).mockImplementation(this.handleSearchRules);
(getRuleDetails as jest.Mock).mockImplementation(this.handleGetRuleDetails);
(searchQualityProfiles as jest.Mock).mockImplementation(this.handleSearchQualityProfiles);
(getRulesApp as jest.Mock).mockImplementation(this.handleGetRulesApp);
(bulkActivateRules as jest.Mock).mockImplementation(this.handleBulkActivateRules);
(bulkDeactivateRules as jest.Mock).mockImplementation(this.handleBulkDeactivateRules);
(getFacet as jest.Mock).mockImplementation(this.handleGetGacet);
(getCurrentUser as jest.Mock).mockImplementation(this.handleGetCurrentUser);
(dismissNotice as jest.Mock).mockImplementation(this.handleDismissNotification);
this.rules = cloneDeep(this.defaultRules);
}
getRuleWithoutDetails() {
return this.rules.map((r) =>
pick(r, [
'isTemplate',
'key',
'lang',
'langName',
'name',
'params',
'severity',
'status',
'sysTags',
'tags',
'type',
])
);
}
filterFacet({ languages }: FacetFilter) {
let filteredRules = this.getRuleWithoutDetails();
if (languages) {
filteredRules = filteredRules.filter((r) => r.lang && languages.includes(r.lang));
}
return filteredRules;
}
setIsAdmin() {
this.isAdmin = true;
}
activateWithWarning() {
this.applyWithWarning = true;
}
reset() {
this.isAdmin = false;
this.applyWithWarning = false;
this.dismissedNoticesEP = false;
this.rules = cloneDeep(this.defaultRules);
}
allRulesCount() {
return this.rules.length;
}
allRulesName() {
return this.rules.map((r) => r.name);
}
allQualityProfile(language: string) {
return this.qualityProfile.filter((qp) => qp.language === language);
}
handleGetGacet = (): Promise<{
facet: { count: number; val: string }[];
response: RawIssuesResponse;
}> => {
return this.reply({
facet: [],
response: {
components: [],
effortTotal: 0,
facets: [],
issues: [],
languages: [],
paging: { total: 0, pageIndex: 1, pageSize: 1 },
},
});
};
handleGetRuleDetails = (parameters: {
actives?: boolean;
key: string;
}): Promise<{ actives?: RuleActivation[]; rule: RuleDetails }> => {
const rule = this.rules.find((r) => r.key === parameters.key);
if (!rule) {
return Promise.reject({
errors: [{ msg: `No rule has been found for id ${parameters.key}` }],
});
}
return this.reply({ actives: parameters.actives ? [] : undefined, rule });
};
handleUpdateRule = (data: RulesUpdateRequest): Promise<RuleDetails> => {
const rule = this.rules.find((r) => r.key === data.key);
if (rule === undefined) {
return Promise.reject({
errors: [{ msg: `No rule has been found for id ${data.key}` }],
});
}
const template = this.rules.find((r) => r.key === rule.templateKey);
// Lets not convert the md to html in test.
rule.mdDesc = data.markdown_description !== undefined ? data.markdown_description : rule.mdDesc;
rule.htmlDesc =
data.markdown_description !== undefined ? data.markdown_description : rule.htmlDesc;
rule.mdNote = data.markdown_note !== undefined ? data.markdown_note : rule.mdNote;
rule.htmlNote = data.markdown_note !== undefined ? data.markdown_note : rule.htmlNote;
rule.name = data.name !== undefined ? data.name : rule.name;
if (template && data.params) {
rule.params = [];
data.params.split(';').forEach((param) => {
const parts = param.split('=');
const paramsDef = template.params?.find((p) => p.key === parts[0]);
rule.params?.push({
key: parts[0],
type: paramsDef?.type || 'STRING',
defaultValue: trim(parts[1], '" '),
htmlDesc: paramsDef?.htmlDesc,
});
});
}
rule.remFnBaseEffort =
data.remediation_fn_base_effort !== undefined
? data.remediation_fn_base_effort
: rule.remFnBaseEffort;
rule.remFnType =
data.remediation_fn_type !== undefined ? data.remediation_fn_type : rule.remFnType;
rule.severity = data.severity !== undefined ? data.severity : rule.severity;
rule.status = data.status !== undefined ? data.status : rule.status;
rule.tags = data.tags !== undefined ? data.tags.split(';') : rule.tags;
return this.reply(rule);
};
handleSearchRules = ({ facets, languages, p, ps, rule_key }: SearchRulesQuery) => {
const countFacet = (facets || '').split(',').map((facet: keyof Rule) => {
const facetCount = countBy(
this.rules.map((r) => r[FACET_RULE_MAP[facet] || facet] as string)
);
return {
property: facet,
values: Object.keys(facetCount).map((val) => ({ val, count: facetCount[val] })),
};
});
const currentPs = ps || 10;
const currentP = p || 1;
let filteredRules: Rule[] = [];
if (rule_key) {
filteredRules = this.getRuleWithoutDetails().filter((r) => r.key === rule_key);
} else {
filteredRules = this.filterFacet({ languages });
}
const responseRules = filteredRules.slice((currentP - 1) * currentPs, currentP * currentPs);
return this.reply({
total: filteredRules.length,
p: currentP,
ps: currentPs,
rules: responseRules,
facets: countFacet,
});
};
handleBulkActivateRules = () => {
if (this.applyWithWarning) {
return this.reply({
succeeded: this.rules.length - 1,
failed: 1,
errors: [{ msg: 'c rule c:S6069 cannot be activated on cpp profile SonarSource' }],
});
}
return this.reply({
succeeded: this.rules.length,
failed: 0,
errors: [],
});
};
handleBulkDeactivateRules = () => {
return this.reply({
succeeded: this.rules.length,
failed: 0,
});
};
handleSearchQualityProfiles = ({
language,
}: SearchQualityProfilesParameters = {}): Promise<SearchQualityProfilesResponse> => {
let profiles: Profile[] = this.isAdmin
? this.qualityProfile.map((p) => ({ ...p, actions: { edit: true } }))
: this.qualityProfile;
if (language) {
profiles = profiles.filter((p) => p.language === language);
}
return this.reply({ profiles });
};
handleGetRulesApp = () => {
return this.reply({ canWrite: this.isAdmin, repositories: this.repositories });
};
handleGetCurrentUser = () => {
return this.reply(
mockCurrentUser({
dismissedNotices: {
educationPrinciples: this.dismissedNoticesEP,
},
})
);
};
handleDismissNotification = (noticeType: NoticeType) => {
if (noticeType === NoticeType.EDUCATION_PRINCIPLES) {
this.dismissedNoticesEP = true;
return this.reply(true);
}
return Promise.reject();
};
reply<T>(response: T): Promise<T> {
return Promise.resolve(cloneDeep(response));
}
}
|