-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
164 additions
and
27 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
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
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,49 @@ | ||
const { Op } = require("sequelize"); | ||
const db = require("../sequelize/db"); | ||
const pg = require("../database/postgres/pg"); | ||
const es = require("../database/elasticsearch/elastic"); | ||
const logger = require("./log")(__filename); | ||
|
||
const util = {}; | ||
|
||
util.cleanAnonymous = async () => { | ||
logger.info("Checking expired anonymous accounts..."); | ||
try { | ||
const { Accounts } = db.getModels(); | ||
const userAccount = await Accounts.findAll({ | ||
attributes: ["id", "username"], | ||
where: { | ||
[Op.and]: [ | ||
{ | ||
createdAt: { | ||
[Op.lt]: new Date(new Date() - 5 * 24 * 60 * 60 * 1000), | ||
}, | ||
}, | ||
{ email: null }, | ||
], | ||
}, | ||
}); | ||
logger.info(`${userAccount.length} expired accounts are found`); | ||
return Promise.all( | ||
userAccount.map(async (e) => { | ||
const pgDbExists = await pg.userHasPgAccount(e.username); | ||
if (pgDbExists) await pg.deletePgAccount(e); | ||
const esDbExists = await es.checkAccount(e); | ||
if (esDbExists) await es.deleteAccount(e); | ||
return await e.destroy(); | ||
}) | ||
).then(() => { | ||
logger.info("Cleaning expired accounts has completed"); | ||
const timer = setTimeout(util.cleanAnonymous, 24 * 60 * 60 * 1000); | ||
return { | ||
stop: () => { | ||
clearTimeout(timer); | ||
}, | ||
}; | ||
}); | ||
} catch (err) { | ||
logger.error("Cleaning expired accounts has failed", err); | ||
} | ||
}; | ||
|
||
module.exports = util; |
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,72 @@ | ||
jest.mock("sequelize"); | ||
jest.mock("../sequelize/db"); | ||
jest.mock("../database/postgres/pg"); | ||
jest.mock("../database/elasticsearch/elastic"); | ||
jest.mock("./log"); | ||
|
||
const sequelize = require("sequelize"); | ||
const db = require("../sequelize/db"); | ||
const pg = require("../database/postgres/pg"); | ||
const es = require("../database/elasticsearch/elastic"); | ||
|
||
sequelize.Op = { and: "and", lt: "lt" }; | ||
const Accounts = { | ||
findAll: jest.fn(), | ||
}; | ||
db.getModels = () => { | ||
return { Accounts: Accounts }; | ||
}; | ||
pg.deletePgAccount = jest.fn(); | ||
es.deleteAccount = jest.fn(); | ||
const logGen = require("./log"); | ||
const logger = { | ||
info: jest.fn(), | ||
error: jest.fn(), | ||
}; | ||
logGen.mockReturnValue(logger); | ||
|
||
const util = require("./util"); | ||
|
||
describe("Testing cleanAnonymous function", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it("should call findAll with correct queries", async () => { | ||
// Hi future engineers, I wrote some strict test here because I thought | ||
// that it's important to protect this query. Someone can accidently | ||
// clear up the whole user account database by making a little change. | ||
// If you are changing this, please do so with caution and plentiful of tests. | ||
Accounts.findAll.mockReturnValue([]); | ||
await util.cleanAnonymous(); | ||
const args = Accounts.findAll.mock.calls[0][0]; | ||
expect(args.attributes[0]).toEqual("id"); | ||
expect(args.attributes[1]).toEqual("username"); | ||
expect(Number(args.where.and[0].createdAt.lt)).toBeLessThan( | ||
Number(new Date(new Date() - 5 * 24 * 60 * 60 * 1000 + 1)) | ||
); | ||
expect(args.where.and[1].email).toBeNull(); | ||
}); | ||
it("should not call database functions if expired accounts are found but database does not exist", async () => { | ||
Accounts.findAll.mockReturnValue([{ destroy: () => {} }]); | ||
pg.userHasPgAccount = () => false; | ||
es.checkAccount = () => false; | ||
await util.cleanAnonymous(); | ||
expect(pg.deletePgAccount).not.toHaveBeenCalled(); | ||
expect(es.deleteAccount).not.toHaveBeenCalled(); | ||
}); | ||
it("should call database functions if expired accounts are found", async () => { | ||
Accounts.findAll.mockReturnValue([{ destroy: () => {} }]); | ||
pg.userHasPgAccount = () => true; | ||
es.checkAccount = () => true; | ||
await util.cleanAnonymous(); | ||
expect(pg.deletePgAccount).toHaveBeenCalled(); | ||
expect(es.deleteAccount).toHaveBeenCalled(); | ||
}); | ||
it("should call logger.error if cleaning fails", async () => { | ||
Accounts.findAll.mockImplementation(() => { | ||
throw new Error("error"); | ||
}); | ||
await util.cleanAnonymous(); | ||
expect(logger.error).toHaveBeenCalled(); | ||
}); | ||
}); |
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