HttpControllerTests.coffee 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "./WebApiManager": @WebApiManager = {}
  12. "logger-sharelatex": @logger = { log: sinon.stub() }
  13. @user_id = "mock-user-id"
  14. @contact_id = "mock-contact-id"
  15. @req = {}
  16. @res = {}
  17. @res.status = sinon.stub().returns @res
  18. @res.end = sinon.stub()
  19. @res.send = sinon.stub()
  20. @next = sinon.stub()
  21. describe "addContact", ->
  22. beforeEach ->
  23. @req.params =
  24. user_id: @user_id
  25. @ContactManager.touchContact = sinon.stub().callsArg(2)
  26. describe "with a valid user_id and contact_id", ->
  27. beforeEach ->
  28. @req.body =
  29. contact_id: @contact_id
  30. @HttpController.addContact @req, @res, @next
  31. it "should update the contact in the user's contact list", ->
  32. @ContactManager.touchContact
  33. .calledWith(@user_id, @contact_id)
  34. .should.equal true
  35. it "should update the user in the contact's contact list", ->
  36. @ContactManager.touchContact
  37. .calledWith(@contact_id, @user_id)
  38. .should.equal true
  39. it "should send back a 204 status", ->
  40. @res.status.calledWith(204).should.equal true
  41. @res.end.called.should.equal true
  42. describe "with an invalid contact id", ->
  43. beforeEach ->
  44. @req.body =
  45. contact_id: ""
  46. @HttpController.addContact @req, @res, @next
  47. it "should return 400, Bad Request", ->
  48. @res.status.calledWith(400).should.equal true
  49. @res.send.calledWith("contact_id should be a non-blank string").should.equal true
  50. describe "getContacts", ->
  51. beforeEach ->
  52. @req.params =
  53. user_id: @user_id
  54. now = Date.now()
  55. @contacts = {
  56. "user-id-1": { n: 2, ts: new Date(now) }
  57. "user-id-2": { n: 4, ts: new Date(now) }
  58. "user-id-3": { n: 2, ts: new Date(now - 1000) }
  59. }
  60. @user_details = {
  61. "user-id-1": { _id: "user-id-1", email: "joe@example.com", first_name: "Joe", last_name: "Example", extra: "foo" }
  62. "user-id-2": { _id: "user-id-2", email: "jane@example.com", first_name: "Sarah", last_name: "Example", extra: "foo" }
  63. "user-id-3": { _id: "user-id-3", email: "sam@example.com", first_name: "Sam", last_name: "Example", extra: "foo" }
  64. }
  65. @ContactManager.getContacts = sinon.stub().callsArgWith(1, null, @contacts)
  66. @WebApiManager.getUserDetails = (user_id, callback = (error, user) ->) =>
  67. callback null, @user_details[user_id]
  68. sinon.spy @WebApiManager, "getUserDetails"
  69. describe "normally", ->
  70. beforeEach ->
  71. @HttpController.getContacts @req, @res, @next
  72. it "should look up the contacts in mongo", ->
  73. @ContactManager.getContacts
  74. .calledWith(@user_id)
  75. .should.equal true
  76. it "should look up each contact in web for their details", ->
  77. for user_id, data of @contacts
  78. @WebApiManager.getUserDetails
  79. .calledWith(user_id)
  80. .should.equal true
  81. it "should return a sorted list of contacts by count and timestamp", ->
  82. @res.send
  83. .calledWith({
  84. contacts: [
  85. { id: "user-id-2", email: "jane@example.com", first_name: "Sarah", last_name: "Example" }
  86. { id: "user-id-1", email: "joe@example.com", first_name: "Joe", last_name: "Example" }
  87. { id: "user-id-3", email: "sam@example.com", first_name: "Sam", last_name: "Example" }
  88. ]
  89. })
  90. .should.equal true
  91. describe "with more contacts than the limit", ->
  92. beforeEach ->
  93. @req.query =
  94. limit: 2
  95. @HttpController.getContacts @req, @res, @next
  96. it "should return the most commonly used contacts up to the limit", ->
  97. @res.send
  98. .calledWith({
  99. contacts: [
  100. { id: "user-id-2", email: "jane@example.com", first_name: "Sarah", last_name: "Example" }
  101. { id: "user-id-1", email: "joe@example.com", first_name: "Joe", last_name: "Example" }
  102. ]
  103. })
  104. .should.equal true
  105. describe "without a contact list", ->
  106. beforeEach ->
  107. @ContactManager.getContacts = sinon.stub().callsArgWith(1, null, null)
  108. @HttpController.getContacts @req, @res, @next
  109. it "should return an empty list", ->
  110. @res.send
  111. .calledWith({
  112. contacts: []
  113. })
  114. .should.equal true
  115. describe "with a holding account", ->
  116. it "should not return holding accounts"