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
@@ -0,0 +1,479 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { BlogsService } from "./blogs.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { RevalidationService } from "../common/revalidation/revalidation.service";
|
||||
import { NotFoundException } from "@nestjs/common";
|
||||
|
||||
describe("Admin BlogsService", () => {
|
||||
let service: BlogsService;
|
||||
|
||||
const mockPrisma = {
|
||||
blog: {
|
||||
findMany: jest.fn(),
|
||||
count: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
},
|
||||
blogCategory: {
|
||||
upsert: jest.fn(),
|
||||
findMany: jest.fn(),
|
||||
create: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
},
|
||||
blogTag: {
|
||||
findMany: jest.fn(),
|
||||
create: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
},
|
||||
blogComment: {
|
||||
findMany: jest.fn(),
|
||||
count: jest.fn(),
|
||||
findUnique: jest.fn(),
|
||||
update: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
},
|
||||
user: {
|
||||
findMany: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
const mockRevalidationService = {
|
||||
revalidateBlog: jest.fn().mockResolvedValue(true),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
BlogsService,
|
||||
{ provide: PrismaService, useValue: mockPrisma },
|
||||
{ provide: RevalidationService, useValue: mockRevalidationService },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<BlogsService>(BlogsService);
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
service.onModuleDestroy();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("onModuleInit and seedDefaultCategories", () => {
|
||||
it("should seed default categories and set interval timer", async () => {
|
||||
mockPrisma.blogCategory.upsert.mockResolvedValue({});
|
||||
mockPrisma.blog.findMany.mockResolvedValue([]);
|
||||
|
||||
await service.onModuleInit();
|
||||
|
||||
expect(mockPrisma.blogCategory.upsert).toHaveBeenCalledTimes(5);
|
||||
expect(mockPrisma.blog.findMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
status: "SCHEDULED",
|
||||
scheduledAt: { lte: expect.any(Date) },
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
slug: true,
|
||||
title: true,
|
||||
scheduledAt: true,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("getBlogs", () => {
|
||||
it("should return paginated blogs without filters", async () => {
|
||||
const mockBlogs = [{ id: "b1", title: "Post 1" }];
|
||||
mockPrisma.blog.findMany.mockResolvedValue(mockBlogs);
|
||||
mockPrisma.blog.count.mockResolvedValue(1);
|
||||
|
||||
const result = await service.getBlogs({});
|
||||
|
||||
expect(mockPrisma.blog.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
skip: 0,
|
||||
take: 10,
|
||||
where: {},
|
||||
}),
|
||||
);
|
||||
expect(result).toEqual({
|
||||
data: mockBlogs,
|
||||
meta: { total: 1, page: 1, limit: 10, lastPage: 1 },
|
||||
});
|
||||
});
|
||||
|
||||
it("should apply search, status, and categoryId filters", async () => {
|
||||
mockPrisma.blog.findMany.mockResolvedValue([]);
|
||||
mockPrisma.blog.count.mockResolvedValue(0);
|
||||
|
||||
await service.getBlogs({
|
||||
page: 2,
|
||||
limit: 5,
|
||||
search: "canine",
|
||||
status: "PUBLISHED",
|
||||
categoryId: "cat-1",
|
||||
});
|
||||
|
||||
expect(mockPrisma.blog.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
skip: 5,
|
||||
take: 5,
|
||||
where: {
|
||||
OR: [
|
||||
{ title: { contains: "canine", mode: "insensitive" } },
|
||||
{ excerpt: { contains: "canine", mode: "insensitive" } },
|
||||
{ slug: { contains: "canine", mode: "insensitive" } },
|
||||
],
|
||||
status: "PUBLISHED",
|
||||
categoryId: "cat-1",
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getBlogById", () => {
|
||||
it("should throw NotFoundException if blog not found", async () => {
|
||||
mockPrisma.blog.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(service.getBlogById("unknown-id")).rejects.toThrow(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
it("should return blog if found", async () => {
|
||||
const mockBlog = { id: "b1", title: "Test Blog" };
|
||||
mockPrisma.blog.findUnique.mockResolvedValue(mockBlog);
|
||||
|
||||
const result = await service.getBlogById("b1");
|
||||
expect(result).toEqual(mockBlog);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createBlog", () => {
|
||||
it("should create an immediately published blog with tags and trigger revalidation", async () => {
|
||||
const blogData = {
|
||||
title: "New Health Post",
|
||||
slug: "new-health-post",
|
||||
content: "Content here",
|
||||
tagNames: ["تغذیه سالم", "Dog Food"],
|
||||
categoryId: "cat-1",
|
||||
readingTime: 5,
|
||||
};
|
||||
|
||||
const createdBlog = { id: "blog-1", ...blogData };
|
||||
mockPrisma.blog.create.mockResolvedValue(createdBlog);
|
||||
|
||||
const result = await service.createBlog(blogData, "user-admin-1");
|
||||
|
||||
expect(mockPrisma.blog.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
title: "New Health Post",
|
||||
status: "PUBLISHED",
|
||||
isPublished: true,
|
||||
author: { connect: { id: "user-admin-1" } },
|
||||
category: { connect: { id: "cat-1" } },
|
||||
readingTime: 5,
|
||||
tags: {
|
||||
connectOrCreate: [
|
||||
{
|
||||
where: { name: "تغذیه سالم" },
|
||||
create: { name: "تغذیه سالم", slug: "تغذیه-سالم" },
|
||||
},
|
||||
{
|
||||
where: { name: "Dog Food" },
|
||||
create: { name: "Dog Food", slug: "dog-food" },
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
||||
"new-health-post",
|
||||
);
|
||||
expect(result).toEqual(createdBlog);
|
||||
});
|
||||
|
||||
it("should create a SCHEDULED blog if scheduledAt is in the future", async () => {
|
||||
const futureDate = new Date(Date.now() + 86400000).toISOString();
|
||||
const blogData = {
|
||||
title: "Future Post",
|
||||
slug: "future-post",
|
||||
scheduledAt: futureDate,
|
||||
};
|
||||
|
||||
mockPrisma.blog.create.mockResolvedValue({
|
||||
id: "b-fut",
|
||||
slug: "future-post",
|
||||
});
|
||||
|
||||
await service.createBlog(blogData, "user-admin-1");
|
||||
|
||||
expect(mockPrisma.blog.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
status: "SCHEDULED",
|
||||
isPublished: false,
|
||||
publishedAt: null,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateBlog", () => {
|
||||
it("should throw NotFoundException if blog does not exist", async () => {
|
||||
mockPrisma.blog.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.updateBlog("b-unknown", { title: "Updated" }),
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it("should update blog, handle future scheduledAt, and revalidate", async () => {
|
||||
const existing = { id: "b1", slug: "my-post", title: "Old Title" };
|
||||
mockPrisma.blog.findUnique.mockResolvedValue(existing);
|
||||
|
||||
const futureDate = new Date(Date.now() + 60000).toISOString();
|
||||
const updated = { ...existing, title: "New Title", status: "SCHEDULED" };
|
||||
mockPrisma.blog.update.mockResolvedValue(updated);
|
||||
|
||||
const result = await service.updateBlog("b1", {
|
||||
title: "New Title",
|
||||
scheduledAt: futureDate,
|
||||
tagNames: ["تگ جدید"],
|
||||
});
|
||||
|
||||
expect(mockPrisma.blog.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { id: "b1" },
|
||||
data: expect.objectContaining({
|
||||
title: "New Title",
|
||||
status: "SCHEDULED",
|
||||
isPublished: false,
|
||||
tags: expect.objectContaining({
|
||||
set: [],
|
||||
connectOrCreate: expect.any(Array),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
||||
"my-post",
|
||||
);
|
||||
expect(result).toEqual(updated);
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteBlog", () => {
|
||||
it("should throw NotFoundException if blog not found", async () => {
|
||||
mockPrisma.blog.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(service.deleteBlog("b-none")).rejects.toThrow(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
|
||||
it("should delete blog and trigger revalidation", async () => {
|
||||
const mockBlog = { id: "b1", slug: "del-slug" };
|
||||
mockPrisma.blog.findUnique.mockResolvedValue(mockBlog);
|
||||
mockPrisma.blog.delete.mockResolvedValue(mockBlog);
|
||||
|
||||
const result = await service.deleteBlog("b1");
|
||||
|
||||
expect(mockPrisma.blog.delete).toHaveBeenCalledWith({
|
||||
where: { id: "b1" },
|
||||
});
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
||||
"del-slug",
|
||||
);
|
||||
expect(result).toEqual(mockBlog);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Authors", () => {
|
||||
it("should fetch authors list", async () => {
|
||||
const users = [{ id: "u1", firstName: "Admin", role: "ADMIN" }];
|
||||
mockPrisma.user.findMany.mockResolvedValue(users);
|
||||
|
||||
const result = await service.getAuthors();
|
||||
expect(result).toEqual(users);
|
||||
expect(mockPrisma.user.findMany).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Blog Categories Management", () => {
|
||||
it("should get categories", async () => {
|
||||
mockPrisma.blogCategory.findMany.mockResolvedValue([]);
|
||||
const res = await service.getBlogCategories();
|
||||
expect(res).toEqual([]);
|
||||
expect(mockPrisma.blogCategory.findMany).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should create blog category", async () => {
|
||||
const cat = { name: "Cat 1", slug: "cat-1" };
|
||||
mockPrisma.blogCategory.create.mockResolvedValue(cat);
|
||||
|
||||
const res = await service.createBlogCategory(cat);
|
||||
expect(res).toEqual(cat);
|
||||
expect(mockPrisma.blogCategory.create).toHaveBeenCalledWith({
|
||||
data: cat,
|
||||
});
|
||||
});
|
||||
|
||||
it("should update blog category", async () => {
|
||||
mockPrisma.blogCategory.update.mockResolvedValue({
|
||||
id: "c1",
|
||||
name: "Updated",
|
||||
});
|
||||
const res = await service.updateBlogCategory("c1", { name: "Updated" });
|
||||
expect(res.name).toBe("Updated");
|
||||
});
|
||||
|
||||
it("should delete blog category", async () => {
|
||||
mockPrisma.blogCategory.delete.mockResolvedValue({ id: "c1" });
|
||||
const res = await service.deleteBlogCategory("c1");
|
||||
expect(res.id).toBe("c1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Blog Tags Management", () => {
|
||||
it("should get tags", async () => {
|
||||
mockPrisma.blogTag.findMany.mockResolvedValue([]);
|
||||
const res = await service.getBlogTags();
|
||||
expect(res).toEqual([]);
|
||||
});
|
||||
|
||||
it("should create blog tag with custom or generated slug", async () => {
|
||||
mockPrisma.blogTag.create.mockImplementation(({ data }) => data);
|
||||
|
||||
const res = await service.createBlogTag({ name: "سلامت سگ" });
|
||||
expect(res.slug).toBe("سلامت-سگ");
|
||||
expect(res.name).toBe("سلامت سگ");
|
||||
});
|
||||
|
||||
it("should delete blog tag", async () => {
|
||||
mockPrisma.blogTag.delete.mockResolvedValue({ id: "t1" });
|
||||
const res = await service.deleteBlogTag("t1");
|
||||
expect(res.id).toBe("t1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Blog Comments Management", () => {
|
||||
it("should get comments with pagination and filters", async () => {
|
||||
mockPrisma.blogComment.findMany.mockResolvedValue([]);
|
||||
mockPrisma.blogComment.count.mockResolvedValue(0);
|
||||
|
||||
const res = await service.getComments({
|
||||
page: 1,
|
||||
limit: 10,
|
||||
status: "PENDING",
|
||||
blogId: "b1",
|
||||
});
|
||||
expect(res.meta.total).toBe(0);
|
||||
expect(mockPrisma.blogComment.findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { status: "PENDING", blogId: "b1" },
|
||||
skip: 0,
|
||||
take: 10,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("should update comment status and trigger revalidation", async () => {
|
||||
mockPrisma.blogComment.findUnique.mockResolvedValue({
|
||||
id: "comm-1",
|
||||
blog: { slug: "target-blog" },
|
||||
});
|
||||
mockPrisma.blogComment.update.mockResolvedValue({
|
||||
id: "comm-1",
|
||||
status: "APPROVED",
|
||||
});
|
||||
|
||||
const res = await service.updateCommentStatus("comm-1", "APPROVED");
|
||||
expect(res.status).toBe("APPROVED");
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
||||
"target-blog",
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if comment to update is not found", async () => {
|
||||
mockPrisma.blogComment.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
service.updateCommentStatus("c-none", "REJECTED"),
|
||||
).rejects.toThrow(NotFoundException);
|
||||
});
|
||||
|
||||
it("should delete comment and trigger revalidation", async () => {
|
||||
mockPrisma.blogComment.findUnique.mockResolvedValue({
|
||||
id: "comm-2",
|
||||
blog: { slug: "target-blog" },
|
||||
});
|
||||
mockPrisma.blogComment.delete.mockResolvedValue({ id: "comm-2" });
|
||||
|
||||
const res = await service.deleteComment("comm-2");
|
||||
expect(res.id).toBe("comm-2");
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
||||
"target-blog",
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw NotFoundException if comment to delete is not found", async () => {
|
||||
mockPrisma.blogComment.findUnique.mockResolvedValue(null);
|
||||
|
||||
await expect(service.deleteComment("c-none")).rejects.toThrow(
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("publishDueScheduledBlogs", () => {
|
||||
it("should return 0 when no due scheduled blogs found", async () => {
|
||||
mockPrisma.blog.findMany.mockResolvedValue([]);
|
||||
|
||||
const count = await service.publishDueScheduledBlogs();
|
||||
expect(count).toBe(0);
|
||||
expect(mockPrisma.blog.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should publish due scheduled blogs and revalidate", async () => {
|
||||
const scheduledDate = new Date(Date.now() - 3600000);
|
||||
const due = [
|
||||
{
|
||||
id: "b1",
|
||||
slug: "due-blog-1",
|
||||
title: "Due Blog 1",
|
||||
scheduledAt: scheduledDate,
|
||||
},
|
||||
];
|
||||
mockPrisma.blog.findMany.mockResolvedValue(due);
|
||||
mockPrisma.blog.update.mockResolvedValue({});
|
||||
|
||||
const count = await service.publishDueScheduledBlogs();
|
||||
|
||||
expect(count).toBe(1);
|
||||
expect(mockPrisma.blog.update).toHaveBeenCalledWith({
|
||||
where: { id: "b1" },
|
||||
data: {
|
||||
status: "PUBLISHED",
|
||||
isPublished: true,
|
||||
publishedAt: scheduledDate,
|
||||
createdAt: scheduledDate,
|
||||
},
|
||||
});
|
||||
expect(mockRevalidationService.revalidateBlog).toHaveBeenCalledWith(
|
||||
"due-blog-1",
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user