Transloadit.spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import test from 'tape'
  2. import Core from '../../src/core/Core'
  3. import Transloadit from '../../src/plugins/Transloadit'
  4. test('Transloadit: Throws errors if options are missing', (t) => {
  5. const uppy = new Core()
  6. t.throws(() => {
  7. uppy.use(Transloadit, { params: {} })
  8. }, /The `params\.auth\.key` option is required/)
  9. t.end()
  10. })
  11. test('Transloadit: Accepts a JSON string as `params` for signature authentication', (t) => {
  12. const uppy = new Core()
  13. t.throws(() => {
  14. uppy.use(Transloadit, {
  15. params: 'not json'
  16. })
  17. }, /The `params` option is a malformed JSON string/)
  18. t.throws(() => {
  19. uppy.use(Transloadit, {
  20. params: '{"template_id":"some template id string"}'
  21. })
  22. }, /The `params\.auth\.key` option is required/)
  23. t.doesNotThrow(() => {
  24. uppy.use(Transloadit, {
  25. params: '{"auth":{"key":"some auth key string"},"template_id":"some template id string"}'
  26. })
  27. }, /The `params\.auth\.key` option is required/)
  28. t.end()
  29. })
  30. test('Transloadit: Validates response from getAssemblyOptions()', (t) => {
  31. const uppy = new Core({ autoProceed: false })
  32. uppy.use(Transloadit, {
  33. getAssemblyOptions: (file) => {
  34. t.equal(file.name, 'testfile')
  35. return {
  36. params: '{"some":"json"}'
  37. }
  38. }
  39. })
  40. const data = Buffer.alloc(4000)
  41. data.size = data.byteLength
  42. uppy.addFile({
  43. name: 'testfile',
  44. data
  45. }).then(() => {
  46. uppy.upload().then(t.fail, (err) => {
  47. t.ok(/The `params\.auth\.key` option is required/.test(err.message), 'should reject invalid dynamic params')
  48. t.end()
  49. })
  50. }, t.fail)
  51. })
  52. test('Transloadit: Uses different assemblies for different params', (t) => {
  53. t.plan(5)
  54. const uppy = new Core({ autoProceed: false })
  55. uppy.use(Transloadit, {
  56. getAssemblyOptions: (file) => ({
  57. params: {
  58. auth: { key: 'fake key' },
  59. steps: {
  60. fake_step: { data: file.name }
  61. }
  62. }
  63. })
  64. })
  65. const tl = uppy.getPlugin('Transloadit')
  66. const files = ['a.png', 'b.png', 'c.png', 'd.png']
  67. let i = 0
  68. tl.client.createAssembly = (opts) => {
  69. t.equal(opts.params.steps.fake_step.data, files[i], `assembly for file ${files[i]}`)
  70. i++
  71. // Short-circuit upload
  72. return Promise.reject('short-circuit')
  73. }
  74. const data = Buffer.alloc(10)
  75. data.size = data.byteLength
  76. Promise.all([
  77. uppy.addFile({ name: 'a.png', data }),
  78. uppy.addFile({ name: 'b.png', data }),
  79. uppy.addFile({ name: 'c.png', data }),
  80. uppy.addFile({ name: 'd.png', data })
  81. ]).then(() => {
  82. uppy.upload().then(t.fail, () => {
  83. t.equal(i, 4, 'created 4 assemblies')
  84. t.end()
  85. })
  86. }, t.fail)
  87. })
  88. test('Transloadit: Should merge files with same parameters into one assembly', (t) => {
  89. t.plan(3)
  90. const uppy = new Core({ autoProceed: false })
  91. uppy.use(Transloadit, {
  92. getAssemblyOptions: (file) => ({
  93. params: {
  94. auth: { key: 'fake key' },
  95. steps: {
  96. fake_step: { data: file.size }
  97. }
  98. }
  99. })
  100. })
  101. const tl = uppy.getPlugin('Transloadit')
  102. const assemblies = [
  103. { data: 10, files: ['a.png', 'b.png', 'c.png'] },
  104. { data: 20, files: ['d.png'] }
  105. ]
  106. let i = 0
  107. tl.client.createAssembly = (opts) => {
  108. const assembly = assemblies[i]
  109. t.equal(opts.params.steps.fake_step.data, assembly.data, `assembly for files ${assembly.files.join(',')}`)
  110. i++
  111. // Short-circuit upload
  112. return Promise.reject('short-circuit')
  113. }
  114. const data = Buffer.alloc(10)
  115. data.size = data.byteLength
  116. const data2 = Buffer.alloc(20)
  117. data2.size = data2.byteLength
  118. Promise.all([
  119. uppy.addFile({ name: 'a.png', data }),
  120. uppy.addFile({ name: 'b.png', data }),
  121. uppy.addFile({ name: 'c.png', data }),
  122. uppy.addFile({ name: 'd.png', data: data2 })
  123. ]).then(() => {
  124. uppy.upload().then(t.fail, () => {
  125. t.equal(i, 2, 'created two assemblies')
  126. t.end()
  127. })
  128. }, t.fail)
  129. })