You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testMocks.ts 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { To } from 'react-router-dom';
  21. import { CompareResponse } from '../api/quality-profiles';
  22. import { RuleDescriptionSections } from '../apps/coding-rules/rule';
  23. import { Exporter, Profile, ProfileChangelogEvent } from '../apps/quality-profiles/types';
  24. import { LogsLevels } from '../apps/system/utils';
  25. import { Location, Router } from '../components/hoc/withRouter';
  26. import { AppState } from '../types/appstate';
  27. import {
  28. CleanCodeAttribute,
  29. CleanCodeAttributeCategory,
  30. SoftwareImpactSeverity,
  31. SoftwareQuality,
  32. } from '../types/clean-code-taxonomy';
  33. import { RuleRepository } from '../types/coding-rules';
  34. import { EditionKey } from '../types/editions';
  35. import { IssueScope, IssueSeverity, IssueStatus, IssueType, RawIssue } from '../types/issues';
  36. import { Language } from '../types/languages';
  37. import { Notification } from '../types/notifications';
  38. import { DumpStatus, DumpTask } from '../types/project-dump';
  39. import { TaskStatuses } from '../types/tasks';
  40. import {
  41. AlmApplication,
  42. Condition,
  43. FlowLocation,
  44. Group,
  45. HealthTypes,
  46. IdentityProvider,
  47. Issue,
  48. Measure,
  49. MeasureEnhanced,
  50. Metric,
  51. Paging,
  52. Period,
  53. ProfileInheritanceDetails,
  54. Rule,
  55. RuleActivation,
  56. RuleDetails,
  57. RuleParameter,
  58. SysInfoBase,
  59. SysInfoCluster,
  60. SysInfoLogging,
  61. SysInfoStandalone,
  62. UserGroupMember,
  63. UserSelected,
  64. } from '../types/types';
  65. import { CurrentUser, LoggedInUser, RestUserDetailed, User } from '../types/users';
  66. export function mockAlmApplication(overrides: Partial<AlmApplication> = {}): AlmApplication {
  67. return {
  68. backgroundColor: '#444444',
  69. iconPath: '/images/sonarcloud/github-white.svg',
  70. installationUrl: 'https://github.com/apps/greg-sonarcloud/installations/new',
  71. key: 'github',
  72. name: 'GitHub',
  73. ...overrides,
  74. };
  75. }
  76. export function mockAppState(overrides: Partial<AppState> = {}): AppState {
  77. return {
  78. edition: EditionKey.community,
  79. productionDatabase: true,
  80. qualifiers: ['TRK'],
  81. settings: {},
  82. version: '1.0',
  83. documentationUrl: 'https://docs.sonarsource.com/sonarqube/10.0',
  84. ...overrides,
  85. };
  86. }
  87. export function mockBaseSysInfo(overrides: Partial<any> = {}): SysInfoBase {
  88. return {
  89. Health: HealthTypes.GREEN,
  90. 'Health Causes': [],
  91. System: {
  92. Version: '7.8',
  93. },
  94. Database: {
  95. Database: 'PostgreSQL',
  96. 'Database Version': '10.3',
  97. Username: 'sonar',
  98. URL: 'jdbc:postgresql://localhost/sonar',
  99. Driver: 'PostgreSQL JDBC Driver',
  100. 'Driver Version': '42.2.5',
  101. },
  102. 'Compute Engine Tasks': {
  103. 'Total Pending': 0,
  104. 'Total In Progress': 0,
  105. },
  106. 'Search State': { State: 'GREEN', Nodes: 3 },
  107. 'Search Indexes': {
  108. 'Index components - Docs': 30445,
  109. 'Index components - Shards': 10,
  110. },
  111. ...overrides,
  112. };
  113. }
  114. export function mockClusterSysInfo(overrides: Partial<any> = {}): SysInfoCluster {
  115. const baseInfo = mockBaseSysInfo(overrides);
  116. return {
  117. ...baseInfo,
  118. System: {
  119. ...baseInfo.System,
  120. 'High Availability': true,
  121. 'Server ID': 'asd564-asd54a-5dsfg45',
  122. },
  123. Settings: {
  124. 'sonar.cluster.enabled': 'true',
  125. 'sonar.cluster.node.name': 'server9.example.com',
  126. },
  127. 'Application Nodes': [
  128. {
  129. Name: 'server1.example.com',
  130. Host: '10.0.0.0',
  131. Health: HealthTypes.RED,
  132. 'Health Causes': ['Something is wrong'],
  133. System: {
  134. Version: '7.8',
  135. },
  136. Plugins: {
  137. java: '5.13.0.17924 [SonarJava]',
  138. },
  139. 'Web JVM State': {
  140. 'Max Memory (MB)': 1024,
  141. 'Free Memory (MB)': 122,
  142. },
  143. 'Web Database Connection': {
  144. 'Pool Active Connections': 1,
  145. },
  146. 'Web Logging': { 'Logs Level': 'DEBUG' },
  147. 'Web JVM Properties': {
  148. 'file.encoding': 'UTF-8',
  149. 'file.separator': '/',
  150. },
  151. 'Compute Engine Tasks': {
  152. Pending: 0,
  153. 'In Progress': 0,
  154. },
  155. 'Compute Engine JVM State': {
  156. 'Max Memory (MB)': 1024,
  157. 'Free Memory (MB)': 78,
  158. },
  159. 'Compute Engine Database Connection': {
  160. 'Pool Initial Size': 0,
  161. 'Pool Active Connections': 0,
  162. },
  163. 'Compute Engine Logging': {
  164. 'Logs Level': 'INFO',
  165. },
  166. 'Compute Engine JVM Properties': {
  167. 'file.encoding': 'UTF-8',
  168. 'file.separator': '/',
  169. },
  170. },
  171. {
  172. Name: 'server2.example.com',
  173. Host: '10.0.0.0',
  174. Health: HealthTypes.YELLOW,
  175. 'Health Causes': ['Friendly warning'],
  176. System: {
  177. Version: '7.8',
  178. },
  179. Plugins: {
  180. java: '5.13.0.17924 [SonarJava]',
  181. },
  182. 'Web JVM State': {
  183. 'Max Memory (MB)': 1024,
  184. 'Free Memory (MB)': 111,
  185. },
  186. 'Web Database Connection': {
  187. 'Pool Active Connections': 0,
  188. 'Pool Max Connections': 60,
  189. },
  190. 'Web Logging': { 'Logs Level': 'INFO' },
  191. 'Web JVM Properties': {
  192. 'file.encoding': 'UTF-8',
  193. 'file.separator': '/',
  194. },
  195. 'Compute Engine Tasks': {
  196. Pending: 0,
  197. 'In Progress': 0,
  198. },
  199. 'Compute Engine JVM State': {
  200. 'Max Memory (MB)': 1024,
  201. 'Free Memory (MB)': 89,
  202. },
  203. 'Compute Engine Database Connection': {
  204. 'Pool Initial Size': 0,
  205. 'Pool Active Connections': 0,
  206. },
  207. 'Compute Engine Logging': {
  208. 'Logs Level': 'INFO',
  209. },
  210. 'Compute Engine JVM Properties': {
  211. 'file.encoding': 'UTF-8',
  212. 'file.separator': '/',
  213. },
  214. },
  215. ],
  216. 'Search Nodes': [
  217. {
  218. Name: 'server1.example.com',
  219. Host: '10.0.0.0',
  220. 'Search State': {
  221. 'CPU Usage (%)': 0,
  222. 'Disk Available': '93 GB',
  223. },
  224. },
  225. {
  226. Name: 'server2.example.com',
  227. Host: '10.0.0.0',
  228. 'Search State': {
  229. 'CPU Usage (%)': 0,
  230. 'Disk Available': '93 GB',
  231. },
  232. },
  233. {
  234. Name: 'server3.example.com',
  235. Host: '10.0.0.0',
  236. 'Search State': {
  237. 'CPU Usage (%)': 0,
  238. 'Disk Available': '93 GB',
  239. },
  240. },
  241. ],
  242. Statistics: {
  243. ncloc: 989880,
  244. },
  245. ...overrides,
  246. };
  247. }
  248. export function mockCondition(overrides: Partial<Condition> = {}): Condition {
  249. return {
  250. error: '10',
  251. id: '1',
  252. metric: 'coverage',
  253. op: 'LT',
  254. ...overrides,
  255. };
  256. }
  257. export function mockCurrentUser(overrides: Partial<CurrentUser> = {}): CurrentUser {
  258. return {
  259. isLoggedIn: false,
  260. dismissedNotices: {
  261. educationPrinciples: false,
  262. },
  263. ...overrides,
  264. };
  265. }
  266. export function mockLoggedInUser(overrides: Partial<LoggedInUser> = {}): LoggedInUser {
  267. return {
  268. groups: [],
  269. isLoggedIn: true,
  270. login: 'luke',
  271. name: 'Skywalker',
  272. scmAccounts: [],
  273. dismissedNotices: {
  274. educationPrinciples: false,
  275. },
  276. ...overrides,
  277. };
  278. }
  279. export function mockGroup(overrides: Partial<Group> = {}): Group {
  280. return {
  281. membersCount: 1,
  282. name: 'Foo',
  283. managed: false,
  284. ...overrides,
  285. };
  286. }
  287. export function mockRawIssue(withLocations = false, overrides: Partial<RawIssue> = {}): RawIssue {
  288. const rawIssue: RawIssue = {
  289. actions: [],
  290. component: 'main.js',
  291. key: 'AVsae-CQS-9G3txfbFN2',
  292. creationDate: '2023-01-15T09:36:01+0100',
  293. line: 25,
  294. project: 'myproject',
  295. rule: 'javascript:S1067',
  296. severity: IssueSeverity.Major,
  297. status: IssueStatus.Open,
  298. textRange: { startLine: 25, endLine: 26, startOffset: 0, endOffset: 15 },
  299. type: IssueType.CodeSmell,
  300. transitions: [],
  301. scope: IssueScope.Main,
  302. cleanCodeAttributeCategory: CleanCodeAttributeCategory.Responsible,
  303. cleanCodeAttribute: CleanCodeAttribute.Respectful,
  304. impacts: [
  305. { softwareQuality: SoftwareQuality.Maintainability, severity: SoftwareImpactSeverity.Medium },
  306. ],
  307. ...overrides,
  308. };
  309. if (withLocations) {
  310. const loc = mockFlowLocation;
  311. rawIssue.flows = [
  312. {
  313. locations: [
  314. loc({ component: overrides.component }),
  315. loc({ component: overrides.component }),
  316. ],
  317. },
  318. ];
  319. }
  320. return {
  321. ...rawIssue,
  322. ...overrides,
  323. };
  324. }
  325. export function mockIssue(withLocations = false, overrides: Partial<Issue> = {}): Issue {
  326. const issue: Issue = {
  327. actions: [],
  328. component: 'main.js',
  329. componentEnabled: true,
  330. componentLongName: 'main.js',
  331. componentQualifier: 'FIL',
  332. componentUuid: 'foo1234',
  333. creationDate: '2017-03-01T09:36:01+0100',
  334. flows: [],
  335. flowsWithType: [],
  336. key: 'AVsae-CQS-9G3txfbFN2',
  337. line: 25,
  338. message: 'Reduce the number of conditional operators (4) used in the expression',
  339. project: 'myproject',
  340. projectKey: 'foo',
  341. projectName: 'Foo',
  342. rule: 'javascript:S1067',
  343. ruleName: 'foo',
  344. scope: IssueScope.Main,
  345. secondaryLocations: [],
  346. severity: IssueSeverity.Major,
  347. status: IssueStatus.Open,
  348. textRange: { startLine: 25, endLine: 26, startOffset: 0, endOffset: 15 },
  349. transitions: [],
  350. type: 'BUG',
  351. cleanCodeAttributeCategory: CleanCodeAttributeCategory.Responsible,
  352. cleanCodeAttribute: CleanCodeAttribute.Respectful,
  353. impacts: [
  354. { softwareQuality: SoftwareQuality.Maintainability, severity: SoftwareImpactSeverity.Medium },
  355. ],
  356. };
  357. const loc = mockFlowLocation;
  358. if (withLocations) {
  359. issue.flows = [
  360. [loc(), loc(), loc()],
  361. [loc(), loc()],
  362. ];
  363. issue.secondaryLocations = [loc(), loc()];
  364. }
  365. return {
  366. ...issue,
  367. ...overrides,
  368. };
  369. }
  370. export function mockLocation(overrides: Partial<Location> = {}): Location {
  371. return {
  372. hash: '',
  373. key: 'key',
  374. pathname: '/path',
  375. query: {},
  376. search: '',
  377. state: {},
  378. ...overrides,
  379. };
  380. }
  381. export function mockMetric(overrides: Partial<Pick<Metric, 'key' | 'name' | 'type'>> = {}): Metric {
  382. const key = overrides.key || 'coverage';
  383. const name = overrides.name || key;
  384. const type = overrides.type || 'PERCENT';
  385. return {
  386. id: key,
  387. key,
  388. name,
  389. type,
  390. };
  391. }
  392. export function mockMeasure(overrides: Partial<Measure> = {}): Measure {
  393. return {
  394. bestValue: true,
  395. metric: 'bugs',
  396. period: {
  397. bestValue: true,
  398. index: 1,
  399. value: '1.0',
  400. },
  401. value: '1.0',
  402. ...overrides,
  403. };
  404. }
  405. export function mockMeasureEnhanced(overrides: Partial<MeasureEnhanced> = {}): MeasureEnhanced {
  406. return {
  407. bestValue: true,
  408. leak: '1',
  409. metric: mockMetric({ ...(overrides.metric || {}) }),
  410. period: {
  411. bestValue: true,
  412. index: 1,
  413. value: '1.0',
  414. },
  415. value: '1.0',
  416. ...overrides,
  417. };
  418. }
  419. export function mockNotification(overrides: Partial<Notification> = {}): Notification {
  420. return {
  421. channel: 'channel1',
  422. type: 'type-global',
  423. project: 'foo',
  424. projectName: 'Foo',
  425. ...overrides,
  426. };
  427. }
  428. export function mockPeriod(overrides: Partial<Period> = {}): Period {
  429. return {
  430. date: '2019-04-23T02:12:32+0100',
  431. index: 0,
  432. mode: 'previous_version',
  433. ...overrides,
  434. };
  435. }
  436. export function mockQualityProfile(overrides: Partial<Profile> = {}): Profile {
  437. return {
  438. activeDeprecatedRuleCount: 2,
  439. activeRuleCount: 10,
  440. childrenCount: 0,
  441. depth: 1,
  442. isBuiltIn: false,
  443. isDefault: false,
  444. isInherited: false,
  445. key: 'key',
  446. language: 'js',
  447. languageName: 'JavaScript',
  448. name: 'name',
  449. projectCount: 3,
  450. ...overrides,
  451. };
  452. }
  453. export function mockCompareResult(overrides: Partial<CompareResponse> = {}): CompareResponse {
  454. return {
  455. left: { name: 'Profile A' },
  456. right: { name: 'Profile B' },
  457. inLeft: [
  458. {
  459. key: 'java:S4604',
  460. name: 'Rule in left',
  461. severity: 'MINOR',
  462. },
  463. ],
  464. inRight: [
  465. {
  466. key: 'java:S5128',
  467. name: 'Rule in right',
  468. severity: 'MAJOR',
  469. },
  470. ],
  471. modified: [
  472. {
  473. key: 'java:S1698',
  474. name: '== and != should not be used when equals is overridden',
  475. left: { params: {}, severity: 'MINOR' },
  476. right: { params: {}, severity: 'CRITICAL' },
  477. },
  478. ],
  479. ...overrides,
  480. };
  481. }
  482. export function mockQualityProfileInheritance(
  483. overrides: Partial<ProfileInheritanceDetails> = {}
  484. ): ProfileInheritanceDetails {
  485. return {
  486. activeRuleCount: 4,
  487. isBuiltIn: false,
  488. key: 'foo',
  489. name: 'Foo',
  490. overridingRuleCount: 0,
  491. ...overrides,
  492. };
  493. }
  494. export function mockQualityProfileChangelogEvent(
  495. eventOverride?: Partial<ProfileChangelogEvent>
  496. ): ProfileChangelogEvent {
  497. return {
  498. action: 'ACTIVATED',
  499. date: '2019-04-23T02:12:32+0100',
  500. params: {
  501. severity: 'MAJOR',
  502. },
  503. ruleKey: 'rule-key',
  504. ruleName: 'rule-name',
  505. ...eventOverride,
  506. };
  507. }
  508. export function mockQualityProfileExporter(override?: Partial<Exporter>): Exporter {
  509. return {
  510. key: 'exporter-key',
  511. name: 'exporter-name',
  512. languages: ['first-lang', 'second-lang'],
  513. ...override,
  514. };
  515. }
  516. export function mockRouter(
  517. overrides: {
  518. push?: (loc: To) => void;
  519. replace?: (loc: To) => void;
  520. } = {}
  521. ) {
  522. return {
  523. createHref: jest.fn(),
  524. createPath: jest.fn(),
  525. go: jest.fn(),
  526. goBack: jest.fn(),
  527. goForward: jest.fn(),
  528. isActive: jest.fn(),
  529. push: jest.fn(),
  530. replace: jest.fn(),
  531. setRouteLeaveHook: jest.fn(),
  532. ...overrides,
  533. } as Router;
  534. }
  535. export function mockRule(overrides: Partial<Rule> = {}): Rule {
  536. return {
  537. key: 'javascript:S1067',
  538. lang: 'js',
  539. langName: 'JavaScript',
  540. name: 'Use foo',
  541. severity: 'MAJOR',
  542. status: 'READY',
  543. sysTags: ['a', 'b'],
  544. tags: ['x'],
  545. type: 'CODE_SMELL',
  546. ...overrides,
  547. } as Rule;
  548. }
  549. export function mockRuleActivation(overrides: Partial<RuleActivation> = {}): RuleActivation {
  550. return {
  551. createdAt: '2020-02-01',
  552. inherit: 'NONE',
  553. params: [{ key: 'foo', value: 'Bar' }],
  554. qProfile: 'baz',
  555. severity: 'MAJOR',
  556. ...overrides,
  557. };
  558. }
  559. export function mockRuleDetails(overrides: Partial<RuleDetails> = {}): RuleDetails {
  560. return {
  561. cleanCodeAttributeCategory: CleanCodeAttributeCategory.Adaptable,
  562. cleanCodeAttribute: CleanCodeAttribute.Clear,
  563. key: 'squid:S1337',
  564. repo: 'squid',
  565. name: '".equals()" should not be used to test the values of "Atomic" classes',
  566. createdAt: '2014-12-16T17:26:54+0100',
  567. descriptionSections: [
  568. {
  569. key: RuleDescriptionSections.DEFAULT,
  570. content: '<b>Why</b> Because',
  571. },
  572. ],
  573. htmlDesc: '',
  574. mdDesc: '',
  575. severity: 'MAJOR',
  576. status: 'READY',
  577. isTemplate: false,
  578. impacts: [
  579. { softwareQuality: SoftwareQuality.Maintainability, severity: SoftwareImpactSeverity.High },
  580. ],
  581. tags: [],
  582. sysTags: ['multi-threading'],
  583. lang: 'java',
  584. langName: 'Java',
  585. params: [],
  586. defaultRemFnType: 'CONSTANT_ISSUE',
  587. defaultRemFnBaseEffort: '5min',
  588. remFnType: 'CONSTANT_ISSUE',
  589. remFnBaseEffort: '5min',
  590. remFnOverloaded: false,
  591. scope: 'MAIN',
  592. isExternal: false,
  593. type: 'BUG',
  594. ...overrides,
  595. };
  596. }
  597. export function mockRuleDetailsParameter(overrides: Partial<RuleParameter> = {}): RuleParameter {
  598. return {
  599. defaultValue: '1',
  600. htmlDesc: 'description',
  601. key: '1',
  602. type: 'number',
  603. ...overrides,
  604. };
  605. }
  606. export function mockLogs(logsLevel: LogsLevels = LogsLevels.INFO): SysInfoLogging {
  607. return { 'Logs Level': logsLevel, 'Logs Dir': '/logs' };
  608. }
  609. export function mockStandaloneSysInfo(overrides: Partial<any> = {}): SysInfoStandalone {
  610. const baseInfo = mockBaseSysInfo(overrides);
  611. return {
  612. ...baseInfo,
  613. System: {
  614. ...baseInfo.System,
  615. 'High Availability': false,
  616. 'Server ID': 'asd564-asd54a-5dsfg45',
  617. },
  618. Settings: {
  619. 'sonar.cluster.enabled': 'true',
  620. 'sonar.cluster.node.name': 'server9.example.com',
  621. },
  622. 'Web JVM State': {
  623. 'Max Memory (MB)': 1024,
  624. 'Free Memory (MB)': 111,
  625. },
  626. 'Web Database Connection': {
  627. 'Pool Active Connections': 0,
  628. 'Pool Max Connections': 60,
  629. },
  630. 'Web Logging': mockLogs(),
  631. 'Web JVM Properties': {
  632. 'file.encoding': 'UTF-8',
  633. 'file.separator': '/',
  634. },
  635. 'Compute Engine Tasks': {
  636. Pending: 0,
  637. 'In Progress': 0,
  638. },
  639. 'Compute Engine JVM State': {
  640. 'Max Memory (MB)': 1024,
  641. 'Free Memory (MB)': 89,
  642. },
  643. 'Compute Engine Database Connection': {
  644. 'Pool Initial Size': 0,
  645. 'Pool Active Connections': 0,
  646. },
  647. 'Compute Engine Logging': mockLogs(),
  648. 'Compute Engine JVM Properties': {
  649. 'file.encoding': 'UTF-8',
  650. 'file.separator': '/',
  651. },
  652. ALMs: {},
  653. Bundled: {},
  654. Plugins: {},
  655. ...overrides,
  656. };
  657. }
  658. export function mockUser(overrides: Partial<User> = {}): User {
  659. return {
  660. active: true,
  661. local: true,
  662. login: 'john.doe',
  663. name: 'John Doe',
  664. managed: false,
  665. ...overrides,
  666. };
  667. }
  668. export function mockRestUser(overrides: Partial<RestUserDetailed> = {}): RestUserDetailed {
  669. return {
  670. id: Math.random().toString(),
  671. login: 'buzz.aldrin',
  672. name: 'Buzz Aldrin',
  673. email: 'buzz.aldrin@nasa.com',
  674. active: true,
  675. local: true,
  676. managed: false,
  677. externalProvider: '',
  678. externalLogin: '',
  679. sonarQubeLastConnectionDate: null,
  680. sonarLintLastConnectionDate: null,
  681. scmAccounts: [],
  682. avatar: 'buzzonthemoon',
  683. ...overrides,
  684. };
  685. }
  686. export function mockUserSelected(overrides: Partial<UserSelected> = {}): UserSelected {
  687. return {
  688. active: true,
  689. login: 'john.doe',
  690. name: 'John Doe',
  691. selected: true,
  692. ...overrides,
  693. };
  694. }
  695. export function mockUserGroupMember(overrides: Partial<UserGroupMember> = {}): UserGroupMember {
  696. return {
  697. login: 'john.doe',
  698. name: 'John Doe',
  699. managed: false,
  700. selected: true,
  701. ...overrides,
  702. };
  703. }
  704. export function mockDocumentationMarkdown(
  705. overrides: Partial<{ content: string; title: string; key: string }> = {}
  706. ): string {
  707. const content =
  708. overrides.content ||
  709. `
  710. ## Lorem Ipsum
  711. Donec at est elit. In finibus justo ut augue rhoncus, vitae consequat mauris mattis.
  712. Nunc ante est, volutpat ac volutpat ac, pharetra in libero.
  713. `;
  714. const frontMatter = `
  715. ---
  716. ${overrides.title ? 'title: ' + overrides.title : ''}
  717. ${overrides.key ? 'key: ' + overrides.key : ''}
  718. ---`;
  719. return `${frontMatter}
  720. ${content}`;
  721. }
  722. export function mockLanguage(overrides: Partial<Language> = {}): Language {
  723. return {
  724. key: 'css',
  725. name: 'CSS',
  726. ...overrides,
  727. };
  728. }
  729. export function mockFlowLocation(overrides: Partial<FlowLocation> = {}): FlowLocation {
  730. return {
  731. component: 'main.js',
  732. textRange: {
  733. startLine: 1,
  734. startOffset: 1,
  735. endLine: 2,
  736. endOffset: 2,
  737. },
  738. ...overrides,
  739. };
  740. }
  741. export function mockIdentityProvider(overrides: Partial<IdentityProvider> = {}): IdentityProvider {
  742. return {
  743. backgroundColor: '#000000',
  744. iconPath: '/path/icon.svg',
  745. key: 'github',
  746. name: 'Github',
  747. ...overrides,
  748. };
  749. }
  750. export function mockRef(
  751. overrides: Partial<React.RefObject<Partial<HTMLElement>>> = {}
  752. ): React.RefObject<HTMLElement> {
  753. return {
  754. current: {
  755. getBoundingClientRect: jest.fn(),
  756. ...overrides.current,
  757. },
  758. } as React.RefObject<HTMLElement>;
  759. }
  760. export function mockPaging(overrides: Partial<Paging> = {}): Paging {
  761. return {
  762. pageIndex: 1,
  763. pageSize: 100,
  764. total: 1000,
  765. ...overrides,
  766. };
  767. }
  768. export function mockDumpTask(props: Partial<DumpTask> = {}): DumpTask {
  769. return {
  770. status: TaskStatuses.Success,
  771. startedAt: '2020-03-12T12:20:20Z',
  772. submittedAt: '2020-03-12T12:15:20Z',
  773. executedAt: '2020-03-12T12:22:20Z',
  774. ...props,
  775. };
  776. }
  777. export function mockDumpStatus(props: Partial<DumpStatus> = {}): DumpStatus {
  778. return {
  779. canBeExported: true,
  780. canBeImported: true,
  781. dumpToImport: '',
  782. exportedDump: '',
  783. ...props,
  784. };
  785. }
  786. export function mockRuleRepository(override: Partial<RuleRepository> = {}) {
  787. return { key: 'css', language: 'css', name: 'SonarQube', ...override };
  788. }