dashboard-transloadit.spec.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. describe('Dashboard with Transloadit', () => {
  2. beforeEach(() => {
  3. cy.visit('/dashboard-transloadit')
  4. cy.get('.uppy-Dashboard-input:first').as('file-input')
  5. cy.intercept('/assemblies').as('createAssemblies')
  6. cy.intercept('/assemblies/*').as('assemblies')
  7. cy.intercept('/resumable/*').as('resumable')
  8. })
  9. it('should upload cat image successfully', () => {
  10. cy.get('@file-input').selectFile('cypress/fixtures/images/cat.jpg', {
  11. force: true,
  12. })
  13. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  14. cy.wait(['@assemblies', '@resumable']).then(() => {
  15. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
  16. })
  17. })
  18. it('should close assembly polling when cancelled', () => {
  19. cy.intercept({
  20. method: 'GET',
  21. url: '/assemblies/*',
  22. }).as('assemblyPolling')
  23. cy.intercept(
  24. { method: 'DELETE', pathname: '/assemblies/*', times: 1 },
  25. { statusCode: 204, body: {} },
  26. ).as('delete')
  27. cy.window().then(({ uppy }) => {
  28. cy.get('@file-input').selectFile(
  29. [
  30. 'cypress/fixtures/images/cat.jpg',
  31. 'cypress/fixtures/images/traffic.jpg',
  32. 'cypress/fixtures/images/car.jpg',
  33. ],
  34. { force: true },
  35. )
  36. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  37. cy.wait(['@createAssemblies']).then(() => {
  38. // eslint-disable-next-line
  39. // @ts-ignore fix me
  40. expect(
  41. Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every(
  42. (a: any) => a.pollInterval,
  43. ),
  44. ).to.equal(true)
  45. uppy.cancelAll()
  46. cy.wait(['@delete']).then(() => {
  47. // eslint-disable-next-line
  48. // @ts-ignore fix me
  49. expect(
  50. Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some(
  51. (a: any) => a.pollInterval,
  52. ),
  53. ).to.equal(false)
  54. })
  55. })
  56. })
  57. })
  58. // Too flaky at the moment. Arguably, this is not the right place
  59. // as this is doing white box testing (testing internal state).
  60. // But E2e is more about black box testing, you don’t care about the internals, only the result.
  61. // May make more sense to turn this into a unit test.
  62. it.skip('should emit one assembly-cancelled event when cancelled', () => {
  63. const spy = cy.spy()
  64. cy.window().then(({ uppy }) => {
  65. // eslint-disable-next-line
  66. // @ts-ignore fix me
  67. uppy.on('transloadit:assembly-cancelled', spy)
  68. cy.get('@file-input').selectFile(
  69. [
  70. 'cypress/fixtures/images/cat.jpg',
  71. 'cypress/fixtures/images/traffic.jpg',
  72. ],
  73. { force: true },
  74. )
  75. cy.intercept({
  76. method: 'GET',
  77. url: '/assemblies/*',
  78. }).as('assemblyPolling')
  79. cy.intercept(
  80. { method: 'PATCH', pathname: '/files/*', times: 1 },
  81. { statusCode: 204, body: {} },
  82. )
  83. cy.intercept(
  84. { method: 'DELETE', pathname: '/resumable/files/*', times: 2 },
  85. { statusCode: 204, body: {} },
  86. ).as('fileDeletion')
  87. cy.intercept({
  88. method: 'DELETE',
  89. pathname: '/assemblies/*',
  90. times: 1,
  91. }).as('assemblyDeletion')
  92. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  93. cy.wait('@assemblyPolling')
  94. cy.get('button[data-cy=cancel]').click()
  95. cy.wait('@assemblyDeletion')
  96. // Unfortunately, waiting on a network request somehow often results in a race condition.
  97. // We just want to know wether this is called or not, so waiting for 2 sec to be sure.
  98. // eslint-disable-next-line cypress/no-unnecessary-waiting
  99. cy.wait(2000)
  100. expect(spy).to.be.calledOnce
  101. })
  102. })
  103. it.skip('should close assembly polling when all files are removed', () => {
  104. const spy = cy.spy()
  105. cy.window().then(({ uppy }) => {
  106. // eslint-disable-next-line
  107. // @ts-ignore fix me
  108. uppy.on('transloadit:assembly-cancelled', spy)
  109. cy.get('@file-input').selectFile(
  110. [
  111. 'cypress/fixtures/images/cat.jpg',
  112. 'cypress/fixtures/images/traffic.jpg',
  113. ],
  114. { force: true },
  115. )
  116. cy.intercept({
  117. method: 'GET',
  118. url: '/assemblies/*',
  119. }).as('assemblyPolling')
  120. cy.intercept(
  121. { method: 'PATCH', pathname: '/files/*', times: 1 },
  122. { statusCode: 204, body: {} },
  123. )
  124. cy.intercept(
  125. { method: 'DELETE', pathname: '/resumable/files/*', times: 2 },
  126. { statusCode: 204, body: {} },
  127. ).as('fileDeletion')
  128. cy.intercept({
  129. method: 'DELETE',
  130. pathname: '/assemblies/*',
  131. times: 1,
  132. }).as('assemblyDeletion')
  133. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  134. cy.wait('@assemblyPolling')
  135. // eslint-disable-next-line
  136. // @ts-ignore fix me
  137. expect(
  138. Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every(
  139. (a: any) => a.pollInterval,
  140. ),
  141. ).to.equal(true)
  142. const { files } = uppy.getState()
  143. // eslint-disable-next-line
  144. // @ts-ignore fix me
  145. uppy.removeFiles(Object.keys(files))
  146. cy.wait('@assemblyDeletion').then(() => {
  147. // eslint-disable-next-line
  148. // @ts-ignore fix me
  149. expect(
  150. Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some(
  151. (a: any) => a.pollInterval,
  152. ),
  153. ).to.equal(false)
  154. expect(spy).to.be.calledOnce
  155. })
  156. })
  157. })
  158. it('should not create assembly when all individual files have been cancelled', () => {
  159. cy.window().then(({ uppy }) => {
  160. cy.get('@file-input').selectFile(
  161. [
  162. 'cypress/fixtures/images/cat.jpg',
  163. 'cypress/fixtures/images/traffic.jpg',
  164. ],
  165. { force: true },
  166. )
  167. // eslint-disable-next-line
  168. // @ts-ignore fix me
  169. expect(
  170. Object.values(uppy.getPlugin('Transloadit').activeAssemblies).length,
  171. ).to.equal(0)
  172. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  173. const { files } = uppy.getState()
  174. // eslint-disable-next-line
  175. // @ts-ignore fix me
  176. uppy.removeFiles(Object.keys(files))
  177. cy.wait('@createAssemblies').then(() => {
  178. // eslint-disable-next-line
  179. // @ts-ignore fix me
  180. expect(
  181. Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some(
  182. (a: any) => a.pollInterval,
  183. ),
  184. ).to.equal(false)
  185. })
  186. })
  187. })
  188. it('should not emit error if upload is cancelled right away', () => {
  189. cy.get('@file-input').selectFile('cypress/fixtures/images/cat.jpg', {
  190. force: true,
  191. })
  192. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  193. const handler = cy.spy()
  194. cy.window().then(({ uppy }) => {
  195. const { files } = uppy.getState()
  196. uppy.on('upload-error', handler)
  197. const [fileID] = Object.keys(files)
  198. uppy.removeFile(fileID)
  199. uppy.removeFile(fileID)
  200. cy.wait('@createAssemblies').then(() => expect(handler).not.to.be.called)
  201. })
  202. })
  203. it('should not re-use erroneous tus keys', () => {
  204. function createAssemblyStatus({
  205. message,
  206. assembly_id,
  207. bytes_expected,
  208. ...other
  209. }) {
  210. return {
  211. message,
  212. assembly_id,
  213. parent_id: null,
  214. account_id: 'deadbeef',
  215. account_name: 'foo',
  216. account_slug: 'foo',
  217. template_id: null,
  218. template_name: null,
  219. instance: 'test.transloadit.com',
  220. assembly_url: `http://api2.test.transloadit.com/assemblies/${assembly_id}`,
  221. assembly_ssl_url: `https://api2-test.transloadit.com/assemblies/${assembly_id}`,
  222. uppyserver_url: 'https://api2-test.transloadit.com/companion/',
  223. companion_url: 'https://api2-test.transloadit.com/companion/',
  224. websocket_url: 'about:blank',
  225. tus_url: 'https://api2-test.transloadit.com/resumable/files/',
  226. bytes_received: 0,
  227. bytes_expected,
  228. upload_duration: 0.162,
  229. client_agent: null,
  230. client_ip: null,
  231. client_referer: null,
  232. transloadit_client:
  233. 'uppy-core:3.2.0,uppy-transloadit:3.1.3,uppy-tus:3.1.0,uppy-dropbox:3.1.1,uppy-box:2.1.1,uppy-facebook:3.1.1,uppy-google-drive:3.1.1,uppy-instagram:3.1.1,uppy-onedrive:3.1.1,uppy-zoom:2.1.1,uppy-url:3.3.1',
  234. start_date: new Date().toISOString(),
  235. upload_meta_data_extracted: false,
  236. warnings: [],
  237. is_infinite: false,
  238. has_dupe_jobs: false,
  239. execution_start: null,
  240. execution_duration: null,
  241. queue_duration: 0.009,
  242. jobs_queue_duration: 0,
  243. notify_start: null,
  244. notify_url: null,
  245. notify_response_code: null,
  246. notify_response_data: null,
  247. notify_duration: null,
  248. last_job_completed: null,
  249. fields: {},
  250. running_jobs: [],
  251. bytes_usage: 0,
  252. executing_jobs: [],
  253. started_jobs: [],
  254. parent_assembly_status: null,
  255. params: '{}',
  256. template: null,
  257. merged_params: '{}',
  258. expected_tus_uploads: 1,
  259. started_tus_uploads: 0,
  260. finished_tus_uploads: 0,
  261. tus_uploads: [],
  262. uploads: [],
  263. results: {},
  264. build_id: '4765326956',
  265. status_endpoint: `https://api2-test.transloadit.com/assemblies/${assembly_id}`,
  266. ...other,
  267. }
  268. }
  269. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg'], {
  270. force: true,
  271. })
  272. // SETUP for FIRST ATTEMPT (error response from Transloadit backend)
  273. const assemblyIDAttempt1 = '500e56004f4347a288194edd7c7a0ae1'
  274. const tusIDAttempt1 = 'a9daed4af4981880faf29b0e9596a14d'
  275. cy.intercept('POST', 'https://api2.transloadit.com/assemblies', {
  276. statusCode: 200,
  277. body: JSON.stringify(
  278. createAssemblyStatus({
  279. ok: 'ASSEMBLY_UPLOADING',
  280. message: 'The Assembly is still in the process of being uploaded.',
  281. assembly_id: assemblyIDAttempt1,
  282. bytes_expected: 263871,
  283. }),
  284. ),
  285. }).as('createAssembly')
  286. cy.intercept('POST', 'https://api2-test.transloadit.com/resumable/files/', {
  287. statusCode: 201,
  288. headers: {
  289. Location: `https://api2-test.transloadit.com/resumable/files/${tusIDAttempt1}`,
  290. },
  291. times: 1,
  292. }).as('tusCall')
  293. cy.intercept(
  294. 'PATCH',
  295. `https://api2-test.transloadit.com/resumable/files/${tusIDAttempt1}`,
  296. {
  297. statusCode: 204,
  298. headers: {
  299. 'Upload-Length': '263871',
  300. 'Upload-Offset': '263871',
  301. },
  302. times: 1,
  303. },
  304. )
  305. cy.intercept(
  306. 'HEAD',
  307. `https://api2-test.transloadit.com/resumable/files/${tusIDAttempt1}`,
  308. { statusCode: 204 },
  309. )
  310. cy.intercept(
  311. 'GET',
  312. `https://api2-test.transloadit.com/assemblies/${assemblyIDAttempt1}`,
  313. {
  314. statusCode: 200,
  315. body: JSON.stringify(
  316. createAssemblyStatus({
  317. error: 'INVALID_FILE_META_DATA',
  318. http_code: 400,
  319. message: 'Whatever error message from Transloadit backend',
  320. reason: 'Whatever reason',
  321. msg: 'Whatever error from Transloadit backend',
  322. assembly_id: '500e56004f4347a288194edd7c7a0ae1',
  323. bytes_expected: 263871,
  324. }),
  325. ),
  326. },
  327. ).as('failureReported')
  328. cy.intercept('POST', 'https://transloaditstatus.com/client_error', {
  329. statusCode: 200,
  330. body: '{}',
  331. })
  332. // FIRST ATTEMPT
  333. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  334. cy.wait(['@createAssembly', '@tusCall', '@failureReported'])
  335. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Upload failed')
  336. // SETUP for SECOND ATTEMPT
  337. const assemblyIDAttempt2 = '6a3fa40e527d4d73989fce678232a5e1'
  338. const tusIDAttempt2 = 'b8ebed4af4981880faf29b0e9596b25e'
  339. cy.intercept('POST', 'https://api2.transloadit.com/assemblies', {
  340. statusCode: 200,
  341. body: JSON.stringify(
  342. createAssemblyStatus({
  343. ok: 'ASSEMBLY_UPLOADING',
  344. message: 'The Assembly is still in the process of being uploaded.',
  345. assembly_id: assemblyIDAttempt2,
  346. tus_url: 'https://api2-test.transloadit.com/resumable/files/attempt2',
  347. bytes_expected: 263871,
  348. }),
  349. ),
  350. }).as('createAssembly-attempt2')
  351. cy.intercept(
  352. 'POST',
  353. 'https://api2-test.transloadit.com/resumable/files/attempt2',
  354. {
  355. statusCode: 201,
  356. headers: {
  357. 'Upload-Length': '263871',
  358. 'Upload-Offset': '0',
  359. Location: `https://api2-test.transloadit.com/resumable/files/${tusIDAttempt2}`,
  360. },
  361. times: 1,
  362. },
  363. ).as('tusCall-attempt2')
  364. cy.intercept(
  365. 'PATCH',
  366. `https://api2-test.transloadit.com/resumable/files/${tusIDAttempt2}`,
  367. {
  368. statusCode: 204,
  369. headers: {
  370. 'Upload-Length': '263871',
  371. 'Upload-Offset': '263871',
  372. 'Tus-Resumable': '1.0.0',
  373. },
  374. times: 1,
  375. },
  376. )
  377. cy.intercept(
  378. 'HEAD',
  379. `https://api2-test.transloadit.com/resumable/files/${tusIDAttempt2}`,
  380. { statusCode: 204 },
  381. )
  382. cy.intercept(
  383. 'GET',
  384. `https://api2-test.transloadit.com/assemblies/${assemblyIDAttempt2}`,
  385. {
  386. statusCode: 200,
  387. body: JSON.stringify(
  388. createAssemblyStatus({
  389. ok: 'ASSEMBLY_COMPLETED',
  390. http_code: 200,
  391. message: 'The Assembly was successfully completed.',
  392. assembly_id: assemblyIDAttempt2,
  393. bytes_received: 263871,
  394. bytes_expected: 263871,
  395. }),
  396. ),
  397. },
  398. ).as('assemblyCompleted-attempt2')
  399. // SECOND ATTEMPT
  400. cy.get('.uppy-StatusBar-actions > .uppy-c-btn').click()
  401. cy.wait([
  402. '@createAssembly-attempt2',
  403. '@tusCall-attempt2',
  404. '@assemblyCompleted-attempt2',
  405. ])
  406. })
  407. it('should complete on retry', () => {
  408. cy.get('@file-input').selectFile(
  409. [
  410. 'cypress/fixtures/images/cat.jpg',
  411. 'cypress/fixtures/images/traffic.jpg',
  412. ],
  413. { force: true },
  414. )
  415. cy.intercept('POST', 'https://transloaditstatus.com/client_error', {
  416. statusCode: 200,
  417. body: '{}',
  418. })
  419. cy.intercept(
  420. { method: 'POST', pathname: '/assemblies', times: 1 },
  421. { statusCode: 500, body: {} },
  422. ).as('failedAssemblyCreation')
  423. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  424. cy.wait('@failedAssemblyCreation')
  425. cy.get('button[data-cy=retry]').click()
  426. cy.wait(['@assemblies', '@resumable'])
  427. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
  428. })
  429. it('should complete when resuming after pause', () => {
  430. cy.get('@file-input').selectFile(
  431. [
  432. 'cypress/fixtures/images/cat.jpg',
  433. 'cypress/fixtures/images/traffic.jpg',
  434. ],
  435. { force: true },
  436. )
  437. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  438. cy.wait('@createAssemblies')
  439. cy.get('button[data-cy=togglePauseResume]').click()
  440. // eslint-disable-next-line cypress/no-unnecessary-waiting
  441. cy.wait(300) // Wait an arbitrary amount of time as a user would do.
  442. cy.get('button[data-cy=togglePauseResume]').click()
  443. cy.wait('@resumable')
  444. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
  445. })
  446. })