ContactsManagerTests.coffee 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../app/js/ContactManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. ObjectId = require("mongojs").ObjectId
  8. tk = require("timekeeper")
  9. describe "ContactManager", ->
  10. beforeEach ->
  11. tk.freeze(Date.now())
  12. @ContactManager = SandboxedModule.require modulePath, requires:
  13. "./mongojs": {
  14. db: @db = contacts: {}
  15. ObjectId: ObjectId
  16. },
  17. 'logger-sharelatex': {log: sinon.stub()},
  18. 'metrics-sharelatex': {timeAsyncMethod: sinon.stub()}
  19. @user_id = ObjectId().toString()
  20. @contact_id = ObjectId().toString()
  21. @callback = sinon.stub()
  22. afterEach ->
  23. tk.reset()
  24. describe "touchContact", ->
  25. beforeEach ->
  26. @db.contacts.update = sinon.stub().callsArg(3)
  27. describe "with a valid user_id", ->
  28. beforeEach ->
  29. @ContactManager.touchContact @user_id, @contact_id = "mock_contact", @callback
  30. it "should increment the contact count and timestamp", ->
  31. @db.contacts.update
  32. .calledWith({
  33. user_id: ObjectId(@user_id)
  34. }, {
  35. $inc:
  36. "contacts.mock_contact.n": 1
  37. $set:
  38. "contacts.mock_contact.ts": new Date()
  39. }, {
  40. upsert: true
  41. })
  42. .should.equal true
  43. it "should call the callback", ->
  44. @callback.called.should.equal true
  45. describe "with an invalid user id", ->
  46. beforeEach ->
  47. @ContactManager.touchContact "not-valid-object-id", @contact_id, @callback
  48. it "should call the callback with an error", ->
  49. @callback.calledWith(new Error()).should.equal true
  50. describe "getContacts", ->
  51. beforeEach ->
  52. @user = {
  53. contacts: ["mock", "contacts"]
  54. }
  55. @db.contacts.findOne = sinon.stub().callsArgWith(1, null, @user)
  56. describe "with a valid user_id", ->
  57. beforeEach ->
  58. @ContactManager.getContacts @user_id, @callback
  59. it "should find the user's contacts", ->
  60. @db.contacts.findOne
  61. .calledWith({
  62. user_id: ObjectId(@user_id)
  63. })
  64. .should.equal true
  65. it "should call the callback with the contacts", ->
  66. @callback.calledWith(null, @user.contacts).should.equal true
  67. describe "with an invalid user id", ->
  68. beforeEach ->
  69. @ContactManager.getContacts "not-valid-object-id", @callback
  70. it "should call the callback with an error", ->
  71. @callback.calledWith(new Error()).should.equal true