66 authenticationError,
77 noAuthorizationError,
88} = require ( "../graphql/variables/errorMessages" ) ;
9+ const {
10+ testCreateOrganization,
11+ testCreateUser,
12+ testLoginUser,
13+ testCreateCategory,
14+ testCreateTopic,
15+ } = require ( "../config/testVariables" ) ;
916const app = require ( "../app" ) . app ;
1017const supertest = require ( "supertest" ) ;
1118const request = supertest ( app ) ;
@@ -31,65 +38,9 @@ beforeAll(async (done) => {
3138 await User . deleteMany ( { } ) ;
3239 await Category . deleteMany ( { } ) ;
3340 await Topic . deleteMany ( { } ) ;
34- organizationResponse = await request
35- . post ( "/graphql" )
36- . send ( {
37- query : `mutation{ createOrganization(organizationInput: {
38- name: "Test Organization"
39- description: {
40- shortDescription: "Lorem Ipsum"
41- }
42- contactInfo: {
43- email: "test@email.com"
44- website: "www.website.com"
45- }
46- }) {
47- result
48- }}` ,
49- } )
50- . set ( "Accept" , "application/json" ) ;
51- firstUserSignupResponse = await request
52- . post ( "/graphql" )
53- . send ( {
54- query : `mutation{ createUser(userInput: {
55- name: {
56- firstName: "TestUser"
57- lastName: "1"
58- }
59- email: "abc1@email.com"
60- password: "password"
61- info: {
62- about: {
63- shortDescription: "Lorem Ipsum"
64- }
65- }
66- }) {
67- _id
68- name {
69- firstName
70- lastName
71- }
72- email
73- phone
74- isAdmin
75- }}` ,
76- } )
77- . set ( "Accept" , "application/json" ) ;
78- firstUserLoginResponse = await request
79- . post ( "/graphql" )
80- . send ( {
81- query : `{ login(
82- email: "abc1@email.com"
83- password: "password"
84- ) {
85- name {
86- firstName
87- lastName
88- }
89- token
90- } }` ,
91- } )
92- . set ( "Accept" , "application/json" ) ;
41+ organizationResponse = await testCreateOrganization ( ) ;
42+ firstUserSignupResponse = await testCreateUser ( 1 ) ;
43+ firstUserLoginResponse = await testLoginUser ( 1 ) ;
9344 firstUserToken = firstUserLoginResponse . body . data . login . token ;
9445 await done ( ) ;
9546} ) ;
@@ -100,45 +51,14 @@ afterAll(async () => {
10051} ) ;
10152
10253test ( "should not create a new category when user logged out" , async ( ) => {
103- const response = await request
104- . post ( "/graphql" )
105- . send ( {
106- query : `mutation{ createCategory(
107- categoryInput: {
108- name: "Test Category"
109- description: "Lorem Ipsum"
110- }
111- ) {
112- _id
113- name
114- description
115- createdBy
116- }}` ,
117- } )
118- . set ( "Accept" , "application/json" )
54+ const response = await testCreateCategory ( ) ;
11955 expect ( response . type ) . toBe ( "application/json" ) ;
12056 expect ( response . status ) . toBe ( 500 ) ;
12157 expect ( response . body . errors [ 0 ] . message ) . toBe ( authenticationError ) ;
12258} ) ;
12359
12460test ( "should create a new category when user logged in" , async ( ) => {
125- const response = await request
126- . post ( "/graphql" )
127- . send ( {
128- query : `mutation{ createCategory(
129- categoryInput: {
130- name: "Test Category"
131- description: "Lorem Ipsum"
132- }
133- ) {
134- _id
135- name
136- description
137- createdBy
138- }}` ,
139- } )
140- . set ( "Accept" , "application/json" )
141- . set ( "Authorization" , `Bearer ${ firstUserToken } ` ) ;
61+ const response = await testCreateCategory ( firstUserToken ) ;
14262 categoryId = response . body . data . createCategory . _id ;
14363 expect ( response . type ) . toBe ( "application/json" ) ;
14464 expect ( response . status ) . toBe ( 200 ) ;
@@ -169,25 +89,7 @@ test("get all categories", async () => {
16989} ) ;
17090
17191test ( "get topics of a particular category" , async ( ) => {
172- const topicCreationResponse = await request
173- . post ( "/graphql" )
174- . send ( {
175- query : `mutation{ createTopic(
176- topicInput: {
177- name: "Test Topic"
178- description: "Lorem Ipsum"
179- parentCategory: "${ categoryId } "
180- }
181- ) {
182- _id
183- name
184- description
185- createdBy
186- parentCategory
187- }}` ,
188- } )
189- . set ( "Accept" , "application/json" )
190- . set ( "Authorization" , `Bearer ${ firstUserToken } ` ) ;
92+ const topicCreationResponse = await testCreateTopic ( firstUserToken , categoryId ) ;
19193 const response = await request
19294 . post ( "/graphql" )
19395 . send ( {
@@ -208,45 +110,9 @@ test("get topics of a particular category", async () => {
208110} ) ;
209111
210112test ( "should not archive a category by any third user" , async ( ) => {
211- const userSignupResponse = await request
212- . post ( "/graphql" )
213- . send ( {
214- query : `mutation{ createUser(userInput: {
215- name: {
216- firstName: "TestUser"
217- lastName: "2"
218- }
219- email: "abc2@email.com"
220- password: "password"
221- info: {
222- about: {
223- shortDescription: "Lorem Ipsum"
224- }
225- }
226- }) {
227- _id
228- name {
229- firstName
230- lastName
231- }
232- email
233- phone
234- isAdmin
235- }}` ,
236- } )
237- . set ( "Accept" , "application/json" ) ;
113+ const userSignupResponse = await testCreateUser ( 2 ) ;
238114 expect ( userSignupResponse . body . data . createUser . isAdmin ) . toBe ( false ) ;
239- const userLoginResponse = await request
240- . post ( "/graphql" )
241- . send ( {
242- query : `{ login(
243- email: "abc2@email.com"
244- password: "password"
245- ) {
246- token
247- } }` ,
248- } )
249- . set ( "Accept" , "application/json" ) ;
115+ const userLoginResponse = await testLoginUser ( 2 ) ;
250116 const token = userLoginResponse . body . data . login . token ;
251117 const response = await request
252118 . post ( "/graphql" )
0 commit comments