test: comprehensive test suites and quality gates pass across backend, storefront and admin
- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
This commit is contained in:
1 parent
1e73715169
commit
ddbf1ca026
439 files changed
+32911
-6107
No files matched your search
@@ -1,8 +1,10 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { UsersService } from './users.service';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { UsersService } from "./users.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { BadRequestException, NotFoundException } from "@nestjs/common";
|
||||
import * as bcrypt from "bcryptjs";
|
||||
|
||||
describe('UsersService', () => {
|
||||
describe("UsersService", () => {
|
||||
let service: UsersService;
|
||||
let prisma: PrismaService;
|
||||
|
||||
@@ -35,98 +37,160 @@ describe('UsersService', () => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
it('should call prisma findUnique in findById', async () => {
|
||||
const mockUser = { id: 'user-id' };
|
||||
mockPrisma.user.findUnique.mockResolvedValue(mockUser);
|
||||
const result = await service.findById('user-id');
|
||||
expect(prisma.user.findUnique).toHaveBeenCalled();
|
||||
expect(result).toEqual({ ...mockUser, hasPassword: false });
|
||||
describe("findById", () => {
|
||||
it("should return null when user does not exist", async () => {
|
||||
mockPrisma.user.findUnique.mockResolvedValue(null);
|
||||
const res = await service.findById("missing-id");
|
||||
expect(res).toBeNull();
|
||||
});
|
||||
|
||||
it("should return safe user profile hiding password hash but indicating hasPassword", async () => {
|
||||
mockPrisma.user.findUnique.mockResolvedValue({
|
||||
id: "u-1",
|
||||
mobile: "09121111111",
|
||||
password: "hashed-password-string",
|
||||
firstName: "Parsa",
|
||||
});
|
||||
const res = await service.findById("u-1");
|
||||
expect(res).not.toHaveProperty("password");
|
||||
expect(res?.hasPassword).toBe(true);
|
||||
expect(res?.firstName).toBe("Parsa");
|
||||
});
|
||||
});
|
||||
|
||||
it('should call prisma update in update', async () => {
|
||||
const data = { firstName: 'Ali' };
|
||||
mockPrisma.user.update.mockResolvedValue({ id: 'user-id', ...data });
|
||||
const result = await service.update('user-id', data);
|
||||
expect(prisma.user.update).toHaveBeenCalled();
|
||||
expect(result.firstName).toBe('Ali');
|
||||
describe("update profile & security checks", () => {
|
||||
it("should throw NotFoundException if user is not found", async () => {
|
||||
mockPrisma.user.findUnique.mockResolvedValue(null);
|
||||
await expect(
|
||||
service.update("u-missing", { firstName: "Test" }),
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it("should throw BadRequestException if new mobile is already taken by another user", async () => {
|
||||
mockPrisma.user.findUnique
|
||||
.mockResolvedValueOnce({ id: "u-1", mobile: "09121111111" }) // current user
|
||||
.mockResolvedValueOnce({ id: "u-2", mobile: "09122222222" }); // conflict user
|
||||
|
||||
await expect(
|
||||
service.update("u-1", { mobile: "09122222222" }),
|
||||
).rejects.toThrow("این شماره موبایل قبلاً توسط کاربر دیگری ثبت شده است");
|
||||
});
|
||||
|
||||
it("should throw BadRequestException if new email is already taken by another user", async () => {
|
||||
mockPrisma.user.findUnique
|
||||
.mockResolvedValueOnce({ id: "u-1", email: "user1@canina.ir" }) // current user
|
||||
.mockResolvedValueOnce({ id: "u-2", email: "conflict@canina.ir" }); // conflict user
|
||||
|
||||
await expect(
|
||||
service.update("u-1", { email: "conflict@canina.ir" }),
|
||||
).rejects.toThrow("این آدرس ایمیل قبلاً توسط کاربر دیگری ثبت شده است");
|
||||
});
|
||||
|
||||
it("should require currentPassword if user already has password set and attempts password change", async () => {
|
||||
mockPrisma.user.findUnique.mockResolvedValue({
|
||||
id: "u-1",
|
||||
password: "existing-hash",
|
||||
});
|
||||
|
||||
await expect(
|
||||
service.update("u-1", { password: "new-strong-password" }),
|
||||
).rejects.toThrow(
|
||||
"برای تغییر رمز عبور، وارد کردن رمز عبور فعلی الزامی است",
|
||||
);
|
||||
});
|
||||
|
||||
it("should reject password update if currentPassword does not match existing password", async () => {
|
||||
const hashedOld = await bcrypt.hash("correct-old-password", 10);
|
||||
mockPrisma.user.findUnique.mockResolvedValue({
|
||||
id: "u-1",
|
||||
password: hashedOld,
|
||||
});
|
||||
|
||||
await expect(
|
||||
service.update("u-1", {
|
||||
currentPassword: "wrong-password",
|
||||
password: "new-strong-password",
|
||||
}),
|
||||
).rejects.toThrow("رمز عبور فعلی اشتباه است");
|
||||
});
|
||||
|
||||
it("should update password and return safe user when currentPassword matches", async () => {
|
||||
const hashedOld = await bcrypt.hash("correct-old-password", 10);
|
||||
mockPrisma.user.findUnique.mockResolvedValue({
|
||||
id: "u-1",
|
||||
password: hashedOld,
|
||||
});
|
||||
mockPrisma.user.update.mockResolvedValue({
|
||||
id: "u-1",
|
||||
firstName: "Parsa",
|
||||
password: "new-hashed-password",
|
||||
});
|
||||
|
||||
const res = await service.update("u-1", {
|
||||
currentPassword: "correct-old-password",
|
||||
password: "new-password-123",
|
||||
});
|
||||
|
||||
expect(res).not.toHaveProperty("password");
|
||||
expect(res?.hasPassword).toBe(true);
|
||||
expect(mockPrisma.user.update).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('should addAddress', async () => {
|
||||
const addressData = {
|
||||
title: 'Home',
|
||||
receptorName: 'Ali',
|
||||
phone: '09123456789',
|
||||
province: 'Tehran',
|
||||
city: 'Tehran',
|
||||
detail: 'Street 1',
|
||||
zipCode: '1234567890',
|
||||
isDefault: true,
|
||||
};
|
||||
mockPrisma.userAddress.create.mockResolvedValue({
|
||||
id: 'addr-id',
|
||||
...addressData,
|
||||
describe("address management", () => {
|
||||
it("should addAddress and unset previous default addresses if isDefault is true", async () => {
|
||||
const addressData = {
|
||||
title: "Home",
|
||||
receptorName: "Parsa",
|
||||
phone: "09121111111",
|
||||
province: "Tehran",
|
||||
city: "Tehran",
|
||||
detail: "Street 1",
|
||||
zipCode: "1234567890",
|
||||
isDefault: true,
|
||||
};
|
||||
mockPrisma.userAddress.create.mockResolvedValue({
|
||||
id: "addr-id",
|
||||
...addressData,
|
||||
});
|
||||
|
||||
const result = await service.addAddress("user-id", addressData);
|
||||
expect(prisma.userAddress.updateMany).toHaveBeenCalledWith({
|
||||
where: { userId: "user-id" },
|
||||
data: { isDefault: false },
|
||||
});
|
||||
expect(prisma.userAddress.create).toHaveBeenCalled();
|
||||
expect(result.id).toBe("addr-id");
|
||||
});
|
||||
|
||||
const result = await service.addAddress('user-id', addressData);
|
||||
expect(prisma.userAddress.updateMany).toHaveBeenCalledWith({
|
||||
where: { userId: 'user-id' },
|
||||
data: { isDefault: false },
|
||||
});
|
||||
expect(prisma.userAddress.create).toHaveBeenCalled();
|
||||
expect(result.id).toBe('addr-id');
|
||||
});
|
||||
|
||||
it('should updateAddress', async () => {
|
||||
const addressData = {
|
||||
title: 'Work',
|
||||
receptorName: 'Ali',
|
||||
phone: '09123456789',
|
||||
province: 'Tehran',
|
||||
city: 'Tehran',
|
||||
detail: 'Street 2',
|
||||
zipCode: '1234567890',
|
||||
isDefault: true,
|
||||
};
|
||||
mockPrisma.userAddress.update.mockResolvedValue({
|
||||
id: 'addr-id',
|
||||
...addressData,
|
||||
it("should deleteAddress ensuring scoped by userId", async () => {
|
||||
mockPrisma.userAddress.delete.mockResolvedValue({ id: "addr-id" });
|
||||
const result = await service.deleteAddress("user-id", "addr-id");
|
||||
expect(prisma.userAddress.delete).toHaveBeenCalledWith({
|
||||
where: { id: "addr-id", userId: "user-id" },
|
||||
});
|
||||
expect(result.id).toBe("addr-id");
|
||||
});
|
||||
|
||||
const result = await service.updateAddress(
|
||||
'user-id',
|
||||
'addr-id',
|
||||
addressData,
|
||||
);
|
||||
expect(prisma.userAddress.updateMany).toHaveBeenCalled();
|
||||
expect(prisma.userAddress.update).toHaveBeenCalled();
|
||||
expect(result.title).toBe('Work');
|
||||
});
|
||||
|
||||
it('should deleteAddress', async () => {
|
||||
mockPrisma.userAddress.delete.mockResolvedValue({ id: 'addr-id' });
|
||||
const result = await service.deleteAddress('user-id', 'addr-id');
|
||||
expect(prisma.userAddress.delete).toHaveBeenCalled();
|
||||
expect(result.id).toBe('addr-id');
|
||||
});
|
||||
|
||||
it('should setDefaultAddress', async () => {
|
||||
mockPrisma.userAddress.update.mockResolvedValue({
|
||||
id: 'addr-id',
|
||||
isDefault: true,
|
||||
it("should setDefaultAddress by clearing others and updating target", async () => {
|
||||
mockPrisma.userAddress.update.mockResolvedValue({
|
||||
id: "addr-id",
|
||||
isDefault: true,
|
||||
});
|
||||
const result = await service.setDefaultAddress("user-id", "addr-id");
|
||||
expect(prisma.userAddress.updateMany).toHaveBeenCalledWith({
|
||||
where: { userId: "user-id" },
|
||||
data: { isDefault: false },
|
||||
});
|
||||
expect(prisma.userAddress.update).toHaveBeenCalledWith({
|
||||
where: { id: "addr-id", userId: "user-id" },
|
||||
data: { isDefault: true },
|
||||
});
|
||||
expect(result.isDefault).toBe(true);
|
||||
});
|
||||
const result = await service.setDefaultAddress('user-id', 'addr-id');
|
||||
expect(prisma.userAddress.updateMany).toHaveBeenCalledWith({
|
||||
where: { userId: 'user-id' },
|
||||
data: { isDefault: false },
|
||||
});
|
||||
expect(prisma.userAddress.update).toHaveBeenCalledWith({
|
||||
where: { id: 'addr-id', userId: 'user-id' },
|
||||
data: { isDefault: true },
|
||||
});
|
||||
expect(result.isDefault).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user