Skip to content

Commit db1c78b

Browse files
authored
Merge branch 'develop' into feat/test-user-ooo-acknowledge
2 parents 130cc1a + ee881bb commit db1c78b

File tree

3 files changed

+287
-57
lines changed

3 files changed

+287
-57
lines changed

test/fixtures/oooRequest/oooRequest.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { REQUEST_STATE, REQUEST_TYPE } from "../../../constants/requests";
2+
// import { UserStatus } from "../../../types/userStatus";
23

34
export const createOooStatusRequests = {
45
type: "OOO",
@@ -19,6 +20,35 @@ export const validOooStatusRequests = {
1920
state: REQUEST_STATE.PENDING,
2021
};
2122

23+
// export const createdOOORequest = {
24+
// id: "Js7JnT6uRBLjGvSJM5X5",
25+
// type: validOooStatusRequests.type,
26+
// from: validOooStatusRequests.from,
27+
// until: validOooStatusRequests.until,
28+
// reason: validOooStatusRequests.reason,
29+
// status: "PENDING",
30+
// lastModifiedBy: null,
31+
// requestedBy: "suraj-maity-1",
32+
// userId: "jCqqOYCnm93mcmaYuSsQ",
33+
// comment: null
34+
// };
35+
36+
export const validUserCurrentStatus = {
37+
from: Date.now(),
38+
until: Date.now() + 1 * 24 * 60 * 60 * 1000,
39+
message: "",
40+
state: "ACTIVE",
41+
updatedAt: Date.now(),
42+
};
43+
44+
// export const testUserStatus: UserStatus = {
45+
// id: "wcl0ZLsnngKUNZY9GkCo",
46+
// data: {
47+
// currentStatus: validUserCurrentStatus
48+
// },
49+
// userStatusExists: true
50+
// };
51+
2252
export const invalidOooStatusRequests = {
2353
type: "OOO",
2454
from: Date.now() + 100000,

test/integration/requests.test.ts

Lines changed: 162 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import app from "../../server";
88
import cleanDb from "../utils/cleanDb";
99
import authService from "../../services/authService";
1010
import userDataFixture from "../fixtures/user/user";
11+
import sinon from "sinon";
1112
const cookieName = config.get("userToken.cookieName");
1213
import addUser from "../utils/addUser";
1314
import {
@@ -30,11 +31,16 @@ import {
3031
REQUEST_REJECTED_SUCCESSFULLY,
3132
REQUEST_ALREADY_REJECTED,
3233
// UNAUTHORIZED_TO_ACKNOWLEDGE_OOO_REQUEST,
33-
INVALID_REQUEST_TYPE,
34+
// UNAUTHORIZED_TO_CREATE_OOO_REQUEST,
35+
// USER_STATUS_NOT_FOUND,
36+
// OOO_STATUS_ALREADY_EXIST,
3437
} from "../../constants/requests";
3538
import { updateTask } from "../../models/tasks";
3639
import { validTaskAssignmentRequest, validTaskCreqtionRequest } from "../fixtures/taskRequests/taskRequests";
37-
import * as logUtils from "../../services/logService";
40+
import { deleteUserStatus, updateUserStatus } from "../../models/userStatus";
41+
import * as requestsQuery from "../../models/requests";
42+
import { userState } from "../../constants/userStatus";
43+
3844
const userData = userDataFixture();
3945
chai.use(chaiHttp);
4046

@@ -47,13 +53,18 @@ let oooRequestData: any;
4753
let oooRequestData2: any;
4854
let testUserId: string;
4955
let testSuperUserId: string;
56+
let testArchivedUserId: string;
5057

5158
describe("/requests OOO", function () {
59+
60+
const requestsEndpoint: string = "/requests?dev=true";
61+
5262
beforeEach(async function () {
53-
const userIdPromises = [addUser(userData[16]), addUser(userData[4])];
54-
const [userId, superUserId] = await Promise.all(userIdPromises);
63+
const userIdPromises = [addUser(userData[16]), addUser(userData[4]), addUser(userData[18])];
64+
const [userId, superUserId, archivedUserId] = await Promise.all(userIdPromises);
5565
testUserId = userId;
5666
testSuperUserId = superUserId;
67+
testArchivedUserId = archivedUserId;
5768

5869
oooRequestData = { ...createOooRequests, requestedBy: userId };
5970
oooRequestData2 = { ...createOooRequests2, requestedBy: superUserId };
@@ -76,137 +87,239 @@ describe("/requests OOO", function () {
7687
});
7788

7889
afterEach(async function () {
90+
sinon.restore();
7991
await cleanDb();
8092
});
8193

8294
describe("POST /requests", function () {
95+
8396
beforeEach(async function () {
8497
const userIdPromises = [addUser(userData[16])];
8598
const [userId] = await Promise.all(userIdPromises);
8699

87100
authToken = authService.generateAuthToken({ userId });
101+
102+
const testUserStatus = {
103+
currentStatus: {
104+
state: userState.ACTIVE
105+
}
106+
};
107+
108+
await updateUserStatus(userId, testUserStatus);
88109
});
89110

90111
afterEach(async function () {
91112
await cleanDb();
92113
});
93114

115+
it.skip("should return 501 and 'Feature not implemented' message when dev is false", function (done) {
116+
chai
117+
.request(app)
118+
.post("/requests?dev=false")
119+
.set("cookie", `${cookieName}=${authToken}`)
120+
.send(validOooStatusRequests)
121+
.end(function (err, res) {
122+
if (err) {
123+
return done(err);
124+
}
125+
expect(res.statusCode).to.equal(501);
126+
expect(res.body.message).to.equal("Feature not implemented");
127+
done();
128+
});
129+
});
130+
94131
it("should return 401 if user is not logged in", function (done) {
95132
chai
96133
.request(app)
97-
.post("/requests")
134+
.post(requestsEndpoint)
98135
.send(validOooStatusRequests)
99136
.end(function (err, res) {
100137
expect(res).to.have.status(401);
138+
expect(res.body.error).to.equal("Unauthorized");
139+
expect(res.body.message).to.equal("Unauthenticated User");
101140
done();
102141
});
103142
});
104143

105-
it("should create a new request", function (done) {
144+
it.skip("should return 403 if user is not part of discord", function (done) {
145+
const authTokenForArchivedUserId = authService.generateAuthToken(
146+
{ userId: testArchivedUserId }
147+
);
106148
chai
107149
.request(app)
108-
.post("/requests")
109-
.set("cookie", `${cookieName}=${authToken}`)
150+
.post(requestsEndpoint)
151+
.set("cookie", `${cookieName}=${authTokenForArchivedUserId}`)
110152
.send(validOooStatusRequests)
111153
.end(function (err, res) {
112-
expect(res).to.have.status(201);
113-
expect(res.body).to.have.property("message");
114-
expect(res.body.message).to.equal(REQUEST_CREATED_SUCCESSFULLY);
154+
expect(res).to.have.status(403);
155+
expect(res.body.error).to.equal("Forbidden");
156+
// expect(res.body.message).to.equal(UNAUTHORIZED_TO_CREATE_OOO_REQUEST);
115157
done();
116158
});
117159
});
118160

119-
it("should return 400, if already created request is created again", async function () {
120-
await chai
121-
.request(app)
122-
.post("/requests")
123-
.set("cookie", `${cookieName}=${authToken}`)
124-
.send(validOooStatusRequests);
125-
const response = await chai
126-
.request(app)
127-
.post("/requests")
161+
it("should return 500 response when creating OOO request fails", function (done) {
162+
sinon.stub(requestsQuery, "createRequest")
163+
.throws("Error while creating OOO request");
164+
chai.request(app)
165+
.post(requestsEndpoint)
128166
.set("cookie", `${cookieName}=${authToken}`)
129-
.send(validOooStatusRequests);
130-
expect(response).to.have.status(400);
131-
expect(response.body).to.have.property("message");
132-
expect(response.body.message).to.equal(REQUEST_ALREADY_PENDING);
167+
.send(validOooStatusRequests)
168+
.end(function (err, res) {
169+
if (err) return done(err);
170+
expect(res.statusCode).to.equal(500);
171+
expect(res.body.message).to.equal("An internal server error occurred");
172+
done();
173+
});
133174
});
134175

135-
it("should create a new request and have all the required fields in the response", function (done) {
176+
it.skip("should create a new request when dev is true", function (done) {
136177
chai
137178
.request(app)
138-
.post("/requests")
179+
.post(requestsEndpoint)
139180
.set("cookie", `${cookieName}=${authToken}`)
140181
.send(validOooStatusRequests)
141-
.end(function (err, res) {
182+
.end(async function (err, res) {
183+
if (err) return done(err);
142184
expect(res).to.have.status(201);
143185
expect(res.body).to.have.property("message");
144-
expect(Object.keys(res.body.data)).to.have.lengthOf(9);
145-
expect(res.body.data.until).to.be.above(res.body.data.from);
146-
expect(res.body.data).to.have.property("requestedBy");
147-
expect(res.body.data.type).to.equal(REQUEST_TYPE.OOO);
148-
expect(res.body.data.state).to.equal(REQUEST_STATE.PENDING);
149186
expect(res.body.message).to.equal(REQUEST_CREATED_SUCCESSFULLY);
187+
expect(res.body).to.not.have.property("data");
188+
189+
await requestsQuery.getRequestByKeyValues({
190+
userId: testUserId,
191+
type: REQUEST_TYPE.OOO,
192+
status: REQUEST_STATE.PENDING
193+
}).then((request) => {
194+
expect(request).to.not.be.null;
195+
// expect(request.reason).to.equal(validOooStatusRequests.reason);
196+
done();
197+
}).catch(done);
198+
});
199+
});
200+
201+
it("should return error if invalid type is passed", function (done) {
202+
const type = "ACTIVE";
203+
chai
204+
.request(app)
205+
.post(requestsEndpoint)
206+
.set("cookie", `${cookieName}=${authToken}`)
207+
.send({ ...validOooStatusRequests, type })
208+
.end(function (err, res) {
209+
expect(res).to.have.status(400);
210+
expect(res.body).to.have.property("message");
211+
expect(res.body.message).to.equal(`Invalid request type: ${type}`);
150212
done();
151213
});
152214
});
153215

154-
it("should create a new request", function (done) {
216+
it("should return 400 when until date is smalller than from date in request body", function (done) {
155217
chai
156218
.request(app)
157-
.post("/requests")
219+
.post(requestsEndpoint)
158220
.set("cookie", `${cookieName}=${authToken}`)
159-
.send(validOooStatusRequests)
221+
.send({...validOooStatusRequests, until: Date.now()})
160222
.end(function (err, res) {
161-
expect(res).to.have.status(201);
223+
if (err) return done(err);
224+
expect(res).to.have.status(400);
162225
expect(res.body).to.have.property("message");
163-
expect(res.body.message).to.equal(REQUEST_CREATED_SUCCESSFULLY);
226+
expect(res.body.message).to.equal("until date must be greater than or equal to from date");
164227
done();
165228
});
166229
});
167230

168-
it("should return error if invalid type is passed", function (done) {
169-
const type = "ACTIVE";
231+
it("should return 400 when from date is less than today's date in request body", function (done) {
170232
chai
171233
.request(app)
172-
.post("/requests")
234+
.post(requestsEndpoint)
173235
.set("cookie", `${cookieName}=${authToken}`)
174-
.send({ ...validOooStatusRequests, type })
236+
.send({...validOooStatusRequests, from: Date.now() - 1 * 24 * 60 * 60 * 1000 })
175237
.end(function (err, res) {
238+
if (err) return done(err);
176239
expect(res).to.have.status(400);
177240
expect(res.body).to.have.property("message");
178-
expect(res.body.message).to.equal(`Invalid request type: ${type}`);
241+
expect(res.body.message).to.equal("from date must be greater than or equal to Today's date");
179242
done();
180243
});
181244
});
182245

183-
it("should return error if message is not present in body", function (done) {
246+
it("should return 400 when message field is missing in request body", function (done) {
184247
chai
185248
.request(app)
186-
.post("/requests")
249+
.post(requestsEndpoint)
187250
.set("cookie", `${cookieName}=${authToken}`)
188251
.send(_.omit(validOooStatusRequests, "message"))
189252
.end(function (err, res) {
253+
if (err) return done(err);
190254
expect(res).to.have.status(400);
191255
expect(res.body).to.have.property("message");
192256
expect(res.body.message).to.equal("message is required");
193257
done();
194258
});
195259
});
196260

197-
it("should return error if state in the body is not PENDING", function (done) {
261+
it("should return 400 with error when status field is included in request body", function (done) {
198262
chai
199263
.request(app)
200-
.post("/requests")
264+
.post(requestsEndpoint)
201265
.set("cookie", `${cookieName}=${authToken}`)
202-
.send({ ...validOooStatusRequests, state: REQUEST_STATE.APPROVED })
266+
.send({ ...validOooStatusRequests, status: REQUEST_STATE.APPROVED })
203267
.end(function (err, res) {
268+
if (err) return done(err);
204269
expect(res).to.have.status(400);
205270
expect(res.body).to.have.property("message");
206-
expect(res.body.message).to.equal("state must be PENDING");
271+
expect(res.body.message).to.equal(`"status" is not allowed`);
207272
done();
208273
});
209274
});
275+
276+
it.skip("should return 404 with error when user status not found", async function () {
277+
await deleteUserStatus(testUserId);
278+
const response = await chai
279+
.request(app)
280+
.post(requestsEndpoint)
281+
.set("cookie", `${cookieName}=${authToken}`)
282+
.send(validOooStatusRequests);
283+
284+
expect(response).to.have.status(404);
285+
expect(response.body).to.have.property("message");
286+
// expect(response.body.message).to.equal(USER_STATUS_NOT_FOUND);
287+
});
288+
289+
it.skip("should return 403 with error when user status is already OOO", async function () {
290+
const testOOOUserStatus = {
291+
currentStatus: {
292+
state: userState.OOO
293+
}
294+
};
295+
await updateUserStatus(testUserId, testOOOUserStatus);
296+
const response = await chai
297+
.request(app)
298+
.post(requestsEndpoint)
299+
.set("cookie", `${cookieName}=${authToken}`)
300+
.send(validOooStatusRequests);
301+
302+
expect(response).to.have.status(403);
303+
expect(response.body).to.have.property("message");
304+
// expect(response.body.message).to.equal(OOO_STATUS_ALREADY_EXIST);
305+
});
306+
307+
it.skip("should return 409 with error when user already have pending OOO request", async function () {
308+
await chai
309+
.request(app)
310+
.post(requestsEndpoint)
311+
.set("cookie", `${cookieName}=${authToken}`)
312+
.send(validOooStatusRequests);
313+
const response = await chai
314+
.request(app)
315+
.post(requestsEndpoint)
316+
.set("cookie", `${cookieName}=${authToken}`)
317+
.send(validOooStatusRequests);
318+
319+
expect(response).to.have.status(409);
320+
expect(response.body).to.have.property("message");
321+
expect(response.body.message).to.equal(REQUEST_ALREADY_PENDING);
322+
});
210323
});
211324

212325
describe.skip("PATCH /requests/:id", function () {
@@ -1019,4 +1132,4 @@ describe("/requests Task", function () {
10191132
});
10201133
});
10211134
});
1022-
});
1135+
});

0 commit comments

Comments
 (0)