2022-12-16 10:42:39 +00:00
|
|
|
import { ObjectId } from 'mongodb'
|
|
|
|
import request from 'request'
|
|
|
|
import async from 'async'
|
|
|
|
import { app } from '../../../app/js/server.js'
|
|
|
|
|
2024-04-25 12:56:00 +00:00
|
|
|
const HOST = 'http://127.0.0.1:3036'
|
2015-10-07 13:20:30 +00:00
|
|
|
|
2020-06-04 07:49:46 +00:00
|
|
|
describe('Getting Contacts', function () {
|
2022-12-16 10:42:39 +00:00
|
|
|
before(function (done) {
|
2024-04-25 12:56:00 +00:00
|
|
|
this.server = app.listen(3036, '127.0.0.1', error => {
|
2022-12-16 10:42:39 +00:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
this.server.close()
|
|
|
|
})
|
|
|
|
|
2020-06-04 07:49:46 +00:00
|
|
|
describe('with no contacts', function () {
|
2022-12-16 10:42:39 +00:00
|
|
|
beforeEach(function () {
|
2023-10-23 08:21:16 +00:00
|
|
|
this.user_id = new ObjectId().toString()
|
2020-02-17 07:38:24 +00:00
|
|
|
})
|
|
|
|
|
2022-12-16 10:42:39 +00:00
|
|
|
it('should return an empty array', function (done) {
|
|
|
|
request(
|
2020-02-17 07:38:24 +00:00
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
url: `${HOST}/user/${this.user_id}/contacts`,
|
2021-07-13 11:04:47 +00:00
|
|
|
json: true,
|
2020-02-17 07:38:24 +00:00
|
|
|
},
|
|
|
|
(error, response, body) => {
|
2022-12-16 10:42:39 +00:00
|
|
|
if (error) {
|
|
|
|
return done(error)
|
|
|
|
}
|
2020-02-17 07:38:24 +00:00
|
|
|
response.statusCode.should.equal(200)
|
|
|
|
body.contact_ids.should.deep.equal([])
|
2022-12-16 10:42:39 +00:00
|
|
|
done()
|
2020-02-17 07:38:24 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-12-16 10:42:39 +00:00
|
|
|
describe('with contacts', function () {
|
2020-06-04 07:49:46 +00:00
|
|
|
beforeEach(function (done) {
|
2023-10-23 08:21:16 +00:00
|
|
|
this.user_id = new ObjectId().toString()
|
|
|
|
this.contact_id_1 = new ObjectId().toString()
|
|
|
|
this.contact_id_2 = new ObjectId().toString()
|
|
|
|
this.contact_id_3 = new ObjectId().toString()
|
2020-02-17 07:38:24 +00:00
|
|
|
|
2022-12-16 10:42:39 +00:00
|
|
|
const touchContact = (userId, contactId, cb) =>
|
2020-02-17 07:38:24 +00:00
|
|
|
request(
|
|
|
|
{
|
|
|
|
method: 'POST',
|
2022-12-16 10:42:39 +00:00
|
|
|
url: `${HOST}/user/${userId}/contacts`,
|
2020-02-17 07:38:24 +00:00
|
|
|
json: {
|
2022-12-16 10:42:39 +00:00
|
|
|
contact_id: contactId,
|
2021-07-13 11:04:47 +00:00
|
|
|
},
|
2020-02-17 07:38:24 +00:00
|
|
|
},
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
|
2022-12-16 10:42:39 +00:00
|
|
|
async.series(
|
2020-02-17 07:38:24 +00:00
|
|
|
[
|
|
|
|
// 2 is preferred since touched twice, then 3 since most recent, then 1
|
2021-07-13 11:04:47 +00:00
|
|
|
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 07:38:24 +00:00
|
|
|
],
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-06-04 07:49:46 +00:00
|
|
|
it('should return a sorted list of contacts', function (done) {
|
2022-12-16 10:42:39 +00:00
|
|
|
request(
|
2020-02-17 07:38:24 +00:00
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
url: `${HOST}/user/${this.user_id}/contacts`,
|
2021-07-13 11:04:47 +00:00
|
|
|
json: true,
|
2020-02-17 07:38:24 +00:00
|
|
|
},
|
|
|
|
(error, response, body) => {
|
2022-12-16 10:42:39 +00:00
|
|
|
if (error) {
|
|
|
|
return done(error)
|
|
|
|
}
|
2020-02-17 07:38:24 +00:00
|
|
|
response.statusCode.should.equal(200)
|
|
|
|
body.contact_ids.should.deep.equal([
|
|
|
|
this.contact_id_2,
|
|
|
|
this.contact_id_3,
|
2021-07-13 11:04:47 +00:00
|
|
|
this.contact_id_1,
|
2020-02-17 07:38:24 +00:00
|
|
|
])
|
2022-12-16 10:42:39 +00:00
|
|
|
done()
|
2020-02-17 07:38:24 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-12-16 10:42:39 +00:00
|
|
|
it('should respect a limit and only return top X contacts', function (done) {
|
|
|
|
request(
|
2020-02-17 07:38:24 +00:00
|
|
|
{
|
|
|
|
method: 'GET',
|
|
|
|
url: `${HOST}/user/${this.user_id}/contacts?limit=2`,
|
2021-07-13 11:04:47 +00:00
|
|
|
json: true,
|
2020-02-17 07:38:24 +00:00
|
|
|
},
|
|
|
|
(error, response, body) => {
|
2022-12-16 10:42:39 +00:00
|
|
|
if (error) {
|
|
|
|
return done(error)
|
|
|
|
}
|
2020-02-17 07:38:24 +00:00
|
|
|
response.statusCode.should.equal(200)
|
|
|
|
body.contact_ids.should.deep.equal([
|
|
|
|
this.contact_id_2,
|
2021-07-13 11:04:47 +00:00
|
|
|
this.contact_id_3,
|
2020-02-17 07:38:24 +00:00
|
|
|
])
|
2022-12-16 10:42:39 +00:00
|
|
|
done()
|
2020-02-17 07:38:24 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|