Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: add maintain job to clear name #72

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const bodyParser = require('body-parser');
const authorizationRoute = require('./routes/authorization');
const subscriptionRoute = require('./routes/subscription');
const notificationRoute = require('./routes/notification');
const maintainRoute = require('./routes/maintain');

const viewRoute = require('./routes/view');
const constants = require('./lib/constants');
const { checkAuth } = require('./middlewares/auth');
Expand Down Expand Up @@ -61,6 +63,8 @@ app.get(constants.route.forClient.GET_FORM_DATA, refererChecker, checkAuth, subs
app.post(constants.route.forThirdParty.NOTIFICATION, notificationRoute.notification);
// Home page
app.get('/home', viewRoute.home);
app.get('/maintain/remove-user-name', maintainRoute.removeUserName);

if (process.env.GOOGLE_SITE_VERIFICATION_TOKEN) {
app.get(`/${process.env.GOOGLE_SITE_VERIFICATION_TOKEN}.html`, (req, res) => {
res.send(`google-site-verification: ${process.env.GOOGLE_SITE_VERIFICATION_TOKEN}.html`);
Expand Down
41 changes: 41 additions & 0 deletions src/server/routes/maintain.js
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;
64 changes: 64 additions & 0 deletions tests/maintain.test.js
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' } });
});
});
Loading