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.

urls-test.ts 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 { DEFAULT_ISSUES_QUERY } from '../../../components/shared/utils';
  21. import { SecurityStandard } from '../../../types/security';
  22. import {
  23. getComponentIssuesUrl,
  24. getComponentSecurityHotspotsUrl,
  25. queryToSearchString,
  26. } from '../urls';
  27. const SIMPLE_COMPONENT_KEY = 'sonarqube';
  28. describe('#queryToSearchString', () => {
  29. it('should handle query as array', () => {
  30. expect(
  31. queryToSearchString([
  32. ['key1', 'value1'],
  33. ['key1', 'value2'],
  34. ['key2', 'value1'],
  35. ]),
  36. ).toBe('?key1=value1&key1=value2&key2=value1');
  37. });
  38. it('should handle query as string', () => {
  39. expect(queryToSearchString('a=1')).toBe('?a=1');
  40. });
  41. it('should handle query as URLSearchParams', () => {
  42. expect(queryToSearchString(new URLSearchParams({ a: '1', b: '2' }))).toBe('?a=1&b=2');
  43. });
  44. it('should handle all types', () => {
  45. const query = {
  46. author: ['GRRM', 'JKR', 'Stross'],
  47. b1: true,
  48. b2: false,
  49. number: 0,
  50. emptyArray: [],
  51. normalString: 'hello',
  52. undef: undefined,
  53. };
  54. expect(queryToSearchString(query)).toBe(
  55. '?author=GRRM&author=JKR&author=Stross&b1=true&b2=false&number=0&normalString=hello',
  56. );
  57. });
  58. it('should handle an missing query', () => {
  59. expect(queryToSearchString()).toBeUndefined();
  60. });
  61. });
  62. describe('#getComponentIssuesUrl', () => {
  63. it('should work without parameters', () => {
  64. expect(getComponentIssuesUrl(SIMPLE_COMPONENT_KEY)).toEqual(
  65. expect.objectContaining({
  66. pathname: '/project/issues',
  67. search: queryToSearchString({ id: SIMPLE_COMPONENT_KEY }),
  68. }),
  69. );
  70. });
  71. it('should work with parameters', () => {
  72. expect(getComponentIssuesUrl(SIMPLE_COMPONENT_KEY, DEFAULT_ISSUES_QUERY)).toEqual(
  73. expect.objectContaining({
  74. pathname: '/project/issues',
  75. search: queryToSearchString({ ...DEFAULT_ISSUES_QUERY, id: SIMPLE_COMPONENT_KEY }),
  76. }),
  77. );
  78. });
  79. });
  80. describe('#getComponentSecurityHotspotsUrl', () => {
  81. it('should work with no extra parameters', () => {
  82. expect(getComponentSecurityHotspotsUrl(SIMPLE_COMPONENT_KEY)).toEqual(
  83. expect.objectContaining({
  84. pathname: '/security_hotspots',
  85. search: queryToSearchString({ id: SIMPLE_COMPONENT_KEY }),
  86. }),
  87. );
  88. });
  89. it('should forward some query parameters', () => {
  90. expect(
  91. getComponentSecurityHotspotsUrl(SIMPLE_COMPONENT_KEY, undefined, {
  92. inNewCodePeriod: 'true',
  93. [SecurityStandard.OWASP_TOP10_2021]: 'a1',
  94. [SecurityStandard.CWE]: '213',
  95. [SecurityStandard.OWASP_TOP10]: 'a1',
  96. [SecurityStandard.SONARSOURCE]: 'command-injection',
  97. [SecurityStandard.PCI_DSS_3_2]: '4.2',
  98. [SecurityStandard.PCI_DSS_4_0]: '4.1',
  99. ignoredParam: '1234',
  100. }),
  101. ).toEqual(
  102. expect.objectContaining({
  103. pathname: '/security_hotspots',
  104. search: queryToSearchString({
  105. id: SIMPLE_COMPONENT_KEY,
  106. inNewCodePeriod: 'true',
  107. [SecurityStandard.OWASP_TOP10_2021]: 'a1',
  108. [SecurityStandard.OWASP_TOP10]: 'a1',
  109. [SecurityStandard.SONARSOURCE]: 'command-injection',
  110. [SecurityStandard.CWE]: '213',
  111. [SecurityStandard.PCI_DSS_3_2]: '4.2',
  112. [SecurityStandard.PCI_DSS_4_0]: '4.1',
  113. }),
  114. }),
  115. );
  116. });
  117. });