|
| 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 | + |
0 commit comments