ExportsControllerTests.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. no-useless-escape,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const SandboxedModule = require('sandboxed-module')
  15. const assert = require('assert')
  16. const { expect } = require('chai')
  17. const sinon = require('sinon')
  18. const modulePath = require('path').join(
  19. __dirname,
  20. '../../../../app/src/Features/Exports/ExportsController.js'
  21. )
  22. describe('ExportsController', function () {
  23. const projectId = '123njdskj9jlk'
  24. const userId = '123nd3ijdks'
  25. const brandVariationId = 22
  26. const firstName = 'first'
  27. const lastName = 'last'
  28. const title = 'title'
  29. const description = 'description'
  30. const author = 'author'
  31. const license = 'other'
  32. const showSource = true
  33. beforeEach(function () {
  34. this.handler = { getUserNotifications: sinon.stub().callsArgWith(1) }
  35. this.req = {
  36. params: {
  37. project_id: projectId,
  38. brand_variation_id: brandVariationId,
  39. },
  40. body: {
  41. firstName,
  42. lastName,
  43. },
  44. session: {
  45. user: {
  46. _id: userId,
  47. },
  48. },
  49. i18n: {
  50. translate() {},
  51. },
  52. }
  53. this.res = {
  54. json: sinon.stub(),
  55. status: sinon.stub(),
  56. }
  57. this.res.status.returns(this.res)
  58. this.next = sinon.stub()
  59. this.AuthenticationController = {
  60. getLoggedInUserId: sinon.stub().returns(this.req.session.user._id),
  61. }
  62. return (this.controller = SandboxedModule.require(modulePath, {
  63. requires: {
  64. './ExportsHandler': this.handler,
  65. '../Authentication/AuthenticationController':
  66. this.AuthenticationController,
  67. },
  68. }))
  69. })
  70. describe('without gallery fields', function () {
  71. it('should ask the handler to perform the export', function (done) {
  72. this.handler.exportProject = sinon
  73. .stub()
  74. .yields(null, { iAmAnExport: true, v1_id: 897 })
  75. const expected = {
  76. project_id: projectId,
  77. user_id: userId,
  78. brand_variation_id: brandVariationId,
  79. first_name: firstName,
  80. last_name: lastName,
  81. }
  82. return this.controller.exportProject(this.req, {
  83. json: body => {
  84. expect(this.handler.exportProject.args[0][0]).to.deep.equal(expected)
  85. expect(body).to.deep.equal({ export_v1_id: 897, message: undefined })
  86. return done()
  87. },
  88. })
  89. })
  90. })
  91. describe('with a message from v1', function () {
  92. it('should ask the handler to perform the export', function (done) {
  93. this.handler.exportProject = sinon.stub().yields(null, {
  94. iAmAnExport: true,
  95. v1_id: 897,
  96. message: 'RESUBMISSION',
  97. })
  98. const expected = {
  99. project_id: projectId,
  100. user_id: userId,
  101. brand_variation_id: brandVariationId,
  102. first_name: firstName,
  103. last_name: lastName,
  104. }
  105. return this.controller.exportProject(this.req, {
  106. json: body => {
  107. expect(this.handler.exportProject.args[0][0]).to.deep.equal(expected)
  108. expect(body).to.deep.equal({
  109. export_v1_id: 897,
  110. message: 'RESUBMISSION',
  111. })
  112. return done()
  113. },
  114. })
  115. })
  116. })
  117. describe('with gallery fields', function () {
  118. beforeEach(function () {
  119. this.req.body.title = title
  120. this.req.body.description = description
  121. this.req.body.author = author
  122. this.req.body.license = license
  123. return (this.req.body.showSource = true)
  124. })
  125. it('should ask the handler to perform the export', function (done) {
  126. this.handler.exportProject = sinon
  127. .stub()
  128. .yields(null, { iAmAnExport: true, v1_id: 897 })
  129. const expected = {
  130. project_id: projectId,
  131. user_id: userId,
  132. brand_variation_id: brandVariationId,
  133. first_name: firstName,
  134. last_name: lastName,
  135. title,
  136. description,
  137. author,
  138. license,
  139. show_source: showSource,
  140. }
  141. return this.controller.exportProject(this.req, {
  142. json: body => {
  143. expect(this.handler.exportProject.args[0][0]).to.deep.equal(expected)
  144. expect(body).to.deep.equal({ export_v1_id: 897, message: undefined })
  145. return done()
  146. },
  147. })
  148. })
  149. })
  150. describe('with an error return from v1 to forward to the publish modal', function () {
  151. it('should forward the response onward', function (done) {
  152. this.error_json = { status: 422, message: 'nope' }
  153. this.handler.exportProject = sinon
  154. .stub()
  155. .yields({ forwardResponse: this.error_json })
  156. this.controller.exportProject(this.req, this.res, this.next)
  157. expect(this.res.json.args[0][0]).to.deep.equal(this.error_json)
  158. expect(this.res.status.args[0][0]).to.equal(this.error_json.status)
  159. return done()
  160. })
  161. })
  162. it('should ask the handler to return the status of an export', function (done) {
  163. this.handler.fetchExport = sinon.stub().yields(
  164. null,
  165. `{ \
  166. \"id\":897, \
  167. \"status_summary\":\"completed\", \
  168. \"status_detail\":\"all done\", \
  169. \"partner_submission_id\":\"abc123\", \
  170. \"v2_user_email\":\"la@tex.com\", \
  171. \"v2_user_first_name\":\"Arthur\", \
  172. \"v2_user_last_name\":\"Author\", \
  173. \"title\":\"my project\", \
  174. \"token\":\"token\" \
  175. }`
  176. )
  177. this.req.params = { project_id: projectId, export_id: 897 }
  178. return this.controller.exportStatus(this.req, {
  179. json: body => {
  180. expect(body).to.deep.equal({
  181. export_json: {
  182. status_summary: 'completed',
  183. status_detail: 'all done',
  184. partner_submission_id: 'abc123',
  185. v2_user_email: 'la@tex.com',
  186. v2_user_first_name: 'Arthur',
  187. v2_user_last_name: 'Author',
  188. title: 'my project',
  189. token: 'token',
  190. },
  191. })
  192. return done()
  193. },
  194. })
  195. })
  196. })