resolveRoute.spec.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {resolveRoute} from './resolveRoute';
  3. const mockDeployPreviewConfig = jest.fn();
  4. jest.mock('sentry/constants', () => {
  5. const sentryConstants = jest.requireActual('sentry/constants');
  6. return {
  7. ...sentryConstants,
  8. get DEPLOY_PREVIEW_CONFIG() {
  9. return mockDeployPreviewConfig();
  10. },
  11. };
  12. });
  13. describe('resolveRoute', () => {
  14. let devUi, host, initialData;
  15. const organization = OrganizationFixture();
  16. const otherOrg = OrganizationFixture({
  17. features: ['customer-domains'],
  18. slug: 'other-org',
  19. });
  20. beforeEach(() => {
  21. devUi = window.__SENTRY_DEV_UI;
  22. host = window.location.host;
  23. initialData = window.__initialData;
  24. });
  25. afterEach(() => {
  26. window.__SENTRY_DEV_UI = devUi;
  27. window.location.host = host;
  28. window.__initialData = initialData;
  29. mockDeployPreviewConfig.mockReset();
  30. });
  31. it('should replace domains with dev-ui mode on localhost', () => {
  32. window.__SENTRY_DEV_UI = true;
  33. window.location.host = 'acme.localhost:7999';
  34. const result = resolveRoute('/issues/', organization, otherOrg);
  35. expect(result).toBe('https://other-org.localhost:7999/issues/');
  36. });
  37. it('should replace domains with dev-ui mode on dev.getsentry.net', () => {
  38. window.__SENTRY_DEV_UI = true;
  39. window.location.host = 'acme.dev.getsentry.net:7999';
  40. const result = resolveRoute('/issues/', organization, otherOrg);
  41. expect(result).toBe('https://other-org.dev.getsentry.net:7999/issues/');
  42. });
  43. it('should use path slugs on sentry.dev', () => {
  44. // Vercel previews don't let us have additional subdomains.
  45. window.__SENTRY_DEV_UI = true;
  46. window.location.host = 'sentry-abc123.sentry.dev';
  47. mockDeployPreviewConfig.mockReturnValue({
  48. branch: 'test',
  49. commitSha: 'abc123',
  50. githubOrg: 'getsentry',
  51. githubRepo: 'sentry',
  52. });
  53. const result = resolveRoute(
  54. `/organizations/${otherOrg.slug}/issues/`,
  55. organization,
  56. otherOrg
  57. );
  58. expect(result).toBe(
  59. 'https://sentry-abc123.sentry.dev/organizations/other-org/issues/'
  60. );
  61. });
  62. it('will not replace domains with dev-ui mode and an unsafe host', () => {
  63. window.__SENTRY_DEV_UI = true;
  64. window.location.host = 'bad-domain.com';
  65. const result = resolveRoute('/issues/', organization, otherOrg);
  66. expect(result).toBe('https://other-org.sentry.io/issues/');
  67. });
  68. it('should add domain when switching to customer-domain org', () => {
  69. let result = resolveRoute('/issues/', organization, otherOrg);
  70. expect(result).toBe('https://other-org.sentry.io/issues/');
  71. // Same result when we don't have a current org
  72. result = resolveRoute('/issues/', null, otherOrg);
  73. expect(result).toBe('https://other-org.sentry.io/issues/');
  74. });
  75. it('should use path slugs when org does not have customer-domains', () => {
  76. const result = resolveRoute(
  77. `/organizations/${organization.slug}/issues/`,
  78. organization
  79. );
  80. expect(result).toBe(`/organizations/${organization.slug}/issues/`);
  81. });
  82. it('should use slugless URL when org has customer domains', () => {
  83. window.__initialData = {
  84. ...window.__initialData,
  85. customerDomain: {
  86. subdomain: otherOrg.slug,
  87. organizationUrl: `https://${otherOrg.slug}.sentry.io`,
  88. sentryUrl: `https://sentry.io`,
  89. },
  90. };
  91. const result = resolveRoute(`/organizations/${otherOrg.slug}/issues/`, otherOrg);
  92. expect(result).toBe(`/issues/`);
  93. });
  94. it('should use sentryUrl when going from customer-domain to not', () => {
  95. const result = resolveRoute(
  96. `/organizations/${organization.slug}/issues/`,
  97. otherOrg,
  98. organization
  99. );
  100. expect(result).toBe(`https://sentry.io/organizations/${organization.slug}/issues/`);
  101. });
  102. });