Skip to content

Commit 8981e23

Browse files
committed
Add Tasks API
1 parent f07cd8d commit 8981e23

File tree

17 files changed

+874
-30
lines changed

17 files changed

+874
-30
lines changed

__tests__/task.spec.js

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
const {
2+
taskDeleteResult,
3+
taskCompleteResult,
4+
} = require("../graphql/variables/resultMessages");
5+
const {
6+
authenticationError,
7+
noAuthorizationError,
8+
} = require("../graphql/variables/errorMessages");
9+
const {
10+
testCreateOrganization,
11+
testCreateUser,
12+
testLoginUser,
13+
testCreateCategory,
14+
testCreateTopic,
15+
testCreateMessage,
16+
testCreateTask,
17+
} = require("../config/testVariables");
18+
const app = require("../app").app;
19+
const supertest = require("supertest");
20+
const request = supertest(app);
21+
const mongoose = require("mongoose");
22+
const Organization = require("../models/organization");
23+
const User = require("../models/user");
24+
const Category = require("../models/category");
25+
const Topic = require("../models/topic");
26+
const Message = require("../models/message");
27+
const Task = require("../models/task");
28+
const server = require("../app").server;
29+
const jwt = require("jsonwebtoken");
30+
31+
let connection,
32+
organizationResponse,
33+
firstUserSignupResponse,
34+
secondUserSignupResponse,
35+
firstUserLoginResponse,
36+
firstUserToken,
37+
categoryResponse,
38+
topicResponse,
39+
messageResponse,
40+
firstTaskId,
41+
secondTaskId;
42+
43+
beforeAll(async (done) => {
44+
connection = await server.listen(process.env.PORT);
45+
console.log(`Test: listening on ${process.env.PORT}`);
46+
await Organization.deleteMany({});
47+
await User.deleteMany({});
48+
await Category.deleteMany({});
49+
await Topic.deleteMany({});
50+
await Message.deleteMany({});
51+
organizationResponse = await testCreateOrganization();
52+
firstUserSignupResponse = await testCreateUser(1);
53+
secondUserSignupResponse = await testCreateUser(2);
54+
firstUserLoginResponse = await testLoginUser(1);
55+
firstUserToken = firstUserLoginResponse.body.data.login.token;
56+
categoryResponse = await testCreateCategory(firstUserToken);
57+
topicResponse = await testCreateTopic(
58+
firstUserToken,
59+
categoryResponse.body.data.createCategory._id
60+
);
61+
messageResponse = await testCreateMessage(
62+
firstUserToken,
63+
topicResponse.body.data.createTopic._id
64+
);
65+
await done();
66+
});
67+
68+
afterAll(async () => {
69+
await connection.close();
70+
await mongoose.connection.close();
71+
});
72+
73+
test("should create a task without message reference when user logged in", async () => {
74+
const response = await testCreateTask(
75+
firstUserToken,
76+
topicResponse.body.data.createTopic._id
77+
);
78+
expect(response.type).toBe("application/json");
79+
expect(response.status).toBe(200);
80+
firstTaskId = response.body.data.createTask._id;
81+
expect(response.body.data.createTask.description).toBe("Lorem Ipsum");
82+
expect(response.body.data.createTask.attachedMessage).toBe(null);
83+
expect(response.body.data.createTask.deadline).toBe("2020-02-20T02:20:20.000Z");
84+
});
85+
86+
test("should create a task with message reference when user logged in", async () => {
87+
const response = await testCreateTask(
88+
firstUserToken,
89+
topicResponse.body.data.createTopic._id,
90+
messageResponse.body.data.createMessage._id,
91+
secondUserSignupResponse.body.data.createUser._id,
92+
);
93+
expect(response.type).toBe("application/json");
94+
expect(response.status).toBe(200);
95+
secondTaskId = response.body.data.createTask._id;
96+
expect(response.body.data.createTask.description).toBe("Lorem Ipsum");
97+
expect(response.body.data.createTask.attachedMessage).toBe(
98+
messageResponse.body.data.createMessage._id
99+
);
100+
expect(response.body.data.createTask.deadline).toBe(
101+
"2020-02-20T02:20:20.000Z"
102+
);
103+
const topic = await Topic.findById(topicResponse.body.data.createTopic._id).lean();
104+
expect(topic.tasks.length).toBe(2);
105+
const firstUser = await User.findById(
106+
firstUserSignupResponse.body.data.createUser._id
107+
).lean();
108+
expect(firstUser.tasksCreated.length).toBe(2);
109+
const secondUser = await User.findById(
110+
secondUserSignupResponse.body.data.createUser._id
111+
).lean();
112+
expect(secondUser.tasksAssigned.length).toBe(1);
113+
});
114+
115+
test("should update a task without message reference when user logged in", async () => {
116+
const response = await request
117+
.post("/graphql")
118+
.send({
119+
query: `mutation{ updateTask(
120+
taskInput: {
121+
_id: "${firstTaskId}"
122+
description: "Updated Lorem Ipsum"
123+
deadline: "2022-02-20T02:20:20.000Z"
124+
}
125+
) {
126+
_id
127+
description
128+
deadline
129+
}}`,
130+
})
131+
.set("Accept", "application/json")
132+
.set("Authorization", `Bearer ${firstUserToken}`);
133+
expect(response.type).toBe("application/json");
134+
expect(response.status).toBe(200);
135+
expect(response.body.data.updateTask.description).toBe("Updated Lorem Ipsum");
136+
expect(response.body.data.updateTask.deadline).toBe(
137+
"2022-02-20T02:20:20.000Z"
138+
);
139+
});
140+
141+
test("should update a task with message reference when user logged in", async () => {
142+
const response = await request
143+
.post("/graphql")
144+
.send({
145+
query: `mutation{ updateTask(
146+
taskInput: {
147+
_id: "${secondTaskId}"
148+
description: "Updated Lorem Ipsum"
149+
deadline: "2022-02-20T02:20:20.000Z"
150+
}
151+
) {
152+
_id
153+
description
154+
deadline
155+
attachedMessage
156+
}}`,
157+
})
158+
.set("Accept", "application/json")
159+
.set("Authorization", `Bearer ${firstUserToken}`);
160+
expect(response.type).toBe("application/json");
161+
expect(response.status).toBe(200);
162+
expect(response.body.data.updateTask.description).toBe("Updated Lorem Ipsum");
163+
expect(response.body.data.updateTask.deadline).toBe(
164+
"2022-02-20T02:20:20.000Z"
165+
);
166+
const message = await Message.findById(response.body.data.updateTask.attachedMessage);
167+
expect(message.description).toBe("Updated Lorem Ipsum");
168+
});
169+
170+
test("should mark a task complete when user logged in", async () => {
171+
const response = await request
172+
.post("/graphql")
173+
.send({
174+
query: `mutation{ completeTask(
175+
taskFindInput: {
176+
_id: "${firstTaskId}"
177+
}
178+
) {
179+
result
180+
}}`,
181+
})
182+
.set("Accept", "application/json")
183+
.set("Authorization", `Bearer ${firstUserToken}`);
184+
expect(response.type).toBe("application/json");
185+
expect(response.status).toBe(200);
186+
expect(response.body.data.completeTask.result).toBe(taskCompleteResult);
187+
const task = await Task.findById(firstTaskId);
188+
expect(task.isCompleted).toBe(true);
189+
});
190+
191+
test("get a task when user logged in", async () => {
192+
const response = await request
193+
.post("/graphql")
194+
.send({
195+
query: `{ getTask(
196+
taskFindInput: {
197+
_id: "${firstTaskId}"
198+
}
199+
) {
200+
_id
201+
description
202+
isCompleted
203+
}}`,
204+
})
205+
.set("Accept", "application/json")
206+
.set("Authorization", `Bearer ${firstUserToken}`);
207+
expect(response.type).toBe("application/json");
208+
expect(response.status).toBe(200);
209+
expect(response.body.data.getTask.description).toBe("Updated Lorem Ipsum");
210+
expect(response.body.data.getTask.isCompleted).toBe(true);
211+
});
212+
213+
test("delete a task when user logged in", async () => {
214+
const task = await Task.findById(secondTaskId).lean();
215+
const response = await request
216+
.post("/graphql")
217+
.send({
218+
query: `mutation{ deleteTask(
219+
taskFindInput: {
220+
_id: "${secondTaskId}"
221+
}
222+
) {
223+
result
224+
}}`,
225+
})
226+
.set("Accept", "application/json")
227+
.set("Authorization", `Bearer ${firstUserToken}`);
228+
expect(response.type).toBe("application/json");
229+
expect(response.status).toBe(200);
230+
expect(response.body.data.deleteTask.result).toBe(taskDeleteResult);
231+
const message = await Message.findById(task.attachedMessage).lean();
232+
expect(message.isTasked).toBe(false);
233+
const topic = await Topic.findById(
234+
topicResponse.body.data.createTopic._id
235+
).lean();
236+
expect(topic.tasks.length).toBe(1);
237+
const firstUser = await User.findById(
238+
firstUserSignupResponse.body.data.createUser._id
239+
).lean();
240+
expect(firstUser.tasksCreated.length).toBe(1);
241+
const secondUser = await User.findById(
242+
secondUserSignupResponse.body.data.createUser._id
243+
).lean();
244+
expect(secondUser.tasksAssigned.length).toBe(0);
245+
});
246+

