Skip to content

Commit b441561

Browse files
committed
Reformatted the code for testing files!
1 parent 2676423 commit b441561

4 files changed

Lines changed: 54 additions & 132 deletions

File tree

tests/group-chats.test.js

Lines changed: 16 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ const wickhamToken = await wickham.getToken();
2727
const lydiaToken = await lydia.getToken();
2828
const bingleyToken = await bingley.getToken();
2929

30-
//console.log("LIZ TOKEN ", elizabethToken);
31-
3230
await elizabeth.sendFriendRequest(darcy.email);
3331
await darcy.respondToFriendRequest(elizabeth.email, "ACCEPTED");
3432
await elizabeth.getPrivateChats(elizabeth.email);
@@ -40,19 +38,8 @@ await lydia.respondToFriendRequest(wickham.email, "ACCEPTED");
4038
const createGroupURL = `${process.env.BASE_URL}/chats/groups/create`;
4139
const addFriendToGroupURL = `${process.env.BASE_URL}/chats/groups/add-friend`;
4240

43-
//let groupID;
44-
45-
//
46-
4741
describe("Group chats tests", () => {
4842
describe("Group Creation", () => {
49-
// Cases =>
50-
// 1. Elizabeth creates a group chat -> 200
51-
// 2. Elizabeth tries to create a group with missing fields -> 400
52-
// - groupName missing
53-
// - groupDescription missing
54-
// - adminEmail missing
55-
5643
it("Create a group chat -> 200", async () => {
5744
const res = await request(app)
5845
.post(createGroupURL)
@@ -62,7 +49,6 @@ describe("Group chats tests", () => {
6249
groupName: "Pride and Prejudice",
6350
groupDescription: "A group chat for discussing the novel",
6451
});
65-
//groupID = res.body.body;
6652
expect(res.status).toBe(200);
6753
});
6854

@@ -96,20 +82,10 @@ describe("Group chats tests", () => {
9682
});
9783

9884
describe("Add someone to group", () => {
99-
// Cases =>
100-
// 1. Elizabeth adds Darcy to the group -> 200
101-
// 2. Elizabeth tries to add Darcy to the group with missing fields -> 400
102-
// - groupID missing
103-
// - friendEmail missing
104-
// - adminEmail missing
105-
// 3. Elizabeth tries to add Darcy to the group with wrong groupID non-existent group -> 400
106-
// 4. Elizabeth tries to add Darcy to the group with wrong friendEmail -> 404
107-
// 5. Wickham tries to add Darcy to the group -> 403
108-
10985
it("Add friend to group -> 200", async () => {
11086
const groupID = await elizabeth.createGroup(
11187
"Pride and Prejudice",
112-
"A group chat for discussing the novel",
88+
"A group chat for discussing the novel"
11389
);
11490

11591
const res = await request(app)
@@ -120,14 +96,13 @@ describe("Group chats tests", () => {
12096
userEmail: darcy.email,
12197
chatID: groupID,
12298
});
123-
//console.log("GROUP ID ", groupID);
12499
expect(res.status).toBe(200);
125100
});
126101

127102
it("Add friend to group with missing fields -> 400", async () => {
128103
const groupID = await elizabeth.createGroup(
129104
"Pride and Prejudice",
130-
"A group chat for discussing the novel",
105+
"A group chat for discussing the novel"
131106
);
132107

133108
const possibleResponses = [
@@ -165,7 +140,7 @@ describe("Group chats tests", () => {
165140
it("Add friend to group with wrong groupID non-existent group -> 404", async () => {
166141
await elizabeth.createGroup(
167142
"Pride and Prejudice",
168-
"A group chat for discussing the novel",
143+
"A group chat for discussing the novel"
169144
);
170145
const groupID = "313233343536373839303132";
171146
const res = await request(app)
@@ -182,7 +157,7 @@ describe("Group chats tests", () => {
182157
it("Add friend to group with wrong friendEmail -> 404", async () => {
183158
const groupID = await elizabeth.createGroup(
184159
"Pride and Prejudice",
185-
"A group chat for discussing the novel",
160+
"A group chat for discussing the novel"
186161
);
187162

188163
const res = await request(app)
@@ -199,7 +174,7 @@ describe("Group chats tests", () => {
199174
it("User not in group trying to add another user, both are not friends -> 403", async () => {
200175
const groupID = await elizabeth.createGroup(
201176
"Pride and Prejudice",
202-
"A group chat for discussing the novel",
177+
"A group chat for discussing the novel"
203178
);
204179

205180
const res = await request(app)
@@ -219,7 +194,7 @@ describe("Group chats tests", () => {
219194

220195
const groupID = await elizabeth.createGroup(
221196
"Pride and Prejudice",
222-
"A group chat for discussing the novel",
197+
"A group chat for discussing the novel"
223198
);
224199

225200
const res = await request(app)
@@ -236,7 +211,7 @@ describe("Group chats tests", () => {
236211
it("User in group trying to add another user, both are not friends -> 403", async () => {
237212
const groupID = await elizabeth.createGroup(
238213
"Pride and Prejudice",
239-
"A group chat for discussing the novel",
214+
"A group chat for discussing the novel"
240215
);
241216

242217
await elizabeth.addFriendToGroup(groupID, wickham.email);
@@ -249,31 +224,20 @@ describe("Group chats tests", () => {
249224
userEmail: lydia.email,
250225
chatID: groupID,
251226
});
252-
//console.log("USER ADD FRIEND TO GROUP ", res.body);
253227
expect(res.status).toBe(403);
254228
});
255229
});
256230

257231
describe("Get group details", () => {
258-
// Cases =>
259-
// 1. Elizabeth, the admin, gets group details -> 200
260-
// 2. Elizabeth tries to get group details with missing group ID -> 400
261-
// 3. Elizabeth tries to get group details with wrong group ID format -> 400
262-
// 4. Elizabeth tries to get group details for a non-existent group -> 404
263-
// 5. Wickham, who isn't in the group, tries to get group details -> 403
264-
// 6. Darcy, who is in the group, tries to get group details -> 200
265-
//
266-
267232
it("Get group details -> 200", async () => {
268233
const groupID = await elizabeth.createGroup(
269234
"Pride and Prejudice",
270-
"A group chat for discussing the novel",
235+
"A group chat for discussing the novel"
271236
);
272237

273238
const res = await request(app)
274239
.get(`${process.env.BASE_URL}/chats/groups/details/${groupID}`)
275240
.set("Authorization", elizabethToken);
276-
//console.log("GROUP DETAILS ", res.body);
277241
expect(res.status).toBe(200);
278242
});
279243

@@ -294,7 +258,7 @@ describe("Group chats tests", () => {
294258
it("Get group details for a non-existent group -> 404", async () => {
295259
const res = await request(app)
296260
.get(
297-
`${process.env.BASE_URL}/chats/groups/details/313233343536373839303132`,
261+
`${process.env.BASE_URL}/chats/groups/details/313233343536373839303132`
298262
)
299263
.set("Authorization", elizabethToken);
300264
expect(res.status).toBe(404);
@@ -303,7 +267,7 @@ describe("Group chats tests", () => {
303267
it("User not in group trying to get group details -> 403", async () => {
304268
const groupID = await elizabeth.createGroup(
305269
"Pride and Prejudice",
306-
"A group chat for discussing the novel",
270+
"A group chat for discussing the novel"
307271
);
308272

309273
const res = await request(app)
@@ -315,10 +279,10 @@ describe("Group chats tests", () => {
315279
it("User in group trying to get group details -> 200", async () => {
316280
const groupID = await elizabeth.createGroup(
317281
"Pride and Prejudice",
318-
"A group chat for discussing the novel",
282+
"A group chat for discussing the novel"
319283
);
320284

321-
const addDarcy = await elizabeth.addFriendToGroup(groupID, darcy.email);
285+
await elizabeth.addFriendToGroup(groupID, darcy.email);
322286

323287
const res = await request(app)
324288
.get(`${process.env.BASE_URL}/chats/groups/details/${groupID}`)
@@ -328,19 +292,10 @@ describe("Group chats tests", () => {
328292
});
329293

330294
describe("Delete group", () => {
331-
// Cases =>
332-
// 1. Elizabeth, the admin, deletes the group -> 200
333-
// 2. Elizabeth tries to delete the group with missing group ID -> 400
334-
// 3. Elizabeth tries to delete the group with wrong group ID format -> 400
335-
// 4. Elizabeth tries to delete a non-existent group -> 404
336-
// 5. Elizabeth tries to delete the group, without passing admin email -> 400
337-
// 5. Lydia, who isn't in the group, tries to delete the group -> 403
338-
// 6. Darcy, who is in the group, tries to delete the group -> 403
339-
340295
it("Delete group -> 200", async () => {
341296
const groupID = await elizabeth.createGroup(
342297
"Pride and Prejudice",
343-
"A group chat for discussing the novel",
298+
"A group chat for discussing the novel"
344299
);
345300

346301
const res = await request(app)
@@ -360,7 +315,6 @@ describe("Group chats tests", () => {
360315
.send({
361316
email: elizabeth.email,
362317
});
363-
//console.log("DELETE GROUP ", res.body);
364318
expect(res.status).toBe(400);
365319
});
366320

@@ -387,7 +341,7 @@ describe("Group chats tests", () => {
387341
it("Delete group without passing admin email -> 400", async () => {
388342
const groupID = await elizabeth.createGroup(
389343
"Pride and Prejudice",
390-
"A group chat for discussing the novel",
344+
"A group chat for discussing the novel"
391345
);
392346

393347
const res = await request(app)
@@ -402,7 +356,7 @@ describe("Group chats tests", () => {
402356
it("User not in group trying to delete group -> 403", async () => {
403357
const groupID = await elizabeth.createGroup(
404358
"Pride and Prejudice",
405-
"A group chat for discussing the novel",
359+
"A group chat for discussing the novel"
406360
);
407361

408362
const res = await request(app)
@@ -417,7 +371,7 @@ describe("Group chats tests", () => {
417371
it("User in group trying to delete group -> 403", async () => {
418372
const groupID = await elizabeth.createGroup(
419373
"Pride and Prejudice",
420-
"A group chat for discussing the novel",
374+
"A group chat for discussing the novel"
421375
);
422376

423377
await elizabeth.addFriendToGroup(groupID, darcy.email);

tests/registeration.test.js

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import request from "supertest";
2-
import app from "../index.js";
2+
import app from "../index.js";
33
import mongoose from "mongoose";
44
import connetDB, { clearAllCollections } from "../src/db/connection.js";
5+
import env from "dotenv";
56

6-
import env from "dotenv"
7-
env.config()
7+
env.config();
8+
await clearAllCollections(mongoose.connection);
89

9-
await clearAllCollections(mongoose.connection);
10-
1110
describe("POST /users/register", () => {
12-
describe("given a username, email and password", () => {
13-
const url = `${process.env.BASE_URL}/auth/register`;
14-
// all is well
11+
const url = `${process.env.BASE_URL}/auth/register`;
12+
13+
describe("Given a username, email, and password", () => {
1514
it("should respond with status code 201", async () => {
1615
const response = await request(app).post(url).send({
1716
username: "test",
@@ -23,7 +22,7 @@ describe("POST /users/register", () => {
2322

2423
describe("Missing data", () => {
2524
it("should respond with status code 400", async () => {
26-
const possibleResponses = [
25+
const testCases = [
2726
{ username: "test", email: "test@mail.com", password: null },
2827
{ username: "test", email: null, password: "Password123" },
2928
{ username: "test", email: null, password: null },
@@ -33,23 +32,20 @@ describe("POST /users/register", () => {
3332
{ username: null, email: null, password: null },
3433
];
3534

36-
for (const testCase of possibleResponses) {
37-
const response = await request(app)
38-
.post(url)
39-
.send(testCase);
35+
for (const testCase of testCases) {
36+
const response = await request(app).post(url).send(testCase);
4037
expect(response.statusCode).toBe(400);
4138
}
4239
});
4340
});
41+
4442
describe("Data format is wrong", () => {
45-
// password is too short
4643
it("Password too short -> 400", async () => {
4744
const response = await request(app).post(url).send({
4845
username: "test",
4946
email: "test@mail.com",
5047
password: "Test1",
5148
});
52-
//expect(password.length).toBeLessThan(8);
5349
expect(response.statusCode).toBe(400);
5450
});
5551

@@ -59,53 +55,46 @@ describe("POST /users/register", () => {
5955
email: "test@mail.com",
6056
password: "Password123",
6157
});
62-
//expect(username.length).toBeLessThan(3);
6358
expect(response.statusCode).toBe(400);
6459
});
6560

66-
// password is not valid
6761
it("Password is non-compliant -> 400", async () => {
6862
const response = await request(app).post(url).send({
6963
username: "test",
7064
email: "test@mail.com",
7165
password: "Password123",
7266
});
73-
// expect(password).not.toMatch(
74-
// /^ (?=.* [A - Z])(?=.* [a - z])(?=.*\d).+ $/,
75-
// );
7667
expect(response.statusCode).toBe(400);
7768
});
7869
});
7970

80-
// email is not valid
8171
it("Email is non-compliant -> 400", async () => {
8272
const response = await request(app).post(url).send({
8373
username: "test",
8474
email: "testmail.com",
8575
password: "Password123",
8676
});
87-
//expect(email).not.toMatch(/^\S+@\S+\.\S+$/);
8877
expect(response.statusCode).toBe(400);
8978
});
9079

9180
describe("Duplicate field", () => {
92-
93-
it("email is already taken -> 409", async () => {
81+
it("Email is already taken -> 409", async () => {
9482
const existingEmail = "chat@sphere.com";
9583
const response1 = await request(app).post(url).send({
84+
username: "test",
85+
email: existingEmail,
86+
password: "Password123",
87+
});
88+
89+
if (response1.statusCode === 201) {
90+
const response2 = await request(app).post(url).send({
9691
username: "test",
9792
email: existingEmail,
9893
password: "Password123",
9994
});
100-
if(response1.statusCode === 201){
101-
const response2 = await request(app).post(url).send({
102-
username: "test",
103-
email: existingEmail,
104-
password: "Password123",
105-
});
106-
expect(response2.statusCode).toBe(409);
95+
expect(response2.statusCode).toBe(409);
10796
}
108-
expect(response1.statusCode).not.toBe(201)
97+
expect(response1.statusCode).not.toBe(201);
10998
});
11099
});
111100
});

0 commit comments

Comments
 (0)