GettingContactsTests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-undef,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const chai = require('chai')
  16. chai.should()
  17. const { expect } = chai
  18. const { ObjectId } = require('mongodb')
  19. const request = require('request')
  20. const async = require('async')
  21. const ContactsApp = require('./ContactsApp')
  22. const HOST = 'http://localhost:3036'
  23. describe('Getting Contacts', function () {
  24. describe('with no contacts', function () {
  25. beforeEach(function (done) {
  26. this.user_id = ObjectId().toString()
  27. return ContactsApp.ensureRunning(done)
  28. })
  29. return it('should return an empty array', function (done) {
  30. return request(
  31. {
  32. method: 'GET',
  33. url: `${HOST}/user/${this.user_id}/contacts`,
  34. json: true,
  35. },
  36. (error, response, body) => {
  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. response.statusCode.should.equal(200)
  82. body.contact_ids.should.deep.equal([
  83. this.contact_id_2,
  84. this.contact_id_3,
  85. this.contact_id_1,
  86. ])
  87. return done()
  88. }
  89. )
  90. })
  91. return it('should respect a limit and only return top X contacts', function (done) {
  92. return request(
  93. {
  94. method: 'GET',
  95. url: `${HOST}/user/${this.user_id}/contacts?limit=2`,
  96. json: true,
  97. },
  98. (error, response, body) => {
  99. response.statusCode.should.equal(200)
  100. body.contact_ids.should.deep.equal([
  101. this.contact_id_2,
  102. this.contact_id_3,
  103. ])
  104. return done()
  105. }
  106. )
  107. })
  108. })
  109. })