|
|
@@ -1,29 +1,26 @@
|
|
|
-/* 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.
|
|
|
-/*
|
|
|
- * decaffeinate suggestions:
|
|
|
- * DS102: Remove unnecessary code created because of implicit returns
|
|
|
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
- */
|
|
|
-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')
|
|
|
+import sinon from 'sinon'
|
|
|
+import { expect } from 'chai'
|
|
|
+import esmock from 'esmock'
|
|
|
|
|
|
describe('HttpController', function () {
|
|
|
- beforeEach(function () {
|
|
|
- this.HttpController = SandboxedModule.require(modulePath, {
|
|
|
- requires: {
|
|
|
- './ContactManager': (this.ContactManager = {}),
|
|
|
- },
|
|
|
+ beforeEach(async function () {
|
|
|
+ const now = Date.now()
|
|
|
+
|
|
|
+ this.contacts = {
|
|
|
+ 'user-id-1': { n: 2, ts: new Date(now) },
|
|
|
+ 'user-id-2': { n: 4, ts: new Date(now) },
|
|
|
+ 'user-id-3': { n: 2, ts: new Date(now - 1000) },
|
|
|
+ }
|
|
|
+
|
|
|
+ this.ContactManager = {
|
|
|
+ touchContact: sinon.stub().resolves(),
|
|
|
+ getContacts: sinon.stub().resolves(this.contacts),
|
|
|
+ }
|
|
|
+
|
|
|
+ this.HttpController = await esmock('../../../app/js/HttpController', {
|
|
|
+ '../../../app/js/ContactManager': this.ContactManager,
|
|
|
})
|
|
|
+
|
|
|
this.user_id = 'mock-user-id'
|
|
|
this.contact_id = 'mock-contact-id'
|
|
|
|
|
|
@@ -31,118 +28,99 @@ describe('HttpController', function () {
|
|
|
this.res = {}
|
|
|
this.res.status = sinon.stub().returns(this.res)
|
|
|
this.res.end = sinon.stub()
|
|
|
+ this.res.json = sinon.stub()
|
|
|
this.res.send = sinon.stub()
|
|
|
this.res.sendStatus = sinon.stub()
|
|
|
- return (this.next = sinon.stub())
|
|
|
+ this.next = sinon.stub()
|
|
|
})
|
|
|
|
|
|
describe('addContact', function () {
|
|
|
- beforeEach(function () {
|
|
|
- this.req.params = { user_id: this.user_id }
|
|
|
- return (this.ContactManager.touchContact = sinon.stub().callsArg(2))
|
|
|
- })
|
|
|
-
|
|
|
describe('with a valid user_id and contact_id', function () {
|
|
|
- beforeEach(function () {
|
|
|
+ beforeEach(async function () {
|
|
|
+ this.req.params = { user_id: this.user_id }
|
|
|
this.req.body = { contact_id: this.contact_id }
|
|
|
- return this.HttpController.addContact(this.req, this.res, this.next)
|
|
|
+ await this.HttpController.addContact(this.req, this.res, this.next)
|
|
|
})
|
|
|
|
|
|
it("should update the contact in the user's contact list", function () {
|
|
|
- return this.ContactManager.touchContact
|
|
|
- .calledWith(this.user_id, this.contact_id)
|
|
|
- .should.equal(true)
|
|
|
+ expect(this.ContactManager.touchContact).to.be.calledWith(
|
|
|
+ this.user_id,
|
|
|
+ this.contact_id
|
|
|
+ )
|
|
|
})
|
|
|
|
|
|
it("should update the user in the contact's contact list", function () {
|
|
|
- return this.ContactManager.touchContact
|
|
|
- .calledWith(this.contact_id, this.user_id)
|
|
|
- .should.equal(true)
|
|
|
+ expect(this.ContactManager.touchContact).to.be.calledWith(
|
|
|
+ this.contact_id,
|
|
|
+ this.user_id
|
|
|
+ )
|
|
|
})
|
|
|
|
|
|
- return it('should send back a 204 status', function () {
|
|
|
- this.res.sendStatus.calledWith(204).should.equal(true)
|
|
|
+ it('should send back a 204 status', function () {
|
|
|
+ expect(this.res.sendStatus).to.be.calledWith(204)
|
|
|
})
|
|
|
})
|
|
|
|
|
|
- return describe('with an invalid contact id', function () {
|
|
|
- beforeEach(function () {
|
|
|
+ describe('with an invalid contact id', function () {
|
|
|
+ beforeEach(async function () {
|
|
|
+ this.req.params = { user_id: this.user_id }
|
|
|
this.req.body = { contact_id: '' }
|
|
|
- return this.HttpController.addContact(this.req, this.res, this.next)
|
|
|
+ await this.HttpController.addContact(this.req, this.res, this.next)
|
|
|
})
|
|
|
|
|
|
- return it('should return 400, Bad Request', function () {
|
|
|
- this.res.status.calledWith(400).should.equal(true)
|
|
|
- return this.res.send
|
|
|
- .calledWith('contact_id should be a non-blank string')
|
|
|
- .should.equal(true)
|
|
|
+ it('should return 400, Bad Request', function () {
|
|
|
+ expect(this.res.status).to.be.calledWith(400)
|
|
|
+ expect(this.res.send).to.be.calledWith(
|
|
|
+ 'contact_id should be a non-blank string'
|
|
|
+ )
|
|
|
})
|
|
|
})
|
|
|
})
|
|
|
|
|
|
- return describe('getContacts', function () {
|
|
|
- beforeEach(function () {
|
|
|
- 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) },
|
|
|
- 'user-id-3': { n: 2, ts: new Date(now - 1000) },
|
|
|
- }
|
|
|
- return (this.ContactManager.getContacts = sinon
|
|
|
- .stub()
|
|
|
- .callsArgWith(1, null, this.contacts))
|
|
|
- })
|
|
|
-
|
|
|
+ describe('getContacts', function () {
|
|
|
describe('normally', function () {
|
|
|
- beforeEach(function () {
|
|
|
- return this.HttpController.getContacts(this.req, this.res, this.next)
|
|
|
+ beforeEach(async function () {
|
|
|
+ this.req.params = { user_id: this.user_id }
|
|
|
+ this.req.query = {}
|
|
|
+ await this.HttpController.getContacts(this.req, this.res, this.next)
|
|
|
})
|
|
|
|
|
|
it('should look up the contacts in mongo', function () {
|
|
|
- return this.ContactManager.getContacts
|
|
|
- .calledWith(this.user_id)
|
|
|
- .should.equal(true)
|
|
|
+ expect(this.ContactManager.getContacts).to.be.calledWith(this.user_id)
|
|
|
})
|
|
|
|
|
|
- return it('should return a sorted list of contacts by count and timestamp', function () {
|
|
|
- return this.res.send
|
|
|
- .calledWith({
|
|
|
- contact_ids: ['user-id-2', 'user-id-1', 'user-id-3'],
|
|
|
- })
|
|
|
- .should.equal(true)
|
|
|
+ it('should return a sorted list of contacts by count and timestamp', function () {
|
|
|
+ expect(this.res.json).to.be.calledWith({
|
|
|
+ contact_ids: ['user-id-2', 'user-id-1', 'user-id-3'],
|
|
|
+ })
|
|
|
})
|
|
|
})
|
|
|
|
|
|
describe('with more contacts than the limit', function () {
|
|
|
- beforeEach(function () {
|
|
|
+ beforeEach(async function () {
|
|
|
+ this.req.params = { user_id: this.user_id }
|
|
|
this.req.query = { limit: 2 }
|
|
|
- return this.HttpController.getContacts(this.req, this.res, this.next)
|
|
|
+ await this.HttpController.getContacts(this.req, this.res, this.next)
|
|
|
})
|
|
|
|
|
|
- return it('should return the most commonly used contacts up to the limit', function () {
|
|
|
- return this.res.send
|
|
|
- .calledWith({
|
|
|
- contact_ids: ['user-id-2', 'user-id-1'],
|
|
|
- })
|
|
|
- .should.equal(true)
|
|
|
+ it('should return the most commonly used contacts up to the limit', function () {
|
|
|
+ expect(this.res.json).to.be.calledWith({
|
|
|
+ contact_ids: ['user-id-2', 'user-id-1'],
|
|
|
+ })
|
|
|
})
|
|
|
})
|
|
|
|
|
|
describe('without a contact list', function () {
|
|
|
- beforeEach(function () {
|
|
|
- this.ContactManager.getContacts = sinon
|
|
|
- .stub()
|
|
|
- .callsArgWith(1, null, null)
|
|
|
- return this.HttpController.getContacts(this.req, this.res, this.next)
|
|
|
+ beforeEach(async function () {
|
|
|
+ this.ContactManager.getContacts.resolves(null)
|
|
|
+
|
|
|
+ this.req.params = {}
|
|
|
+ this.req.query = {}
|
|
|
+ await this.HttpController.getContacts(this.req, this.res, this.next)
|
|
|
})
|
|
|
|
|
|
- return it('should return an empty list', function () {
|
|
|
- return this.res.send
|
|
|
- .calledWith({
|
|
|
- contact_ids: [],
|
|
|
- })
|
|
|
- .should.equal(true)
|
|
|
+ it('should return an empty list', function () {
|
|
|
+ expect(this.res.json).to.be.calledWith({ contact_ids: [] })
|
|
|
})
|
|
|
})
|
|
|
})
|