BrandVariationsHandlerTests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* eslint-disable
  2. 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. let { 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. ;({ expect } = require('chai'))
  20. const modulePath = path.join(
  21. __dirname,
  22. '../../../../app/src/Features/BrandVariations/BrandVariationsHandler'
  23. )
  24. describe('BrandVariationsHandler', function() {
  25. beforeEach(function() {
  26. this.settings = {
  27. apis: {
  28. v1: {
  29. url: 'http://overleaf.example.com'
  30. }
  31. }
  32. }
  33. this.logger = {
  34. warn() {},
  35. err() {},
  36. log() {}
  37. }
  38. this.V1Api = { request: sinon.stub() }
  39. this.BrandVariationsHandler = SandboxedModule.require(modulePath, {
  40. globals: {
  41. console: console
  42. },
  43. requires: {
  44. 'settings-sharelatex': this.settings,
  45. 'logger-sharelatex': this.logger,
  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(this.settings.apis.v1.url)
  110. ).to.be.true
  111. return done()
  112. }
  113. )
  114. })
  115. })
  116. })