Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 74 additions & 62 deletions server/src/services/__tests__/admin.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('admin.service', () => {
findMany: jest.fn(),
update: jest.fn(),
updateMany: jest.fn(),
delete: jest.fn(),
delete: jest.fn(),
};

const mockFindOne = jest.fn();
Expand All @@ -53,15 +53,16 @@ describe('admin.service', () => {
caster<jest.Mock>(getReportCommentRepository).mockReturnValue(mockReportCommentRepository);
});

const getStrapi = () => caster<StrapiContext>({
strapi: {
contentType: jest.fn().mockReturnValue({ attributes: {} }),
contentTypes: {},
documents: jest.fn().mockReturnValue({
findOne: mockFindOne,
}),
},
});
const getStrapi = () =>
caster<StrapiContext>({
strapi: {
contentType: jest.fn().mockReturnValue({ attributes: {} }),
contentTypes: {},
documents: jest.fn().mockReturnValue({
findOne: mockFindOne,
}),
},
});

const getService = (strapi: StrapiContext) => adminService(strapi);

Expand All @@ -73,21 +74,19 @@ describe('admin.service', () => {
{ id: 1, content: 'Comment 1' },
{ id: 2, content: 'Comment 2' },
];
const mockRelatedEntities = [
{ uid: 'api::test.test', documentId: '1', title: 'Test 1' },
];
const mockRelatedEntities = [{ uid: 'api::test.test', documentId: '1', title: 'Test 1' }];

mockCommentRepository.findWithCount.mockResolvedValue({
results: mockComments,
pagination: { page: 1, pageSize: 10, total: 2 },
});
mockCommonService.findRelatedEntitiesFor.mockResolvedValue(mockRelatedEntities);
mockCommonService.sanitizeCommentEntity.mockImplementation(comment => comment);
mockCommonService.mergeRelatedEntityTo.mockImplementation(comment => comment);
mockCommonService.sanitizeCommentEntity.mockImplementation((comment) => comment);
mockCommonService.mergeRelatedEntityTo.mockImplementation((comment) => comment);

const result = await service.findAll({
page: 1,
pageSize: 10,
const result = await service.findAll({
page: 1,
pageSize: 10,
orderBy: 'created:DESC',
_q: 'test search',
});
Expand Down Expand Up @@ -133,10 +132,10 @@ describe('admin.service', () => {
pagination: { page: 1, pageSize: 10, total: 2 },
});
mockCommentRepository.findMany.mockResolvedValue([]);
mockCommonService.sanitizeCommentEntity.mockImplementation(comment => comment);
mockCommonService.sanitizeCommentEntity.mockImplementation((comment) => comment);

const result = await service.findReports({
page: 1,
const result = await service.findReports({
page: 1,
pageSize: 10,
orderBy: 'custoOrder:DESC',
});
Expand All @@ -147,7 +146,13 @@ describe('admin.service', () => {
orderBy: { custoOrder: 'DESC' },
page: 1,
pageSize: 10,
populate: ['related'],
populate: {
related: {
populate: {
authorUser: true,
},
},
},
where: {
resolved: {
$notNull: true,
Expand All @@ -169,10 +174,10 @@ describe('admin.service', () => {
pagination: { page: 1, pageSize: 10, total: 2 },
});
mockCommentRepository.findMany.mockResolvedValue([]);
mockCommonService.sanitizeCommentEntity.mockImplementation(comment => comment);
mockCommonService.sanitizeCommentEntity.mockImplementation((comment) => comment);

const result = await service.findReports({
page: 1,
const result = await service.findReports({
page: 1,
pageSize: 10,
orderBy: 'resolved:ASC',
});
Expand All @@ -182,7 +187,13 @@ describe('admin.service', () => {
orderBy: { resolved: 'ASC' },
page: 1,
pageSize: 10,
populate: ['related'],
populate: {
related: {
populate: {
authorUser: true,
},
},
},
where: {
resolved: {
$notNull: true,
Expand All @@ -196,19 +207,22 @@ describe('admin.service', () => {
it('should return comment with its thread', async () => {
const strapi = getStrapi();
const service = getService(strapi);
const mockComment = {
id: 1,
const mockComment = {
id: 1,
content: 'Test comment',
related: 'api::test.test:1',
threadOf: null,
};
const mockRelatedEntity = { id: 1, title: 'Test', uid: 'api::test.test' };

mockCommentRepository.findOne.mockResolvedValue(mockComment);
mockCommonService.parseRelationString.mockReturnValue({ uid: 'api::test.test', relatedId: '1' });
mockCommonService.parseRelationString.mockReturnValue({
uid: 'api::test.test',
relatedId: '1',
});
mockFindOne.mockResolvedValue(mockRelatedEntity);
mockCommonService.findAllInHierarchy.mockResolvedValue([]);
mockCommonService.sanitizeCommentEntity.mockImplementation(comment => comment);
mockCommonService.sanitizeCommentEntity.mockImplementation((comment) => comment);

const result = await service.findOneAndThread({ id: 1 });

Expand Down Expand Up @@ -239,10 +253,7 @@ describe('admin.service', () => {
const result = await service.changeBlockedComment(1);

expect(result.blocked).toBe(true);
expect(mockCommonService.updateComment).toHaveBeenCalledWith(
{ id: 1 },
{ blocked: true }
);
expect(mockCommonService.updateComment).toHaveBeenCalledWith({ id: 1 }, { blocked: true });
});
});

Expand All @@ -253,13 +264,13 @@ describe('admin.service', () => {
const mockComment = { id: 1, blocked: false, blockedThread: false };

mockCommonService.findOne.mockResolvedValue(mockComment);
mockCommonService.updateComment.mockResolvedValue({
...mockComment,
blocked: true,
blockedThread: true
mockCommonService.updateComment.mockResolvedValue({
...mockComment,
blocked: true,
blockedThread: true,
});
mockCommonService.modifiedNestedNestedComments.mockResolvedValue(true);
mockCommonService.sanitizeCommentEntity.mockImplementation(comment => comment);
mockCommonService.sanitizeCommentEntity.mockImplementation((comment) => comment);

const result = await service.blockCommentThread(1);

Expand All @@ -279,7 +290,7 @@ describe('admin.service', () => {
...mockComment,
approvalStatus: APPROVAL_STATUS.APPROVED,
});
mockCommonService.sanitizeCommentEntity.mockImplementation(comment => comment);
mockCommonService.sanitizeCommentEntity.mockImplementation((comment) => comment);

const result = await service.approveComment(1);

Expand Down Expand Up @@ -353,11 +364,13 @@ describe('admin.service', () => {

mockCommentRepository.findOne.mockResolvedValue(null);

await expect(service.postComment({
id: 1,
content: 'Admin reply',
author: { id: 1, email: 'admin@test.com' },
})).rejects.toThrow('Not found');
await expect(
service.postComment({
id: 1,
content: 'Admin reply',
author: { id: 1, email: 'admin@test.com' },
})
).rejects.toThrow('Not found');
});
});

Expand Down Expand Up @@ -407,7 +420,7 @@ describe('admin.service', () => {
...mockComment,
approvalStatus: APPROVAL_STATUS.REJECTED,
});
mockCommonService.sanitizeCommentEntity.mockImplementation(comment => comment);
mockCommonService.sanitizeCommentEntity.mockImplementation((comment) => comment);

const result = await service.rejectComment(1);

Expand Down Expand Up @@ -438,7 +451,7 @@ describe('admin.service', () => {
...mockComment,
content: 'Updated content',
});
mockCommonService.sanitizeCommentEntity.mockImplementation(comment => comment);
mockCommonService.sanitizeCommentEntity.mockImplementation((comment) => comment);

const result = await service.updateComment({
id: 1,
Expand All @@ -458,10 +471,12 @@ describe('admin.service', () => {

mockCommentRepository.update.mockResolvedValue(null);

await expect(service.updateComment({
id: 1,
content: 'Updated content',
})).rejects.toThrow('Not found');
await expect(
service.updateComment({
id: 1,
content: 'Updated content',
})
).rejects.toThrow('Not found');
});
});

Expand Down Expand Up @@ -503,16 +518,16 @@ describe('admin.service', () => {
it('should throw error when reports have invalid comment relation', async () => {
const strapi = getStrapi();
const service = getService(strapi);
const mockReports = [
{ id: 1, related: { id: 1 } },
];
const mockReports = [{ id: 1, related: { id: 1 } }];

mockReportCommentRepository.findMany.mockResolvedValue(mockReports);

await expect(service.resolveCommentMultipleAbuseReports({
id: 1,
reportIds: [1, 2],
})).rejects.toThrow('At least one of selected reports got invalid comment entity relation');
await expect(
service.resolveCommentMultipleAbuseReports({
id: 1,
reportIds: [1, 2],
})
).rejects.toThrow('At least one of selected reports got invalid comment entity relation');
});
});

Expand Down Expand Up @@ -551,10 +566,7 @@ describe('admin.service', () => {
it('should resolve all abuse reports for a comment thread', async () => {
const strapi = getStrapi();
const service = getService(strapi);
const mockThreadComments = [
{ id: 2 },
{ id: 3 },
];
const mockThreadComments = [{ id: 2 }, { id: 3 }];

mockCommentRepository.findMany.mockResolvedValue(mockThreadComments);
mockReportCommentRepository.updateMany.mockResolvedValue({ count: 3 });
Expand Down
11 changes: 9 additions & 2 deletions server/src/services/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,21 @@ export default ({ strapi }: StrapiContext) => {
pageSize,
_q,
);
const defaultAuthorUserPopulate = getDefaultAuthorPopulate(strapi);
const { pagination, results } = await getReportCommentRepository(strapi).findPage({
...params,
populate: ['related'],
populate:
{
related: {
populate: {
authorUser: defaultAuthorUserPopulate,
}
}
},
});

const reportCommentsIds = results.map((entity) => typeof entity.related === 'object' ? entity.related.id : null).filter(Boolean);

const defaultAuthorUserPopulate = getDefaultAuthorPopulate(strapi);
const commentsThreads = await getCommentRepository(strapi).findMany({
where: {
threadOf: reportCommentsIds,
Expand Down
10 changes: 5 additions & 5 deletions server/src/validators/repositories/comment.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fileInfoSchema = z.object({
hash: z.string(),
});

const avatarSchema = z.object({
export const avatarSchema = z.object({
id: z.number(),
...fileInfoSchema.shape,
formats: z
Expand Down Expand Up @@ -38,10 +38,10 @@ export const dbBaseCommentSchema = z.object({
authorEmail: z.string().email().nullable(),
authorAvatar: z.string().nullable(),
authorUser: z.union([
z.string(),
z.object({
id: z.number(),
username: z.string(),
z.string(),
z.object({
id: z.number(),
username: z.string(),
email: z.string().email(),
avatar: avatarSchema,
})
Expand Down
18 changes: 13 additions & 5 deletions server/src/validators/repositories/reports.validator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { z } from 'zod';
import { dbBaseCommentSchema } from './comment.schema';

import { dbBaseCommentSchema, avatarSchema } from './comment.schema';
import { paginationSchema } from './utils';

const relatedSchema = z.object({
Expand All @@ -10,10 +9,19 @@ const relatedSchema = z.object({
blocked: z.boolean(),
blockedThread: z.boolean(),
blockReason: z.string().nullable(),
authorId: z.string(),
authorName: z.string(),
authorEmail: z.string(),
authorId: z.string().nullable(),
authorName: z.string().nullable(),
authorEmail: z.string().nullable(),
authorAvatar: z.string().nullable(),
authorUser: z.union([
z.string(),
z.object({
id: z.number(),
username: z.string(),
email: z.string().email(),
avatar: avatarSchema,
})
]).optional().nullable(),
isAdminComment: z.boolean().nullable(),
removed: z.boolean().nullable(),
approvalStatus: z.string().nullable(),
Expand Down