Merge pull request #2226 from overleaf/spd-clearprojectokens

Add script to clear tokens from a specific project

GitOrigin-RevId: a810e2b7fe2e34efb937bffc0e39c409076daa23
This commit is contained in:
Eric Mc Sween 2019-10-14 09:22:45 -04:00 committed by sharelatex
parent 9a492257af
commit 33d1f603df
3 changed files with 39 additions and 1 deletions

View file

@ -27,6 +27,7 @@ module.exports = {
generateUniqueName: callbackify(generateUniqueName),
setPublicAccessLevel: callbackify(setPublicAccessLevel),
ensureTokensArePresent: callbackify(ensureTokensArePresent),
clearTokens: callbackify(clearTokens),
fixProjectName,
promises: {
getDetails,
@ -37,7 +38,8 @@ module.exports = {
validateProjectName,
generateUniqueName,
setPublicAccessLevel,
ensureTokensArePresent
ensureTokensArePresent,
clearTokens
}
}
@ -276,6 +278,13 @@ async function ensureTokensArePresent(projectId) {
return project.tokens
}
async function clearTokens(projectId) {
await Project.update(
{ _id: projectId },
{ $unset: { tokens: 1 }, $set: { publicAccesLevel: 'private' } }
).exec()
}
async function _generateTokens(project, callback) {
if (!project.tokens) {
project.tokens = {}

View file

@ -0,0 +1,19 @@
const ProjectDetailsHandler = require('../app/src/Features/Project/ProjectDetailsHandler')
const projectId = process.argv[2]
if (!/^(?=[a-f\d]{24}$)(\d+[a-f]|[a-f]+\d)/.test(projectId)) {
console.error('Usage: node clear_project_tokens.js projectId')
process.exit(1)
}
ProjectDetailsHandler.clearTokens(projectId, err => {
if (err) {
console.error(
`Error clearing project tokens from project ${projectId}`,
err
)
process.exit(1)
}
console.log(`Successfully cleared project tokens from project ${projectId}`)
process.exit(0)
})

View file

@ -640,4 +640,14 @@ describe('ProjectDetailsHandler', function() {
})
})
})
describe('clearTokens', function() {
it('clears the tokens from the project', async function() {
await this.handler.promises.clearTokens(this.project._id)
expect(this.ProjectModel.update).to.have.been.calledWith(
{ _id: this.project._id },
{ $unset: { tokens: 1 }, $set: { publicAccesLevel: 'private' } }
)
})
})
})