dashboard-transloadit.spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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', { force:true })
  11. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  12. cy.wait(['@assemblies', '@resumable']).then(() => {
  13. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
  14. })
  15. })
  16. it('should close assembly polling when cancelled', () => {
  17. cy.intercept({
  18. method: 'GET',
  19. url: '/assemblies/*',
  20. }).as('assemblyPolling')
  21. cy.intercept(
  22. { method: 'DELETE', pathname: '/assemblies/*', times: 1 },
  23. { statusCode: 204, body: {} },
  24. ).as('delete')
  25. cy.window().then(({ uppy }) => {
  26. cy.get('@file-input').selectFile(
  27. ['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg', 'cypress/fixtures/images/car.jpg'],
  28. { force:true },
  29. )
  30. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  31. cy.wait(['@createAssemblies']).then(() => {
  32. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every((a: any) => a.pollInterval)).to.equal(true)
  33. uppy.cancelAll()
  34. cy.wait(['@delete']).then(() => {
  35. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
  36. })
  37. })
  38. })
  39. })
  40. // Too flaky at the moment. Arguably, this is not the right place
  41. // as this is doing white box testing (testing internal state).
  42. // But E2e is more about black box testing, you don’t care about the internals, only the result.
  43. // May make more sense to turn this into a unit test.
  44. it.skip('should emit one assembly-cancelled event when cancelled', () => {
  45. const spy = cy.spy()
  46. cy.window().then(({ uppy }) => {
  47. uppy.on('transloadit:assembly-cancelled', spy)
  48. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  49. cy.intercept({
  50. method: 'GET',
  51. url: '/assemblies/*',
  52. }).as('assemblyPolling')
  53. cy.intercept(
  54. { method: 'PATCH', pathname: '/files/*', times: 1 },
  55. { statusCode: 204, body: {} },
  56. )
  57. cy.intercept(
  58. { method: 'DELETE', pathname: '/resumable/files/*', times: 2 },
  59. { statusCode: 204, body: {} },
  60. ).as('fileDeletion')
  61. cy.intercept(
  62. { method: 'DELETE', pathname: '/assemblies/*', times: 1 },
  63. ).as('assemblyDeletion')
  64. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  65. cy.wait('@assemblyPolling')
  66. cy.get('button[data-cy=cancel]').click()
  67. cy.wait('@assemblyDeletion')
  68. // Unfortunately, waiting on a network request somehow often results in a race condition.
  69. // We just want to know wether this is called or not, so waiting for 2 sec to be sure.
  70. // eslint-disable-next-line cypress/no-unnecessary-waiting
  71. cy.wait(2000)
  72. expect(spy).to.be.calledOnce
  73. })
  74. })
  75. it.skip('should close assembly polling when all files are removed', () => {
  76. const spy = cy.spy()
  77. cy.window().then(({ uppy }) => {
  78. uppy.on('transloadit:assembly-cancelled', spy)
  79. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  80. cy.intercept({
  81. method: 'GET',
  82. url: '/assemblies/*',
  83. }).as('assemblyPolling')
  84. cy.intercept(
  85. { method: 'PATCH', pathname: '/files/*', times: 1 },
  86. { statusCode: 204, body: {} },
  87. )
  88. cy.intercept(
  89. { method: 'DELETE', pathname: '/resumable/files/*', times: 2 },
  90. { statusCode: 204, body: {} },
  91. ).as('fileDeletion')
  92. cy.intercept(
  93. { method: 'DELETE', pathname: '/assemblies/*', times: 1 },
  94. ).as('assemblyDeletion')
  95. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  96. cy.wait('@assemblyPolling')
  97. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every((a: any) => a.pollInterval)).to.equal(true)
  98. const { files } = uppy.getState()
  99. uppy.removeFiles(Object.keys(files))
  100. cy.wait('@assemblyDeletion').then(() => {
  101. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
  102. expect(spy).to.be.calledOnce
  103. })
  104. })
  105. })
  106. it('should not create assembly when all individual files have been cancelled', () => {
  107. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  108. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  109. cy.window().then(({ uppy }) => {
  110. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).length).to.equal(0)
  111. const { files } = uppy.getState()
  112. uppy.removeFiles(Object.keys(files))
  113. cy.wait('@createAssemblies').then(() => {
  114. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
  115. })
  116. })
  117. })
  118. // Not working, the upstream changes have not landed yet.
  119. it.skip('should create assembly if there is still one file to upload', () => {
  120. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  121. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  122. cy.window().then(({ uppy }) => {
  123. expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).length).to.equal(0)
  124. const { files } = uppy.getState()
  125. const [fileID] = Object.keys(files)
  126. uppy.removeFile(fileID)
  127. cy.wait('@createAssemblies').then(() => {
  128. cy.wait('@resumable')
  129. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
  130. })
  131. })
  132. })
  133. // Not working, the upstream changes have not landed yet.
  134. it.skip('should complete upload if one gets cancelled mid-flight', () => {
  135. cy.get('@file-input').selectFile(['cypress/fixtures/images/cat.jpg', 'cypress/fixtures/images/traffic.jpg'], { force:true })
  136. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  137. cy.wait('@createAssemblies')
  138. cy.wait('@resumable')
  139. cy.window().then(({ uppy }) => {
  140. const { files } = uppy.getState()
  141. const [fileID] = Object.keys(files)
  142. uppy.removeFile(fileID)
  143. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
  144. })
  145. })
  146. it('should not emit error if upload is cancelled right away', () => {
  147. cy.get('@file-input').selectFile('cypress/fixtures/images/cat.jpg', { force:true })
  148. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  149. const handler = cy.spy()
  150. cy.window().then(({ uppy }) => {
  151. const { files } = uppy.getState()
  152. uppy.on('upload-error', handler)
  153. const [fileID] = Object.keys(files)
  154. uppy.removeFile(fileID)
  155. uppy.removeFile(fileID)
  156. cy.wait('@createAssemblies').then(() => expect(handler).not.to.be.called)
  157. })
  158. })
  159. it('should not re-use erroneous tus keys', () => {
  160. function createAssemblyStatus ({ message, assembly_id, bytes_expected, ...other }) {
  161. return {
  162. message,
  163. assembly_id,
  164. parent_id:null,
  165. account_id:'deadbeef',
  166. account_name:'foo',
  167. account_slug:'foo',
  168. template_id:null,
  169. template_name:null,
  170. instance:'test.transloadit.com',
  171. assembly_url:`http://api2.test.transloadit.com/assemblies/${assembly_id}`,
  172. assembly_ssl_url:`https://api2-test.transloadit.com/assemblies/${assembly_id}`,
  173. uppyserver_url:'https://api2-test.transloadit.com/companion/',
  174. companion_url:'https://api2-test.transloadit.com/companion/',
  175. websocket_url:'about:blank',
  176. tus_url:'https://api2-test.transloadit.com/resumable/files/',
  177. bytes_received:0,
  178. bytes_expected,
  179. upload_duration:0.162,
  180. client_agent:null,
  181. client_ip:null,
  182. client_referer:null,
  183. transloadit_client:'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',
  184. start_date: new Date().toISOString(),
  185. upload_meta_data_extracted:false,
  186. warnings:[],
  187. is_infinite:false,
  188. has_dupe_jobs:false,
  189. execution_start:null,
  190. execution_duration:null,
  191. queue_duration:0.009,
  192. jobs_queue_duration:0,
  193. notify_start:null,
  194. notify_url:null,
  195. notify_response_code:null,
  196. notify_response_data:null,
  197. notify_duration:null,
  198. last_job_completed:null,
  199. fields:{},
  200. running_jobs:[],
  201. bytes_usage:0,
  202. executing_jobs:[],
  203. started_jobs:[],
  204. parent_assembly_status:null,
  205. params:'{}',
  206. template:null,
  207. merged_params:'{}',
  208. expected_tus_uploads:1,
  209. started_tus_uploads:0,
  210. finished_tus_uploads:0,
  211. tus_uploads:[],
  212. uploads:[],
  213. results:{},
  214. build_id:'4765326956',
  215. status_endpoint:`https://api2-test.transloadit.com/assemblies/${assembly_id}`,
  216. ...other,
  217. }
  218. }
  219. cy.get('@file-input').selectFile(
  220. ['cypress/fixtures/images/cat.jpg'],
  221. { force:true },
  222. )
  223. // SETUP for FIRST ATTEMPT (error response from Transloadit backend)
  224. const assemblyIDAttempt1 = '500e56004f4347a288194edd7c7a0ae1'
  225. const tusIDAttempt1 = 'a9daed4af4981880faf29b0e9596a14d'
  226. cy.intercept('POST', 'https://api2.transloadit.com/assemblies', {
  227. statusCode: 200,
  228. body: JSON.stringify(createAssemblyStatus({
  229. ok:'ASSEMBLY_UPLOADING',
  230. message:'The Assembly is still in the process of being uploaded.',
  231. assembly_id:assemblyIDAttempt1,
  232. bytes_expected:263871,
  233. })),
  234. }).as('createAssembly')
  235. cy.intercept('POST', 'https://api2-test.transloadit.com/resumable/files/', {
  236. statusCode: 201,
  237. headers: {
  238. Location: `https://api2-test.transloadit.com/resumable/files/${tusIDAttempt1}`,
  239. },
  240. times: 1,
  241. }).as('tusCall')
  242. cy.intercept('PATCH', `https://api2-test.transloadit.com/resumable/files/${tusIDAttempt1}`, {
  243. statusCode: 204,
  244. headers: {
  245. 'Upload-Length': '263871',
  246. 'Upload-Offset': '263871',
  247. },
  248. times: 1,
  249. })
  250. cy.intercept('HEAD', `https://api2-test.transloadit.com/resumable/files/${tusIDAttempt1}`, { statusCode: 204 })
  251. cy.intercept('GET', `https://api2-test.transloadit.com/assemblies/${assemblyIDAttempt1}`, {
  252. statusCode: 200,
  253. body: JSON.stringify(createAssemblyStatus({
  254. error:'INVALID_FILE_META_DATA',
  255. http_code:400,
  256. message:'Whatever error message from Transloadit backend',
  257. reason:'Whatever reason',
  258. msg:'Whatever error from Transloadit backend',
  259. assembly_id:'500e56004f4347a288194edd7c7a0ae1',
  260. bytes_expected:263871,
  261. })),
  262. }).as('failureReported')
  263. cy.intercept('POST', 'https://transloaditstatus.com/client_error', {
  264. statusCode: 200,
  265. body: '{}',
  266. })
  267. // FIRST ATTEMPT
  268. cy.get('.uppy-StatusBar-actionBtn--upload').click()
  269. cy.wait(['@createAssembly', '@tusCall', '@failureReported'])
  270. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Upload failed')
  271. // SETUP for SECOND ATTEMPT
  272. const assemblyIDAttempt2 = '6a3fa40e527d4d73989fce678232a5e1'
  273. const tusIDAttempt2 = 'b8ebed4af4981880faf29b0e9596b25e'
  274. cy.intercept('POST', 'https://api2.transloadit.com/assemblies', {
  275. statusCode: 200,
  276. body: JSON.stringify(createAssemblyStatus({
  277. ok:'ASSEMBLY_UPLOADING',
  278. message:'The Assembly is still in the process of being uploaded.',
  279. assembly_id:assemblyIDAttempt2,
  280. tus_url:'https://api2-test.transloadit.com/resumable/files/attempt2',
  281. bytes_expected:263871,
  282. })),
  283. }).as('createAssembly-attempt2')
  284. cy.intercept('POST', 'https://api2-test.transloadit.com/resumable/files/attempt2', {
  285. statusCode: 201,
  286. headers: {
  287. 'Upload-Length': '263871',
  288. 'Upload-Offset': '0',
  289. Location: `https://api2-test.transloadit.com/resumable/files/${tusIDAttempt2}`,
  290. },
  291. times: 1,
  292. }).as('tusCall-attempt2')
  293. cy.intercept('PATCH', `https://api2-test.transloadit.com/resumable/files/${tusIDAttempt2}`, {
  294. statusCode: 204,
  295. headers: {
  296. 'Upload-Length': '263871',
  297. 'Upload-Offset': '263871',
  298. 'Tus-Resumable': '1.0.0',
  299. },
  300. times: 1,
  301. })
  302. cy.intercept('HEAD', `https://api2-test.transloadit.com/resumable/files/${tusIDAttempt2}`, { statusCode: 204 })
  303. cy.intercept('GET', `https://api2-test.transloadit.com/assemblies/${assemblyIDAttempt2}`, {
  304. statusCode: 200,
  305. body: JSON.stringify(createAssemblyStatus({
  306. ok:'ASSEMBLY_COMPLETED',
  307. http_code:200,
  308. message:'The Assembly was successfully completed.',
  309. assembly_id:assemblyIDAttempt2,
  310. bytes_received:263871,
  311. bytes_expected:263871,
  312. })),
  313. }).as('assemblyCompleted-attempt2')
  314. // SECOND ATTEMPT
  315. cy.get('.uppy-StatusBar-actions > .uppy-c-btn').click()
  316. cy.wait(['@createAssembly-attempt2', '@tusCall-attempt2', '@assemblyCompleted-attempt2'])
  317. cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
  318. })
  319. })