Skip to content

Commit 662243c

Browse files
fix: Strapi user relations (#340)
* fix/auth-user-relations * fix: refactor tests stash * fix: fixed get endpoints * chore: fix tests * chore: add coverage to client.service * chore: add coverage to common.service
1 parent ad9e1b9 commit 662243c

File tree

10 files changed

+183
-70
lines changed

10 files changed

+183
-70
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "strapi-plugin-comments",
3-
"version": "3.0.16",
3+
"version": "3.1.0-beta.1",
44
"description": "Strapi - Comments plugin",
55
"strapi": {
66
"name": "comments",

server/src/controllers/__tests__/client.controller.test.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ describe('Client controller', () => {
130130
},
131131
threadOf: {
132132
populate: {
133-
authorUser: true
133+
authorUser: {
134+
populate: true,
135+
avatar: { populate: true },
136+
},
134137
}
135138
}
136139
}
@@ -179,7 +182,10 @@ describe('Client controller', () => {
179182
},
180183
threadOf: {
181184
populate: {
182-
authorUser: true
185+
authorUser: {
186+
populate: true,
187+
avatar: { populate: true },
188+
},
183189
}
184190
}
185191
}
@@ -222,7 +228,10 @@ describe('Client controller', () => {
222228
},
223229
threadOf: {
224230
populate: {
225-
authorUser: true
231+
authorUser: {
232+
populate: true,
233+
avatar: { populate: true },
234+
},
226235
}
227236
}
228237
}
@@ -259,7 +268,10 @@ describe('Client controller', () => {
259268
},
260269
threadOf: {
261270
populate: {
262-
authorUser: true
271+
authorUser: {
272+
populate: true,
273+
avatar: { populate: true },
274+
},
263275
}
264276
}
265277
}

server/src/controllers/__tests__/utils/parser.test.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ describe('Parser', () => {
2020
},
2121
threadOf: {
2222
populate: {
23-
authorUser: true,
23+
authorUser: {
24+
populate: true,
25+
avatar: {
26+
populate: true,
27+
}
28+
},
2429
},
2530
},
2631
},
@@ -77,7 +82,10 @@ describe('Parser', () => {
7782
},
7883
threadOf: {
7984
populate: {
80-
authorUser: true,
85+
authorUser: {
86+
populate: true,
87+
avatar: { populate: true },
88+
},
8189
},
8290
},
8391
},
@@ -134,7 +142,10 @@ describe('Parser', () => {
134142
},
135143
threadOf: {
136144
populate: {
137-
authorUser: true,
145+
authorUser: {
146+
populate: true,
147+
avatar: { populate: true },
148+
},
138149
},
139150
},
140151
},
@@ -203,7 +214,10 @@ describe('Parser', () => {
203214
},
204215
threadOf: {
205216
populate: {
206-
authorUser: true,
217+
authorUser: {
218+
populate: true,
219+
avatar: { populate: true },
220+
},
207221
},
208222
},
209223
},
@@ -242,7 +256,10 @@ describe('Parser', () => {
242256
},
243257
threadOf: {
244258
populate: {
245-
authorUser: true,
259+
authorUser: {
260+
populate: true,
261+
avatar: { populate: true },
262+
},
246263
},
247264
},
248265
},
@@ -287,7 +304,10 @@ describe('Parser', () => {
287304
},
288305
threadOf: {
289306
populate: {
290-
authorUser: true,
307+
authorUser: {
308+
populate: true,
309+
avatar: { populate: true },
310+
},
291311
},
292312
},
293313
},

server/src/controllers/utils/parsers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ export const flatInput = <T extends FlatInputParams>(payload: T): T => {
5151
let threadOfPopulate = {
5252
threadOf: {
5353
populate: {
54-
authorUser: true,
54+
authorUser: {
55+
populate: true,
56+
avatar: {
57+
populate: true,
58+
},
59+
},
5560
...populate,
5661
} as {
5762
authorUser: boolean | { populate: boolean };

server/src/services/__tests__/client.service.test.ts

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ jest.mock('../../utils/getPluginService', () => ({
1515
getPluginService: jest.fn(),
1616
}));
1717

18+
const defaultPopulate = {
19+
authorUser: {
20+
populate: ['avatar'],
21+
},
22+
};
23+
24+
const defaultThreadOfPopulate = {
25+
threadOf: true,
26+
authorUser: {
27+
populate: ['avatar'],
28+
},
29+
};
30+
1831
describe('client.service', () => {
1932
const mockCommonService = {
2033
parseRelationString: jest.fn(),
@@ -39,6 +52,10 @@ describe('client.service', () => {
3952
findOne: jest.fn(),
4053
});
4154

55+
const mockEmailService = {
56+
send: jest.fn(),
57+
};
58+
4259
beforeEach(() => {
4360
jest.clearAllMocks();
4461
caster<jest.Mock>(getPluginService).mockReturnValue(mockCommonService);
@@ -107,22 +124,16 @@ describe('client.service', () => {
107124
const result = await service.create(mockPayload, mockUser);
108125

109126
expect(result).toEqual(mockSanitizedEntity);
110-
expect(mockUserQuery().findOne).toHaveBeenCalledWith({
111-
where: { id: mockUser.id },
112-
populate: ['avatar'],
113-
});
114127
expect(mockCommentRepository.create).toHaveBeenCalledWith({
115128
data: {
116-
authorId: mockUser.id,
117-
authorEmail: mockUser.email,
118-
authorName: mockUser.username,
119-
authorAvatar: 'avatar-url',
129+
authorUser: mockUser.id,
120130
content: 'Test comment',
121131
related: 'api::test.test:1',
122132
approvalStatus: APPROVAL_STATUS.APPROVED,
123133
locale: 'en',
124134
threadOf: null,
125135
},
136+
populate: defaultPopulate,
126137
});
127138
});
128139

@@ -152,16 +163,14 @@ describe('client.service', () => {
152163
expect(result).toEqual(mockSanitizedEntity);
153164
expect(mockCommentRepository.create).toHaveBeenCalledWith({
154165
data: {
155-
authorId: mockUser.id,
156-
authorEmail: mockUser.email,
157-
authorName: mockUser.username,
158-
authorAvatar: 'avatar-url',
166+
authorUser: mockUser.id,
159167
content: 'Test comment',
160168
related: 'api::test.test:1',
161169
approvalStatus: APPROVAL_STATUS.PENDING,
162170
locale: 'en',
163171
threadOf: null,
164172
},
173+
populate: defaultPopulate,
165174
});
166175
});
167176

@@ -211,46 +220,33 @@ describe('client.service', () => {
211220
locale: 'en',
212221
threadOf: null,
213222
},
223+
populate: defaultPopulate,
214224
});
215225
});
216226

217-
it('should handle user with no avatar properly', async () => {
227+
it('should throw error when no user is provided', async () => {
218228
const strapi = getStrapi();
219229
const service = getService(strapi);
230+
220231
const mockEntity = { id: 1, content: 'Test comment' };
221232
const mockSanitizedEntity = { id: 1, content: 'Clean comment' };
222-
const mockDbUserNoAvatar = { id: 1 }; // User without avatar
223233

224234
mockCommonService.parseRelationString.mockReturnValue({
225235
uid: 'api::test.test',
226236
relatedId: '1',
227237
});
228238
mockFindOne.mockResolvedValue({ id: 1 });
229239
mockCommonService.getConfig.mockResolvedValue([]);
230-
mockCommonService.isValidUserContext.mockReturnValue(true);
240+
mockCommonService.isValidUserContext.mockReturnValue(false);
231241
mockCommonService.checkBadWords.mockResolvedValue('Test comment');
232242
mockCommentRepository.create.mockResolvedValue(mockEntity);
233243
mockCommonService.sanitizeCommentEntity.mockReturnValue(
234244
mockSanitizedEntity
235245
);
236-
mockUserQuery().findOne.mockResolvedValue(mockDbUserNoAvatar);
237-
238-
const result = await service.create(mockPayload, mockUser);
239246

240-
expect(result).toEqual(mockSanitizedEntity);
241-
expect(mockCommentRepository.create).toHaveBeenCalledWith({
242-
data: {
243-
authorId: mockUser.id,
244-
authorEmail: mockUser.email,
245-
authorName: mockUser.username,
246-
authorAvatar: null,
247-
content: 'Test comment',
248-
related: 'api::test.test:1',
249-
approvalStatus: APPROVAL_STATUS.APPROVED,
250-
locale: 'en',
251-
threadOf: null,
252-
},
253-
});
247+
await expect(service.create(mockPayload)).rejects.toThrow(
248+
PluginError
249+
);
254250
});
255251

256252
it('should throw error when relation does not exist', async () => {
@@ -299,7 +295,7 @@ describe('client.service', () => {
299295
expect(mockCommentRepository.update).toHaveBeenCalledWith({
300296
where: { id: 1 },
301297
data: { content: 'Updated comment' },
302-
populate: { threadOf: true, authorUser: true },
298+
populate: defaultThreadOfPopulate,
303299
});
304300
});
305301

@@ -322,7 +318,7 @@ describe('client.service', () => {
322318
expect(mockCommentRepository.update).toHaveBeenCalledWith({
323319
where: { id: 1 },
324320
data: { content: 'Updated comment' },
325-
populate: { threadOf: true, authorUser: true },
321+
populate: defaultThreadOfPopulate,
326322
});
327323
});
328324

@@ -446,7 +442,7 @@ describe('client.service', () => {
446442
related: 'api::test.test:1',
447443
},
448444
data: { removed: true },
449-
populate: { threadOf: true, authorUser: true },
445+
populate: defaultThreadOfPopulate,
450446
});
451447
});
452448

server/src/services/__tests__/common.service.test.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ describe('common.service', () => {
156156
where: { id: 1 },
157157
populate: {
158158
reports: true,
159-
authorUser: true,
159+
authorUser: {
160+
populate: ['avatar'],
161+
},
160162
},
161163
});
162164
});
@@ -221,6 +223,38 @@ describe('common.service', () => {
221223
skip: 0,
222224
});
223225

226+
expect(result.data).toHaveLength(2);
227+
expect(mockCommentRepository.findWithCount).toHaveBeenCalled();
228+
});
229+
it('should return flat list of comments with populated user properties', async () => {
230+
const strapi = getStrapi();
231+
const service = getService(strapi);
232+
const mockComments = [
233+
{ id: 1, content: 'Comment 1', avatar: { url: 'avatar2.png' } },
234+
{ id: 2, content: 'Comment 2', avatar: { url: 'avatar2.png' } },
235+
];
236+
237+
mockCommentRepository.findWithCount.mockResolvedValue({
238+
results: mockComments,
239+
pagination: { total: 2 },
240+
});
241+
caster<jest.Mock>(getOrderBy).mockReturnValue(['createdAt', 'desc']);
242+
243+
mockStoreRepository.getConfig.mockResolvedValue([]);
244+
245+
const result = await service.findAllFlat({
246+
fields: ['id', 'content'],
247+
limit: 10,
248+
skip: 0,
249+
populate: {
250+
authorUser: {
251+
avatar: {
252+
populate: true
253+
}
254+
}
255+
}
256+
});
257+
224258
expect(result.data).toHaveLength(2);
225259
expect(mockCommentRepository.findWithCount).toHaveBeenCalled();
226260
});
@@ -516,7 +550,12 @@ describe('common.service', () => {
516550
expect(mockCommentRepository.findWithCount).toHaveBeenCalledWith({
517551
pageSize: 10,
518552
page: 1,
519-
populate: { authorUser: true },
553+
populate: {
554+
authorUser: {
555+
populate: true,
556+
avatar: { populate: true },
557+
},
558+
},
520559
select: ["id", "content", "related"],
521560
orderBy: { createdAt: "desc" },
522561
where: { authorId: 1 }
@@ -563,7 +602,10 @@ describe('common.service', () => {
563602
select: ['id', 'content', 'related'],
564603
orderBy: { createdAt: 'desc' },
565604
populate: {
566-
authorUser: true,
605+
authorUser: {
606+
populate: true,
607+
avatar: { populate: true },
608+
},
567609
},
568610
});
569611
});

0 commit comments

Comments
 (0)