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 ( / p e r m i s s i o n d e n i e d / 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 ( / t i t l e / i) ;
42
+ expect ( response . body . message ) . toMatch ( / r e q u i r e d / 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 ( / d e s c r i p t i o n / i) ;
58
+ expect ( response . body . message ) . toMatch ( / r e q u i r e d / 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 ( / t e x t / i) ;
74
+ expect ( response . body . message ) . toMatch ( / r e q u i r e d / 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 ( / b l o g U r l / i) ;
90
+ expect ( response . body . message ) . toMatch ( / r e q u i r e d / 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 ( / b l o g U r l / i) ;
107
+ expect ( response . body . message ) . toMatch ( / i n v a l i d / 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 ( / i m g U r l / i) ;
125
+ expect ( response . body . message ) . toMatch ( / v a l i d u r i / 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 ( / m u s t b e a n u m b e r / 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 ( / m u s t b e / i) ;
160
+ expect ( response . body . message ) . toMatch ( / a r r a y / 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 ( / a l r e a d y e x i s t s / 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 ( / c r e a t e d s u c c e s s / i) ;
196
+ expect ( mockBlogFindUrlIfExists ) . toBeCalledTimes ( 1 ) ;
197
+ expect ( mockBlogCreate ) . toBeCalledTimes ( 1 ) ;
198
+ expect ( response . body . data ) . toMatchObject ( { _id : BLOG_ID . toHexString ( ) } ) ;
199
+ } ) ;
200
+ } ) ;
0 commit comments