BrandVariationsHandlerTests.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* eslint-disable
  2. n/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  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 { expect } = require('chai')
  15. const SandboxedModule = require('sandboxed-module')
  16. const assert = require('assert')
  17. const path = require('path')
  18. const sinon = require('sinon')
  19. const modulePath = path.join(
  20. __dirname,
  21. '../../../../app/src/Features/BrandVariations/BrandVariationsHandler'
  22. )
  23. describe('BrandVariationsHandler', function () {
  24. beforeEach(function () {
  25. this.settings = {
  26. apis: {
  27. v1: {
  28. publicUrl: 'http://overleaf.example.com',
  29. },
  30. },
  31. modules: {
  32. sanitize: {
  33. options: {
  34. allowedTags: ['br', 'strong'],
  35. allowedAttributes: {
  36. strong: ['style'],
  37. },
  38. },
  39. },
  40. },
  41. }
  42. this.V1Api = { request: sinon.stub() }
  43. this.BrandVariationsHandler = SandboxedModule.require(modulePath, {
  44. requires: {
  45. '@overleaf/settings': this.settings,
  46. '../V1/V1Api': this.V1Api,
  47. },
  48. })
  49. return (this.mockedBrandVariationDetails = {
  50. id: '12',
  51. active: true,
  52. brand_name: 'The journal',
  53. logo_url: 'http://my.cdn.tld/journal-logo.png',
  54. journal_cover_url: 'http://my.cdn.tld/journal-cover.jpg',
  55. home_url: 'http://www.thejournal.com/',
  56. publish_menu_link_html: 'Submit your paper to the <em>The Journal</em>',
  57. })
  58. })
  59. describe('getBrandVariationById', function () {
  60. it('should call the callback with an error when the branding variation id is not provided', function (done) {
  61. return this.BrandVariationsHandler.getBrandVariationById(
  62. null,
  63. (err, brandVariationDetails) => {
  64. expect(err).to.be.instanceof(Error)
  65. return done()
  66. }
  67. )
  68. })
  69. it('should call the callback with an error when the request errors', function (done) {
  70. this.V1Api.request.callsArgWith(1, new Error())
  71. return this.BrandVariationsHandler.getBrandVariationById(
  72. '12',
  73. (err, brandVariationDetails) => {
  74. expect(err).to.be.instanceof(Error)
  75. return done()
  76. }
  77. )
  78. })
  79. it('should call the callback with branding details when request succeeds', function (done) {
  80. this.V1Api.request.callsArgWith(
  81. 1,
  82. null,
  83. { statusCode: 200 },
  84. this.mockedBrandVariationDetails
  85. )
  86. return this.BrandVariationsHandler.getBrandVariationById(
  87. '12',
  88. (err, brandVariationDetails) => {
  89. expect(err).to.not.exist
  90. expect(brandVariationDetails).to.deep.equal(
  91. this.mockedBrandVariationDetails
  92. )
  93. return done()
  94. }
  95. )
  96. })
  97. it('should transform relative URLs in v1 absolute ones', function (done) {
  98. this.mockedBrandVariationDetails.logo_url = '/journal-logo.png'
  99. this.V1Api.request.callsArgWith(
  100. 1,
  101. null,
  102. { statusCode: 200 },
  103. this.mockedBrandVariationDetails
  104. )
  105. return this.BrandVariationsHandler.getBrandVariationById(
  106. '12',
  107. (err, brandVariationDetails) => {
  108. expect(
  109. brandVariationDetails.logo_url.startsWith(
  110. this.settings.apis.v1.publicUrl
  111. )
  112. ).to.be.true
  113. return done()
  114. }
  115. )
  116. })
  117. it("should sanitize 'submit_button_html'", function (done) {
  118. this.mockedBrandVariationDetails.submit_button_html =
  119. '<br class="break"/><strong style="color:#B39500">AGU Journal</strong><iframe>hello</iframe>'
  120. this.V1Api.request.callsArgWith(
  121. 1,
  122. null,
  123. { statusCode: 200 },
  124. this.mockedBrandVariationDetails
  125. )
  126. return this.BrandVariationsHandler.getBrandVariationById(
  127. '12',
  128. (err, brandVariationDetails) => {
  129. expect(brandVariationDetails.submit_button_html).to.equal(
  130. '<br /><strong style="color:#B39500">AGU Journal</strong>hello'
  131. )
  132. return done()
  133. }
  134. )
  135. })
  136. })
  137. })