PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/packages/integration/src/helpers.test.ts

https://github.com/backstage/backstage
TypeScript | 183 lines | 155 code | 13 blank | 15 comment | 6 complexity | 2978cef6e9cf1f307dca0e270083e6c0 MD5 | raw file
Possible License(s): Apache-2.0, MIT, BSD-3-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * Copyright 2020 The Backstage Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import { BitbucketIntegration } from './bitbucket';
  17. import {
  18. basicIntegrations,
  19. defaultScmResolveUrl,
  20. isValidHost,
  21. } from './helpers';
  22. describe('basicIntegrations', () => {
  23. describe('byUrl', () => {
  24. it('handles hosts without a port', () => {
  25. const integration = new BitbucketIntegration({ host: 'host.com' });
  26. const integrations = basicIntegrations<BitbucketIntegration>(
  27. [integration],
  28. i => i.config.host,
  29. );
  30. expect(integrations.byUrl('https://host.com/a')).toBe(integration);
  31. expect(integrations.byUrl('https://host.com:8080/a')).toBeUndefined();
  32. });
  33. it('handles hosts with a port', () => {
  34. const integration = new BitbucketIntegration({ host: 'host.com:8080' });
  35. const integrations = basicIntegrations<BitbucketIntegration>(
  36. [integration],
  37. i => i.config.host,
  38. );
  39. expect(integrations.byUrl('https://host.com:8080/a')).toBe(integration);
  40. expect(integrations.byUrl('https://host.com/a')).toBeUndefined();
  41. });
  42. });
  43. });
  44. describe('isValidHost', () => {
  45. it.each([
  46. ['example.com', true],
  47. ['foo', true],
  48. ['foo:1', true],
  49. ['foo:10000', true],
  50. ['foo.bar', true],
  51. ['foo.bar.baz', true],
  52. ['1.2.3.4', true],
  53. ['[::]', true],
  54. ['[::1]', true],
  55. ['[1:2:3:4:5:6:7:8]', true],
  56. ['1.2.3.4.5.6.7.8', true],
  57. ['https://example.com', false],
  58. ['foo:100000', false],
  59. ['FOO', false],
  60. ['Foo', false],
  61. ['foo/bar', false],
  62. ['//foo', false],
  63. ['foo:bar', false],
  64. ['foo?', false],
  65. ['foo?bar', false],
  66. ['foo#', false],
  67. ['foo#bar', false],
  68. ['::', false],
  69. ['::1', false],
  70. ['1:2:3:4:5:6:7:8', false],
  71. ['???????', false],
  72. ['€&()=)&(', false],
  73. ['höst', false],
  74. ['πœπœfiπœ', false],
  75. ])('Should check whether %s is a valid host', (str, expected) => {
  76. expect(isValidHost(str)).toBe(expected);
  77. });
  78. });
  79. describe('defaultScmResolveUrl', () => {
  80. it('works for relative paths and retains query params', () => {
  81. expect(
  82. defaultScmResolveUrl({
  83. url: './b.yaml',
  84. base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml',
  85. }),
  86. ).toBe(
  87. 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/b.yaml',
  88. );
  89. expect(
  90. defaultScmResolveUrl({
  91. url: './b.yaml',
  92. base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml?at=master',
  93. }),
  94. ).toBe(
  95. 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/b.yaml?at=master',
  96. );
  97. expect(
  98. defaultScmResolveUrl({
  99. url: 'b.yaml',
  100. base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml',
  101. }),
  102. ).toBe(
  103. 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/b.yaml',
  104. );
  105. });
  106. it('works for absolute paths and retains query params', () => {
  107. expect(
  108. defaultScmResolveUrl({
  109. url: '/other/b.yaml',
  110. base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml',
  111. }),
  112. ).toBe(
  113. 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/other/b.yaml',
  114. );
  115. expect(
  116. defaultScmResolveUrl({
  117. url: '/other/b.yaml',
  118. base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml?at=master',
  119. }),
  120. ).toBe(
  121. 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/other/b.yaml?at=master',
  122. );
  123. });
  124. it('works in various situations with line numbers', () => {
  125. expect(
  126. defaultScmResolveUrl({
  127. url: './b.yaml',
  128. base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml?at=master',
  129. lineNumber: 11,
  130. }),
  131. ).toBe(
  132. 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/b.yaml?at=master#L11',
  133. );
  134. expect(
  135. defaultScmResolveUrl({
  136. url: 'b.yaml',
  137. base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml',
  138. lineNumber: 12,
  139. }),
  140. ).toBe(
  141. 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/b.yaml#L12',
  142. );
  143. expect(
  144. defaultScmResolveUrl({
  145. url: '/other/b.yaml',
  146. base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml',
  147. lineNumber: 13,
  148. }),
  149. ).toBe(
  150. 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/other/b.yaml#L13',
  151. );
  152. expect(
  153. defaultScmResolveUrl({
  154. url: '/other/b.yaml',
  155. base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml?at=master',
  156. lineNumber: 14,
  157. }),
  158. ).toBe(
  159. 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/other/b.yaml?at=master#L14',
  160. );
  161. });
  162. it('works for full urls and throws away query params', () => {
  163. expect(
  164. defaultScmResolveUrl({
  165. url: 'https://b.com/b.yaml',
  166. base: 'https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/folder/a.yaml?at=master',
  167. }),
  168. ).toBe('https://b.com/b.yaml');
  169. });
  170. });