|
1 | 1 | import { type Config, createConfig } from '@redocly/openapi-core'; |
2 | | -import { getDomain } from '../domains.js'; |
| 2 | +import { getDomain, isValidReuniteUrl } from '../domains.js'; |
3 | 3 | import { getReuniteUrl } from '../domains.js'; |
4 | 4 |
|
5 | | -describe('getDomain()', () => { |
6 | | - afterEach(() => { |
7 | | - delete process.env.REDOCLY_DOMAIN; |
8 | | - }); |
| 5 | +describe('domains', () => { |
| 6 | + describe('getDomain()', () => { |
| 7 | + afterEach(() => { |
| 8 | + delete process.env.REDOCLY_DOMAIN; |
| 9 | + }); |
9 | 10 |
|
10 | | - it('should return the domain from environment variable', () => { |
11 | | - process.env.REDOCLY_DOMAIN = 'test-domain'; |
| 11 | + it('should return the domain from environment variable', () => { |
| 12 | + process.env.REDOCLY_DOMAIN = 'test-domain'; |
12 | 13 |
|
13 | | - expect(getDomain()).toBe('test-domain'); |
14 | | - }); |
| 14 | + expect(getDomain()).toBe('test-domain'); |
| 15 | + }); |
15 | 16 |
|
16 | | - it('should return the default domain if no domain provided', () => { |
17 | | - process.env.REDOCLY_DOMAIN = ''; |
| 17 | + it('should return the default domain if no domain provided', () => { |
| 18 | + process.env.REDOCLY_DOMAIN = ''; |
18 | 19 |
|
19 | | - expect(getDomain()).toBe('https://app.cloud.redocly.com'); |
| 20 | + expect(getDomain()).toBe('https://app.cloud.redocly.com'); |
| 21 | + }); |
20 | 22 | }); |
21 | | -}); |
22 | 23 |
|
23 | | -describe('getReuniteUrl()', () => { |
24 | | - let testConfig: Config; |
25 | | - beforeAll(async () => { |
26 | | - testConfig = await createConfig(); |
27 | | - }); |
| 24 | + describe('getReuniteUrl()', () => { |
| 25 | + let testConfig: Config; |
| 26 | + beforeAll(async () => { |
| 27 | + testConfig = await createConfig(); |
| 28 | + }); |
28 | 29 |
|
29 | | - it('should return US API URL when US region specified', () => { |
30 | | - expect(getReuniteUrl(testConfig, 'us')).toBe('https://app.cloud.redocly.com'); |
31 | | - }); |
| 30 | + it('should return US API URL when US region specified', () => { |
| 31 | + expect(getReuniteUrl(testConfig, 'us')).toBe('https://app.cloud.redocly.com'); |
| 32 | + }); |
32 | 33 |
|
33 | | - it('should return EU API URL when EU region specified', () => { |
34 | | - expect(getReuniteUrl(testConfig, 'eu')).toBe('https://app.cloud.eu.redocly.com'); |
35 | | - }); |
| 34 | + it('should return EU API URL when EU region specified', () => { |
| 35 | + expect(getReuniteUrl(testConfig, 'eu')).toBe('https://app.cloud.eu.redocly.com'); |
| 36 | + }); |
36 | 37 |
|
37 | | - it('should return custom domain API URL when custom domain specified', () => { |
38 | | - const customDomain = 'https://custom.domain.com'; |
39 | | - expect(getReuniteUrl(testConfig, customDomain)).toBe('https://custom.domain.com'); |
40 | | - }); |
| 38 | + it('should return custom domain API URL when custom domain specified', () => { |
| 39 | + const customDomain = 'https://custom.domain.com'; |
| 40 | + expect(getReuniteUrl(testConfig, customDomain)).toBe('https://custom.domain.com'); |
| 41 | + }); |
41 | 42 |
|
42 | | - it('should return US API URL when no region specified', () => { |
43 | | - expect(getReuniteUrl(testConfig)).toBe('https://app.cloud.redocly.com'); |
44 | | - }); |
| 43 | + it('should return US API URL when no region specified', () => { |
| 44 | + expect(getReuniteUrl(testConfig)).toBe('https://app.cloud.redocly.com'); |
| 45 | + }); |
45 | 46 |
|
46 | | - it('should use residency from config when no second parameter provided', async () => { |
47 | | - const config = await createConfig({ residency: 'eu' }); |
48 | | - expect(getReuniteUrl(config)).toBe('https://app.cloud.eu.redocly.com'); |
49 | | - }); |
| 47 | + it('should use residency from config when no second parameter provided', async () => { |
| 48 | + const config = await createConfig({ residency: 'eu' }); |
| 49 | + expect(getReuniteUrl(config)).toBe('https://app.cloud.eu.redocly.com'); |
| 50 | + }); |
50 | 51 |
|
51 | | - it('should use fromProjectUrl from config when no residency provided', async () => { |
52 | | - const config = await createConfig({ |
53 | | - scorecard: { fromProjectUrl: 'https://app.cloud.eu.redocly.com/org/test/project/test' }, |
| 52 | + it('should use fromProjectUrl from config when no residency provided', async () => { |
| 53 | + const config = await createConfig({ |
| 54 | + scorecard: { fromProjectUrl: 'https://app.cloud.eu.redocly.com/org/test/project/test' }, |
| 55 | + }); |
| 56 | + expect(getReuniteUrl(config)).toBe('https://app.cloud.eu.redocly.com'); |
54 | 57 | }); |
55 | | - expect(getReuniteUrl(config)).toBe('https://app.cloud.eu.redocly.com'); |
56 | | - }); |
57 | 58 |
|
58 | | - it('should prioritize second parameter over config residency and fromProjectUrl', async () => { |
59 | | - const config = await createConfig({ |
60 | | - residency: 'us', |
61 | | - scorecard: { fromProjectUrl: 'https://app.cloud.eu.redocly.com/org/test/project/test' }, |
| 59 | + it('should prioritize second parameter over config residency and fromProjectUrl', async () => { |
| 60 | + const config = await createConfig({ |
| 61 | + residency: 'us', |
| 62 | + scorecard: { fromProjectUrl: 'https://app.cloud.eu.redocly.com/org/test/project/test' }, |
| 63 | + }); |
| 64 | + expect(getReuniteUrl(config, 'eu')).toBe('https://app.cloud.eu.redocly.com'); |
62 | 65 | }); |
63 | | - expect(getReuniteUrl(config, 'eu')).toBe('https://app.cloud.eu.redocly.com'); |
64 | | - }); |
65 | 66 |
|
66 | | - it('should prioritize residency over fromProjectUrl when both are provided', async () => { |
67 | | - const config = await createConfig({ |
68 | | - residency: 'us', |
69 | | - scorecard: { fromProjectUrl: 'https://app.cloud.eu.redocly.com/org/test/project/test' }, |
| 67 | + it('should prioritize residency over fromProjectUrl when both are provided', async () => { |
| 68 | + const config = await createConfig({ |
| 69 | + residency: 'us', |
| 70 | + scorecard: { fromProjectUrl: 'https://app.cloud.eu.redocly.com/org/test/project/test' }, |
| 71 | + }); |
| 72 | + expect(getReuniteUrl(config)).toBe('https://app.cloud.redocly.com'); |
70 | 73 | }); |
71 | | - expect(getReuniteUrl(config)).toBe('https://app.cloud.redocly.com'); |
72 | | - }); |
73 | 74 |
|
74 | | - it('should handle custom domain from second parameter', async () => { |
75 | | - const config = await createConfig({ residency: 'us' }); |
76 | | - expect(getReuniteUrl(config, 'https://custom.redocly.com')).toBe('https://custom.redocly.com'); |
77 | | - }); |
| 75 | + it('should handle custom domain from second parameter', async () => { |
| 76 | + const config = await createConfig({ residency: 'us' }); |
| 77 | + expect(getReuniteUrl(config, 'https://custom.redocly.com')).toBe( |
| 78 | + 'https://custom.redocly.com' |
| 79 | + ); |
| 80 | + }); |
78 | 81 |
|
79 | | - it('should handle fromProjectUrl with custom domain and port', async () => { |
80 | | - const config = await createConfig({ |
81 | | - scorecard: { fromProjectUrl: 'https://custom.redocly.com:8080/org/test/project/test' }, |
| 82 | + it('should handle fromProjectUrl with custom domain and port', async () => { |
| 83 | + const config = await createConfig({ |
| 84 | + scorecard: { fromProjectUrl: 'https://custom.redocly.com:8080/org/test/project/test' }, |
| 85 | + }); |
| 86 | + expect(getReuniteUrl(config)).toBe('https://custom.redocly.com:8080'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should throw error for invalid residency or malformed URL', async () => { |
| 90 | + const config = await createConfig({ residency: 'invalid-region' }); |
| 91 | + expect(() => getReuniteUrl(config)).toThrow('Invalid Reunite URL'); |
| 92 | + expect(() => getReuniteUrl(testConfig, 'not-a-valid-url')).toThrow('Invalid Reunite URL'); |
82 | 93 | }); |
83 | | - expect(getReuniteUrl(config)).toBe('https://custom.redocly.com:8080'); |
84 | | - }); |
85 | 94 |
|
86 | | - it('should throw error for invalid residency or malformed URL', async () => { |
87 | | - const config = await createConfig({ residency: 'invalid-region' }); |
88 | | - expect(() => getReuniteUrl(config)).toThrow('Invalid Reunite URL'); |
89 | | - expect(() => getReuniteUrl(testConfig, 'not-a-valid-url')).toThrow('Invalid Reunite URL'); |
| 95 | + it('should throw error for invalid fromProjectUrl', async () => { |
| 96 | + const config = await createConfig({ |
| 97 | + scorecard: { fromProjectUrl: 'not-a-valid-url' }, |
| 98 | + }); |
| 99 | + expect(() => getReuniteUrl(config)).toThrow('Invalid Reunite URL'); |
| 100 | + }); |
90 | 101 | }); |
91 | 102 |
|
92 | | - it('should throw error for invalid fromProjectUrl', async () => { |
93 | | - const config = await createConfig({ |
94 | | - scorecard: { fromProjectUrl: 'not-a-valid-url' }, |
| 103 | + describe('isValidReuniteUrl', () => { |
| 104 | + it('should return true for default config', async () => { |
| 105 | + expect(isValidReuniteUrl('us')).toBe(true); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should return true when residency is a valid URL', async () => { |
| 109 | + expect(isValidReuniteUrl('https://app.cloud.cba.redocly.com')).toBe(true); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should return false when residency is not a valid URL', async () => { |
| 113 | + expect(isValidReuniteUrl('not-a-valid-url')).toBe(false); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should return false when residency has an invalid protocol', async () => { |
| 117 | + expect(isValidReuniteUrl('http:app.cloud.redocly.com')).toBe(false); |
95 | 118 | }); |
96 | | - expect(() => getReuniteUrl(config)).toThrow('Invalid Reunite URL'); |
97 | 119 | }); |
98 | 120 | }); |
0 commit comments