HttpControllerTests.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import sinon from 'sinon'
  2. import { expect } from 'chai'
  3. import esmock from 'esmock'
  4. describe('HttpController', function () {
  5. beforeEach(async function () {
  6. const now = Date.now()
  7. this.contacts = {
  8. 'user-id-1': { n: 2, ts: new Date(now) },
  9. 'user-id-2': { n: 4, ts: new Date(now) },
  10. 'user-id-3': { n: 2, ts: new Date(now - 1000) },
  11. }
  12. this.ContactManager = {
  13. touchContact: sinon.stub().resolves(),
  14. getContacts: sinon.stub().resolves(this.contacts),
  15. }
  16. this.HttpController = await esmock('../../../app/js/HttpController', {
  17. '../../../app/js/ContactManager': this.ContactManager,
  18. })
  19. this.user_id = 'mock-user-id'
  20. this.contact_id = 'mock-contact-id'
  21. this.req = {}
  22. this.res = {}
  23. this.res.status = sinon.stub().returns(this.res)
  24. this.res.end = sinon.stub()
  25. this.res.json = sinon.stub()
  26. this.res.send = sinon.stub()
  27. this.res.sendStatus = sinon.stub()
  28. this.next = sinon.stub()
  29. })
  30. describe('addContact', function () {
  31. describe('with a valid user_id and contact_id', function () {
  32. beforeEach(async function () {
  33. this.req.params = { user_id: this.user_id }
  34. this.req.body = { contact_id: this.contact_id }
  35. await this.HttpController.addContact(this.req, this.res, this.next)
  36. })
  37. it("should update the contact in the user's contact list", function () {
  38. expect(this.ContactManager.touchContact).to.be.calledWith(
  39. this.user_id,
  40. this.contact_id
  41. )
  42. })
  43. it("should update the user in the contact's contact list", function () {
  44. expect(this.ContactManager.touchContact).to.be.calledWith(
  45. this.contact_id,
  46. this.user_id
  47. )
  48. })
  49. it('should send back a 204 status', function () {
  50. expect(this.res.sendStatus).to.be.calledWith(204)
  51. })
  52. })
  53. describe('with an invalid contact id', function () {
  54. beforeEach(async function () {
  55. this.req.params = { user_id: this.user_id }
  56. this.req.body = { contact_id: '' }
  57. await this.HttpController.addContact(this.req, this.res, this.next)
  58. })
  59. it('should return 400, Bad Request', function () {
  60. expect(this.res.status).to.be.calledWith(400)
  61. expect(this.res.send).to.be.calledWith(
  62. 'contact_id should be a non-blank string'
  63. )
  64. })
  65. })
  66. })
  67. describe('getContacts', function () {
  68. describe('normally', function () {
  69. beforeEach(async function () {
  70. this.req.params = { user_id: this.user_id }
  71. this.req.query = {}
  72. await this.HttpController.getContacts(this.req, this.res, this.next)
  73. })
  74. it('should look up the contacts in mongo', function () {
  75. expect(this.ContactManager.getContacts).to.be.calledWith(this.user_id)
  76. })
  77. it('should return a sorted list of contacts by count and timestamp', function () {
  78. expect(this.res.json).to.be.calledWith({
  79. contact_ids: ['user-id-2', 'user-id-1', 'user-id-3'],
  80. })
  81. })
  82. })
  83. describe('with more contacts than the limit', function () {
  84. beforeEach(async function () {
  85. this.req.params = { user_id: this.user_id }
  86. this.req.query = { limit: 2 }
  87. await this.HttpController.getContacts(this.req, this.res, this.next)
  88. })
  89. it('should return the most commonly used contacts up to the limit', function () {
  90. expect(this.res.json).to.be.calledWith({
  91. contact_ids: ['user-id-2', 'user-id-1'],
  92. })
  93. })
  94. })
  95. describe('without a contact list', function () {
  96. beforeEach(async function () {
  97. this.ContactManager.getContacts.resolves(null)
  98. this.req.params = {}
  99. this.req.query = {}
  100. await this.HttpController.getContacts(this.req, this.res, this.next)
  101. })
  102. it('should return an empty list', function () {
  103. expect(this.res.json).to.be.calledWith({ contact_ids: [] })
  104. })
  105. })
  106. })
  107. })