HttpControllerTests.coffee 3.2 KB

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