relocation.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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('regions', [{name: 'USA', url: 'https://example.com'}]);
  80. const orgSlugsInput = await screen.getByLabelText('org-slugs');
  81. const continueButton = await screen.getByRole('button', {name: 'Continue'});
  82. await userEvent.type(orgSlugsInput, 'test-org');
  83. await userEvent.type(await screen.getByLabelText('region'), 'U');
  84. await userEvent.click(await screen.getByRole('menuitemradio'));
  85. expect(continueButton).toBeEnabled();
  86. });
  87. });
  88. describe('Public Key', function () {
  89. it('should show instructions if key retrieval was successful', async function () {
  90. renderPage('public-key');
  91. await waitFor(() => expect(fetchPublicKey).toHaveBeenCalled());
  92. expect(
  93. await screen.findByText("Save Sentry's public key to your machine")
  94. ).toBeInTheDocument();
  95. expect(await screen.getByText('key.pub')).toBeInTheDocument();
  96. expect(await screen.getByRole('button', {name: 'Continue'})).toBeInTheDocument();
  97. });
  98. it('should show loading indicator if key retrieval still in progress', function () {
  99. MockApiClient.asyncDelay = 1;
  100. renderPage('public-key');
  101. expect(screen.queryByRole('button', {name: 'Continue'})).not.toBeInTheDocument();
  102. expect(screen.queryByText('key.pub')).not.toBeInTheDocument();
  103. });
  104. it('should show loading indicator and error message if key retrieval failed', async function () {
  105. MockApiClient.clearMockResponses();
  106. fetchPublicKey = MockApiClient.addMockResponse({
  107. url: '/publickeys/relocations/',
  108. statusCode: 400,
  109. });
  110. renderPage('public-key');
  111. await waitFor(() => expect(fetchPublicKey).toHaveBeenCalled());
  112. expect(
  113. await screen.queryByRole('button', {name: 'Continue'})
  114. ).not.toBeInTheDocument();
  115. expect(await screen.queryByText('key.pub')).not.toBeInTheDocument();
  116. expect(await screen.getByRole('button', {name: 'Retry'})).toBeInTheDocument();
  117. MockApiClient.addMockResponse({
  118. url: '/publickeys/relocations/',
  119. body: {
  120. public_key: fakePublicKey,
  121. },
  122. });
  123. await userEvent.click(screen.getByRole('button', {name: 'Retry'}));
  124. await waitFor(() => expect(fetchPublicKey).toHaveBeenCalled());
  125. expect(await screen.queryByText('key.pub')).toBeInTheDocument();
  126. expect(await screen.queryByRole('button', {name: 'Continue'})).toBeInTheDocument();
  127. });
  128. });
  129. describe('Encrypt Backup', function () {
  130. it('renders', async function () {
  131. renderPage('encrypt-backup');
  132. await waitFor(() => expect(fetchPublicKey).toHaveBeenCalled());
  133. expect(
  134. await screen.findByText(
  135. 'Create an encrypted backup of your current self-hosted instance'
  136. )
  137. ).toBeInTheDocument();
  138. });
  139. });
  140. describe('Upload Backup', function () {
  141. it('renders', async function () {
  142. renderPage('upload-backup');
  143. expect(
  144. await screen.findByText('Upload Tarball to begin the relocation process')
  145. ).toBeInTheDocument();
  146. });
  147. it('accepts a file upload', async function () {
  148. renderPage('upload-backup');
  149. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  150. const input = screen.getByLabelText('file-upload');
  151. await userEvent.upload(input, relocationFile);
  152. expect(await screen.findByText('hello.tar')).toBeInTheDocument();
  153. expect(await screen.findByText('Start Relocation')).toBeInTheDocument();
  154. });
  155. it('accepts a file upload through drag and drop', async function () {
  156. renderPage('upload-backup');
  157. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  158. const dropzone = screen.getByLabelText('dropzone');
  159. fireEvent.drop(dropzone, {dataTransfer: {files: [relocationFile]}});
  160. expect(await screen.findByText('hello.tar')).toBeInTheDocument();
  161. expect(await screen.findByText('Start Relocation')).toBeInTheDocument();
  162. });
  163. it('correctly removes file and prompts for file upload', async function () {
  164. renderPage('upload-backup');
  165. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  166. const input = screen.getByLabelText('file-upload');
  167. await userEvent.upload(input, relocationFile);
  168. await userEvent.click(screen.getByText('Remove file'));
  169. expect(screen.queryByText('hello.tar')).not.toBeInTheDocument();
  170. expect(
  171. await screen.findByText('Upload Tarball to begin the relocation process')
  172. ).toBeInTheDocument();
  173. });
  174. it('fails to starts relocation job if some form data is missing', async function () {
  175. const mockapi = MockApiClient.addMockResponse({
  176. url: `/relocations/`,
  177. method: 'POST',
  178. });
  179. renderPage('upload-backup');
  180. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  181. const input = screen.getByLabelText('file-upload');
  182. await userEvent.upload(input, relocationFile);
  183. await userEvent.click(await screen.findByText('Start Relocation'));
  184. await waitFor(() => expect(mockapi).not.toHaveBeenCalled());
  185. expect(addErrorMessage).toHaveBeenCalledWith(
  186. 'An error has occurred while trying to start relocation job. Please contact support for further assistance.'
  187. );
  188. });
  189. it('starts relocation job if form data is available from previous steps', async function () {
  190. const mockapi = MockApiClient.addMockResponse({
  191. url: `/relocations/`,
  192. method: 'POST',
  193. });
  194. renderPage('get-started');
  195. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  196. const orgSlugsInput = await screen.getByLabelText('org-slugs');
  197. const continueButton = await screen.getByRole('button', {name: 'Continue'});
  198. await userEvent.type(orgSlugsInput, 'test-org');
  199. await userEvent.type(screen.getByLabelText('region'), 'U');
  200. await userEvent.click(screen.getByRole('menuitemradio'));
  201. await userEvent.click(continueButton);
  202. renderPage('upload-backup');
  203. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  204. const input = screen.getByLabelText('file-upload');
  205. await userEvent.upload(input, relocationFile);
  206. await userEvent.click(await screen.findByText('Start Relocation'));
  207. await waitFor(() =>
  208. expect(mockapi).toHaveBeenCalledWith(
  209. '/relocations/',
  210. expect.objectContaining({host: 'https://example.com', method: 'POST'})
  211. )
  212. );
  213. expect(addSuccessMessage).toHaveBeenCalledWith(
  214. "Your relocation has started - we'll email you with updates as soon as we have 'em!"
  215. );
  216. });
  217. it('throws error if user already has an in-progress relocation job', async function () {
  218. const mockapi = MockApiClient.addMockResponse({
  219. url: `/relocations/`,
  220. method: 'POST',
  221. statusCode: 409,
  222. });
  223. renderPage('get-started');
  224. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  225. const orgSlugsInput = screen.getByLabelText('org-slugs');
  226. const continueButton = screen.getByRole('button', {name: 'Continue'});
  227. await userEvent.type(orgSlugsInput, 'test-org');
  228. await userEvent.type(screen.getByLabelText('region'), 'U');
  229. await userEvent.click(screen.getByRole('menuitemradio'));
  230. await userEvent.click(continueButton);
  231. renderPage('upload-backup');
  232. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  233. const input = screen.getByLabelText('file-upload');
  234. await userEvent.upload(input, relocationFile);
  235. await userEvent.click(await screen.findByText('Start Relocation'));
  236. await waitFor(() => expect(mockapi).toHaveBeenCalled());
  237. expect(addErrorMessage).toHaveBeenCalledWith(
  238. 'You already have an in-progress relocation job.'
  239. );
  240. });
  241. it('throws error if daily limit of relocations has been reached', async function () {
  242. const mockapi = MockApiClient.addMockResponse({
  243. url: `/relocations/`,
  244. method: 'POST',
  245. statusCode: 429,
  246. });
  247. renderPage('get-started');
  248. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  249. const orgSlugsInput = screen.getByLabelText('org-slugs');
  250. const continueButton = screen.getByRole('button', {name: 'Continue'});
  251. await userEvent.type(orgSlugsInput, 'test-org');
  252. await userEvent.type(screen.getByLabelText('region'), 'U');
  253. await userEvent.click(screen.getByRole('menuitemradio'));
  254. await userEvent.click(continueButton);
  255. renderPage('upload-backup');
  256. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  257. const input = screen.getByLabelText('file-upload');
  258. await userEvent.upload(input, relocationFile);
  259. await userEvent.click(await screen.findByText('Start Relocation'));
  260. await waitFor(() => expect(mockapi).toHaveBeenCalled());
  261. expect(addErrorMessage).toHaveBeenCalledWith(
  262. 'We have reached the daily limit of relocations - please try again tomorrow, or contact support.'
  263. );
  264. });
  265. it('throws error if user session has expired', async function () {
  266. const mockapi = MockApiClient.addMockResponse({
  267. url: `/relocations/`,
  268. method: 'POST',
  269. statusCode: 401,
  270. });
  271. renderPage('get-started');
  272. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  273. const orgSlugsInput = screen.getByLabelText('org-slugs');
  274. const continueButton = screen.getByRole('button', {name: 'Continue'});
  275. await userEvent.type(orgSlugsInput, 'test-org');
  276. await userEvent.type(screen.getByLabelText('region'), 'U');
  277. await userEvent.click(screen.getByRole('menuitemradio'));
  278. await userEvent.click(continueButton);
  279. renderPage('upload-backup');
  280. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  281. const input = screen.getByLabelText('file-upload');
  282. await userEvent.upload(input, relocationFile);
  283. await userEvent.click(await screen.findByText('Start Relocation'));
  284. await waitFor(() => expect(mockapi).toHaveBeenCalled());
  285. expect(addErrorMessage).toHaveBeenCalledWith('Your session has expired.');
  286. });
  287. it('throws error for 500 error', async function () {
  288. const mockapi = MockApiClient.addMockResponse({
  289. url: `/relocations/`,
  290. method: 'POST',
  291. statusCode: 500,
  292. });
  293. renderPage('get-started');
  294. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  295. const orgSlugsInput = screen.getByLabelText('org-slugs');
  296. const continueButton = screen.getByRole('button', {name: 'Continue'});
  297. await userEvent.type(orgSlugsInput, 'test-org');
  298. await userEvent.type(screen.getByLabelText('region'), 'U');
  299. await userEvent.click(screen.getByRole('menuitemradio'));
  300. await userEvent.click(continueButton);
  301. renderPage('upload-backup');
  302. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  303. const input = screen.getByLabelText('file-upload');
  304. await userEvent.upload(input, relocationFile);
  305. await userEvent.click(await screen.findByText('Start Relocation'));
  306. await waitFor(() => expect(mockapi).toHaveBeenCalled());
  307. expect(addErrorMessage).toHaveBeenCalledWith(
  308. 'An error has occurred while trying to start relocation job. Please contact support for further assistance.'
  309. );
  310. });
  311. });
  312. });