HttpControllerTests.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* eslint-disable
  2. mocha/no-pending-tests,
  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 sinon = require('sinon')
  14. const chai = require('chai')
  15. const should = chai.should()
  16. const { expect } = chai
  17. const modulePath = '../../../app/js/HttpController.js'
  18. const SandboxedModule = require('sandboxed-module')
  19. describe('HttpController', function () {
  20. beforeEach(function () {
  21. this.HttpController = SandboxedModule.require(modulePath, {
  22. requires: {
  23. './ContactManager': (this.ContactManager = {}),
  24. 'logger-sharelatex': (this.logger = { log: sinon.stub() })
  25. }
  26. })
  27. this.user_id = 'mock-user-id'
  28. this.contact_id = 'mock-contact-id'
  29. this.req = {}
  30. this.res = {}
  31. this.res.status = sinon.stub().returns(this.res)
  32. this.res.end = sinon.stub()
  33. this.res.send = sinon.stub()
  34. this.res.sendStatus = sinon.stub()
  35. return (this.next = sinon.stub())
  36. })
  37. describe('addContact', function () {
  38. beforeEach(function () {
  39. this.req.params = { user_id: this.user_id }
  40. return (this.ContactManager.touchContact = sinon.stub().callsArg(2))
  41. })
  42. describe('with a valid user_id and contact_id', function () {
  43. beforeEach(function () {
  44. this.req.body = { contact_id: this.contact_id }
  45. return this.HttpController.addContact(this.req, this.res, this.next)
  46. })
  47. it("should update the contact in the user's contact list", function () {
  48. return this.ContactManager.touchContact
  49. .calledWith(this.user_id, this.contact_id)
  50. .should.equal(true)
  51. })
  52. it("should update the user in the contact's contact list", function () {
  53. return this.ContactManager.touchContact
  54. .calledWith(this.contact_id, this.user_id)
  55. .should.equal(true)
  56. })
  57. return it('should send back a 204 status', function () {
  58. this.res.sendStatus.calledWith(204).should.equal(true)
  59. })
  60. })
  61. return describe('with an invalid contact id', function () {
  62. beforeEach(function () {
  63. this.req.body = { contact_id: '' }
  64. return this.HttpController.addContact(this.req, this.res, this.next)
  65. })
  66. return it('should return 400, Bad Request', function () {
  67. this.res.status.calledWith(400).should.equal(true)
  68. return this.res.send
  69. .calledWith('contact_id should be a non-blank string')
  70. .should.equal(true)
  71. })
  72. })
  73. })
  74. return describe('getContacts', function () {
  75. beforeEach(function () {
  76. this.req.params = { user_id: this.user_id }
  77. const now = Date.now()
  78. this.contacts = {
  79. 'user-id-1': { n: 2, ts: new Date(now) },
  80. 'user-id-2': { n: 4, ts: new Date(now) },
  81. 'user-id-3': { n: 2, ts: new Date(now - 1000) }
  82. }
  83. return (this.ContactManager.getContacts = sinon
  84. .stub()
  85. .callsArgWith(1, null, this.contacts))
  86. })
  87. describe('normally', function () {
  88. beforeEach(function () {
  89. return this.HttpController.getContacts(this.req, this.res, this.next)
  90. })
  91. it('should look up the contacts in mongo', function () {
  92. return this.ContactManager.getContacts
  93. .calledWith(this.user_id)
  94. .should.equal(true)
  95. })
  96. return it('should return a sorted list of contacts by count and timestamp', function () {
  97. return this.res.send
  98. .calledWith({
  99. contact_ids: ['user-id-2', 'user-id-1', 'user-id-3']
  100. })
  101. .should.equal(true)
  102. })
  103. })
  104. describe('with more contacts than the limit', function () {
  105. beforeEach(function () {
  106. this.req.query = { limit: 2 }
  107. return this.HttpController.getContacts(this.req, this.res, this.next)
  108. })
  109. return it('should return the most commonly used contacts up to the limit', function () {
  110. return this.res.send
  111. .calledWith({
  112. contact_ids: ['user-id-2', 'user-id-1']
  113. })
  114. .should.equal(true)
  115. })
  116. })
  117. describe('without a contact list', function () {
  118. beforeEach(function () {
  119. this.ContactManager.getContacts = sinon
  120. .stub()
  121. .callsArgWith(1, null, null)
  122. return this.HttpController.getContacts(this.req, this.res, this.next)
  123. })
  124. return it('should return an empty list', function () {
  125. return this.res.send
  126. .calledWith({
  127. contact_ids: []
  128. })
  129. .should.equal(true)
  130. })
  131. })
  132. })
  133. })