ContactManagerTests.coffee 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. chai = require('chai')
  2. chai.should()
  3. sinon = require("sinon")
  4. modulePath = "../../../../app/js/Features/Contacts/ContactManager"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "ContactManager", ->
  7. beforeEach ->
  8. @ContactManager = SandboxedModule.require modulePath, requires:
  9. "request" : @request = sinon.stub()
  10. "settings-sharelatex": @settings =
  11. apis:
  12. contacts:
  13. url: "contacts.sharelatex.com"
  14. "logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub(), err:->}
  15. @user_id = "user-id-123"
  16. @contact_id = "contact-id-123"
  17. @callback = sinon.stub()
  18. describe "getContacts", ->
  19. describe "with a successful response code", ->
  20. beforeEach ->
  21. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, { contact_ids: @contact_ids = ["mock", "contact_ids"]})
  22. @ContactManager.getContactIds @user_id, { limit: 42 }, @callback
  23. it "should get the contacts from the contacts api", ->
  24. @request.get
  25. .calledWith({
  26. url: "#{@settings.apis.contacts.url}/user/#{@user_id}/contacts"
  27. qs: { limit: 42 }
  28. json: true
  29. jar: false
  30. })
  31. .should.equal true
  32. it "should call the callback with the contatcs", ->
  33. @callback.calledWith(null, @contact_ids).should.equal true
  34. describe "with a failed response code", ->
  35. beforeEach ->
  36. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 500, null)
  37. @ContactManager.getContactIds @user_id, { limit: 42 }, @callback
  38. it "should call the callback with an error", ->
  39. @callback.calledWith(new Error("contacts api responded with non-success code: 500")).should.equal true
  40. it "should log the error", ->
  41. @logger.error
  42. .calledWith({
  43. err: new Error("contacts api responded with a non-success code: 500")
  44. user_id: @user_id
  45. }, "error getting contacts for user")
  46. .should.equal true
  47. describe "addContact", ->
  48. describe "with a successful response code", ->
  49. beforeEach ->
  50. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 200, null)
  51. @ContactManager.addContact @user_id, @contact_id, @callback
  52. it "should add the contacts for the user in the contacts api", ->
  53. @request.post
  54. .calledWith({
  55. url: "#{@settings.apis.contacts.url}/user/#{@user_id}/contacts"
  56. json: {
  57. contact_id: @contact_id
  58. }
  59. jar: false
  60. })
  61. .should.equal true
  62. it "should call the callback", ->
  63. @callback.called.should.equal true
  64. describe "with a failed response code", ->
  65. beforeEach ->
  66. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500, null)
  67. @ContactManager.addContact @user_id, @contact_id, @callback
  68. it "should call the callback with an error", ->
  69. @callback.calledWith(new Error("contacts api responded with non-success code: 500")).should.equal true
  70. it "should log the error", ->
  71. @logger.error
  72. .calledWith({
  73. err: new Error("contacts api responded with a non-success code: 500")
  74. user_id: @user_id
  75. contact_id: @contact_id
  76. }, "error adding contact for user")
  77. .should.equal true