Skip to content

Commit 604f9b7

Browse files
dsshimelclaude
andcommitted
add tests for /api/auth/me endpoint
Covers instructor response (no cohort fields), student with cohort dates, and student with null cohort dates. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e7eaffd commit 604f9b7

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* @fileoverview Tests for GET /api/auth/me route.
3+
* Verifies role, identity, and cohort date fields in the response.
4+
*/
5+
6+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
7+
import Database from "better-sqlite3";
8+
import express from "express";
9+
import request from "supertest";
10+
import {
11+
createTestDatabase,
12+
createTestCohort,
13+
createTestStudent,
14+
createTestUser,
15+
} from "../utils/testUtils";
16+
17+
// Mock authenticateToken to inject req.user directly
18+
vi.mock("../../api/middleware/auth", () => ({
19+
authenticateToken: vi.fn((req: any, _res: any, next: any) => {
20+
// Test sets req.user before calling the route
21+
next();
22+
}),
23+
}));
24+
25+
// Mock getDatabase to return our test DB
26+
const mockGetDatabase = vi.fn();
27+
vi.mock("../../services/db", () => ({
28+
getDatabase: () => mockGetDatabase(),
29+
}));
30+
31+
import { meRouter } from "../../api/routes/me";
32+
33+
function createApp(user: { authenticated: boolean; username: string; role: "instructor" | "student"; discordAccountId?: string }) {
34+
const app = express();
35+
// Inject req.user before the route runs
36+
app.use((req: any, _res, next) => {
37+
req.user = user;
38+
next();
39+
});
40+
app.use("/api/auth/me", meRouter);
41+
return app;
42+
}
43+
44+
describe("GET /api/auth/me", () => {
45+
let db: Database.Database;
46+
47+
beforeEach(() => {
48+
db = createTestDatabase();
49+
mockGetDatabase.mockReturnValue(db);
50+
});
51+
52+
afterEach(() => {
53+
db.close();
54+
vi.clearAllMocks();
55+
});
56+
57+
it("returns role and name for an instructor", async () => {
58+
const app = createApp({
59+
authenticated: true,
60+
username: "InstructorUser",
61+
role: "instructor",
62+
discordAccountId: "discord-instructor",
63+
});
64+
65+
const res = await request(app).get("/api/auth/me");
66+
67+
expect(res.status).toBe(200);
68+
expect(res.body.role).toBe("instructor");
69+
expect(res.body.name).toBe("InstructorUser");
70+
expect(res.body).not.toHaveProperty("studentId");
71+
expect(res.body).not.toHaveProperty("cohortStartDate");
72+
expect(res.body).not.toHaveProperty("cohortEndDate");
73+
});
74+
75+
it("returns student info with cohort dates when cohort has dates", async () => {
76+
const user = createTestUser(db, "discord-student", "studentuser");
77+
const cohort = createTestCohort(db, "Sp2026");
78+
db.prepare("UPDATE cohorts SET start_date = ?, end_date = ? WHERE id = ?")
79+
.run("2026-02-02", "2026-05-02", cohort.id);
80+
createTestStudent(db, {
81+
name: "Julian Wemmie",
82+
cohortId: cohort.id,
83+
discordUserId: user.author_id,
84+
});
85+
86+
const app = createApp({
87+
authenticated: true,
88+
username: "studentuser",
89+
role: "student",
90+
discordAccountId: "discord-student",
91+
});
92+
93+
const res = await request(app).get("/api/auth/me");
94+
95+
expect(res.status).toBe(200);
96+
expect(res.body.role).toBe("student");
97+
expect(res.body.studentName).toBe("Julian Wemmie");
98+
expect(res.body.cohortId).toBe(cohort.id);
99+
expect(res.body.cohortStartDate).toBe("2026-02-02");
100+
expect(res.body.cohortEndDate).toBe("2026-05-02");
101+
});
102+
103+
it("omits cohort dates when cohort has no dates set", async () => {
104+
const user = createTestUser(db, "discord-student2", "studentuser2");
105+
const cohort = createTestCohort(db, "Fa2025");
106+
// No dates set — columns are NULL by default
107+
createTestStudent(db, {
108+
name: "No Dates Student",
109+
cohortId: cohort.id,
110+
discordUserId: user.author_id,
111+
});
112+
113+
const app = createApp({
114+
authenticated: true,
115+
username: "studentuser2",
116+
role: "student",
117+
discordAccountId: "discord-student2",
118+
});
119+
120+
const res = await request(app).get("/api/auth/me");
121+
122+
expect(res.status).toBe(200);
123+
expect(res.body.role).toBe("student");
124+
expect(res.body.studentId).toBeDefined();
125+
expect(res.body.studentName).toBe("No Dates Student");
126+
expect(res.body.cohortId).toBe(cohort.id);
127+
expect(res.body).not.toHaveProperty("cohortStartDate");
128+
expect(res.body).not.toHaveProperty("cohortEndDate");
129+
});
130+
});

0 commit comments

Comments
 (0)