Skip to content

Commit d304ab3

Browse files
committed
add writer create blog route
1 parent 3e55726 commit d304ab3

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
import { addAuthHeaders } from '../../../../auth/authentication/mock';
2+
3+
// this import should be below authentication/mock to override for role validation to work
4+
import { USER_ID_WRITER } from '../../../../auth/authorization/mock';
5+
6+
import {
7+
mockBlogCreate, mockBlogFindUrlIfExists, BLOG_ID, BLOG_URL
8+
} from './mock';
9+
10+
import supertest from 'supertest';
11+
import app from '../../../../../src/app';
12+
13+
describe('Writer blog routes', () => {
14+
15+
beforeEach(() => {
16+
mockBlogCreate.mockClear();
17+
mockBlogFindUrlIfExists.mockClear();
18+
});
19+
20+
const request = supertest(app);
21+
const endpoint = '/v1/writer/blog';
22+
23+
it('Should send error if the user do have writer role', async () => {
24+
const response = await addAuthHeaders(request.post(endpoint));
25+
expect(response.status).toBe(401);
26+
expect(response.body.message).toMatch(/permission denied/i);
27+
expect(mockBlogFindUrlIfExists).not.toBeCalled();
28+
expect(mockBlogCreate).not.toBeCalled();
29+
});
30+
31+
it('Should send error if blog title not sent', async () => {
32+
const response = await addAuthHeaders(
33+
request.post(endpoint).send({
34+
description: 'description',
35+
text: 'text',
36+
blogUrl: 'blogUrl',
37+
}),
38+
USER_ID_WRITER
39+
);
40+
expect(response.status).toBe(400);
41+
expect(response.body.message).toMatch(/title/i);
42+
expect(response.body.message).toMatch(/required/i);
43+
expect(mockBlogFindUrlIfExists).not.toBeCalled();
44+
expect(mockBlogCreate).not.toBeCalled();
45+
});
46+
47+
it('Should send error if blog description not sent', async () => {
48+
const response = await addAuthHeaders(
49+
request.post(endpoint).send({
50+
title: 'title',
51+
text: 'text',
52+
blogUrl: 'blogUrl',
53+
}),
54+
USER_ID_WRITER
55+
);
56+
expect(response.status).toBe(400);
57+
expect(response.body.message).toMatch(/description/i);
58+
expect(response.body.message).toMatch(/required/i);
59+
expect(mockBlogFindUrlIfExists).not.toBeCalled();
60+
expect(mockBlogCreate).not.toBeCalled();
61+
});
62+
63+
it('Should send error if blog text not sent', async () => {
64+
const response = await addAuthHeaders(
65+
request.post(endpoint).send({
66+
title: 'title',
67+
description: 'description',
68+
blogUrl: 'blogUrl',
69+
}),
70+
USER_ID_WRITER
71+
);
72+
expect(response.status).toBe(400);
73+
expect(response.body.message).toMatch(/text/i);
74+
expect(response.body.message).toMatch(/required/i);
75+
expect(mockBlogFindUrlIfExists).not.toBeCalled();
76+
expect(mockBlogCreate).not.toBeCalled();
77+
});
78+
79+
it('Should send error if blog blogUrl not sent', async () => {
80+
const response = await addAuthHeaders(
81+
request.post(endpoint).send({
82+
title: 'title',
83+
description: 'description',
84+
text: 'text',
85+
}),
86+
USER_ID_WRITER
87+
);
88+
expect(response.status).toBe(400);
89+
expect(response.body.message).toMatch(/blogUrl/i);
90+
expect(response.body.message).toMatch(/required/i);
91+
expect(mockBlogFindUrlIfExists).not.toBeCalled();
92+
expect(mockBlogCreate).not.toBeCalled();
93+
});
94+
95+
it('Should send error if blog blogUrl is not in accepted format', async () => {
96+
const response = await addAuthHeaders(
97+
request.post(endpoint).send({
98+
title: 'title',
99+
description: 'description',
100+
text: 'text',
101+
blogUrl: 'https://abc.com/xyz'
102+
}),
103+
USER_ID_WRITER
104+
);
105+
expect(response.status).toBe(400);
106+
expect(response.body.message).toMatch(/blogUrl/i);
107+
expect(response.body.message).toMatch(/invalid/i);
108+
expect(mockBlogFindUrlIfExists).not.toBeCalled();
109+
expect(mockBlogCreate).not.toBeCalled();
110+
});
111+
112+
it('Should send error if blog imgUrl is not an url', async () => {
113+
const response = await addAuthHeaders(
114+
request.post(endpoint).send({
115+
title: 'title',
116+
description: 'description',
117+
text: 'text',
118+
blogUrl: 'blogUrl',
119+
imgUrl: 'abc'
120+
}),
121+
USER_ID_WRITER
122+
);
123+
expect(response.status).toBe(400);
124+
expect(response.body.message).toMatch(/imgUrl/i);
125+
expect(response.body.message).toMatch(/valid uri/i);
126+
expect(mockBlogFindUrlIfExists).not.toBeCalled();
127+
expect(mockBlogCreate).not.toBeCalled();
128+
});
129+
130+
it('Should send error if blog score is invalid', async () => {
131+
const response = await addAuthHeaders(
132+
request.post(endpoint).send({
133+
title: 'title',
134+
description: 'description',
135+
text: 'text',
136+
blogUrl: 'blogUrl',
137+
score: 'abc'
138+
}),
139+
USER_ID_WRITER
140+
);
141+
expect(response.status).toBe(400);
142+
expect(response.body.message).toMatch(/must be a number/i);
143+
expect(mockBlogFindUrlIfExists).not.toBeCalled();
144+
expect(mockBlogCreate).not.toBeCalled();
145+
});
146+
147+
it('Should send error if blog tags is invalid', async () => {
148+
const response = await addAuthHeaders(
149+
request.post(endpoint).send({
150+
title: 'title',
151+
description: 'description',
152+
text: 'text',
153+
blogUrl: 'blogUrl',
154+
tags: 'abc'
155+
}),
156+
USER_ID_WRITER
157+
);
158+
expect(response.status).toBe(400);
159+
expect(response.body.message).toMatch(/must be/i);
160+
expect(response.body.message).toMatch(/array/i);
161+
expect(mockBlogFindUrlIfExists).not.toBeCalled();
162+
expect(mockBlogCreate).not.toBeCalled();
163+
});
164+
165+
it('Should send error if blog already exists for blogUrl', async () => {
166+
const response = await addAuthHeaders(
167+
request.post(endpoint).send({
168+
title: 'title',
169+
description: 'description',
170+
text: 'text',
171+
blogUrl: BLOG_URL
172+
}),
173+
USER_ID_WRITER
174+
);
175+
expect(response.status).toBe(400);
176+
expect(response.body.message).toMatch(/already exists/i);
177+
expect(mockBlogFindUrlIfExists).toBeCalledTimes(1);
178+
expect(mockBlogCreate).not.toBeCalled();
179+
});
180+
181+
it('Should send success if blog data is correct', async () => {
182+
const response = await addAuthHeaders(
183+
request.post(endpoint).send({
184+
title: 'title',
185+
description: 'description',
186+
text: 'text',
187+
blogUrl: 'blogUrl',
188+
imgUrl: 'https://abc.com/xyz',
189+
score: 0.01,
190+
tags: ['ABC'],
191+
}),
192+
USER_ID_WRITER
193+
);
194+
expect(response.status).toBe(200);
195+
expect(response.body.message).toMatch(/created success/i);
196+
expect(mockBlogFindUrlIfExists).toBeCalledTimes(1);
197+
expect(mockBlogCreate).toBeCalledTimes(1);
198+
expect(response.body.data).toMatchObject({ _id: BLOG_ID.toHexString() });
199+
});
200+
});

tests/routes/v1/blog/writer/mock.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { IBlog } from '../../../../../src/database/model/Blog';
2+
import { Types } from 'mongoose';
3+
4+
jest.unmock('../../../../../src/database/repository/BlogRepo');
5+
6+
export const BLOG_ID = new Types.ObjectId();
7+
export const BLOG_URL = 'abc';
8+
9+
export const mockBlogFindUrlIfExists = jest.fn(async (blogUrl: string): Promise<IBlog> => {
10+
if (blogUrl === BLOG_URL) return <IBlog>{
11+
_id: BLOG_ID,
12+
blogUrl: blogUrl
13+
};
14+
return null;
15+
});
16+
17+
export const mockBlogCreate = jest.fn(async (blog: IBlog): Promise<IBlog> => {
18+
blog._id = BLOG_ID;
19+
return blog;
20+
});
21+
22+
jest.mock('../../../../../src/database/repository/BlogRepo', () => ({
23+
get findUrlIfExists() { return mockBlogFindUrlIfExists; },
24+
get create() { return mockBlogCreate; }
25+
}));

0 commit comments

Comments
 (0)