relocation.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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(() => expect(mockapi).toHaveBeenCalled());
  208. expect(addSuccessMessage).toHaveBeenCalledWith(
  209. "Your relocation has started - we'll email you with updates as soon as we have 'em!"
  210. );
  211. });
  212. it('throws error if user already has an in-progress relocation job', async function () {
  213. const mockapi = MockApiClient.addMockResponse({
  214. url: `/relocations/`,
  215. method: 'POST',
  216. statusCode: 409,
  217. });
  218. renderPage('get-started');
  219. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  220. const orgSlugsInput = screen.getByLabelText('org-slugs');
  221. const continueButton = screen.getByRole('button', {name: 'Continue'});
  222. await userEvent.type(orgSlugsInput, 'test-org');
  223. await userEvent.type(screen.getByLabelText('region'), 'U');
  224. await userEvent.click(screen.getByRole('menuitemradio'));
  225. await userEvent.click(continueButton);
  226. renderPage('upload-backup');
  227. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  228. const input = screen.getByLabelText('file-upload');
  229. await userEvent.upload(input, relocationFile);
  230. await userEvent.click(await screen.findByText('Start Relocation'));
  231. await waitFor(() => expect(mockapi).toHaveBeenCalled());
  232. expect(addErrorMessage).toHaveBeenCalledWith(
  233. 'You already have an in-progress relocation job.'
  234. );
  235. });
  236. it('throws error if daily limit of relocations has been reached', async function () {
  237. const mockapi = MockApiClient.addMockResponse({
  238. url: `/relocations/`,
  239. method: 'POST',
  240. statusCode: 429,
  241. });
  242. renderPage('get-started');
  243. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  244. const orgSlugsInput = screen.getByLabelText('org-slugs');
  245. const continueButton = screen.getByRole('button', {name: 'Continue'});
  246. await userEvent.type(orgSlugsInput, 'test-org');
  247. await userEvent.type(screen.getByLabelText('region'), 'U');
  248. await userEvent.click(screen.getByRole('menuitemradio'));
  249. await userEvent.click(continueButton);
  250. renderPage('upload-backup');
  251. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  252. const input = screen.getByLabelText('file-upload');
  253. await userEvent.upload(input, relocationFile);
  254. await userEvent.click(await screen.findByText('Start Relocation'));
  255. await waitFor(() => expect(mockapi).toHaveBeenCalled());
  256. expect(addErrorMessage).toHaveBeenCalledWith(
  257. 'We have reached the daily limit of relocations - please try again tomorrow, or contact support.'
  258. );
  259. });
  260. it('throws error if user session has expired', async function () {
  261. const mockapi = MockApiClient.addMockResponse({
  262. url: `/relocations/`,
  263. method: 'POST',
  264. statusCode: 401,
  265. });
  266. renderPage('get-started');
  267. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  268. const orgSlugsInput = screen.getByLabelText('org-slugs');
  269. const continueButton = screen.getByRole('button', {name: 'Continue'});
  270. await userEvent.type(orgSlugsInput, 'test-org');
  271. await userEvent.type(screen.getByLabelText('region'), 'U');
  272. await userEvent.click(screen.getByRole('menuitemradio'));
  273. await userEvent.click(continueButton);
  274. renderPage('upload-backup');
  275. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  276. const input = screen.getByLabelText('file-upload');
  277. await userEvent.upload(input, relocationFile);
  278. await userEvent.click(await screen.findByText('Start Relocation'));
  279. await waitFor(() => expect(mockapi).toHaveBeenCalled());
  280. expect(addErrorMessage).toHaveBeenCalledWith('Your session has expired.');
  281. });
  282. it('throws error for 500 error', async function () {
  283. const mockapi = MockApiClient.addMockResponse({
  284. url: `/relocations/`,
  285. method: 'POST',
  286. statusCode: 500,
  287. });
  288. renderPage('get-started');
  289. ConfigStore.set('regions', [{name: 'USA', url: 'https://example.com'}]);
  290. const orgSlugsInput = screen.getByLabelText('org-slugs');
  291. const continueButton = screen.getByRole('button', {name: 'Continue'});
  292. await userEvent.type(orgSlugsInput, 'test-org');
  293. await userEvent.type(screen.getByLabelText('region'), 'U');
  294. await userEvent.click(screen.getByRole('menuitemradio'));
  295. await userEvent.click(continueButton);
  296. renderPage('upload-backup');
  297. const relocationFile = new File(['hello'], 'hello.tar', {type: 'file'});
  298. const input = screen.getByLabelText('file-upload');
  299. await userEvent.upload(input, relocationFile);
  300. await userEvent.click(await screen.findByText('Start Relocation'));
  301. await waitFor(() => expect(mockapi).toHaveBeenCalled());
  302. expect(addErrorMessage).toHaveBeenCalledWith(
  303. 'An error has occurred while trying to start relocation job. Please contact support for further assistance.'
  304. );
  305. });
  306. });
  307. });