-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc: add maintain job to clear name
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const { User } = require('../models/userModel'); | ||
const { errorLogger } = require('../lib/logger'); | ||
|
||
async function removeUserName(req, res) { | ||
if (!process.env.MAINTAIN_TOKEN) { | ||
res.status(404); | ||
res.send('Not found'); | ||
return; | ||
} | ||
if (req.query.maintain_token !== process.env.MAINTAIN_TOKEN) { | ||
res.status(403); | ||
res.send('Forbidden'); | ||
return; | ||
} | ||
let lastKey = req.query.last_key; | ||
try { | ||
const users = await User.findAll({ | ||
limit: 50, | ||
lastKey: lastKey ? { id: lastKey } : undefined, | ||
}); | ||
if (users.lastKey) { | ||
lastKey = users.lastKey.id; | ||
} else { | ||
lastKey = ''; | ||
} | ||
for (const user of users) { | ||
if (user.name) { | ||
await User.update({ name: '' }, { where: { id: user.id } }); | ||
} | ||
} | ||
res.json({ | ||
last_key: lastKey, | ||
}); | ||
} catch (e) { | ||
errorLogger(e); | ||
res.status(500); | ||
res.send('Internal error'); | ||
} | ||
} | ||
|
||
exports.removeUserName = removeUserName; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const request = require('supertest'); | ||
const { server } = require('../src/server'); | ||
const { User } = require('../src/server/models/userModel'); | ||
|
||
describe('Maintain', () => { | ||
it('should return 404 when no MAINTAIN_TOKEN env', async () => { | ||
const res = await request(server).get('/maintain/remove-user-name'); | ||
expect(res.status).toEqual(404); | ||
}); | ||
|
||
it('should return 403 when maintain_token is invalid', async () => { | ||
process.env.MAINTAIN_TOKEN = 'valid'; | ||
const res = await request(server).get('/maintain/remove-user-name?maintain_token=invalid'); | ||
expect(res.status).toEqual(403); | ||
}); | ||
|
||
it('should return 200 when maintain_token is valid', async () => { | ||
await User.create({ | ||
id: 'test1', | ||
name: 'test' | ||
}); | ||
await User.create({ | ||
id: 'test2', | ||
name: '' | ||
}); | ||
await User.create({ | ||
id: 'test3', | ||
name: 'test3' | ||
}); | ||
const user2 = await User.findByPk('test2'); | ||
process.env.MAINTAIN_TOKEN = 'valid'; | ||
const res = await request(server).get('/maintain/remove-user-name?maintain_token=valid'); | ||
expect(res.status).toEqual(200); | ||
const users = await User.findAll(); | ||
expect(users[0].name).toEqual(''); | ||
expect(users[1].name).toEqual(''); | ||
expect(users[2].name).toEqual(''); | ||
expect(user2.updatedAt).toEqual(users[1].updatedAt); | ||
await User.destroy({ where: { id: 'test1' } }); | ||
await User.destroy({ where: { id: 'test2' } }); | ||
await User.destroy({ where: { id: 'test3' } }); | ||
}); | ||
|
||
it('should return 200 with lastKey', async () => { | ||
await User.create({ | ||
id: 'test1', | ||
name: 'test' | ||
}); | ||
await User.create({ | ||
id: 'test2', | ||
name: '' | ||
}); | ||
await User.create({ | ||
id: 'test3', | ||
name: 'test3' | ||
}); | ||
process.env.MAINTAIN_TOKEN = 'valid'; | ||
const res = await request(server).get('/maintain/remove-user-name?maintain_token=valid&last_key=test1'); | ||
expect(res.status).toEqual(200); | ||
await User.destroy({ where: { id: 'test1' } }); | ||
await User.destroy({ where: { id: 'test2' } }); | ||
await User.destroy({ where: { id: 'test3' } }); | ||
}); | ||
}); |