BrandVariationsHandler.test.mjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath =
  4. '../../../../app/src/Features/BrandVariations/BrandVariationsHandler.mjs'
  5. describe('BrandVariationsHandler', function () {
  6. beforeEach(async function (ctx) {
  7. ctx.settings = {
  8. apis: {
  9. v1: {
  10. publicUrl: 'http://overleaf.example.com',
  11. },
  12. },
  13. modules: {
  14. sanitize: {
  15. options: {
  16. allowedTags: ['br', 'strong'],
  17. allowedAttributes: {
  18. strong: ['style'],
  19. },
  20. },
  21. },
  22. },
  23. }
  24. ctx.V1Api = { request: sinon.stub() }
  25. vi.doMock('@overleaf/settings', () => ({
  26. default: ctx.settings,
  27. }))
  28. vi.doMock('../../../../app/src/Features/V1/V1Api', () => ({
  29. default: ctx.V1Api,
  30. }))
  31. ctx.BrandVariationsHandler = (await import(modulePath)).default
  32. ctx.mockedBrandVariationDetails = {
  33. id: '12',
  34. active: true,
  35. brand_name: 'The journal',
  36. logo_url: 'http://my.cdn.tld/journal-logo.png',
  37. journal_cover_url: 'http://my.cdn.tld/journal-cover.jpg',
  38. home_url: 'http://www.thejournal.com/',
  39. publish_menu_link_html: 'Submit your paper to the <em>The Journal</em>',
  40. }
  41. })
  42. describe('getBrandVariationById', function () {
  43. it('should reject with an error when the branding variation id is not provided', async function (ctx) {
  44. await expect(
  45. ctx.BrandVariationsHandler.promises.getBrandVariationById(null)
  46. ).to.be.rejected
  47. })
  48. it('should reject with an error when the request errors', async function (ctx) {
  49. ctx.V1Api.request.callsArgWith(1, new Error())
  50. await expect(
  51. ctx.BrandVariationsHandler.promises.getBrandVariationById('12')
  52. ).to.be.rejected
  53. })
  54. it('should return branding details when request succeeds', async function (ctx) {
  55. ctx.V1Api.request.callsArgWith(
  56. 1,
  57. null,
  58. { statusCode: 200 },
  59. ctx.mockedBrandVariationDetails
  60. )
  61. const brandVariationDetails =
  62. await ctx.BrandVariationsHandler.promises.getBrandVariationById('12')
  63. expect(brandVariationDetails).to.deep.equal(
  64. ctx.mockedBrandVariationDetails
  65. )
  66. })
  67. it('should transform relative URLs in v1 absolute ones', async function (ctx) {
  68. ctx.mockedBrandVariationDetails.logo_url = '/journal-logo.png'
  69. ctx.V1Api.request.callsArgWith(
  70. 1,
  71. null,
  72. { statusCode: 200 },
  73. ctx.mockedBrandVariationDetails
  74. )
  75. const brandVariationDetails =
  76. await ctx.BrandVariationsHandler.promises.getBrandVariationById('12')
  77. expect(
  78. brandVariationDetails.logo_url.startsWith(
  79. ctx.settings.apis.v1.publicUrl
  80. )
  81. ).to.be.true
  82. })
  83. it("should sanitize 'submit_button_html'", async function (ctx) {
  84. ctx.mockedBrandVariationDetails.submit_button_html =
  85. '<br class="break"/><strong style="color:#B39500">AGU Journal</strong><iframe>hello</iframe>'
  86. ctx.V1Api.request.callsArgWith(
  87. 1,
  88. null,
  89. { statusCode: 200 },
  90. ctx.mockedBrandVariationDetails
  91. )
  92. const brandVariationDetails =
  93. await ctx.BrandVariationsHandler.promises.getBrandVariationById('12')
  94. expect(brandVariationDetails.submit_button_html).to.equal(
  95. '<br /><strong style="color:#B39500">AGU Journal</strong>hello'
  96. )
  97. })
  98. it("should sanitize and remove breaks in 'submit_button_html_no_br'", async function (ctx) {
  99. ctx.mockedBrandVariationDetails.submit_button_html =
  100. 'Submit to<br class="break"/><strong style="color:#B39500">AGU Journal</strong><iframe>hello</iframe>'
  101. ctx.V1Api.request.callsArgWith(
  102. 1,
  103. null,
  104. { statusCode: 200 },
  105. ctx.mockedBrandVariationDetails
  106. )
  107. const brandVariationDetails =
  108. await ctx.BrandVariationsHandler.promises.getBrandVariationById('12')
  109. expect(brandVariationDetails.submit_button_html_no_br).to.equal(
  110. 'Submit to <strong style="color:#B39500">AGU Journal</strong>hello'
  111. )
  112. })
  113. })
  114. })