Skip to content

Commit 6380c06

Browse files
committed
Finish auth.test.ts unit tests
1 parent c20790b commit 6380c06

File tree

1 file changed

+213
-76
lines changed

1 file changed

+213
-76
lines changed
Lines changed: 213 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,218 @@
11
import request from "supertest";
2-
import { describe, expect, it, test } from "@jest/globals";
3-
import app from "../src/index";
2+
import { Request, Response } from "express";
3+
import {
4+
jest,
5+
describe,
6+
test,
7+
it,
8+
expect,
9+
afterAll,
10+
} from "@jest/globals";
11+
import app, { server } from "../src/index";
412

13+
jest.mock("node-cron", () => ({
14+
schedule: jest.fn(), // Mock the `schedule` function
15+
}));
16+
17+
jest.mock("../src/controllers/userController", () => ({
18+
signUp: jest.fn((req: Request, res: Response) => {
19+
const { email, password, username } = req.body;
20+
if (!email || !password || !username) {
21+
return res.status(400).json({ error: "Email, password, and username are required" });
22+
}
23+
if (email === "[email protected]") {
24+
return res.status(400).json({ error: "Email is already taken" });
25+
}
26+
res.status(201).json({ message: "User registered successfully", user: { email, username } });
27+
}),
28+
login: jest.fn((req: Request, res: Response) => {
29+
const { email, password } = req.body;
30+
if (!email || !password) {
31+
return res.status(400).json({ error: "Email and password are required" });
32+
}
33+
if (email === "validUser" && password === "validPassword") {
34+
res.status(200).json({ token: "mockedToken" });
35+
} else {
36+
res.status(401).json({ error: "Invalid credentials" });
37+
}
38+
}),
39+
logout: jest.fn((req: Request, res: Response) => {
40+
const token = req.headers.authorization?.split(" ")[1];
41+
if (!token) {
42+
return res.status(401).json({ error: "Unauthorized" });
43+
}
44+
res.status(200).json({ message: "Logged out successfully" });
45+
}),
46+
session: jest.fn((req: Request, res: Response) => {
47+
const token = req.headers.authorization?.split(" ")[1];
48+
if (!token || token !== "mockedToken") {
49+
return res.status(401).json({ error: "Unauthorized" });
50+
}
51+
res.status(200).json({ message: "Session valid", user: { email: "validUser" } });
52+
}),
53+
requestPasswordReset: jest.fn((req: Request, res: Response) => {
54+
const { email } = req.body;
55+
if (!email) {
56+
return res.status(400).json({ error: "Email is required" });
57+
}
58+
res.status(200).json({ message: "Password reset link sent" });
59+
}),
60+
resetPassword: jest.fn((req: Request, res: Response) => {
61+
const { password, token } = req.body;
62+
if (!password || !token) {
63+
return res.status(400).json({ error: "Password and token are required" });
64+
}
65+
res.status(200).json({ message: "Password reset successfully" });
66+
}),
67+
accountDelete: jest.fn((req: Request, res: Response) => {
68+
const { userId } = req.body;
69+
if (!userId) {
70+
return res.status(400).json({ error: "User ID is required" });
71+
}
72+
res.status(200).json({ message: "Account deletion requested" });
73+
}),
74+
updateUsername: jest.fn((req: Request, res: Response) => {
75+
const { userId, newUsername } = req.body;
76+
if (!userId || !newUsername) {
77+
return res.status(400).json({ error: "User ID and new username are required" });
78+
}
79+
res.status(200).json({ message: "Username updated successfully" });
80+
}),
81+
usernameFromUserId: jest.fn((req: Request, res: Response) => {
82+
const { userId } = req.query;
83+
if (!userId) {
84+
return res.status(400).json({ error: "User ID is required" });
85+
}
86+
res.status(200).json({ username: "mockedUsername" });
87+
}),
88+
}));
89+
90+
afterAll(async () => {
91+
server.close();
92+
});
93+
594
describe("Authentication API", () => {
6-
// The unit tests below are currently commented out because they require a database connection.
7-
// They will be uncommented out once all the necessary mocks are in place.
95+
describe('POST /auth/signup', () => {
96+
it('should return 201 and a success message for valid signup data', async () => {
97+
const response = await request(app).post('/auth/signup').send({
98+
99+
password: 'securePassword123',
100+
username: 'newUser',
101+
});
102+
expect(response.status).toBe(201);
103+
expect(response.body).toHaveProperty('message', 'User registered successfully');
104+
expect(response.body.user).toHaveProperty('email', '[email protected]');
105+
expect(response.body.user).toHaveProperty('username', 'newUser');
106+
});
107+
108+
it('should return 400 if email is already taken', async () => {
109+
const response = await request(app).post('/auth/signup').send({
110+
111+
password: 'securePassword123',
112+
username: 'existingUser',
113+
});
114+
expect(response.status).toBe(400);
115+
expect(response.body).toHaveProperty('error', 'Email is already taken');
116+
});
117+
118+
it('should return 400 if required fields are missing', async () => {
119+
const response = await request(app).post('/auth/signup').send({
120+
121+
});
122+
expect(response.status).toBe(400);
123+
expect(response.body).toHaveProperty('error', 'Email, password, and username are required');
124+
});
125+
});
126+
127+
describe('POST /auth/login', () => {
128+
it('should return 200 and a token for valid credentials', async () => {
129+
const response = await request(app).post('/auth/login').send({ email: 'validUser', password: 'validPassword' });
130+
expect(response.status).toBe(200);
131+
expect(response.body).toHaveProperty('token');
132+
});
133+
it('should return 401 for invalid credentials', async () => {
134+
const response = await request(app).post('/auth/login').send({ email: 'invalidUser', password: 'wrongPassword' });
135+
expect(response.status).toBe(401);
136+
expect(response.body).toHaveProperty('error', 'Invalid credentials');
137+
});
138+
it('should return 400 if email or password is missing', async () => {
139+
const response = await request(app).post('/auth/login').send({ email: 'validUser' });
140+
expect(response.status).toBe(400);
141+
expect(response.body).toHaveProperty('error', 'Email and password are required');
142+
});
143+
});
144+
145+
describe('POST /auth/logout', () => {
146+
it('should return 200 for successful logout', async () => {
147+
const response = await request(app).post('/auth/logout').set('Authorization', 'Bearer mockedToken');
148+
expect(response.status).toBe(200);
149+
expect(response.body).toHaveProperty('message', 'Logged out successfully');
150+
});
151+
it('should return 401 if token is missing', async () => {
152+
const response = await request(app).post('/auth/logout');
153+
expect(response.status).toBe(401);
154+
expect(response.body).toHaveProperty('error', 'Unauthorized');
155+
});
156+
});
157+
158+
describe('GET /auth/session', () => {
159+
it('should return 200 and session info for valid token', async () => {
160+
const response = await request(app).get('/auth/session').set('Authorization', 'Bearer mockedToken');
161+
expect(response.status).toBe(200);
162+
expect(response.body).toHaveProperty('message', 'Session valid');
163+
expect(response.body.user).toHaveProperty('email', 'validUser');
164+
});
165+
it('should return 401 if token is missing or invalid', async () => {
166+
const response = await request(app).get('/auth/session').set('Authorization', 'Bearer invalidToken');
167+
expect(response.status).toBe(401);
168+
expect(response.body).toHaveProperty('error', 'Unauthorized');
169+
});
170+
it('should return 401 if token is missing', async () => {
171+
const response = await request(app).get('/auth/session');
172+
expect(response.status).toBe(401);
173+
expect(response.body).toHaveProperty('error', 'Unauthorized');
174+
});
175+
});
176+
177+
describe("POST /auth/request-password-reset", () => {
178+
it("should return 200 and a success message when email is provided", async () => {
179+
const response = await request(app).post("/auth/request-password-reset").send({ email: "[email protected]" });
180+
expect(response.status).toBe(200);
181+
expect(response.body).toHaveProperty("message", "Password reset link sent");
182+
});
183+
184+
it("should return 400 if email is missing", async () => {
185+
const response = await request(app).post("/auth/request-password-reset").send({});
186+
expect(response.status).toBe(400);
187+
expect(response.body).toHaveProperty("error", "Email is required");
188+
});
189+
});
190+
191+
describe("POST /auth/reset-password", () => {
192+
it("should return 200 and a success message when password and token are provided", async () => {
193+
const response = await request(app).post("/auth/reset-password").send({ password: "newPassword123", token: "validToken" });
194+
expect(response.status).toBe(200);
195+
expect(response.body).toHaveProperty("message", "Password reset successfully");
196+
});
197+
198+
it("should return 400 if password or token is missing", async () => {
199+
const response = await request(app).post("/auth/reset-password").send({ password: "newPassword123" });
200+
expect(response.status).toBe(400);
201+
expect(response.body).toHaveProperty("error", "Password and token are required");
202+
});
203+
});
204+
205+
describe("DELETE /auth/accountDelete", () => {
206+
it("should return 200 and a success message when userId is provided", async () => {
207+
const response = await request(app).delete("/auth/accountDelete").send({ userId: "12345" });
208+
expect(response.status).toBe(200);
209+
expect(response.body).toHaveProperty("message", "Account deletion requested");
210+
});
8211

9-
// describe('POST /auth/login', () => {
10-
// it('should return 200 and a token for valid credentials', async () => {
11-
// const response = await request(app)
12-
// .post('/auth/login')
13-
// .send({ username: 'validUser', password: 'validPassword' });
14-
// expect(response.status).toBe(200);
15-
// expect(response.body).toHaveProperty('token');
16-
// });
17-
// it('should return 401 for invalid credentials', async () => {
18-
// const response = await request(app)
19-
// .post('/auth/login')
20-
// .send({ username: 'invalidUser', password: 'wrongPassword' });
21-
// expect(response.status).toBe(401);
22-
// expect(response.body).toHaveProperty('error', 'Invalid credentials');
23-
// });
24-
// it('should return 400 if username or password is missing', async () => {
25-
// const response = await request(app)
26-
// .post('/auth/login')
27-
// .send({ username: 'validUser' });
28-
// expect(response.status).toBe(400);
29-
// expect(response.body).toHaveProperty('error', 'Username and password are required');
30-
// });
31-
// });
32-
// describe('POST /auth/register', () => {
33-
// it('should return 201 and create a new user for valid input', async () => {
34-
// const response = await request(app)
35-
// .post('/auth/register')
36-
// .send({ username: 'newUser', password: 'newPassword' });
37-
// expect(response.status).toBe(201);
38-
// expect(response.body).toHaveProperty('message', 'User registered successfully');
39-
// });
40-
// it('should return 400 if username is already taken', async () => {
41-
// await request(app)
42-
// .post('/auth/register')
43-
// .send({ username: 'existingUser', password: 'password123' });
44-
// const response = await request(app)
45-
// .post('/auth/register')
46-
// .send({ username: 'existingUser', password: 'password123' });
47-
// expect(response.status).toBe(400);
48-
// expect(response.body).toHaveProperty('error', 'Username is already taken');
49-
// });
50-
// it('should return 400 if username or password is missing', async () => {
51-
// const response = await request(app)
52-
// .post('/auth/register')
53-
// .send({ username: '' });
54-
// expect(response.status).toBe(400);
55-
// expect(response.body).toHaveProperty('error', 'Username and password are required');
56-
// });
57-
// });
58-
// describe('GET /auth/profile', () => {
59-
// it('should return 200 and user profile for valid token', async () => {
60-
// const loginResponse = await request(app)
61-
// .post('/auth/login')
62-
// .send({ username: 'validUser', password: 'validPassword' });
63-
// const token = loginResponse.body.token;
64-
// const response = await request(app)
65-
// .get('/auth/profile')
66-
// .set('Authorization', `Bearer ${token}`);
67-
// expect(response.status).toBe(200);
68-
// expect(response.body).toHaveProperty('username', 'validUser');
69-
// });
70-
// it('should return 401 if token is missing or invalid', async () => {
71-
// const response = await request(app)
72-
// .get('/auth/profile')
73-
// .set('Authorization', 'Bearer invalidToken');
74-
// expect(response.status).toBe(401);
75-
// expect(response.body).toHaveProperty('error', 'Unauthorized');
76-
// });
77-
// });
78-
it("template test", () => {
79-
expect(2 + 3).toEqual(5);
80-
});
212+
it("should return 400 if userId is missing", async () => {
213+
const response = await request(app).delete("/auth/accountDelete").send({});
214+
expect(response.status).toBe(400);
215+
expect(response.body).toHaveProperty("error", "User ID is required");
216+
});
217+
});
81218
});

0 commit comments

Comments
 (0)