Skip to content

Commit f46808b

Browse files
committed
2 parents 429a6a2 + 89a75a3 commit f46808b

File tree

7 files changed

+563
-429
lines changed

7 files changed

+563
-429
lines changed
Lines changed: 296 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,301 @@
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 { jest, describe, it, expect, afterAll } from "@jest/globals";
4+
import app, { server } from "../src/index";
5+
6+
jest.mock("@ai-sdk/openai", () => ({
7+
createOpenAI: jest.fn(() => ({
8+
chat: jest.fn(),
9+
})),
10+
}));
11+
12+
jest.mock("ai", () => ({
13+
streamText: jest.fn(() =>
14+
Promise.resolve({ pipeDataStreamToResponse: jest.fn() }),
15+
),
16+
}));
17+
18+
jest.mock("@pinecone-database/pinecone", () => ({
19+
Pinecone: jest.fn(() => ({
20+
Index: jest.fn(() => ({
21+
query: jest.fn(),
22+
upsert: jest.fn(),
23+
delete: jest.fn(),
24+
})),
25+
})),
26+
}));
27+
28+
jest.mock("node-cron", () => ({
29+
schedule: jest.fn(), // Mock the `schedule` function
30+
}));
31+
32+
jest.mock("../src/db/setupDb", () => ({
33+
supabase: {
34+
from: jest.fn(() => ({
35+
select: jest.fn(),
36+
insert: jest.fn(),
37+
update: jest.fn(),
38+
delete: jest.fn(),
39+
})),
40+
},
41+
}));
42+
43+
jest.mock("../src/controllers/userController", () => ({
44+
signUp: jest.fn((req: Request, res: Response) => {
45+
const { email, password, username } = req.body;
46+
if (!email || !password || !username) {
47+
return res
48+
.status(400)
49+
.json({ error: "Email, password, and username are required" });
50+
}
51+
if (email === "[email protected]") {
52+
return res.status(400).json({ error: "Email is already taken" });
53+
}
54+
res.status(201).json({
55+
message: "User registered successfully",
56+
user: { email, username },
57+
});
58+
}),
59+
login: jest.fn((req: Request, res: Response) => {
60+
const { email, password } = req.body;
61+
if (!email || !password) {
62+
return res.status(400).json({ error: "Email and password are required" });
63+
}
64+
if (email === "validUser" && password === "validPassword") {
65+
res.status(200).json({ token: "mockedToken" });
66+
} else {
67+
res.status(401).json({ error: "Invalid credentials" });
68+
}
69+
}),
70+
logout: jest.fn((req: Request, res: Response) => {
71+
const token = req.headers.authorization?.split(" ")[1];
72+
if (!token) {
73+
return res.status(401).json({ error: "Unauthorized" });
74+
}
75+
res.status(200).json({ message: "Logged out successfully" });
76+
}),
77+
session: jest.fn((req: Request, res: Response) => {
78+
const token = req.headers.authorization?.split(" ")[1];
79+
if (!token || token !== "mockedToken") {
80+
return res.status(401).json({ error: "Unauthorized" });
81+
}
82+
res
83+
.status(200)
84+
.json({ message: "Session valid", user: { email: "validUser" } });
85+
}),
86+
requestPasswordReset: jest.fn((req: Request, res: Response) => {
87+
const { email } = req.body;
88+
if (!email) {
89+
return res.status(400).json({ error: "Email is required" });
90+
}
91+
res.status(200).json({ message: "Password reset link sent" });
92+
}),
93+
resetPassword: jest.fn((req: Request, res: Response) => {
94+
const { password, token } = req.body;
95+
if (!password || !token) {
96+
return res.status(400).json({ error: "Password and token are required" });
97+
}
98+
res.status(200).json({ message: "Password reset successfully" });
99+
}),
100+
accountDelete: jest.fn((req: Request, res: Response) => {
101+
const { userId } = req.body;
102+
if (!userId) {
103+
return res.status(400).json({ error: "User ID is required" });
104+
}
105+
res.status(200).json({ message: "Account deletion requested" });
106+
}),
107+
updateUsername: jest.fn((req: Request, res: Response) => {
108+
const { userId, newUsername } = req.body;
109+
if (!userId || !newUsername) {
110+
return res
111+
.status(400)
112+
.json({ error: "User ID and new username are required" });
113+
}
114+
res.status(200).json({ message: "Username updated successfully" });
115+
}),
116+
usernameFromUserId: jest.fn((req: Request, res: Response) => {
117+
const { userId } = req.query;
118+
if (!userId) {
119+
return res.status(400).json({ error: "User ID is required" });
120+
}
121+
res.status(200).json({ username: "mockedUsername" });
122+
}),
123+
}));
124+
125+
afterAll(async () => {
126+
server.close();
127+
});
4128

