Skip to content

Commit 24601e1

Browse files
committed
test(api): test get user posts api
1 parent 5d6e8a8 commit 24601e1

File tree

49 files changed

+221
-21
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+221
-21
lines changed

__tests__/integration/post.test.ts

Lines changed: 217 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ beforeAll((done) => {
1414
if (err) return console.log('Failed to connect to DB', err);
1515
done();
1616
});
17+
18+
jest.mock('@src/utils/sendEmail', () => ({
19+
sendEmailVerificationEmail: jest.fn().mockResolvedValue('Sending Email Success'),
20+
}));
1721
});
1822

1923
afterAll(async () => {
@@ -34,7 +38,7 @@ afterEach(async () => {
3438

3539
describe('Post', () => {
3640
/**
37-
* Testing get all post endpoint
41+
* Testing get all posts endpoint
3842
*/
3943
describe('GET /api/v1/feed/posts', () => {
4044
describe('given no post in db', () => {
@@ -88,7 +92,7 @@ describe('Post', () => {
8892
});
8993

9094
/**
91-
* Testing get timeline post endpoint
95+
* Testing get timeline posts endpoint
9296
*/
9397
describe('GET /api/v1/feed/posts/timeline', () => {
9498
describe('given the user is not logged in', () => {
@@ -211,6 +215,217 @@ describe('Post', () => {
211215
});
212216
});
213217

218+
/**
219+
* Testing get user posts endpoint
220+
*/
221+
describe('GET /api/v1/feed/posts/user-posts', () => {
222+
describe('given the user is not logged in', () => {
223+
it('should return a 401 status with a json message - Auth Failed', async () => {
224+
const response = await request(app).get('/api/v1/feed/posts/user-posts');
225+
226+
expect(response.body).toMatchObject({
227+
data: null,
228+
success: false,
229+
error: true,
230+
message: expect.any(String),
231+
status: 401,
232+
stack: expect.any(String),
233+
});
234+
});
235+
});
236+
237+
describe('given no post in db', () => {
238+
it('should return a 200 status with a json contain empty array', async () => {
239+
try {
240+
const authUser = new User({
241+
...userPayload,
242+
});
243+
244+
await authUser.save();
245+
246+
const authResponse = await request(app).post('/api/v1/auth/login').send({
247+
email: userPayload.email,
248+
password: userPayload.password,
249+
});
250+
251+
const token = (authResponse && authResponse?.body?.data?.accessToken) || '';
252+
253+
if (token) {
254+
const response = await request(app)
255+
.get('/api/v1/feed/posts/user-posts')
256+
.set('Authorization', `Bearer ${token}`)
257+
.send({ postId: validMongooseObjectId, commentId: validMongooseObjectId });
258+
259+
expect(response?.body?.data?.posts?.length).toBe(0);
260+
expect(response.body).toMatchObject({
261+
success: true,
262+
error: false,
263+
message: expect.any(String),
264+
status: 200,
265+
});
266+
expect(response?.body?.message).toMatch('No post found for user');
267+
}
268+
} catch (error) {
269+
console.log(error);
270+
}
271+
});
272+
});
273+
274+
describe('given the user is logged in and has 3 post in DB', () => {
275+
it('should return a 200 status with a json contain array of 3 posts', async () => {
276+
try {
277+
const authUser = new User({
278+
...userPayload,
279+
});
280+
281+
const testUser = new User({
282+
...userPayload,
283+
email: 'test@gmail.com',
284+
});
285+
286+
const authPost = { ...postPayload, author: authUser._id };
287+
const testUserPost = { ...postPayload, author: testUser._id };
288+
289+
await Post.insertMany([authPost, authPost, testUserPost, authPost]);
290+
291+
await authUser.save();
292+
293+
const authResponse = await request(app).post('/api/v1/auth/login').send({
294+
email: userPayload.email,
295+
password: userPayload.password,
296+
});
297+
298+
const token = (authResponse && authResponse?.body?.data?.accessToken) || '';
299+
300+
if (token) {
301+
const response = await request(app)
302+
.get('/api/v1/feed/posts/user-posts')
303+
.set('Authorization', `Bearer ${token}`);
304+
305+
expect(response?.body?.data?.posts?.length).toBe(3);
306+
expect(response.body).toMatchObject({
307+
success: true,
308+
error: false,
309+
message: expect.any(String),
310+
status: 200,
311+
});
312+
expect(response?.body?.message).toMatch('found all posts for user');
313+
}
314+
} catch (error) {
315+
console.log(error);
316+
}
317+
});
318+
});
319+
320+
describe('given added 3 posts in db', () => {
321+
it('should return a 200 status with a json contain array of 3 posts', async () => {
322+
const user = new User(userPayload);
323+
await user.save();
324+
const post = { ...postPayload, author: user._id };
325+
await Post.insertMany([post, post, post]);
326+
327+
await request(app)
328+
.get('/api/v1/feed/posts')
329+
.expect('Content-Type', /json/)
330+
.then((response) => {
331+
expect(response.body).toMatchObject({
332+
success: true,
333+
error: false,
334+
message: 'Successful Found posts',
335+
status: 200,
336+
data: {
337+
posts: expect.any(Array),
338+
totalDocs: expect.any(Number),
339+
},
340+
});
341+
})
342+
.catch((error) => {
343+
console.log(error);
344+
});
345+
});
346+
});
347+
});
348+
349+
/**
350+
* Testing get single post endpoint
351+
*/
352+
describe('GET /api/v1/feed/posts/:postId', () => {
353+
describe('given post id is not valid ', () => {
354+
it('should return a 422 status with validation message', async () => {
355+
// postId not vaild
356+
try {
357+
const response = await request(app).get(`/api/v1/feed/posts/notvaild`);
358+
expect(response.body).toMatchObject({
359+
data: null,
360+
error: true,
361+
status: 422,
362+
message: expect.any(String),
363+
stack: expect.any(String),
364+
});
365+
expect(response?.body?.message).toMatch(/fails to match the valid mongo id pattern/);
366+
} catch (error) {
367+
console.log(error);
368+
}
369+
});
370+
});
371+
372+
describe('given the post does not exist', () => {
373+
it('should return a 400 status', async () => {
374+
try {
375+
const response = await request(app)
376+
.get(`/api/v1/feed/posts/${validMongooseObjectId}`)
377+
.set('Accept', 'application/json')
378+
.expect('Content-Type', /json/);
379+
expect(response.body).toMatchObject({
380+
data: null,
381+
success: false,
382+
error: true,
383+
message: 'Bad Request',
384+
status: 400,
385+
stack: expect.any(String),
386+
});
387+
} catch (error) {
388+
console.log(error);
389+
}
390+
});
391+
});
392+
393+
describe('given the post does exist', () => {
394+
it('should return a 200 status and the product', async () => {
395+
try {
396+
const user = new User({
397+
...userPayload,
398+
email: (adminEmails && adminEmails[0]) || userPayload.email,
399+
role: authorizationRoles.admin,
400+
});
401+
402+
await user.save();
403+
404+
const post = new Post({
405+
...postPayload,
406+
author: user._id,
407+
});
408+
await post.save();
409+
const response = await request(app)
410+
.get(`/api/v1/feed/posts/${post?._id}`)
411+
.set('Accept', 'application/json')
412+
.expect('Content-Type', /json/);
413+
414+
expect(response?.body?.data?.post).toMatchObject(postPayload);
415+
416+
expect(response.body).toMatchObject({
417+
success: true,
418+
error: false,
419+
message: expect.any(String),
420+
status: 200,
421+
});
422+
} catch (error) {
423+
console.log(error);
424+
}
425+
});
426+
});
427+
});
428+
214429
/**
215430
* Testing like/un-like post endpoint
216431
*/

__tests__/integration/user.test.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,7 @@ describe('User', () => {
16441644
message: expect.any(String),
16451645
stack: expect.any(String),
16461646
});
1647-
expect(response?.body?.message).toMatch('cant follow yourself');
1647+
expect(response?.body?.message).toMatch('cannot follow yourself');
16481648
})
16491649
.catch((error) => {
16501650
console.log(error);
@@ -1665,14 +1665,7 @@ describe('User', () => {
16651665

16661666
const toBeFollowedUser = new User({
16671667
...userPayload,
1668-
followers: [
1669-
{
1670-
userId: currentUser?._id,
1671-
name: currentUser?.name,
1672-
surname: currentUser?.surname,
1673-
profileImage: currentUser?.profileImage,
1674-
},
1675-
],
1668+
followers: [currentUser?._id],
16761669
});
16771670
await toBeFollowedUser.save();
16781671

@@ -1698,7 +1691,7 @@ describe('User', () => {
16981691
message: expect.any(String),
16991692
stack: expect.any(String),
17001693
});
1701-
expect(response?.body?.message).toMatch('already follow this user');
1694+
expect(response?.body?.message).toMatch('already followed this user');
17021695
})
17031696
.catch((error) => {
17041697
console.log(error);
@@ -1956,14 +1949,7 @@ describe('User', () => {
19561949

19571950
const toBeUnFollowedUser = new User({
19581951
...userPayload,
1959-
followers: [
1960-
{
1961-
userId: currentUser?._id,
1962-
name: currentUser?.name,
1963-
surname: currentUser?.surname,
1964-
profileImage: currentUser?.profileImage,
1965-
},
1966-
],
1952+
followers: [currentUser?._id],
19671953
});
19681954
await toBeUnFollowedUser.save();
19691955

0 commit comments

Comments
 (0)