Skip to content

Commit e00db5b

Browse files
authored
force Merge pull request #19 from aayush-05/addTasksAPI
Add Tasks API
2 parents 7b696f8 + 8981e23 commit e00db5b

File tree

21 files changed

+1157
-975
lines changed

21 files changed

+1157
-975
lines changed

__tests__/auth.spec.js

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
const {
2+
testCreateOrganization,
3+
testCreateUser,
4+
testLoginUser,
5+
} = require("../config/testVariables");
16
const app = require("../app").app;
27
const supertest = require("supertest");
38
const request = supertest(app);
@@ -14,49 +19,8 @@ beforeAll(async (done) => {
1419
console.log(`Test: listening on ${process.env.PORT}`);
1520
await User.deleteMany({});
1621
await Organization.deleteMany({});
17-
const organizationResponse = await request
18-
.post("/graphql")
19-
.send({
20-
query: `mutation{ createOrganization(organizationInput: {
21-
name: "Test Organization"
22-
description: {
23-
shortDescription: "Lorem Ipsum"
24-
}
25-
contactInfo: {
26-
email: "test@email.com"
27-
website: "www.website.com"
28-
}
29-
}) {
30-
result
31-
}}`,
32-
})
33-
.set("Accept", "application/json");
34-
const userResponse = await request
35-
.post("/graphql")
36-
.send({
37-
query: `mutation{ createUser(userInput: {
38-
name: {
39-
firstName: "TestUser"
40-
lastName: "1"
41-
}
42-
email: "abc@email.com"
43-
password: "password"
44-
info: {
45-
about: {
46-
shortDescription: "Lorem Ipsum"
47-
}
48-
}
49-
}) {
50-
_id
51-
name {
52-
firstName
53-
lastName
54-
}
55-
email
56-
phone
57-
}}`,
58-
})
59-
.set("Accept", "application/json");
22+
const organizationResponse = await testCreateOrganization();
23+
const userResponse = await testCreateUser(1);
6024
userId = userResponse.body.data.createUser._id;
6125
await done();
6226
});
@@ -67,21 +31,7 @@ afterAll(async () => {
6731
});
6832

6933
test("login existing user", async () => {
70-
const response = await request
71-
.post("/graphql")
72-
.send({
73-
query: `{ login(
74-
email: "abc@email.com"
75-
password: "password"
76-
) {
77-
name {
78-
firstName
79-
lastName
80-
}
81-
token
82-
} }`,
83-
})
84-
.set("Accept", "application/json");
34+
const response = await testLoginUser(1);
8535
expect(response.type).toBe("application/json");
8636
expect(response.status).toBe(200);
8737
expect(response.body.data.login.name).toStrictEqual({

__tests__/category.spec.js

Lines changed: 15 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ const {
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");
916
const app = require("../app").app;
1017
const supertest = require("supertest");
1118
const 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

10253
test("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

12460
test("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

17191
test("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

210112
test("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

Comments
 (0)