relocation.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {
  3. fireEvent,
  4. render,
  5. screen,
  6. userEvent,
  7. waitFor,
  8. } from 'sentry-test/reactTestingLibrary';
  9. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  10. import ConfigStore from 'sentry/stores/configStore';
  11. import Relocation from 'sentry/views/relocation/relocation';
  12. jest.mock('sentry/actionCreators/indicator');
  13. const fakePublicKey = `-----BEGIN PUBLIC KEY-----
  14. MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw5Or1zsGE1XJTL4q+1c4
  15. Ztu8+7SC/exrnEYlWH+LVLI8TVyuGwDTAXrgKHGwaMM5ZnjijP5i8+ph8lfLrybT
  16. l+2D81qPIqagEtNMDaHqUDm5Tq7I2qvxkJ5YuDLawRUPccKMwWlIDR2Gvfe3efce
  17. 870EicPsExz4uPOkNXGHJZ/FwCQrLo87MXFeqrqj+0Cf+qwCQSCW9qFWe5cj+zqt
  18. eeJa0qflcHHQzxK4/EKKpl/hkt4zi0aE/PuJgvJz2KB+X3+LzekTy90LzW3VhR4y
  19. IAxCAaGQJVsg9dhKOORjAf4XK9aXHvy/jUSyT43opj6AgNqXlKEQjb1NBA8qbJJS
  20. 8wIDAQAB
  21. -----END PUBLIC KEY-----`;
  22. describe('Relocation', function () {
  23. let fetchPublicKey: jest.Mock;
  24. beforeEach(function () {
  25. MockApiClient.asyncDelay = undefined;
  26. MockApiClient.clearMockResponses();
  27. fetchPublicKey = MockApiClient.addMockResponse({
  28. url: '/publickeys/relocations/',
  29. body: {
  30. public_key: fakePublicKey,
  31. },
  32. });
  33. // The tests fail because we have a "component update was not wrapped in act" error. It should
  34. // be safe to ignore this error, but we should remove the mock once we move to react testing
  35. // library.
  36. //
  37. // eslint-disable-next-line no-console
  38. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  39. });
  40. afterEach(function () {
  41. // console.error = consoleError;
  42. MockApiClient.clearMockResponses();
  43. MockApiClient.asyncDelay = undefined;
  44. });
  45. function renderPage(step) {
  46. const routeParams = {
  47. step,
  48. };
  49. const {routerProps, routerContext, organization} = initializeOrg({
  50. router: {
  51. params: routeParams,
  52. },
  53. });
  54. return render(<Relocation {...routerProps} />, {
  55. context: routerContext,
  56. organization,
  57. });
  58. }
  59. describe('Get Started', function () {
  60. it('renders', async function () {
  61. renderPage('get-started');
  62. await waitFor(() => expect(fetchPublicKey).toHaveBeenCalled());
  63. expect(
  64. await screen.findByText('Basic information needed to get started')
  65. ).toBeInTheDocument();
  66. expect(
  67. await screen.findByText('Organization slugs being relocated')
  68. ).toBeInTheDocument();
  69. expect(await screen.findByText('Choose a datacenter region')).toBeInTheDocument();
  70. });
  71. it('should prevent user from going to the next step if no org slugs or region are entered', async function () {
  72. renderPage('get-started');
  73. await waitFor(() => expect(fetchPublicKey).toHaveBeenCalled());
  74. expect(await screen.getByRole('button', {name: 'Continue'})).toBeDisabled();
  75. });
  76. it('should be allowed to go to next step if org slug is entered and region is selected', async function () {
  77. renderPage('get-started');
  78. await waitFor(() => expect(fetchPublicKey).toHaveBeenCalled());
  79. ConfigStore.set('relocationConfig', {selectableRegions: ['USA']});
  80. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  81. const orgSlugsInput = await screen.getByLabelText('org-slugs');
  82. const continueButton = await screen.getByRole('button', {name: 'Continue'});
  83. await userEvent.type(orgSlugsInput, 'test-org');
  84. await userEvent.type(await screen.getByLabelText('region'), 'U');
  85. await userEvent.click(await screen.getByRole('menuitemradio'));
  86. expect(continueButton).toBeEnabled();
  87. });
  88. });
  89. describe('Public Key', function () {
  90. it('should show instructions if key retrieval was successful', async function () {
  91. renderPage('public-key');
  92. await waitFor(() => expect(fetchPublicKey).toHaveBeenCalled());
  93. expect(
  94. await screen.findByText("Save Sentry's public key to your machine")
  95. ).toBeInTheDocument();
  96. expect(await screen.getByText('key.pub')).toBeInTheDocument();
  97. expect(await screen.getByRole('button', {name: 'Continue'})).toBeInTheDocument();
  98. });
  99. it('should show loading indicator if key retrieval still in progress', function () {
  100. MockApiClient.asyncDelay = 1;
  101. renderPage('public-key');
  102. expect(screen.queryByRole('button', {name: 'Continue'})).not.toBeInTheDocument();
  103. expect(screen.queryByText('key.pub')).not.toBeInTheDocument();
  104. });
  105. it('should show loading indicator and error message if key retrieval failed', async function () {
  106. MockApiClient.clearMockResponses();
  107. fetchPublicKey = MockApiClient.addMockResponse({
  108. url: '/publickeys/relocations/',
  109. statusCode: 400,
  110. });
  111. renderPage('public-key');
  112. await waitFor(() => expect(fetchPublicKey).toHaveBeenCalled());
  113. expect(
  114. await screen.queryByRole('button', {name: 'Continue'})
  115. ).not.toBeInTheDocument();
  116. expect(await screen.queryByText('key.pub')).not.toBeInTheDocument();
  117. expect(await screen.getByRole('button', {name: 'Retry'})).toBeInTheDocument();
  118. MockApiClient.addMockResponse({
  119. url: '/publickeys/relocations/',
  120. body: {
  121. public_key: fakePublicKey,
  122. },
  123. });
  124. await userEvent.click(screen.getByRole('button', {name: 'Retry'}));
  125. await waitFor(() => expect(fetchPublicKey).toHaveBeenCalled());
  126. expect(await screen.queryByText('key.pub')).toBeInTheDocument();
  127. expect(await screen.queryByRole('button', {name: 'Continue'})).toBeInTheDocument();
  128. });
  129. });
  130. describe('Encrypt Backup', function () {
  131. it('renders', async function () {
  132. renderPage('encrypt-backup');
  133. await waitFor(() => expect(fetchPublicKey).toHaveBeenCalled());
  134. expect(
  135. await screen.findByText(
  136. 'Create an encrypted backup of your current self-hosted instance'
  137. )
  138. ).toBeInTheDocument();
  139. });
  140. });
  141. describe('Upload Backup', function () {
  142. it('renders', async function () {
  143. renderPage('upload-backup');
  144. expect(
  145. await screen.findByText('Upload Tarball to begin the relocation process')
  146. ).toBeInTheDocument();
  147. });
  148. it('accepts a file upload', async function () {
  149. renderPage('upload-backup');
  150. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  151. const input = screen.getByLabelText('file-upload');
  152. await userEvent.upload(input, relocationFile);
  153. expect(await screen.findByText('hello.tar')).toBeInTheDocument();
  154. expect(await screen.findByText('Start Relocation')).toBeInTheDocument();
  155. });
  156. it('accepts a file upload through drag and drop', async function () {
  157. renderPage('upload-backup');
  158. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  159. const dropzone = screen.getByLabelText('dropzone');
  160. fireEvent.drop(dropzone, {dataTransfer: {files: [relocationFile]}});
  161. expect(await screen.findByText('hello.tar')).toBeInTheDocument();
  162. expect(await screen.findByText('Start Relocation')).toBeInTheDocument();
  163. });
  164. it('correctly removes file and prompts for file upload', async function () {
  165. renderPage('upload-backup');
  166. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  167. const input = screen.getByLabelText('file-upload');
  168. await userEvent.upload(input, relocationFile);
  169. await userEvent.click(screen.getByText('Remove file'));
  170. expect(screen.queryByText('hello.tar')).not.toBeInTheDocument();
  171. expect(
  172. await screen.findByText('Upload Tarball to begin the relocation process')
  173. ).toBeInTheDocument();
  174. });
  175. it('fails to starts relocation job if some form data is missing', async function () {
  176. const mockapi = MockApiClient.addMockResponse({
  177. url: `/relocations/`,
  178. method: 'POST',
  179. });
  180. renderPage('upload-backup');
  181. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  182. const input = screen.getByLabelText('file-upload');
  183. await userEvent.upload(input, relocationFile);
  184. await userEvent.click(await screen.findByText('Start Relocation'));
  185. await waitFor(() => expect(mockapi).not.toHaveBeenCalled());
  186. expect(addErrorMessage).toHaveBeenCalledWith(
  187. 'An error has occurred while trying to start relocation job. Please contact support for further assistance.'
  188. );
  189. });
  190. it('starts relocation job if form data is available from previous steps', async function () {
  191. const mockapi = MockApiClient.addMockResponse({
  192. url: `/relocations/`,
  193. method: 'POST',
  194. });
  195. renderPage('get-started');
  196. ConfigStore.set('relocationConfig', {selectableRegions: ['USA']});
  197. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  198. const orgSlugsInput = await screen.getByLabelText('org-slugs');
  199. const continueButton = await screen.getByRole('button', {name: 'Continue'});
  200. await userEvent.type(orgSlugsInput, 'test-org');
  201. await userEvent.type(screen.getByLabelText('region'), 'U');
  202. await userEvent.click(screen.getByRole('menuitemradio'));
  203. await userEvent.click(continueButton);
  204. renderPage('upload-backup');
  205. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  206. const input = screen.getByLabelText('file-upload');
  207. await userEvent.upload(input, relocationFile);
  208. await userEvent.click(await screen.findByText('Start Relocation'));
  209. await waitFor(() =>
  210. expect(mockapi).toHaveBeenCalledWith(
  211. '/relocations/',
  212. expect.objectContaining({host: 'https://example.com', method: 'POST'})
  213. )
  214. );
  215. expect(addSuccessMessage).toHaveBeenCalledWith(
  216. "Your relocation has started - we'll email you with updates as soon as we have 'em!"
  217. );
  218. });
  219. it('throws error if user already has an in-progress relocation job', async function () {
  220. const mockapi = MockApiClient.addMockResponse({
  221. url: `/relocations/`,
  222. method: 'POST',
  223. statusCode: 409,
  224. });
  225. renderPage('get-started');
  226. ConfigStore.set('relocationConfig', {selectableRegions: ['USA']});
  227. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  228. const orgSlugsInput = screen.getByLabelText('org-slugs');
  229. const continueButton = screen.getByRole('button', {name: 'Continue'});
  230. await userEvent.type(orgSlugsInput, 'test-org');
  231. await userEvent.type(screen.getByLabelText('region'), 'U');
  232. await userEvent.click(screen.getByRole('menuitemradio'));
  233. await userEvent.click(continueButton);
  234. renderPage('upload-backup');
  235. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  236. const input = screen.getByLabelText('file-upload');
  237. await userEvent.upload(input, relocationFile);
  238. await userEvent.click(await screen.findByText('Start Relocation'));
  239. await waitFor(() => expect(mockapi).toHaveBeenCalled());
  240. expect(addErrorMessage).toHaveBeenCalledWith(
  241. 'You already have an in-progress relocation job.'
  242. );
  243. });
  244. it('throws error if daily limit of relocations has been reached', async function () {
  245. const mockapi = MockApiClient.addMockResponse({
  246. url: `/relocations/`,
  247. method: 'POST',
  248. statusCode: 429,
  249. });
  250. renderPage('get-started');
  251. ConfigStore.set('relocationConfig', {selectableRegions: ['USA']});
  252. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  253. const orgSlugsInput = screen.getByLabelText('org-slugs');
  254. const continueButton = screen.getByRole('button', {name: 'Continue'});
  255. await userEvent.type(orgSlugsInput, 'test-org');
  256. await userEvent.type(screen.getByLabelText('region'), 'U');
  257. await userEvent.click(screen.getByRole('menuitemradio'));
  258. await userEvent.click(continueButton);
  259. renderPage('upload-backup');
  260. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  261. const input = screen.getByLabelText('file-upload');
  262. await userEvent.upload(input, relocationFile);
  263. await userEvent.click(await screen.findByText('Start Relocation'));
  264. await waitFor(() => expect(mockapi).toHaveBeenCalled());
  265. expect(addErrorMessage).toHaveBeenCalledWith(
  266. 'We have reached the daily limit of relocations - please try again tomorrow, or contact support.'
  267. );
  268. });
  269. it('throws error if user session has expired', async function () {
  270. const mockapi = MockApiClient.addMockResponse({
  271. url: `/relocations/`,
  272. method: 'POST',
  273. statusCode: 401,
  274. });
  275. renderPage('get-started');
  276. ConfigStore.set('relocationConfig', {selectableRegions: ['USA']});
  277. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  278. const orgSlugsInput = screen.getByLabelText('org-slugs');
  279. const continueButton = screen.getByRole('button', {name: 'Continue'});
  280. await userEvent.type(orgSlugsInput, 'test-org');
  281. await userEvent.type(screen.getByLabelText('region'), 'U');
  282. await userEvent.click(screen.getByRole('menuitemradio'));
  283. await userEvent.click(continueButton);
  284. renderPage('upload-backup');
  285. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  286. const input = screen.getByLabelText('file-upload');
  287. await userEvent.upload(input, relocationFile);
  288. await userEvent.click(await screen.findByText('Start Relocation'));
  289. await waitFor(() => expect(mockapi).toHaveBeenCalled());
  290. expect(addErrorMessage).toHaveBeenCalledWith('Your session has expired.');
  291. });
  292. it('throws error for 500 error', async function () {
  293. const mockapi = MockApiClient.addMockResponse({
  294. url: `/relocations/`,
  295. method: 'POST',
  296. statusCode: 500,
  297. });
  298. renderPage('get-started');
  299. ConfigStore.set('relocationConfig', {selectableRegions: ['USA']});
  300. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  301. const orgSlugsInput = screen.getByLabelText('org-slugs');
  302. const continueButton = screen.getByRole('button', {name: 'Continue'});
  303. await userEvent.type(orgSlugsInput, 'test-org');
  304. await userEvent.type(screen.getByLabelText('region'), 'U');
  305. await userEvent.click(screen.getByRole('menuitemradio'));
  306. await userEvent.click(continueButton);
  307. renderPage('upload-backup');
  308. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  309. const input = screen.getByLabelText('file-upload');
  310. await userEvent.upload(input, relocationFile);
  311. await userEvent.click(await screen.findByText('Start Relocation'));
  312. await waitFor(() => expect(mockapi).toHaveBeenCalled());
  313. expect(addErrorMessage).toHaveBeenCalledWith(
  314. 'An error has occurred while trying to start relocation job. Please contact support for further assistance.'
  315. );
  316. });
  317. });
  318. });