|
| 1 | +jest.mock("sequelize"); |
| 2 | +jest.mock("../sequelize/db"); |
| 3 | +jest.mock("../database/postgres/pg"); |
| 4 | +jest.mock("../database/elasticsearch/elastic"); |
| 5 | +jest.mock("./log"); |
| 6 | + |
| 7 | +const sequelize = require("sequelize"); |
| 8 | +const db = require("../sequelize/db"); |
| 9 | +const pg = require("../database/postgres/pg"); |
| 10 | +const es = require("../database/elasticsearch/elastic"); |
| 11 | + |
| 12 | +sequelize.Op = { and: "and", lt: "lt" }; |
| 13 | +const Accounts = { |
| 14 | + findAll: jest.fn(), |
| 15 | +}; |
| 16 | +db.getModels = () => { |
| 17 | + return { Accounts: Accounts }; |
| 18 | +}; |
| 19 | +pg.deletePgAccount = jest.fn(); |
| 20 | +es.deleteAccount = jest.fn(); |
| 21 | +const logGen = require("./log"); |
| 22 | +const logger = { |
| 23 | + info: jest.fn(), |
| 24 | + error: jest.fn(), |
| 25 | +}; |
| 26 | +logGen.mockReturnValue(logger); |
| 27 | + |
| 28 | +const util = require("./util"); |
| 29 | + |
| 30 | +describe("Testing cleanAnonymous function", () => { |
| 31 | + beforeEach(() => { |
| 32 | + jest.clearAllMocks(); |
| 33 | + }); |
| 34 | + it("should call findAll with correct queries", async () => { |
| 35 | + // Hi future engineers, I wrote some strict test here because I thought |
| 36 | + // that it's important to protect this query. Someone can accidently |
| 37 | + // clear up the whole user account database by making a little change. |
| 38 | + // If you are changing this, please do so with caution and plentiful of tests. |
| 39 | + Accounts.findAll.mockReturnValue([]); |
| 40 | + await util.cleanAnonymous(); |
| 41 | + const args = Accounts.findAll.mock.calls[0][0]; |
| 42 | + expect(args.attributes[0]).toEqual("id"); |
| 43 | + expect(args.attributes[1]).toEqual("username"); |
| 44 | + expect(Number(args.where.and[0].createdAt.lt)).toBeLessThan( |
| 45 | + Number(new Date(new Date() - 5 * 24 * 60 * 60 * 1000 + 1)) |
| 46 | + ); |
| 47 | + expect(args.where.and[1].email).toBeNull(); |
| 48 | + }); |
| 49 | + it("should not call database functions if expired accounts are found but database does not exist", async () => { |
| 50 | + Accounts.findAll.mockReturnValue([{ destroy: () => {} }]); |
| 51 | + pg.userHasPgAccount = () => false; |
| 52 | + es.checkAccount = () => false; |
| 53 | + await util.cleanAnonymous(); |
| 54 | + expect(pg.deletePgAccount).not.toHaveBeenCalled(); |
| 55 | + expect(es.deleteAccount).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + it("should call database functions if expired accounts are found", async () => { |
| 58 | + Accounts.findAll.mockReturnValue([{ destroy: () => {} }]); |
| 59 | + pg.userHasPgAccount = () => true; |
| 60 | + es.checkAccount = () => true; |
| 61 | + await util.cleanAnonymous(); |
| 62 | + expect(pg.deletePgAccount).toHaveBeenCalled(); |
| 63 | + expect(es.deleteAccount).toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + it("should call logger.error if cleaning fails", async () => { |
| 66 | + Accounts.findAll.mockImplementation(() => { |
| 67 | + throw new Error("error"); |
| 68 | + }); |
| 69 | + await util.cleanAnonymous(); |
| 70 | + expect(logger.error).toHaveBeenCalled(); |
| 71 | + }); |
| 72 | +}); |
0 commit comments