BrandVariationsHandlerTests.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 reject with an error when the branding variation id is not provided', async function () {
  61. await expect(
  62. this.BrandVariationsHandler.promises.getBrandVariationById(null)
  63. ).to.be.rejected
  64. })
  65. it('should reject with an error when the request errors', async function () {
  66. this.V1Api.request.callsArgWith(1, new Error())
  67. await expect(
  68. this.BrandVariationsHandler.promises.getBrandVariationById('12')
  69. ).to.be.rejected
  70. })
  71. it('should return branding details when request succeeds', async function () {
  72. this.V1Api.request.callsArgWith(
  73. 1,
  74. null,
  75. { statusCode: 200 },
  76. this.mockedBrandVariationDetails
  77. )
  78. const brandVariationDetails =
  79. await this.BrandVariationsHandler.promises.getBrandVariationById('12')
  80. expect(brandVariationDetails).to.deep.equal(
  81. this.mockedBrandVariationDetails
  82. )
  83. })
  84. it('should transform relative URLs in v1 absolute ones', async function () {
  85. this.mockedBrandVariationDetails.logo_url = '/journal-logo.png'
  86. this.V1Api.request.callsArgWith(
  87. 1,
  88. null,
  89. { statusCode: 200 },
  90. this.mockedBrandVariationDetails
  91. )
  92. const brandVariationDetails =
  93. await this.BrandVariationsHandler.promises.getBrandVariationById('12')
  94. expect(
  95. brandVariationDetails.logo_url.startsWith(
  96. this.settings.apis.v1.publicUrl
  97. )
  98. ).to.be.true
  99. })
  100. it("should sanitize 'submit_button_html'", async function () {
  101. this.mockedBrandVariationDetails.submit_button_html =
  102. '<br class="break"/><strong style="color:#B39500">AGU Journal</strong><iframe>hello</iframe>'
  103. this.V1Api.request.callsArgWith(
  104. 1,
  105. null,
  106. { statusCode: 200 },
  107. this.mockedBrandVariationDetails
  108. )
  109. const brandVariationDetails =
  110. await this.BrandVariationsHandler.promises.getBrandVariationById('12')
  111. expect(brandVariationDetails.submit_button_html).to.equal(
  112. '<br /><strong style="color:#B39500">AGU Journal</strong>hello'
  113. )
  114. })
  115. it("should sanitize and remove breaks in 'submit_button_html_no_br'", async function () {
  116. this.mockedBrandVariationDetails.submit_button_html =
  117. 'Submit to<br class="break"/><strong style="color:#B39500">AGU Journal</strong><iframe>hello</iframe>'
  118. this.V1Api.request.callsArgWith(
  119. 1,
  120. null,
  121. { statusCode: 200 },
  122. this.mockedBrandVariationDetails
  123. )
  124. const brandVariationDetails =
  125. await this.BrandVariationsHandler.promises.getBrandVariationById('12')
  126. expect(brandVariationDetails.submit_button_html_no_br).to.equal(
  127. 'Submit to <strong style="color:#B39500">AGU Journal</strong>hello'
  128. )
  129. })
  130. })
  131. })