more backend unit test coverage
This commit is contained in:
@@ -431,4 +431,113 @@ describe("Users Routes", () => {
|
||||
expect(response.body).toEqual({ error: "Database error" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /admin/:id/ban", () => {
|
||||
const mockTargetUser = {
|
||||
id: 2,
|
||||
role: "user",
|
||||
isBanned: false,
|
||||
banUser: jest.fn().mockResolvedValue(),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockUserFindByPk.mockResolvedValue(mockTargetUser);
|
||||
});
|
||||
|
||||
it("should ban a user with reason", async () => {
|
||||
const response = await request(app)
|
||||
.post("/users/admin/2/ban")
|
||||
.send({ reason: "Violation of terms" });
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.message).toContain("banned successfully");
|
||||
expect(mockTargetUser.banUser).toHaveBeenCalledWith(1, "Violation of terms");
|
||||
});
|
||||
|
||||
it("should return 400 when reason is not provided", async () => {
|
||||
const response = await request(app)
|
||||
.post("/users/admin/2/ban")
|
||||
.send({});
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.body).toEqual({ error: "Ban reason is required" });
|
||||
});
|
||||
|
||||
it("should return 404 for non-existent user", async () => {
|
||||
mockUserFindByPk.mockResolvedValue(null);
|
||||
|
||||
const response = await request(app)
|
||||
.post("/users/admin/999/ban")
|
||||
.send({ reason: "Test" });
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(response.body).toEqual({ error: "User not found" });
|
||||
});
|
||||
|
||||
it("should return 403 when trying to ban admin", async () => {
|
||||
const adminUser = { ...mockTargetUser, role: "admin" };
|
||||
mockUserFindByPk.mockResolvedValue(adminUser);
|
||||
|
||||
const response = await request(app)
|
||||
.post("/users/admin/2/ban")
|
||||
.send({ reason: "Test" });
|
||||
|
||||
expect(response.status).toBe(403);
|
||||
expect(response.body).toEqual({ error: "Cannot ban admin users" });
|
||||
});
|
||||
|
||||
it("should return 400 when user is already banned", async () => {
|
||||
const bannedUser = { ...mockTargetUser, isBanned: true };
|
||||
mockUserFindByPk.mockResolvedValue(bannedUser);
|
||||
|
||||
const response = await request(app)
|
||||
.post("/users/admin/2/ban")
|
||||
.send({ reason: "Test" });
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.body).toEqual({ error: "User is already banned" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /admin/:id/unban", () => {
|
||||
const mockBannedUser = {
|
||||
id: 2,
|
||||
isBanned: true,
|
||||
unbanUser: jest.fn().mockResolvedValue(),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockUserFindByPk.mockResolvedValue(mockBannedUser);
|
||||
});
|
||||
|
||||
it("should unban a banned user", async () => {
|
||||
const response = await request(app)
|
||||
.post("/users/admin/2/unban");
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.message).toContain("unbanned successfully");
|
||||
expect(mockBannedUser.unbanUser).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should return 404 for non-existent user", async () => {
|
||||
mockUserFindByPk.mockResolvedValue(null);
|
||||
|
||||
const response = await request(app)
|
||||
.post("/users/admin/999/unban");
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(response.body).toEqual({ error: "User not found" });
|
||||
});
|
||||
|
||||
it("should return 400 when user is not banned", async () => {
|
||||
const notBannedUser = { ...mockBannedUser, isBanned: false };
|
||||
mockUserFindByPk.mockResolvedValue(notBannedUser);
|
||||
|
||||
const response = await request(app)
|
||||
.post("/users/admin/2/unban");
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.body).toEqual({ error: "User is not banned" });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user