Files
canina/backend/src/admin/admin-users-list.spec.ts
T

73 lines
3.4 KiB
TypeScript

import { Test } from "@nestjs/testing";
import { ExecutionContext } from "@nestjs/common";
import { firstValueFrom, of } from "rxjs";
import { AdminService } from "./admin.service";
import { AdminController } from "./admin.controller";
import { PrismaService } from "../prisma/prisma.service";
import { RedisService } from "../redis/redis.service";
import { RevalidationService } from "../common/revalidation/revalidation.service";
import { DecimalInterceptor } from "../common/interceptors/decimal.interceptor";
import { Prisma } from "@prisma/client";
describe("admin users list contract", () => {
const prisma = { user: { findMany: jest.fn(), count: jest.fn() } };
let service: AdminService;
beforeEach(async () => {
jest.clearAllMocks();
const module = await Test.createTestingModule({
providers: [
AdminService,
{ provide: PrismaService, useValue: prisma },
{ provide: RedisService, useValue: {} },
{ provide: RevalidationService, useValue: {} },
],
}).compile();
service = module.get(AdminService);
});
it("returns only allowlisted list fields with aggregates through the controller and decimal interceptor", async () => {
// Deliberately return extra fields even if the query selects safely.
prisma.user.findMany.mockResolvedValue([{
id: "fixture-user", firstName: "Fixture", lastName: "User",
mobile: "fixture-mobile", email: null, role: "ADMIN",
walletBalance: new Prisma.Decimal(0), charityDonationTotal: new Prisma.Decimal(12),
createdAt: new Date("2026-01-01T00:00:00Z"),
password: "test-only-password-hash", passwordHash: "test-only-hash",
refreshToken: "test-only-token", futureSecret: "must-not-leak",
walletTransactions: [{ transactionReference: "private-reference" }],
addresses: [], orders: [{ totalAmount: new Prisma.Decimal(100) }, { totalAmount: 25 }],
_count: { orders: 3, reviews: 2, blogComments: 1, pets: 4 },
}]);
prisma.user.count.mockResolvedValue(11);
const controller = new AdminController(service, {} as never);
const result = await controller.getUsers({ page: 2, limit: 10 });
const body = await firstValueFrom(new DecimalInterceptor().intercept({} as ExecutionContext, { handle: () => of(result) }));
expect(body).toEqual({
data: [{
id: "fixture-user", firstName: "Fixture", lastName: "User", mobile: "fixture-mobile",
email: null, role: "ADMIN", walletBalance: "0", charityDonationTotal: "12",
createdAt: new Date("2026-01-01T00:00:00Z"), addresses: [],
totalSpent: 125, ordersCount: 3, reviewsCount: 3, petsCount: 4,
}],
meta: { total: 11, page: 2, limit: 10, lastPage: 2 },
});
const query = prisma.user.findMany.mock.calls[0][0];
expect(query.select).toBeDefined();
expect(query.select.password).toBeUndefined();
expect(query.include).toBeUndefined();
expect(query.skip).toBe(10);
});
it("preserves the empty paginated list contract and filters", async () => {
prisma.user.findMany.mockResolvedValue([]);
prisma.user.count.mockResolvedValue(0);
expect(await service.getUsers({ search: "fixture", role: "ADMIN" })).toEqual({
data: [], meta: { total: 0, page: 1, limit: 10, lastPage: 1 },
});
expect(prisma.user.findMany).toHaveBeenCalledWith(expect.objectContaining({
where: expect.objectContaining({ role: "ADMIN", OR: expect.any(Array) }),
}));
});
});