__tests__/topic.spec.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
testCreateCategory,
1414
testCreateTopic,
1515
testCreateMessage,
16+
testCreateTask,
1617
} = require("../config/testVariables");
1718
const app = require("../app").app;
1819
const supertest = require("supertest");
@@ -23,6 +24,7 @@ const User = require("../models/user");
2324
const Category = require("../models/category");
2425
const Topic = require("../models/topic");
2526
const Message = require("../models/message");
27+
const Task = require("../models/task");
2628
const server = require("../app").server;
2729
const jwt = require("jsonwebtoken");
2830
const { response } = require("express");
@@ -33,7 +35,8 @@ let connection,
3335
firstUserLoginResponse,
3436
firstUserToken,
3537
categoryResponse,
36-
topicId;
38+
topicId,
39+
messageId;
3740

3841
beforeAll(async (done) => {
3942
connection = await server.listen(process.env.PORT);
@@ -43,6 +46,7 @@ beforeAll(async (done) => {
4346
await Category.deleteMany({});
4447
await Topic.deleteMany({});
4548
await Message.deleteMany({});
49+
await Task.deleteMany({});
4650
organizationResponse = await testCreateOrganization();
4751
firstUserSignupResponse = await testCreateUser(1);
4852
firstUserLoginResponse = await testLoginUser(1);
@@ -200,7 +204,8 @@ test("should get all messages in a particular topic", async () => {
200204
firstUserToken,
201205
topicCreationResponse.body.data.createTopic._id
202206
);
203-
const messageId = messageCreationResponse.body.data.createMessage._id;
207+
topicId = topicCreationResponse.body.data.createTopic._id;
208+
messageId = messageCreationResponse.body.data.createMessage._id;
204209
const response = await request
205210
.post("/graphql")
206211
.send({
@@ -230,3 +235,30 @@ test("should get all messages in a particular topic", async () => {
230235
expect(response.body.data.getTopicChats[0].user._id)
231236
.toEqual(firstUserSignupResponse.body.data.createUser._id);
232237
});
238+
239+
test("should get all tasks in a particular topic", async () => {
240+
const taskCreationResponse = await testCreateTask(
241+
firstUserToken,
242+
topicId,
243+
messageId
244+
);
245+
const response = await request
246+
.post("/graphql")
247+
.send({
248+
query: `{ getTopicTasks(
249+
topicFindInput: {
250+
_id: "${topicId}"
251+
}
252+
) {
253+
_id
254+
description
255+
parentTopic
256+
}}`,
257+
})
258+
.set("Accept", "application/json")
259+
expect(response.type).toBe("application/json");
260+
expect(response.status).toBe(200);
261+
expect(response.body.data.getTopicTasks.length).toBe(1);
262+
expect(response.body.data.getTopicTasks[0].description).toBe("Lorem Ipsum");
263+
expect(response.body.data.getTopicTasks[0].parentTopic).toEqual(topicId);
264+
});

0 commit comments

Comments
 (0)