5129
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.
8-
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);
130+
describe("POST /auth/signup", () => {
131+
it("should return 201 and a success message for valid signup data", async () => {
132+
const response = await request(app).post("/auth/signup").send({
133+
134+
password: "securePassword123",
135+
username: "newUser",
136+
});
137+
expect(response.status).toBe(201);
138+
expect(response.body).toHaveProperty(
139+
"message",
140+
"User registered successfully",
141+
);
142+
expect(response.body.user).toHaveProperty("email", "[email protected]");
143+
expect(response.body.user).toHaveProperty("username", "newUser");
144+
});
145+
146+
it("should return 400 if email is already taken", async () => {
147+
const response = await request(app).post("/auth/signup").send({
148+
149+
password: "securePassword123",
150+
username: "existingUser",
151+
});
152+
expect(response.status).toBe(400);
153+
expect(response.body).toHaveProperty("error", "Email is already taken");
154+
});
155+
156+
it("should return 400 if required fields are missing", async () => {
157+
const response = await request(app).post("/auth/signup").send({
158+
159+
});
160+
expect(response.status).toBe(400);
161+
expect(response.body).toHaveProperty(
162+
"error",
163+
"Email, password, and username are required",
164+
);
165+
});
166+
});
167+
168+
describe("POST /auth/login", () => {
169+
it("should return 200 and a token for valid credentials", async () => {
170+
const response = await request(app)
171+
.post("/auth/login")
172+
.send({ email: "validUser", password: "validPassword" });
173+
expect(response.status).toBe(200);
174+
expect(response.body).toHaveProperty("token");
175+
});
176+
it("should return 401 for invalid credentials", async () => {
177+
const response = await request(app)
178+
.post("/auth/login")
179+
.send({ email: "invalidUser", password: "wrongPassword" });
180+
expect(response.status).toBe(401);
181+
expect(response.body).toHaveProperty("error", "Invalid credentials");
182+
});
183+
it("should return 400 if email or password is missing", async () => {
184+
const response = await request(app)
185+
.post("/auth/login")
186+
.send({ email: "validUser" });
187+
expect(response.status).toBe(400);
188+
expect(response.body).toHaveProperty(
189+
"error",
190+
"Email and password are required",
191+
);
192+
});
193+
});
194+
195+
describe("POST /auth/logout", () => {
196+
it("should return 200 for successful logout", async () => {
197+
const response = await request(app)
198+
.post("/auth/logout")
199+
.set("Authorization", "Bearer mockedToken");
200+
expect(response.status).toBe(200);
201+
expect(response.body).toHaveProperty(
202+
"message",
203+
"Logged out successfully",
204+
);
205+
});
206+
it("should return 401 if token is missing", async () => {
207+
const response = await request(app).post("/auth/logout");
208+
expect(response.status).toBe(401);
209+
expect(response.body).toHaveProperty("error", "Unauthorized");
210+
});
211+
});
212+
213+
describe("GET /auth/session", () => {
214+
it("should return 200 and session info for valid token", async () => {
215+
const response = await request(app)
216+
.get("/auth/session")
217+
.set("Authorization", "Bearer mockedToken");
218+
expect(response.status).toBe(200);
219+
expect(response.body).toHaveProperty("message", "Session valid");
220+
expect(response.body.user).toHaveProperty("email", "validUser");
221+
});
222+
it("should return 401 if token is missing or invalid", async () => {
223+
const response = await request(app)
224+
.get("/auth/session")
225+
.set("Authorization", "Bearer invalidToken");
226+
expect(response.status).toBe(401);
227+
expect(response.body).toHaveProperty("error", "Unauthorized");
228+
});
229+
it("should return 401 if token is missing", async () => {
230+
const response = await request(app).get("/auth/session");
231+
expect(response.status).toBe(401);
232+
expect(response.body).toHaveProperty("error", "Unauthorized");
233+
});
234+
});
235+
236+
describe("POST /auth/request-password-reset", () => {
237+
it("should return 200 and a success message when email is provided", async () => {
238+
const response = await request(app)
239+
.post("/auth/request-password-reset")
240+
.send({ email: "[email protected]" });
241+
expect(response.status).toBe(200);
242+
expect(response.body).toHaveProperty(
243+
"message",
244+
"Password reset link sent",
245+
);
246+
});
247+
248+
it("should return 400 if email is missing", async () => {
249+
const response = await request(app)
250+
.post("/auth/request-password-reset")
251+
.send({});
252+
expect(response.status).toBe(400);
253+
expect(response.body).toHaveProperty("error", "Email is required");
254+
});
255+
});
256+
257+
describe("POST /auth/reset-password", () => {
258+
it("should return 200 and a success message when password and token are provided", async () => {
259+
const response = await request(app)
260+
.post("/auth/reset-password")
261+
.send({ password: "newPassword123", token: "validToken" });
262+
expect(response.status).toBe(200);
263+
expect(response.body).toHaveProperty(
264+
"message",
265+
"Password reset successfully",
266+
);
267+
});
268+
269+
it("should return 400 if password or token is missing", async () => {
270+
const response = await request(app)
271+
.post("/auth/reset-password")
272+
.send({ password: "newPassword123" });
273+
expect(response.status).toBe(400);
274+
expect(response.body).toHaveProperty(
275+
"error",
276+
"Password and token are required",
277+
);
278+
});
279+
});
280+
281+
describe("DELETE /auth/accountDelete", () => {
282+
it("should return 200 and a success message when userId is provided", async () => {
283+
const response = await request(app)
284+
.delete("/auth/accountDelete")
285+
.send({ userId: "12345" });
286+
expect(response.status).toBe(200);
287+
expect(response.body).toHaveProperty(
288+
"message",
289+
"Account deletion requested",
290+
);
291+
});
292+
293+
it("should return 400 if userId is missing", async () => {
294+
const response = await request(app)
295+
.delete("/auth/accountDelete")
296+
.send({});
297+
expect(response.status).toBe(400);
298+
expect(response.body).toHaveProperty("error", "User ID is required");
299+
});
80300
});
81301
});

0 commit comments

Comments
 (0)