GettingContactsTests.coffee 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. expect = chai.expect
  5. ObjectId = require("mongojs").ObjectId
  6. request = require "request"
  7. async = require "async"
  8. HOST = "http://localhost:3036"
  9. describe "Getting Contacts", ->
  10. describe "with no contacts", ->
  11. beforeEach ->
  12. @user_id = ObjectId().toString()
  13. it "should return an empty array", (done) ->
  14. request {
  15. method: "GET"
  16. url: "#{HOST}/user/#{@user_id}/contacts"
  17. json: true
  18. }, (error, response, body) ->
  19. response.statusCode.should.equal 200
  20. body.contact_ids.should.deep.equal []
  21. done()
  22. describe "with contacts", ->
  23. beforeEach (done) ->
  24. @user_id = ObjectId().toString()
  25. @contact_id_1 = ObjectId().toString()
  26. @contact_id_2 = ObjectId().toString()
  27. @contact_id_3 = ObjectId().toString()
  28. touchContact = (user_id, contact_id, cb) ->
  29. request({
  30. method: "POST"
  31. url: "#{HOST}/user/#{user_id}/contacts"
  32. json: {
  33. contact_id: contact_id
  34. }
  35. }, cb)
  36. async.series [
  37. # 2 is preferred since touched twice, then 3 since most recent, then 1
  38. (cb) => touchContact @user_id, @contact_id_1, cb
  39. (cb) => touchContact @user_id, @contact_id_2, cb
  40. (cb) => touchContact @user_id, @contact_id_2, cb
  41. (cb) => touchContact @user_id, @contact_id_3, cb
  42. ], done
  43. it "should return a sorted list of contacts", (done) ->
  44. request {
  45. method: "GET"
  46. url: "#{HOST}/user/#{@user_id}/contacts"
  47. json: true
  48. }, (error, response, body) =>
  49. response.statusCode.should.equal 200
  50. body.contact_ids.should.deep.equal [@contact_id_2, @contact_id_3, @contact_id_1]
  51. done()
  52. it "should respect a limit and only return top X contacts", ->
  53. request {
  54. method: "GET"
  55. url: "#{HOST}/user/#{@user_id}/contacts?limit=2"
  56. json: true
  57. }, (error, response, body) =>
  58. response.statusCode.should.equal 200
  59. body.contact_ids.should.deep.equal [@contact_id_2, @contact_id_3]
  60. done()