/ opencodehub / tests / integration / user-route.test.ts
Swadhin Biswas feat: production readiness — fix critical bugs, achieve 100% test pass rate, complete documentation · 4/30/2026
ac8f8cc
139 lines · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 * Integration tests for GET /api/user
 */
import { beforeEach, describe, expect, it, vi } from "vitest";

/* ─── hoisted mocks ─── */
const mocks = vi.hoisted(() => ({
  getUserFromRequestMock: vi.fn().mockResolvedValue({
    userId: "usr_1",
    username: "alice",
    email: "[email protected]",
  }),
  fakeSchema: { users: "users" },
}));

vi.mock("@/db", () => ({
  getDatabase: () => mockDb,
  schema: mocks.fakeSchema,
}));

vi.mock("@/db/schema/users", () => ({
  users: { id: "id" },
}));

vi.mock("@/lib/auth", () => ({
  getUserFromRequest: mocks.getUserFromRequestMock,
}));

vi.mock("@/lib/errors", () => ({
  withErrorHandler: (fn: any) => fn,
}));

vi.mock("drizzle-orm", () => ({
  eq: vi.fn((...a: any[]) => a),
}));

/* ─── helpers ─── */
const fakeUser = {
  id: "usr_1",
  username: "alice",
  email: "[email protected]",
  displayName: "Alice",
  avatarUrl: null,
  bio: "dev",
  location: "NYC",
  website: "",
  company: "ACME",
  isAdmin: false,
  createdAt: new Date(),
};

function makeDb() {
  return {
    query: {
      users: {
        findFirst: vi.fn().mockResolvedValue(fakeUser),
      },
    },
  };
}

let mockDb: ReturnType<typeof makeDb>;
const readJson = (r: Response) => r.json();

function ctx() {
  return {
    request: new Request("http://localhost/api/user"),
  } as any;
}

/* ─── import route ─── */
import { GET } from "@/pages/api/user/index";

/* ─── tests ─── */
describe("GET /api/user", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockDb = makeDb();
    mocks.getUserFromRequestMock.mockResolvedValue({
      userId: "usr_1",
      username: "alice",
      email: "[email protected]",
    });
  });

  it("returns 200 with user profile", async () => {
    const res = await GET(ctx());
    expect(res.status).toBe(200);
    const json = await readJson(res);
    expect(json.data.username).toBe("alice");
    expect(json.data.email).toBe("[email protected]");
    expect(json.data.id).toBe("usr_1");
    expect(json.data.isAdmin).toBe(false);
  });

  it("returns 401 when not authenticated", async () => {
    mocks.getUserFromRequestMock.mockResolvedValue(null);
    const res = await GET(ctx());
    expect(res.status).toBe(401);
  });

  it("returns 401 when user not found in db", async () => {
    mockDb.query.users.findFirst.mockResolvedValue(null);
    const res = await GET(ctx());
    expect(res.status).toBe(401);
  });

  it("does not include passwordHash in response", async () => {
    mockDb.query.users.findFirst.mockResolvedValue({
      ...fakeUser,
      passwordHash: "secret_hash",
    });
    const res = await GET(ctx());
    const json = await readJson(res);
    expect(json.data.passwordHash).toBeUndefined();
  });

  it("includes all expected fields", async () => {
    const res = await GET(ctx());
    const json = await readJson(res);
    const expectedFields = [
      "id",
      "username",
      "email",
      "displayName",
      "avatarUrl",
      "bio",
      "location",
      "website",
      "company",
      "isAdmin",
      "createdAt",
    ];
    for (const field of expectedFields) {
      expect(json.data).toHaveProperty(field);
    }
  });
});