GettingContactsTests.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { ObjectId } from 'mongodb'
  2. import request from 'request'
  3. import async from 'async'
  4. import { app } from '../../../app/js/server.js'
  5. const HOST = 'http://127.0.0.1:3036'
  6. describe('Getting Contacts', function () {
  7. before(function (done) {
  8. this.server = app.listen(3036, '127.0.0.1', error => {
  9. if (error != null) {
  10. throw error
  11. }
  12. done()
  13. })
  14. })
  15. after(function () {
  16. this.server.close()
  17. })
  18. describe('with no contacts', function () {
  19. beforeEach(function () {
  20. this.user_id = new ObjectId().toString()
  21. })
  22. it('should return an empty array', function (done) {
  23. request(
  24. {
  25. method: 'GET',
  26. url: `${HOST}/user/${this.user_id}/contacts`,
  27. json: true,
  28. },
  29. (error, response, body) => {
  30. if (error) {
  31. return done(error)
  32. }
  33. response.statusCode.should.equal(200)
  34. body.contact_ids.should.deep.equal([])
  35. done()
  36. }
  37. )
  38. })
  39. })
  40. describe('with contacts', function () {
  41. beforeEach(function (done) {
  42. this.user_id = new ObjectId().toString()
  43. this.contact_id_1 = new ObjectId().toString()
  44. this.contact_id_2 = new ObjectId().toString()
  45. this.contact_id_3 = new ObjectId().toString()
  46. const touchContact = (userId, contactId, cb) =>
  47. request(
  48. {
  49. method: 'POST',
  50. url: `${HOST}/user/${userId}/contacts`,
  51. json: {
  52. contact_id: contactId,
  53. },
  54. },
  55. cb
  56. )
  57. async.series(
  58. [
  59. // 2 is preferred since touched twice, then 3 since most recent, then 1
  60. cb => touchContact(this.user_id, this.contact_id_1, cb),
  61. cb => touchContact(this.user_id, this.contact_id_2, cb),
  62. cb => touchContact(this.user_id, this.contact_id_2, cb),
  63. cb => touchContact(this.user_id, this.contact_id_3, cb),
  64. ],
  65. done
  66. )
  67. })
  68. it('should return a sorted list of contacts', function (done) {
  69. request(
  70. {
  71. method: 'GET',
  72. url: `${HOST}/user/${this.user_id}/contacts`,
  73. json: true,
  74. },
  75. (error, response, body) => {
  76. if (error) {
  77. return done(error)
  78. }
  79. response.statusCode.should.equal(200)
  80. body.contact_ids.should.deep.equal([
  81. this.contact_id_2,
  82. this.contact_id_3,
  83. this.contact_id_1,
  84. ])
  85. done()
  86. }
  87. )
  88. })
  89. it('should respect a limit and only return top X contacts', function (done) {
  90. request(
  91. {
  92. method: 'GET',
  93. url: `${HOST}/user/${this.user_id}/contacts?limit=2`,
  94. json: true,
  95. },
  96. (error, response, body) => {
  97. if (error) {
  98. return done(error)
  99. }
  100. response.statusCode.should.equal(200)
  101. body.contact_ids.should.deep.equal([
  102. this.contact_id_2,
  103. this.contact_id_3,
  104. ])
  105. done()
  106. }
  107. )
  108. })
  109. })
  110. })