HttpControllerTests.js 4.6 KB

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