BlogControllerTests.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const should = require('chai').should()
  14. const SandboxedModule = require('sandboxed-module')
  15. const assert = require('assert')
  16. const path = require('path')
  17. const sinon = require('sinon')
  18. const modulePath = path.join(
  19. __dirname,
  20. '../../../../app/src/Features/Blog/BlogController'
  21. )
  22. const { expect } = require('chai')
  23. describe('BlogController', function() {
  24. beforeEach(function() {
  25. this.settings = {
  26. apis: {
  27. blog: {
  28. url: 'http://blog.sharelatex.env'
  29. }
  30. },
  31. cdn: { web: { host: null } }
  32. }
  33. this.request = { get: sinon.stub() }
  34. this.ErrorController = {}
  35. this.BlogController = SandboxedModule.require(modulePath, {
  36. globals: {
  37. console: console
  38. },
  39. requires: {
  40. 'settings-sharelatex': this.settings,
  41. 'logger-sharelatex': {
  42. log() {}
  43. },
  44. '../Errors/ErrorController': this.ErrorController,
  45. request: this.request
  46. }
  47. })
  48. this.req = {}
  49. return (this.res = {})
  50. })
  51. describe('getPage', function() {
  52. it('should get the data from the blog api', function(done) {
  53. this.req.url = '/blog/something.html'
  54. const body = { stuff: 'here' }
  55. this.request.get.callsArgWith(1, null, null, JSON.stringify(body))
  56. this.res.render = (view, data) => {
  57. this.request.get.calledWith(
  58. `${this.settings.apis.blog.url}${this.req.url}`
  59. )
  60. view.should.equal('blog/blog_holder')
  61. assert.deepEqual(body, data)
  62. return done()
  63. }
  64. return this.BlogController.getPage(this.req, this.res)
  65. })
  66. it('should send to the error controller if the blog responds 404', function(done) {
  67. this.req.url = '/blog/something.html'
  68. this.request.get.callsArgWith(1, null, { statusCode: 404 })
  69. this.ErrorController.notFound = (req, res) => {
  70. assert.deepEqual(req, this.req)
  71. assert.deepEqual(res, this.res)
  72. return done()
  73. }
  74. return this.BlogController.getPage(this.req, this.res)
  75. })
  76. it('should proxy the image urls', function(done) {
  77. this.BlogController._directProxy = sinon.stub()
  78. this.req.url = '/something.png'
  79. this.BlogController.getPage(this.req, this.res)
  80. this.BlogController._directProxy
  81. .calledWith(`${this.settings.apis.blog.url}${this.req.url}`, this.res)
  82. .should.equal(true)
  83. return done()
  84. })
  85. })
  86. describe('getIndexPage', function() {
  87. it('should change the url and send it to getPage', function(done) {
  88. this.req.url = '/blog'
  89. this.BlogController.getPage = function(req, res) {
  90. req.url.should.equal('/blog/index.html')
  91. return done()
  92. }
  93. return this.BlogController.getIndexPage(this.req, this.res)
  94. })
  95. })
  96. })