feat: Cleanup api keys and webhooks for suspended users (#3756)
This commit is contained in:
102
server/queues/tasks/CleanupDemotedUserTask.test.ts
Normal file
102
server/queues/tasks/CleanupDemotedUserTask.test.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import { ApiKey } from "@server/models";
|
||||
import {
|
||||
buildUser,
|
||||
buildApiKey,
|
||||
buildAdmin,
|
||||
buildWebhookSubscription,
|
||||
buildViewer,
|
||||
} from "@server/test/factories";
|
||||
import { flushdb } from "@server/test/support";
|
||||
import CleanupDemotedUserTask from "./CleanupDemotedUserTask";
|
||||
|
||||
beforeEach(() => flushdb());
|
||||
|
||||
describe("CleanupDemotedUserTask", () => {
|
||||
it("should delete api keys for suspended user", async () => {
|
||||
const admin = await buildAdmin();
|
||||
const user = await buildUser({
|
||||
teamId: admin.teamId,
|
||||
suspendedAt: new Date(),
|
||||
suspendedById: admin.id,
|
||||
});
|
||||
const apiKey = await buildApiKey({
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
const task = new CleanupDemotedUserTask();
|
||||
await task.perform({ userId: user.id });
|
||||
expect(await ApiKey.findByPk(apiKey.id)).toBeNull();
|
||||
});
|
||||
|
||||
it("should delete api keys for viewer", async () => {
|
||||
const user = await buildViewer();
|
||||
const apiKey = await buildApiKey({
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
const task = new CleanupDemotedUserTask();
|
||||
await task.perform({ userId: user.id });
|
||||
expect(await ApiKey.findByPk(apiKey.id)).toBeNull();
|
||||
});
|
||||
|
||||
it("should retain api keys for member", async () => {
|
||||
const user = await buildUser();
|
||||
const apiKey = await buildApiKey({
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
const task = new CleanupDemotedUserTask();
|
||||
await task.perform({ userId: user.id });
|
||||
expect(await ApiKey.findByPk(apiKey.id)).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should disable webhooks for suspended user", async () => {
|
||||
const admin = await buildAdmin();
|
||||
const user = await buildUser({
|
||||
teamId: admin.teamId,
|
||||
suspendedAt: new Date(),
|
||||
suspendedById: admin.id,
|
||||
});
|
||||
const webhook = await buildWebhookSubscription({
|
||||
teamId: user.teamId,
|
||||
createdById: user.id,
|
||||
});
|
||||
|
||||
const task = new CleanupDemotedUserTask();
|
||||
await task.perform({ userId: user.id });
|
||||
|
||||
await webhook.reload();
|
||||
expect(webhook.enabled).toEqual(false);
|
||||
});
|
||||
|
||||
it("should disable webhooks for member", async () => {
|
||||
const admin = await buildAdmin();
|
||||
const user = await buildUser({
|
||||
teamId: admin.teamId,
|
||||
});
|
||||
const webhook = await buildWebhookSubscription({
|
||||
teamId: user.teamId,
|
||||
createdById: user.id,
|
||||
});
|
||||
|
||||
const task = new CleanupDemotedUserTask();
|
||||
await task.perform({ userId: user.id });
|
||||
|
||||
await webhook.reload();
|
||||
expect(webhook.enabled).toEqual(false);
|
||||
});
|
||||
|
||||
it("should retain webhooks for admin", async () => {
|
||||
const user = await buildAdmin();
|
||||
const webhook = await buildWebhookSubscription({
|
||||
teamId: user.teamId,
|
||||
createdById: user.id,
|
||||
});
|
||||
|
||||
const task = new CleanupDemotedUserTask();
|
||||
await task.perform({ userId: user.id });
|
||||
|
||||
await webhook.reload();
|
||||
expect(webhook.enabled).toEqual(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user