fix formatting

This commit is contained in:
Tim Alby 2020-06-04 09:49:46 +02:00
parent 2a650bb1b8
commit 48bc7595a8
8 changed files with 76 additions and 74 deletions

View file

@ -34,7 +34,7 @@ app.post(
app.get('/status', (req, res) => res.send('contacts is alive')) app.get('/status', (req, res) => res.send('contacts is alive'))
app.use(function(error, req, res, next) { app.use(function (error, req, res, next) {
logger.error({ err: error }, 'request errored') logger.error({ err: error }, 'request errored')
if (error instanceof Errors.NotFoundError) { if (error instanceof Errors.NotFoundError) {
return res.send(404) return res.send(404)
@ -48,7 +48,7 @@ const { host } = Settings.internal.contacts
if (!module.parent) { if (!module.parent) {
// Called directly // Called directly
app.listen(port, host, function(error) { app.listen(port, host, function (error) {
if (error != null) { if (error != null) {
throw error throw error
} }

View file

@ -18,7 +18,7 @@ const metrics = require('metrics-sharelatex')
module.exports = ContactManager = { module.exports = ContactManager = {
touchContact(user_id, contact_id, callback) { touchContact(user_id, contact_id, callback) {
if (callback == null) { if (callback == null) {
callback = function(error) {} callback = function (error) {}
} }
try { try {
user_id = ObjectId(user_id.toString()) user_id = ObjectId(user_id.toString())
@ -45,7 +45,7 @@ module.exports = ContactManager = {
getContacts(user_id, callback) { getContacts(user_id, callback) {
if (callback == null) { if (callback == null) {
callback = function(error) {} callback = function (error) {}
} }
try { try {
user_id = ObjectId(user_id.toString()) user_id = ObjectId(user_id.toString())
@ -58,7 +58,7 @@ module.exports = ContactManager = {
{ {
user_id user_id
}, },
function(error, user) { function (error, user) {
if (error != null) { if (error != null) {
return callback(error) return callback(error)
} }
@ -67,7 +67,7 @@ module.exports = ContactManager = {
) )
} }
} }
;['touchContact', 'getContacts'].map(method => ;['touchContact', 'getContacts'].map((method) =>
metrics.timeAsyncMethod( metrics.timeAsyncMethod(
ContactManager, ContactManager,
method, method,

View file

@ -5,7 +5,7 @@
// TODO: This file was created by bulk-decaffeinate. // TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint. // Fix any style issues and re-enable lint.
let Errors let Errors
var NotFoundError = function(message) { var NotFoundError = function (message) {
const error = new Error(message) const error = new Error(message)
error.name = 'NotFoundError' error.name = 'NotFoundError'
error.__proto__ = NotFoundError.prototype error.__proto__ = NotFoundError.prototype

View file

@ -25,11 +25,11 @@ module.exports = HttpController = {
logger.log({ user_id, contact_id }, 'adding contact') logger.log({ user_id, contact_id }, 'adding contact')
return ContactManager.touchContact(user_id, contact_id, function(error) { return ContactManager.touchContact(user_id, contact_id, function (error) {
if (error != null) { if (error != null) {
return next(error) return next(error)
} }
return ContactManager.touchContact(contact_id, user_id, function(error) { return ContactManager.touchContact(contact_id, user_id, function (error) {
if (error != null) { if (error != null) {
return next(error) return next(error)
} }
@ -52,7 +52,7 @@ module.exports = HttpController = {
logger.log({ user_id }, 'getting contacts') logger.log({ user_id }, 'getting contacts')
return ContactManager.getContacts(user_id, function(error, contact_dict) { return ContactManager.getContacts(user_id, function (error, contact_dict) {
if (error != null) { if (error != null) {
return next(error) return next(error)
} }
@ -70,7 +70,7 @@ module.exports = HttpController = {
HttpController._sortContacts(contacts) HttpController._sortContacts(contacts)
contacts = contacts.slice(0, limit) contacts = contacts.slice(0, limit)
const contact_ids = contacts.map(contact => contact.user_id) const contact_ids = contacts.map((contact) => contact.user_id)
return res.status(200).send({ return res.status(200).send({
contact_ids contact_ids
@ -79,7 +79,7 @@ module.exports = HttpController = {
}, },
_sortContacts(contacts) { _sortContacts(contacts) {
return contacts.sort(function(a, b) { return contacts.sort(function (a, b) {
// Sort by decreasing count, descreasing timestamp. // Sort by decreasing count, descreasing timestamp.
// I.e. biggest count, and most recent at front. // I.e. biggest count, and most recent at front.
if (a.n > b.n) { if (a.n > b.n) {

View file

@ -20,7 +20,7 @@ module.exports = {
callbacks: [], callbacks: [],
ensureRunning(callback) { ensureRunning(callback) {
if (callback == null) { if (callback == null) {
callback = function(error) {} callback = function (error) {}
} }
if (this.running) { if (this.running) {
return callback() return callback()
@ -29,7 +29,7 @@ module.exports = {
} else { } else {
this.initing = true this.initing = true
this.callbacks.push(callback) this.callbacks.push(callback)
return app.listen(3036, 'localhost', error => { return app.listen(3036, 'localhost', (error) => {
if (error != null) { if (error != null) {
throw error throw error
} }

View file

@ -21,14 +21,14 @@ const async = require('async')
const ContactsApp = require('./ContactsApp') const ContactsApp = require('./ContactsApp')
const HOST = 'http://localhost:3036' const HOST = 'http://localhost:3036'
describe('Getting Contacts', function() { describe('Getting Contacts', function () {
describe('with no contacts', function() { describe('with no contacts', function () {
beforeEach(function(done) { beforeEach(function (done) {
this.user_id = ObjectId().toString() this.user_id = ObjectId().toString()
return ContactsApp.ensureRunning(done) return ContactsApp.ensureRunning(done)
}) })
return it('should return an empty array', function(done) { return it('should return an empty array', function (done) {
return request( return request(
{ {
method: 'GET', method: 'GET',
@ -44,8 +44,8 @@ describe('Getting Contacts', function() {
}) })
}) })
return describe('with contacts', function() { return describe('with contacts', function () {
beforeEach(function(done) { beforeEach(function (done) {
this.user_id = ObjectId().toString() this.user_id = ObjectId().toString()
this.contact_id_1 = ObjectId().toString() this.contact_id_1 = ObjectId().toString()
this.contact_id_2 = ObjectId().toString() this.contact_id_2 = ObjectId().toString()
@ -66,17 +66,17 @@ describe('Getting Contacts', function() {
return async.series( return async.series(
[ [
// 2 is preferred since touched twice, then 3 since most recent, then 1 // 2 is preferred since touched twice, then 3 since most recent, then 1
cb => ContactsApp.ensureRunning(cb), (cb) => ContactsApp.ensureRunning(cb),
cb => touchContact(this.user_id, this.contact_id_1, 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_2, cb), (cb) => touchContact(this.user_id, this.contact_id_2, cb),
cb => touchContact(this.user_id, this.contact_id_3, cb) (cb) => touchContact(this.user_id, this.contact_id_3, cb)
], ],
done done
) )
}) })
it('should return a sorted list of contacts', function(done) { it('should return a sorted list of contacts', function (done) {
return request( return request(
{ {
method: 'GET', method: 'GET',
@ -95,7 +95,7 @@ describe('Getting Contacts', function() {
) )
}) })
return it('should respect a limit and only return top X contacts', function(done) { return it('should respect a limit and only return top X contacts', function (done) {
return request( return request(
{ {
method: 'GET', method: 'GET',

View file

@ -18,8 +18,8 @@ const SandboxedModule = require('sandboxed-module')
const { ObjectId } = require('mongojs') const { ObjectId } = require('mongojs')
const tk = require('timekeeper') const tk = require('timekeeper')
describe('ContactManager', function() { describe('ContactManager', function () {
beforeEach(function() { beforeEach(function () {
tk.freeze(Date.now()) tk.freeze(Date.now())
this.ContactManager = SandboxedModule.require(modulePath, { this.ContactManager = SandboxedModule.require(modulePath, {
requires: { requires: {
@ -36,17 +36,17 @@ describe('ContactManager', function() {
return (this.callback = sinon.stub()) return (this.callback = sinon.stub())
}) })
afterEach(function() { afterEach(function () {
return tk.reset() return tk.reset()
}) })
describe('touchContact', function() { describe('touchContact', function () {
beforeEach(function() { beforeEach(function () {
return (this.db.contacts.update = sinon.stub().callsArg(3)) return (this.db.contacts.update = sinon.stub().callsArg(3))
}) })
describe('with a valid user_id', function() { describe('with a valid user_id', function () {
beforeEach(function() { beforeEach(function () {
return this.ContactManager.touchContact( return this.ContactManager.touchContact(
this.user_id, this.user_id,
(this.contact_id = 'mock_contact'), (this.contact_id = 'mock_contact'),
@ -54,12 +54,12 @@ describe('ContactManager', function() {
) )
}) })
it('should increment the contact count and timestamp', function() { it('should increment the contact count and timestamp', function () {
return this.db.contacts.update return this.db.contacts.update
.calledWith( .calledWith(
{ {
user_id: sinon.match( user_id: sinon.match(
o => o.toString() === this.user_id.toString() (o) => o.toString() === this.user_id.toString()
) )
}, },
{ {
@ -77,13 +77,13 @@ describe('ContactManager', function() {
.should.equal(true) .should.equal(true)
}) })
return it('should call the callback', function() { return it('should call the callback', function () {
return this.callback.called.should.equal(true) return this.callback.called.should.equal(true)
}) })
}) })
return describe('with an invalid user id', function() { return describe('with an invalid user id', function () {
beforeEach(function() { beforeEach(function () {
return this.ContactManager.touchContact( return this.ContactManager.touchContact(
'not-valid-object-id', 'not-valid-object-id',
this.contact_id, this.contact_id,
@ -91,14 +91,14 @@ describe('ContactManager', function() {
) )
}) })
return it('should call the callback with an error', function() { return it('should call the callback with an error', function () {
return this.callback.calledWith(sinon.match(Error)).should.equal(true) return this.callback.calledWith(sinon.match(Error)).should.equal(true)
}) })
}) })
}) })
return describe('getContacts', function() { return describe('getContacts', function () {
beforeEach(function() { beforeEach(function () {
this.user = { this.user = {
contacts: ['mock', 'contacts'] contacts: ['mock', 'contacts']
} }
@ -107,35 +107,37 @@ describe('ContactManager', function() {
.callsArgWith(1, null, this.user)) .callsArgWith(1, null, this.user))
}) })
describe('with a valid user_id', function() { describe('with a valid user_id', function () {
beforeEach(function() { beforeEach(function () {
return this.ContactManager.getContacts(this.user_id, this.callback) return this.ContactManager.getContacts(this.user_id, this.callback)
}) })
it("should find the user's contacts", function() { it("should find the user's contacts", function () {
return this.db.contacts.findOne return this.db.contacts.findOne
.calledWith({ .calledWith({
user_id: sinon.match(o => o.toString() === this.user_id.toString()) user_id: sinon.match(
(o) => o.toString() === this.user_id.toString()
)
}) })
.should.equal(true) .should.equal(true)
}) })
return it('should call the callback with the contacts', function() { return it('should call the callback with the contacts', function () {
return this.callback return this.callback
.calledWith(null, this.user.contacts) .calledWith(null, this.user.contacts)
.should.equal(true) .should.equal(true)
}) })
}) })
return describe('with an invalid user id', function() { return describe('with an invalid user id', function () {
beforeEach(function() { beforeEach(function () {
return this.ContactManager.getContacts( return this.ContactManager.getContacts(
'not-valid-object-id', 'not-valid-object-id',
this.callback this.callback
) )
}) })
return it('should call the callback with an error', function() { return it('should call the callback with an error', function () {
return this.callback.calledWith(sinon.match(Error)).should.equal(true) return this.callback.calledWith(sinon.match(Error)).should.equal(true)
}) })
}) })

View file

@ -17,8 +17,8 @@ const { expect } = chai
const modulePath = '../../../app/js/HttpController.js' const modulePath = '../../../app/js/HttpController.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
describe('HttpController', function() { describe('HttpController', function () {
beforeEach(function() { beforeEach(function () {
this.HttpController = SandboxedModule.require(modulePath, { this.HttpController = SandboxedModule.require(modulePath, {
requires: { requires: {
'./ContactManager': (this.ContactManager = {}), './ContactManager': (this.ContactManager = {}),
@ -36,43 +36,43 @@ describe('HttpController', function() {
return (this.next = sinon.stub()) return (this.next = sinon.stub())
}) })
describe('addContact', function() { describe('addContact', function () {
beforeEach(function() { beforeEach(function () {
this.req.params = { user_id: this.user_id } this.req.params = { user_id: this.user_id }
return (this.ContactManager.touchContact = sinon.stub().callsArg(2)) return (this.ContactManager.touchContact = sinon.stub().callsArg(2))
}) })
describe('with a valid user_id and contact_id', function() { describe('with a valid user_id and contact_id', function () {
beforeEach(function() { beforeEach(function () {
this.req.body = { contact_id: this.contact_id } this.req.body = { contact_id: this.contact_id }
return this.HttpController.addContact(this.req, this.res, this.next) return this.HttpController.addContact(this.req, this.res, this.next)
}) })
it("should update the contact in the user's contact list", function() { it("should update the contact in the user's contact list", function () {
return this.ContactManager.touchContact return this.ContactManager.touchContact
.calledWith(this.user_id, this.contact_id) .calledWith(this.user_id, this.contact_id)
.should.equal(true) .should.equal(true)
}) })
it("should update the user in the contact's contact list", function() { it("should update the user in the contact's contact list", function () {
return this.ContactManager.touchContact return this.ContactManager.touchContact
.calledWith(this.contact_id, this.user_id) .calledWith(this.contact_id, this.user_id)
.should.equal(true) .should.equal(true)
}) })
return it('should send back a 204 status', function() { return it('should send back a 204 status', function () {
this.res.status.calledWith(204).should.equal(true) this.res.status.calledWith(204).should.equal(true)
return this.res.end.called.should.equal(true) return this.res.end.called.should.equal(true)
}) })
}) })
return describe('with an invalid contact id', function() { return describe('with an invalid contact id', function () {
beforeEach(function() { beforeEach(function () {
this.req.body = { contact_id: '' } this.req.body = { contact_id: '' }
return this.HttpController.addContact(this.req, this.res, this.next) return this.HttpController.addContact(this.req, this.res, this.next)
}) })
return it('should return 400, Bad Request', function() { return it('should return 400, Bad Request', function () {
this.res.status.calledWith(400).should.equal(true) this.res.status.calledWith(400).should.equal(true)
return this.res.send return this.res.send
.calledWith('contact_id should be a non-blank string') .calledWith('contact_id should be a non-blank string')
@ -81,8 +81,8 @@ describe('HttpController', function() {
}) })
}) })
return describe('getContacts', function() { return describe('getContacts', function () {
beforeEach(function() { beforeEach(function () {
this.req.params = { user_id: this.user_id } this.req.params = { user_id: this.user_id }
const now = Date.now() const now = Date.now()
this.contacts = { this.contacts = {
@ -95,18 +95,18 @@ describe('HttpController', function() {
.callsArgWith(1, null, this.contacts)) .callsArgWith(1, null, this.contacts))
}) })
describe('normally', function() { describe('normally', function () {
beforeEach(function() { beforeEach(function () {
return this.HttpController.getContacts(this.req, this.res, this.next) return this.HttpController.getContacts(this.req, this.res, this.next)
}) })
it('should look up the contacts in mongo', function() { it('should look up the contacts in mongo', function () {
return this.ContactManager.getContacts return this.ContactManager.getContacts
.calledWith(this.user_id) .calledWith(this.user_id)
.should.equal(true) .should.equal(true)
}) })
return it('should return a sorted list of contacts by count and timestamp', function() { return it('should return a sorted list of contacts by count and timestamp', function () {
return this.res.send return this.res.send
.calledWith({ .calledWith({
contact_ids: ['user-id-2', 'user-id-1', 'user-id-3'] contact_ids: ['user-id-2', 'user-id-1', 'user-id-3']
@ -115,13 +115,13 @@ describe('HttpController', function() {
}) })
}) })
describe('with more contacts than the limit', function() { describe('with more contacts than the limit', function () {
beforeEach(function() { beforeEach(function () {
this.req.query = { limit: 2 } this.req.query = { limit: 2 }
return this.HttpController.getContacts(this.req, this.res, this.next) return this.HttpController.getContacts(this.req, this.res, this.next)
}) })
return it('should return the most commonly used contacts up to the limit', function() { return it('should return the most commonly used contacts up to the limit', function () {
return this.res.send return this.res.send
.calledWith({ .calledWith({
contact_ids: ['user-id-2', 'user-id-1'] contact_ids: ['user-id-2', 'user-id-1']
@ -130,15 +130,15 @@ describe('HttpController', function() {
}) })
}) })
describe('without a contact list', function() { describe('without a contact list', function () {
beforeEach(function() { beforeEach(function () {
this.ContactManager.getContacts = sinon this.ContactManager.getContacts = sinon
.stub() .stub()
.callsArgWith(1, null, null) .callsArgWith(1, null, null)
return this.HttpController.getContacts(this.req, this.res, this.next) return this.HttpController.getContacts(this.req, this.res, this.next)
}) })
return it('should return an empty list', function() { return it('should return an empty list', function () {
return this.res.send return this.res.send
.calledWith({ .calledWith({
contact_ids: [] contact_ids: []