2020-02-17 02:38:06 -05:00
|
|
|
/* eslint-disable
|
|
|
|
mocha/no-pending-tests,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-17 02:38:06 -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:13 -05:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const chai = require('chai')
|
|
|
|
const should = chai.should()
|
|
|
|
const { expect } = chai
|
|
|
|
const modulePath = '../../../app/js/HttpController.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
describe('HttpController', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
this.HttpController = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2021-07-13 07:04:47 -04:00
|
|
|
'./ContactManager': (this.ContactManager = {}),
|
|
|
|
},
|
2020-02-17 02:38:13 -05:00
|
|
|
})
|
|
|
|
this.user_id = 'mock-user-id'
|
|
|
|
this.contact_id = 'mock-contact-id'
|
|
|
|
|
|
|
|
this.req = {}
|
|
|
|
this.res = {}
|
|
|
|
this.res.status = sinon.stub().returns(this.res)
|
|
|
|
this.res.end = sinon.stub()
|
|
|
|
this.res.send = sinon.stub()
|
2020-03-18 10:08:14 -04:00
|
|
|
this.res.sendStatus = sinon.stub()
|
2020-02-17 02:38:13 -05:00
|
|
|
return (this.next = sinon.stub())
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
describe('addContact', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
this.req.params = { user_id: this.user_id }
|
|
|
|
return (this.ContactManager.touchContact = sinon.stub().callsArg(2))
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
describe('with a valid user_id and contact_id', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
this.req.body = { contact_id: this.contact_id }
|
|
|
|
return this.HttpController.addContact(this.req, this.res, this.next)
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
it("should update the contact in the user's contact list", function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
return this.ContactManager.touchContact
|
|
|
|
.calledWith(this.user_id, this.contact_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
it("should update the user in the contact's contact list", function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
return this.ContactManager.touchContact
|
|
|
|
.calledWith(this.contact_id, this.user_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
return it('should send back a 204 status', function () {
|
2020-03-18 10:08:14 -04:00
|
|
|
this.res.sendStatus.calledWith(204).should.equal(true)
|
2020-02-17 02:38:13 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
return describe('with an invalid contact id', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
this.req.body = { contact_id: '' }
|
|
|
|
return this.HttpController.addContact(this.req, this.res, this.next)
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
return it('should return 400, Bad Request', function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
this.res.status.calledWith(400).should.equal(true)
|
|
|
|
return this.res.send
|
|
|
|
.calledWith('contact_id should be a non-blank string')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
return describe('getContacts', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
this.req.params = { user_id: this.user_id }
|
|
|
|
const now = Date.now()
|
|
|
|
this.contacts = {
|
|
|
|
'user-id-1': { n: 2, ts: new Date(now) },
|
|
|
|
'user-id-2': { n: 4, ts: new Date(now) },
|
2021-07-13 07:04:47 -04:00
|
|
|
'user-id-3': { n: 2, ts: new Date(now - 1000) },
|
2020-02-17 02:38:13 -05:00
|
|
|
}
|
|
|
|
return (this.ContactManager.getContacts = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.contacts))
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
describe('normally', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
return this.HttpController.getContacts(this.req, this.res, this.next)
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
it('should look up the contacts in mongo', function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
return this.ContactManager.getContacts
|
|
|
|
.calledWith(this.user_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
return it('should return a sorted list of contacts by count and timestamp', function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
return this.res.send
|
|
|
|
.calledWith({
|
2021-07-13 07:04:47 -04:00
|
|
|
contact_ids: ['user-id-2', 'user-id-1', 'user-id-3'],
|
2020-02-17 02:38:13 -05:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
describe('with more contacts than the limit', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
this.req.query = { limit: 2 }
|
|
|
|
return this.HttpController.getContacts(this.req, this.res, this.next)
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
return it('should return the most commonly used contacts up to the limit', function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
return this.res.send
|
|
|
|
.calledWith({
|
2021-07-13 07:04:47 -04:00
|
|
|
contact_ids: ['user-id-2', 'user-id-1'],
|
2020-02-17 02:38:13 -05:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
describe('without a contact list', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
this.ContactManager.getContacts = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, null)
|
|
|
|
return this.HttpController.getContacts(this.req, this.res, this.next)
|
|
|
|
})
|
|
|
|
|
2020-06-04 03:49:46 -04:00
|
|
|
return it('should return an empty list', function () {
|
2020-02-17 02:38:13 -05:00
|
|
|
return this.res.send
|
|
|
|
.calledWith({
|
2021-07-13 07:04:47 -04:00
|
|
|
contact_ids: [],
|
2020-02-17 02:38:13 -05:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|