2020-02-17 02:38:18 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-undef,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 02:38:18 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-17 02:38:24 -05:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const chai = require('chai')
|
|
|
|
chai.should()
|
|
|
|
const { expect } = chai
|
2020-08-21 11:30:18 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2020-02-17 02:38:24 -05:00
|
|
|
const request = require('request')
|
|
|
|
const async = require('async')
|
|
|
|
const ContactsApp = require('./ContactsApp')
|
|
|
|
const HOST = 'http://localhost:3036'
|
2015-10-07 09:20:30 -04:00
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
describe('Getting Contacts', function () {
|
|
|
|
describe('with no contacts', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-17 02:38:24 -05:00
|
|
|
this.user_id = ObjectId().toString()
|
|
|
|
return ContactsApp.ensureRunning(done)
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
return it('should return an empty array', function (done) {
|
2020-02-17 02:38:24 -05:00
|
|
|
return request(
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
url: `${HOST}/user/${this.user_id}/contacts`,
|
2021-07-13 07:04:47 -04:00
|
|
|
json: true,
|
2020-02-17 02:38:24 -05:00
|
|
|
},
|
|
|
|
(error, response, body) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2020-02-17 02:38:24 -05:00
|
|
|
response.statusCode.should.equal(200)
|
|
|
|
body.contact_ids.should.deep.equal([])
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
return describe('with contacts', function () {
|
|
|
|
beforeEach(function (done) {
|
2020-02-17 02:38:24 -05:00
|
|
|
this.user_id = ObjectId().toString()
|
|
|
|
this.contact_id_1 = ObjectId().toString()
|
|
|
|
this.contact_id_2 = ObjectId().toString()
|
|
|
|
this.contact_id_3 = ObjectId().toString()
|
|
|
|
|
|
|
|
const touchContact = (user_id, contact_id, cb) =>
|
|
|
|
request(
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
url: `${HOST}/user/${user_id}/contacts`,
|
|
|
|
json: {
|
2021-07-13 07:04:47 -04:00
|
|
|
contact_id,
|
|
|
|
},
|
2020-02-17 02:38:24 -05:00
|
|
|
},
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
|
|
|
|
return async.series(
|
|
|
|
[
|
|
|
|
// 2 is preferred since touched twice, then 3 since most recent, then 1
|
2021-07-13 07:04:47 -04:00
|
|
|
cb => ContactsApp.ensureRunning(cb),
|
|
|
|
cb => touchContact(this.user_id, this.contact_id_1, cb),
|
|
|
|
cb => touchContact(this.user_id, this.contact_id_2, cb),
|
|
|
|
cb => touchContact(this.user_id, this.contact_id_2, cb),
|
|
|
|
cb => touchContact(this.user_id, this.contact_id_3, cb),
|
2020-02-17 02:38:24 -05:00
|
|
|
],
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
it('should return a sorted list of contacts', function (done) {
|
2020-02-17 02:38:24 -05:00
|
|
|
return request(
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
url: `${HOST}/user/${this.user_id}/contacts`,
|
2021-07-13 07:04:47 -04:00
|
|
|
json: true,
|
2020-02-17 02:38:24 -05:00
|
|
|
},
|
|
|
|
(error, response, body) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2020-02-17 02:38:24 -05:00
|
|
|
response.statusCode.should.equal(200)
|
|
|
|
body.contact_ids.should.deep.equal([
|
|
|
|
this.contact_id_2,
|
|
|
|
this.contact_id_3,
|
2021-07-13 07:04:47 -04:00
|
|
|
this.contact_id_1,
|
2020-02-17 02:38:24 -05:00
|
|
|
])
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
return it('should respect a limit and only return top X contacts', function (done) {
|
2020-02-17 02:38:24 -05:00
|
|
|
return request(
|
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
url: `${HOST}/user/${this.user_id}/contacts?limit=2`,
|
2021-07-13 07:04:47 -04:00
|
|
|
json: true,
|
2020-02-17 02:38:24 -05:00
|
|
|
},
|
|
|
|
(error, response, body) => {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (error) return done(error)
|
2020-02-17 02:38:24 -05:00
|
|
|
response.statusCode.should.equal(200)
|
|
|
|
body.contact_ids.should.deep.equal([
|
|
|
|
this.contact_id_2,
|
2021-07-13 07:04:47 -04:00
|
|
|
this.contact_id_3,
|
2020-02-17 02:38:24 -05:00
|
|
|
])
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|