Merge pull request #2597 from overleaf/as-cleanup-tags-handler

Clean up tags handler

GitOrigin-RevId: 6e83880d9a38b323470933f6ef01fde187d55161
This commit is contained in:
Eric Mc Sween 2020-02-27 07:47:06 -05:00 committed by Copybot
parent e80870f453
commit 77c75bf223
2 changed files with 241 additions and 341 deletions

View file

@ -1,210 +1,156 @@
/* eslint-disable
camelcase,
handle-callback-err,
max-len,
no-return-assign,
*/
// 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
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const settings = require('settings-sharelatex')
const request = require('request')
const logger = require('logger-sharelatex')
const { promisifyAll } = require('../../util/promises')
const TIMEOUT = 10000
const TagsHandler = {
getAllTags(user_id, callback) {
this._requestTags(user_id, (err, allTags) => {
if (allTags == null) {
allTags = []
}
callback(err, allTags)
})
},
createTag(user_id, name, callback) {
if (callback == null) {
callback = function(error, tag) {}
}
const opts = {
url: `${settings.apis.tags.url}/user/${user_id}/tag`,
function getAllTags(userId, callback) {
const opts = {
url: `${settings.apis.tags.url}/user/${userId}/tag`,
json: true,
timeout: TIMEOUT
}
request.get(opts, (err, res, body) =>
_handleResponse(err, res, { userId }, function(error) {
if (error != null) {
return callback(error, [])
}
callback(null, body || [])
})
)
}
function createTag(userId, name, callback) {
const opts = {
url: `${settings.apis.tags.url}/user/${userId}/tag`,
json: {
name
},
timeout: TIMEOUT
}
request.post(opts, (err, res, body) =>
_handleResponse(err, res, { userId }, function(error) {
if (error != null) {
return callback(error)
}
callback(null, body || {})
})
)
}
function renameTag(userId, tagId, name, callback) {
const url = `${settings.apis.tags.url}/user/${userId}/tag/${tagId}/rename`
request.post(
{
url,
json: {
name
},
timeout: TIMEOUT
}
return request.post(opts, (err, res, body) =>
TagsHandler._handleResponse(err, res, { user_id }, function(error) {
if (error != null) {
return callback(error)
}
return callback(null, body || {})
})
)
},
},
(err, res, body) =>
_handleResponse(err, res, { url, userId, tagId, name }, callback)
)
}
renameTag(user_id, tag_id, name, callback) {
if (callback == null) {
callback = function(error) {}
}
const url = `${settings.apis.tags.url}/user/${user_id}/tag/${tag_id}/rename`
return request.post(
{
url,
json: {
name
},
timeout: TIMEOUT
},
(err, res, body) =>
TagsHandler._handleResponse(
err,
res,
{ url, user_id, tag_id, name },
callback
)
)
},
function deleteTag(userId, tagId, callback) {
const url = `${settings.apis.tags.url}/user/${userId}/tag/${tagId}`
request.del({ url, timeout: TIMEOUT }, (err, res, body) =>
_handleResponse(err, res, { url, userId, tagId }, callback)
)
}
deleteTag(user_id, tag_id, callback) {
if (callback == null) {
callback = function(error) {}
}
const url = `${settings.apis.tags.url}/user/${user_id}/tag/${tag_id}`
return request.del({ url, timeout: TIMEOUT }, (err, res, body) =>
TagsHandler._handleResponse(err, res, { url, user_id, tag_id }, callback)
)
},
function updateTagUserIds(oldUserId, newUserId, callback) {
const opts = {
url: `${settings.apis.tags.url}/user/${oldUserId}/tag`,
json: {
user_id: newUserId
},
timeout: TIMEOUT
}
request.put(opts, (err, res, body) =>
_handleResponse(err, res, { oldUserId, newUserId }, callback)
)
}
updateTagUserIds(old_user_id, new_user_id, callback) {
const opts = {
url: `${settings.apis.tags.url}/user/${old_user_id}/tag`,
json: {
user_id: new_user_id
},
timeout: TIMEOUT
}
return request.put(opts, (err, res, body) =>
TagsHandler._handleResponse(
err,
res,
{ old_user_id, new_user_id },
callback
)
)
},
function removeProjectFromTag(userId, tagId, projectId, callback) {
const url = `${
settings.apis.tags.url
}/user/${userId}/tag/${tagId}/project/${projectId}`
request.del({ url, timeout: TIMEOUT }, (err, res, body) =>
_handleResponse(err, res, { url, userId, tagId, projectId }, callback)
)
}
removeProjectFromTag(user_id, tag_id, project_id, callback) {
const url = `${
settings.apis.tags.url
}/user/${user_id}/tag/${tag_id}/project/${project_id}`
return request.del({ url, timeout: TIMEOUT }, (err, res, body) =>
TagsHandler._handleResponse(
err,
res,
{ url, user_id, tag_id, project_id },
callback
)
)
},
function addProjectToTag(userId, tagId, projectId, callback) {
const url = `${
settings.apis.tags.url
}/user/${userId}/tag/${tagId}/project/${projectId}`
request.post({ url, timeout: TIMEOUT }, (err, res, body) =>
_handleResponse(err, res, { url, userId, tagId, projectId }, callback)
)
}
addProjectToTag(user_id, tag_id, project_id, callback) {
const url = `${
settings.apis.tags.url
}/user/${user_id}/tag/${tag_id}/project/${project_id}`
return request.post({ url, timeout: TIMEOUT }, (err, res, body) =>
TagsHandler._handleResponse(
err,
res,
{ url, user_id, tag_id, project_id },
callback
)
)
},
function addProjectToTagName(userId, name, projectId, callback) {
const url = `${
settings.apis.tags.url
}/user/${userId}/tag/project/${projectId}`
const opts = {
json: { name },
timeout: TIMEOUT,
url
}
request.post(opts, (err, res, body) =>
_handleResponse(err, res, { url, userId, name, projectId }, callback)
)
}
addProjectToTagName(user_id, name, project_id, callback) {
const url = `${
settings.apis.tags.url
}/user/${user_id}/tag/project/${project_id}`
const opts = {
json: { name },
timeout: TIMEOUT,
url
}
return request.post(opts, (err, res, body) =>
TagsHandler._handleResponse(
err,
res,
{ url, user_id, name, project_id },
callback
)
)
},
function removeProjectFromAllTags(userId, projectId, callback) {
const url = `${settings.apis.tags.url}/user/${userId}/project/${projectId}`
const opts = {
url,
timeout: TIMEOUT
}
request.del(opts, (err, res, body) =>
_handleResponse(err, res, { url, userId, projectId }, callback)
)
}
removeProjectFromAllTags(user_id, project_id, callback) {
const url = `${
settings.apis.tags.url
}/user/${user_id}/project/${project_id}`
const opts = {
url,
timeout: TIMEOUT
}
return request.del(opts, (err, res, body) =>
TagsHandler._handleResponse(
err,
res,
{ url, user_id, project_id },
callback
)
function _handleResponse(err, res, params, callback) {
if (err != null) {
params.err = err
logger.warn(params, 'error in tag api')
return callback(err)
} else if (res != null && res.statusCode >= 200 && res.statusCode < 300) {
return callback(null)
} else {
err = new Error(
`tags api returned a failure status code: ${
res != null ? res.statusCode : undefined
}`
)
},
_handleResponse(err, res, params, callback) {
if (err != null) {
params.err = err
logger.warn(params, 'error in tag api')
return callback(err)
} else if (res != null && res.statusCode >= 200 && res.statusCode < 300) {
return callback(null)
} else {
err = new Error(
`tags api returned a failure status code: ${
res != null ? res.statusCode : undefined
}`
)
params.err = err
logger.warn(
params,
`tags api returned failure status code: ${
res != null ? res.statusCode : undefined
}`
)
return callback(err)
}
},
_requestTags(user_id, callback) {
const opts = {
url: `${settings.apis.tags.url}/user/${user_id}/tag`,
json: true,
timeout: TIMEOUT
}
return request.get(opts, (err, res, body) =>
TagsHandler._handleResponse(err, res, { user_id }, function(error) {
if (error != null) {
return callback(error, [])
}
return callback(null, body || [])
})
params.err = err
logger.warn(
params,
`tags api returned failure status code: ${
res != null ? res.statusCode : undefined
}`
)
callback(err)
}
}
const TagsHandler = {
getAllTags,
createTag,
renameTag,
deleteTag,
updateTagUserIds,
removeProjectFromTag,
addProjectToTag,
addProjectToTagName,
removeProjectFromAllTags
}
TagsHandler.promises = promisifyAll(TagsHandler)
module.exports = TagsHandler

View file

@ -1,16 +1,3 @@
/* eslint-disable
camelcase,
handle-callback-err,
max-len,
no-return-assign,
*/
// 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 SandboxedModule = require('sandboxed-module')
const { assert } = require('chai')
require('chai').should()
@ -19,12 +6,10 @@ const modulePath = require('path').join(
__dirname,
'../../../../app/src/Features/Tags/TagsHandler.js'
)
const _ = require('underscore')
describe('TagsHandler', function() {
const user_id = 'user-id-123'
const tag_id = 'tag-id-123'
const project_id = 'project-id-123'
const userId = 'user-id-123'
const tagId = 'tag-id-123'
const projectId = 'project-id-123'
const tagsUrl = 'tags.sharelatex.testing'
const tag = 'tag_name'
@ -35,7 +20,7 @@ describe('TagsHandler', function() {
get: sinon.stub()
}
this.callback = sinon.stub()
return (this.handler = SandboxedModule.require(modulePath, {
this.handler = SandboxedModule.require(modulePath, {
globals: {
console: console
},
@ -50,72 +35,19 @@ describe('TagsHandler', function() {
err() {}
}
}
}))
})
})
describe('removeProjectFromAllTags', function() {
it('should tell the tags api to remove the project_id from all the users tags', function(done) {
return this.handler.removeProjectFromAllTags(user_id, project_id, () => {
this.handler.removeProjectFromAllTags(userId, projectId, () => {
this.request.del
.calledWith({
url: `${tagsUrl}/user/${user_id}/project/${project_id}`,
url: `${tagsUrl}/user/${userId}/project/${projectId}`,
timeout: 10000
})
.should.equal(true)
return done()
})
})
})
describe('_requestTags', function() {
it('should return an err and empty array on error', function(done) {
this.request.get.callsArgWith(
1,
{ something: 'wrong' },
{ statusCode: 200 },
[]
)
return this.handler._requestTags(user_id, (err, allTags) => {
allTags.length.should.equal(0)
assert.isDefined(err)
return done()
})
})
it('should return an err and empty array on no body', function(done) {
this.request.get.callsArgWith(
1,
{ something: 'wrong' },
{ statusCode: 200 },
undefined
)
return this.handler._requestTags(user_id, (err, allTags) => {
allTags.length.should.equal(0)
assert.isDefined(err)
return done()
})
})
it('should return an err and empty array on non 200 response', function(done) {
this.request.get.callsArgWith(1, null, { statusCode: 201 }, [])
return this.handler._requestTags(user_id, (err, allTags) => {
allTags.length.should.equal(0)
assert.isDefined(err)
return done()
})
})
it('should return an err and empty array on no body and no response', function(done) {
this.request.get.callsArgWith(
1,
{ something: 'wrong' },
undefined,
undefined
)
return this.handler._requestTags(user_id, (err, allTags) => {
allTags.length.should.equal(0)
assert.isDefined(err)
return done()
done()
})
})
})
@ -131,27 +63,68 @@ describe('TagsHandler', function() {
{ statusCode: 200 },
stubbedAllTags
)
return this.handler.getAllTags(user_id, (err, allTags) => {
this.handler.getAllTags(userId, (err, allTags) => {
assert.notExists(err)
stubbedAllTags.should.deep.equal(allTags)
const getOpts = {
url: `${tagsUrl}/user/${user_id}/tag`,
url: `${tagsUrl}/user/${userId}/tag`,
json: true,
timeout: 10000
}
this.request.get.calledWith(getOpts).should.equal(true)
return done()
done()
})
})
it('should return empty arrays if there are no tags', function() {
this.request.get.callsArgWith(1, null, { statusCode: 200 }, null)
return this.handler.getAllTags(
user_id,
(err, allTags, projectGroupedTags) => {
allTags.length.should.equal(0)
return _.size(projectGroupedTags).should.equal(0)
}
it('should callback with an empty array on error', function(done) {
this.request.get.callsArgWith(
1,
{ something: 'wrong' },
{ statusCode: 200 },
[]
)
this.handler.getAllTags(userId, (err, allTags) => {
allTags.length.should.equal(0)
assert.isDefined(err)
done()
})
})
it('should callback with an empty array if there are no tags', function(done) {
this.request.get.callsArgWith(
1,
{ something: 'wrong' },
{ statusCode: 200 },
undefined
)
this.handler.getAllTags(userId, (err, allTags) => {
allTags.length.should.equal(0)
assert.isDefined(err)
done()
})
})
it('should callback with an empty array on a non 200 response', function(done) {
this.request.get.callsArgWith(1, null, { statusCode: 201 }, [])
this.handler.getAllTags(userId, (err, allTags) => {
allTags.length.should.equal(0)
assert.isDefined(err)
done()
})
})
it('should callback with an empty array on no body and no response', function(done) {
this.request.get.callsArgWith(
1,
{ something: 'wrong' },
undefined,
undefined
)
this.handler.getAllTags(userId, (err, allTags) => {
allTags.length.should.equal(0)
assert.isDefined(err)
done()
})
})
})
@ -160,17 +133,13 @@ describe('TagsHandler', function() {
this.request.post = sinon
.stub()
.callsArgWith(1, null, { statusCode: 204 }, '')
return this.handler.createTag(
user_id,
(this.name = 'tag_name'),
this.callback
)
this.handler.createTag(userId, (this.name = 'tag_name'), this.callback)
})
it('should send a request to the tag backend', function() {
return this.request.post
this.request.post
.calledWith({
url: `${tagsUrl}/user/${user_id}/tag`,
url: `${tagsUrl}/user/${userId}/tag`,
json: {
name: this.name
},
@ -180,7 +149,7 @@ describe('TagsHandler', function() {
})
it('should call the callback with no error', function() {
return this.callback.calledWith(null).should.equal(true)
this.callback.calledWith(null).should.equal(true)
})
})
@ -190,20 +159,20 @@ describe('TagsHandler', function() {
this.request.del = sinon
.stub()
.callsArgWith(1, null, { statusCode: 204 }, '')
return this.handler.deleteTag(user_id, tag_id, this.callback)
this.handler.deleteTag(userId, tagId, this.callback)
})
it('should send a request to the tag backend', function() {
return this.request.del
this.request.del
.calledWith({
url: `${tagsUrl}/user/${user_id}/tag/${tag_id}`,
url: `${tagsUrl}/user/${userId}/tag/${tagId}`,
timeout: 10000
})
.should.equal(true)
})
it('should call the callback with no error', function() {
return this.callback.calledWith(null).should.equal(true)
this.callback.calledWith(null).should.equal(true)
})
})
@ -212,11 +181,11 @@ describe('TagsHandler', function() {
this.request.del = sinon
.stub()
.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.deleteTag(user_id, tag_id, this.callback)
this.handler.deleteTag(userId, tagId, this.callback)
})
it('should call the callback with an Error', function() {
return this.callback
this.callback
.calledWith(sinon.match.instanceOf(Error))
.should.equal(true)
})
@ -229,18 +198,18 @@ describe('TagsHandler', function() {
this.request.post = sinon
.stub()
.callsArgWith(1, null, { statusCode: 204 }, '')
return this.handler.renameTag(
user_id,
tag_id,
this.handler.renameTag(
userId,
tagId,
(this.name = 'new-name'),
this.callback
)
})
it('should send a request to the tag backend', function() {
return this.request.post
this.request.post
.calledWith({
url: `${tagsUrl}/user/${user_id}/tag/${tag_id}/rename`,
url: `${tagsUrl}/user/${userId}/tag/${tagId}/rename`,
json: {
name: this.name
},
@ -250,7 +219,7 @@ describe('TagsHandler', function() {
})
it('should call the callback with no error', function() {
return this.callback.calledWith(null).should.equal(true)
this.callback.calledWith(null).should.equal(true)
})
})
@ -259,11 +228,11 @@ describe('TagsHandler', function() {
this.request.post = sinon
.stub()
.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.renameTag(user_id, tag_id, 'name', this.callback)
this.handler.renameTag(userId, tagId, 'name', this.callback)
})
it('should call the callback with an Error', function() {
return this.callback
this.callback
.calledWith(sinon.match.instanceOf(Error))
.should.equal(true)
})
@ -276,25 +245,25 @@ describe('TagsHandler', function() {
this.request.del = sinon
.stub()
.callsArgWith(1, null, { statusCode: 204 }, '')
return this.handler.removeProjectFromTag(
user_id,
tag_id,
project_id,
this.handler.removeProjectFromTag(
userId,
tagId,
projectId,
this.callback
)
})
it('should send a request to the tag backend', function() {
return this.request.del
this.request.del
.calledWith({
url: `${tagsUrl}/user/${user_id}/tag/${tag_id}/project/${project_id}`,
url: `${tagsUrl}/user/${userId}/tag/${tagId}/project/${projectId}`,
timeout: 10000
})
.should.equal(true)
})
it('should call the callback with no error', function() {
return this.callback.calledWith(null).should.equal(true)
this.callback.calledWith(null).should.equal(true)
})
})
@ -303,16 +272,16 @@ describe('TagsHandler', function() {
this.request.del = sinon
.stub()
.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.removeProjectFromTag(
user_id,
tag_id,
project_id,
this.handler.removeProjectFromTag(
userId,
tagId,
projectId,
this.callback
)
})
it('should call the callback with an Error', function() {
return this.callback
this.callback
.calledWith(sinon.match.instanceOf(Error))
.should.equal(true)
})
@ -325,25 +294,20 @@ describe('TagsHandler', function() {
this.request.post = sinon
.stub()
.callsArgWith(1, null, { statusCode: 204 }, '')
return this.handler.addProjectToTag(
user_id,
tag_id,
project_id,
this.callback
)
this.handler.addProjectToTag(userId, tagId, projectId, this.callback)
})
it('should send a request to the tag backend', function() {
return this.request.post
this.request.post
.calledWith({
url: `${tagsUrl}/user/${user_id}/tag/${tag_id}/project/${project_id}`,
url: `${tagsUrl}/user/${userId}/tag/${tagId}/project/${projectId}`,
timeout: 10000
})
.should.equal(true)
})
it('should call the callback with no error', function() {
return this.callback.calledWith(null).should.equal(true)
this.callback.calledWith(null).should.equal(true)
})
})
@ -352,16 +316,11 @@ describe('TagsHandler', function() {
this.request.post = sinon
.stub()
.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.addProjectToTag(
user_id,
tag_id,
project_id,
this.callback
)
this.handler.addProjectToTag(userId, tagId, projectId, this.callback)
})
it('should call the callback with an Error', function() {
return this.callback
this.callback
.calledWith(sinon.match.instanceOf(Error))
.should.equal(true)
})
@ -374,28 +333,23 @@ describe('TagsHandler', function() {
this.request.post = sinon
.stub()
.callsArgWith(1, null, { statusCode: 204 }, '')
return this.handler.addProjectToTagName(
user_id,
tag,
project_id,
this.callback
)
this.handler.addProjectToTagName(userId, tag, projectId, this.callback)
})
it('should send a request to the tag backend', function() {
return this.request.post
this.request.post
.calledWith({
json: {
name: tag
},
url: `${tagsUrl}/user/${user_id}/tag/project/${project_id}`,
url: `${tagsUrl}/user/${userId}/tag/project/${projectId}`,
timeout: 10000
})
.should.equal(true)
})
it('should call the callback with no error', function() {
return this.callback.calledWith(null).should.equal(true)
this.callback.calledWith(null).should.equal(true)
})
})
@ -404,16 +358,16 @@ describe('TagsHandler', function() {
this.request.post = sinon
.stub()
.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.addProjectToTagName(
user_id,
tag_id,
project_id,
this.handler.addProjectToTagName(
userId,
tagId,
projectId,
this.callback
)
})
it('should call the callback with an Error', function() {
return this.callback
this.callback
.calledWith(sinon.match.instanceOf(Error))
.should.equal(true)
})
@ -426,7 +380,7 @@ describe('TagsHandler', function() {
this.request.put = sinon
.stub()
.callsArgWith(1, null, { statusCode: 204 }, '')
return this.handler.updateTagUserIds(
this.handler.updateTagUserIds(
'old-user-id',
'new-user-id',
this.callback
@ -434,7 +388,7 @@ describe('TagsHandler', function() {
})
it('should send a request to the tag backend', function() {
return this.request.put
this.request.put
.calledWith({
json: {
user_id: 'new-user-id'
@ -446,7 +400,7 @@ describe('TagsHandler', function() {
})
it('should call the callback with no error', function() {
return this.callback.calledWith(null).should.equal(true)
this.callback.calledWith(null).should.equal(true)
})
})
@ -455,7 +409,7 @@ describe('TagsHandler', function() {
this.request.put = sinon
.stub()
.callsArgWith(1, null, { statusCode: 500 }, '')
return this.handler.updateTagUserIds(
this.handler.updateTagUserIds(
'old-user-id',
'new-user-id',
this.callback
@ -463,7 +417,7 @@ describe('TagsHandler', function() {
})
it('should call the callback with an Error', function() {
return this.callback
this.callback
.calledWith(sinon.match.instanceOf(Error))
.should.equal(true)
})