GettingContactsTests.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* eslint-disable
  2. camelcase,
  3. no-undef,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const sinon = require('sinon')
  14. const chai = require('chai')
  15. chai.should()
  16. const { expect } = chai
  17. const { ObjectId } = require('mongodb')
  18. const request = require('request')
  19. const async = require('async')
  20. const ContactsApp = require('./ContactsApp')
  21. const HOST = 'http://localhost:3036'
  22. describe('Getting Contacts', function () {
  23. describe('with no contacts', function () {
  24. beforeEach(function (done) {
  25. this.user_id = ObjectId().toString()
  26. return ContactsApp.ensureRunning(done)
  27. })
  28. return it('should return an empty array', function (done) {
  29. return request(
  30. {
  31. method: 'GET',
  32. url: `${HOST}/user/${this.user_id}/contacts`,
  33. json: true,
  34. },
  35. (error, response, body) => {
  36. if (error) return done(error)
  37. response.statusCode.should.equal(200)
  38. body.contact_ids.should.deep.equal([])
  39. return done()
  40. }
  41. )
  42. })
  43. })
  44. return describe('with contacts', function () {
  45. beforeEach(function (done) {
  46. this.user_id = ObjectId().toString()
  47. this.contact_id_1 = ObjectId().toString()
  48. this.contact_id_2 = ObjectId().toString()
  49. this.contact_id_3 = ObjectId().toString()
  50. const touchContact = (user_id, contact_id, cb) =>
  51. request(
  52. {
  53. method: 'POST',
  54. url: `${HOST}/user/${user_id}/contacts`,
  55. json: {
  56. contact_id,
  57. },
  58. },
  59. cb
  60. )
  61. return async.series(
  62. [
  63. // 2 is preferred since touched twice, then 3 since most recent, then 1
  64. cb => ContactsApp.ensureRunning(cb),
  65. cb => touchContact(this.user_id, this.contact_id_1, cb),
  66. cb => touchContact(this.user_id, this.contact_id_2, cb),
  67. cb => touchContact(this.user_id, this.contact_id_2, cb),
  68. cb => touchContact(this.user_id, this.contact_id_3, cb),
  69. ],
  70. done
  71. )
  72. })
  73. it('should return a sorted list of contacts', function (done) {
  74. return request(
  75. {
  76. method: 'GET',
  77. url: `${HOST}/user/${this.user_id}/contacts`,
  78. json: true,
  79. },
  80. (error, response, body) => {
  81. if (error) return done(error)
  82. response.statusCode.should.equal(200)
  83. body.contact_ids.should.deep.equal([
  84. this.contact_id_2,
  85. this.contact_id_3,
  86. this.contact_id_1,
  87. ])
  88. return done()
  89. }
  90. )
  91. })
  92. return it('should respect a limit and only return top X contacts', function (done) {
  93. return request(
  94. {
  95. method: 'GET',
  96. url: `${HOST}/user/${this.user_id}/contacts?limit=2`,
  97. json: true,
  98. },
  99. (error, response, body) => {
  100. if (error) return done(error)
  101. response.statusCode.should.equal(200)
  102. body.contact_ids.should.deep.equal([
  103. this.contact_id_2,
  104. this.contact_id_3,
  105. ])
  106. return done()
  107. }
  108. )
  109. })
  110. })
  111. })