Merge pull request #4324 from overleaf/jpa-script-create-user

[scripts] refactor create-admin script into generic create-user script

GitOrigin-RevId: a6f5c383888538edd392ffd4da90610c380b83a6
This commit is contained in:
Jakob Ackermann 2021-07-20 11:26:36 +02:00 committed by Copybot
parent fe4c48b7fb
commit f7a4a57487
3 changed files with 78 additions and 76 deletions

View file

@ -1,72 +0,0 @@
const Settings = require('@overleaf/settings')
const { db, waitForDb } = require('../../../app/src/infrastructure/mongodb')
const UserRegistrationHandler = require('../../../app/src/Features/User/UserRegistrationHandler')
const OneTimeTokenHandler = require('../../../app/src/Features/Security/OneTimeTokenHandler')
async function main() {
await waitForDb()
const email = (process.argv.slice(2).pop() || '').replace(/^--email=/, '')
if (!email) {
console.error(`Usage: node ${__filename} --email=joe@example.com`)
process.exit(1)
}
await new Promise((resolve, reject) => {
UserRegistrationHandler.registerNewUser(
{
email,
password: require('crypto').randomBytes(32).toString('hex'),
},
(error, user) => {
if (error && error.message !== 'EmailAlreadyRegistered') {
return reject(error)
}
db.users.updateOne(
{ _id: user._id },
{ $set: { isAdmin: true } },
error => {
if (error) {
return reject(error)
}
const ONE_WEEK = 7 * 24 * 60 * 60 // seconds
OneTimeTokenHandler.getNewToken(
'password',
{
expiresIn: ONE_WEEK,
email: user.email,
user_id: user._id.toString(),
},
(err, token) => {
if (err) {
return reject(err)
}
console.log('')
console.log(`\
Successfully created ${email} as an admin user.
Please visit the following URL to set a password for ${email} and log in:
${Settings.siteUrl}/user/password/set?passwordResetToken=${token}
\
`)
resolve()
}
)
}
)
}
)
})
}
main()
.then(() => {
console.error('Done.')
process.exit(0)
})
.catch(err => {
console.error(err)
process.exit(1)
})

View file

@ -0,0 +1,59 @@
const minimist = require('minimist')
const { db, waitForDb } = require('../../../app/src/infrastructure/mongodb')
const UserRegistrationHandler = require('../../../app/src/Features/User/UserRegistrationHandler')
async function main() {
await waitForDb()
const argv = minimist(process.argv.slice(2), {
string: ['email'],
boolean: ['admin'],
})
const { admin, email } = argv
if (!email) {
console.error(`Usage: node ${__filename} [--admin] --email=joe@example.com`)
process.exit(1)
}
await new Promise((resolve, reject) => {
UserRegistrationHandler.registerNewUserAndSendActivationEmail(
email,
(error, user, setNewPasswordUrl) => {
if (error) {
return reject(error)
}
db.users.updateOne(
{ _id: user._id },
{ $set: { isAdmin: admin } },
error => {
if (error) {
return reject(error)
}
console.log('')
console.log(`\
Successfully created ${email} as ${admin ? 'an admin' : 'a'} user.
Please visit the following URL to set a password for ${email} and log in:
${setNewPasswordUrl}
`)
resolve()
}
)
}
)
})
}
main()
.then(() => {
console.error('Done.')
process.exit(0)
})
.catch(err => {
console.error(err)
process.exit(1)
})

View file

@ -1,5 +1,6 @@
const { execSync } = require('child_process')
const { expect } = require('chai')
const { db } = require('../../../../../app/src/infrastructure/mongodb')
const User = require('../../../../../test/acceptance/src/helpers/User').promises
/**
@ -18,6 +19,10 @@ function run(cmd) {
}).toString()
}
async function getUser(email) {
return db.users.findOne({ email }, { projection: { _id: 0, isAdmin: 1 } })
}
describe('ServerCEScripts', function () {
describe('check-mongodb', function () {
it('should exit with code 0 on success', function () {
@ -55,15 +60,25 @@ describe('ServerCEScripts', function () {
})
})
describe('create-admin', function () {
describe('create-user', function () {
it('should exit with code 0 on success', function () {
const out = run('node create-admin --email=foo@bar.com')
expect(out).to.include('/user/password/set?passwordResetToken=')
const out = run('node create-user --email=foo@bar.com')
expect(out).to.include('/user/activate?token=')
})
it('should create a regular user by default', async function () {
run('node create-user --email=foo@bar.com')
expect(await getUser('foo@bar.com')).to.deep.equal({ isAdmin: false })
})
it('should create an admin user with --admin flag', async function () {
run('node create-user --admin --email=foo@bar.com')
expect(await getUser('foo@bar.com')).to.deep.equal({ isAdmin: true })
})
it('should exit with code 1 on missing email', function () {
try {
run('node create-admin')
run('node create-user')
} catch (e) {
expect(e.status).to.equal(1)
return