@@ -4,13 +4,16 @@ import { addAuthHeaders } from '../../../../auth/authentication/mock';
4
4
import { USER_ID_WRITER } from '../../../../auth/authorization/mock' ;
5
5
6
6
import {
7
- mockBlogCreate , mockBlogFindUrlIfExists , BLOG_ID , BLOG_URL
7
+ BLOG_ID , BLOG_URL , BLOG_ID_2 ,
8
+ mockBlogCreate , mockBlogFindUrlIfExists ,
9
+ mockFindBlogAllDataById , mockBlogUpdate ,
8
10
} from './mock' ;
9
11
10
12
import supertest from 'supertest' ;
11
13
import app from '../../../../../src/app' ;
14
+ import { Types } from 'mongoose' ;
12
15
13
- describe ( 'Writer blog routes' , ( ) => {
16
+ describe ( 'Writer blog create routes' , ( ) => {
14
17
15
18
beforeEach ( ( ) => {
16
19
mockBlogCreate . mockClear ( ) ;
@@ -197,4 +200,192 @@ describe('Writer blog routes', () => {
197
200
expect ( mockBlogCreate ) . toBeCalledTimes ( 1 ) ;
198
201
expect ( response . body . data ) . toMatchObject ( { _id : BLOG_ID . toHexString ( ) } ) ;
199
202
} ) ;
203
+ } ) ;
204
+
205
+ describe ( 'Writer blog submit routes' , ( ) => {
206
+
207
+ beforeEach ( ( ) => {
208
+ mockFindBlogAllDataById . mockClear ( ) ;
209
+ mockBlogUpdate . mockClear ( ) ;
210
+ } ) ;
211
+
212
+ const request = supertest ( app ) ;
213
+ const endpoint = '/v1/writer/blog/submit/' ;
214
+
215
+ it ( 'Should send error if submit blog id is not valid' , async ( ) => {
216
+ const response = await addAuthHeaders (
217
+ request . put ( endpoint + 'abc' ) ,
218
+ USER_ID_WRITER
219
+ ) ;
220
+ expect ( response . status ) . toBe ( 400 ) ;
221
+ expect ( response . body . message ) . toMatch ( / i d / i) ;
222
+ expect ( response . body . message ) . toMatch ( / i n v a l i d / i) ;
223
+ expect ( mockFindBlogAllDataById ) . not . toBeCalled ( ) ;
224
+ expect ( mockBlogUpdate ) . not . toBeCalled ( ) ;
225
+ } ) ;
226
+
227
+ it ( 'Should send error if submit blog do not exist for id' , async ( ) => {
228
+ const response = await addAuthHeaders (
229
+ request . put ( endpoint + new Types . ObjectId ( ) . toHexString ( ) ) ,
230
+ USER_ID_WRITER
231
+ ) ;
232
+ expect ( response . status ) . toBe ( 400 ) ;
233
+ expect ( response . body . message ) . toMatch ( / n o t e x i s t s / i) ;
234
+ expect ( mockFindBlogAllDataById ) . toBeCalledTimes ( 1 ) ;
235
+ expect ( mockBlogUpdate ) . not . toBeCalled ( ) ;
236
+ } ) ;
237
+
238
+ it ( 'Should send success if submit blog for id exists' , async ( ) => {
239
+ const response = await addAuthHeaders (
240
+ request . put ( endpoint + BLOG_ID . toHexString ( ) ) ,
241
+ USER_ID_WRITER
242
+ ) ;
243
+ expect ( response . status ) . toBe ( 200 ) ;
244
+ expect ( response . body . message ) . toMatch ( / s u b m i t t e d s u c c e s s / i) ;
245
+ expect ( mockFindBlogAllDataById ) . toBeCalledTimes ( 1 ) ;
246
+ expect ( mockBlogUpdate ) . toBeCalledTimes ( 1 ) ;
247
+ } ) ;
248
+ } ) ;
249
+
250
+ describe ( 'Writer blog withdraw routes' , ( ) => {
251
+
252
+ beforeEach ( ( ) => {
253
+ mockFindBlogAllDataById . mockClear ( ) ;
254
+ mockBlogUpdate . mockClear ( ) ;
255
+ } ) ;
256
+
257
+ const request = supertest ( app ) ;
258
+ const endpoint = '/v1/writer/blog/withdraw/' ;
259
+
260
+ it ( 'Should send error if withdraw blog id is not valid' , async ( ) => {
261
+ const response = await addAuthHeaders (
262
+ request . put ( endpoint + 'abc' ) ,
263
+ USER_ID_WRITER
264
+ ) ;
265
+ expect ( response . status ) . toBe ( 400 ) ;
266
+ expect ( response . body . message ) . toMatch ( / i d / i) ;
267
+ expect ( response . body . message ) . toMatch ( / i n v a l i d / i) ;
268
+ expect ( mockFindBlogAllDataById ) . not . toBeCalled ( ) ;
269
+ expect ( mockBlogUpdate ) . not . toBeCalled ( ) ;
270
+ } ) ;
271
+
272
+ it ( 'Should send error if withdraw blog do not exist for id' , async ( ) => {
273
+ const response = await addAuthHeaders (
274
+ request . put ( endpoint + new Types . ObjectId ( ) . toHexString ( ) ) ,
275
+ USER_ID_WRITER
276
+ ) ;
277
+ expect ( response . status ) . toBe ( 400 ) ;
278
+ expect ( response . body . message ) . toMatch ( / n o t e x i s t s / i) ;
279
+ expect ( mockFindBlogAllDataById ) . toBeCalledTimes ( 1 ) ;
280
+ expect ( mockBlogUpdate ) . not . toBeCalled ( ) ;
281
+ } ) ;
282
+
283
+ it ( 'Should send success if withdraw blog for id exists' , async ( ) => {
284
+ const response = await addAuthHeaders (
285
+ request . put ( endpoint + BLOG_ID . toHexString ( ) ) ,
286
+ USER_ID_WRITER
287
+ ) ;
288
+ expect ( response . status ) . toBe ( 200 ) ;
289
+ expect ( response . body . message ) . toMatch ( / w i t h d r a w n s u c c e s s / i) ;
290
+ expect ( mockFindBlogAllDataById ) . toBeCalledTimes ( 1 ) ;
291
+ expect ( mockBlogUpdate ) . toBeCalledTimes ( 1 ) ;
292
+ } ) ;
293
+ } ) ;
294
+
295
+ describe ( 'Writer blog delete routes' , ( ) => {
296
+
297
+ beforeEach ( ( ) => {
298
+ mockFindBlogAllDataById . mockClear ( ) ;
299
+ mockBlogUpdate . mockClear ( ) ;
300
+ } ) ;
301
+
302
+ const request = supertest ( app ) ;
303
+ const endpoint = '/v1/writer/blog/id/' ;
304
+
305
+ it ( 'Should send error if deleting blog id is not valid' , async ( ) => {
306
+ const response = await addAuthHeaders (
307
+ request . delete ( endpoint + 'abc' ) ,
308
+ USER_ID_WRITER
309
+ ) ;
310
+ expect ( response . status ) . toBe ( 400 ) ;
311
+ expect ( response . body . message ) . toMatch ( / i d / i) ;
312
+ expect ( response . body . message ) . toMatch ( / i n v a l i d / i) ;
313
+ expect ( mockFindBlogAllDataById ) . not . toBeCalled ( ) ;
314
+ expect ( mockBlogUpdate ) . not . toBeCalled ( ) ;
315
+ } ) ;
316
+
317
+ it ( 'Should send error if deleting blog do not exist for id' , async ( ) => {
318
+ const response = await addAuthHeaders (
319
+ request . delete ( endpoint + new Types . ObjectId ( ) . toHexString ( ) ) ,
320
+ USER_ID_WRITER
321
+ ) ;
322
+ expect ( response . status ) . toBe ( 400 ) ;
323
+ expect ( response . body . message ) . toMatch ( / n o t e x i s t s / i) ;
324
+ expect ( mockFindBlogAllDataById ) . toBeCalledTimes ( 1 ) ;
325
+ expect ( mockBlogUpdate ) . not . toBeCalled ( ) ;
326
+ } ) ;
327
+
328
+ it ( 'Should send success if deleting blog for id exists' , async ( ) => {
329
+ const response = await addAuthHeaders (
330
+ request . delete ( endpoint + BLOG_ID . toHexString ( ) ) ,
331
+ USER_ID_WRITER
332
+ ) ;
333
+ expect ( response . status ) . toBe ( 200 ) ;
334
+ expect ( response . body . message ) . toMatch ( / d e l e t e d s u c c e s s / i) ;
335
+ expect ( mockFindBlogAllDataById ) . toBeCalledTimes ( 1 ) ;
336
+ expect ( mockBlogUpdate ) . toBeCalledTimes ( 1 ) ;
337
+ } ) ;
338
+ } ) ;
339
+
340
+ describe ( 'Writer blog get by id routes' , ( ) => {
341
+
342
+ beforeEach ( ( ) => {
343
+ mockFindBlogAllDataById . mockClear ( ) ;
344
+ } ) ;
345
+
346
+ const request = supertest ( app ) ;
347
+ const endpoint = '/v1/writer/blog/id/' ;
348
+
349
+ it ( 'Should send error if fetching blog id is not valid' , async ( ) => {
350
+ const response = await addAuthHeaders (
351
+ request . get ( endpoint + 'abc' ) ,
352
+ USER_ID_WRITER
353
+ ) ;
354
+ expect ( response . status ) . toBe ( 400 ) ;
355
+ expect ( response . body . message ) . toMatch ( / i d / i) ;
356
+ expect ( response . body . message ) . toMatch ( / i n v a l i d / i) ;
357
+ expect ( mockFindBlogAllDataById ) . not . toBeCalled ( ) ;
358
+ } ) ;
359
+
360
+ it ( 'Should send error if fetching blog do not exist for id' , async ( ) => {
361
+ const response = await addAuthHeaders (
362
+ request . get ( endpoint + new Types . ObjectId ( ) . toHexString ( ) ) ,
363
+ USER_ID_WRITER
364
+ ) ;
365
+ expect ( response . status ) . toBe ( 400 ) ;
366
+ expect ( response . body . message ) . toMatch ( / n o t e x i s t s / i) ;
367
+ expect ( mockFindBlogAllDataById ) . toBeCalledTimes ( 1 ) ;
368
+ } ) ;
369
+
370
+ it ( 'Should send error if author is different' , async ( ) => {
371
+ const response = await addAuthHeaders (
372
+ request . get ( endpoint + BLOG_ID_2 . toHexString ( ) ) ,
373
+ USER_ID_WRITER
374
+ ) ;
375
+ expect ( response . status ) . toBe ( 403 ) ;
376
+ expect ( response . body . message ) . toMatch ( / d o n ' t h a v e / i) ;
377
+ expect ( response . body . message ) . toMatch ( / p e r m i s s i o n / i) ;
378
+ expect ( mockFindBlogAllDataById ) . toBeCalledTimes ( 1 ) ;
379
+ } ) ;
380
+
381
+ it ( 'Should send success if fetching blog for id exists' , async ( ) => {
382
+ const response = await addAuthHeaders (
383
+ request . get ( endpoint + BLOG_ID . toHexString ( ) ) ,
384
+ USER_ID_WRITER
385
+ ) ;
386
+ expect ( response . status ) . toBe ( 200 ) ;
387
+ expect ( response . body . message ) . toMatch ( / s u c c e s s / i) ;
388
+ expect ( mockFindBlogAllDataById ) . toBeCalledTimes ( 1 ) ;
389
+ expect ( response . body . data ) . toMatchObject ( { _id : BLOG_ID . toHexString ( ) } ) ;
390
+ } ) ;
200
391
} ) ;
0 commit comments