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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
import { describe, it, expect, mock, beforeEach, afterAll } from "bun:test";
const _real_db = await import("../db");
const _real_cache = await import("../lib/cache");
let _nextSessionRow: any = null;
let _nextUserRow: any = null;
let _lastSelectFrom: any = null;
const tableName = (t: any): string => {
if (!t || typeof t !== "object") return "?";
if ("token" in t && "expiresAt" in t && "userId" in t) return "sessions";
if ("passwordHash" in t && "username" in t) return "users";
if ("fingerprint" in t || "publicKey" in t) return "ssh_keys";
return "?";
};
const _selectChain: any = {
from: (t: any) => {
_lastSelectFrom = t;
return _selectChain;
},
innerJoin: () => _selectChain,
leftJoin: () => _selectChain,
where: () => _selectChain,
orderBy: () => _selectChain,
limit: async () => {
const name = tableName(_lastSelectFrom);
if (name === "sessions") return _nextSessionRow ? [_nextSessionRow] : [];
if (name === "users") return _nextUserRow ? [_nextUserRow] : [];
return [];
},
then: (resolve: (v: any) => void) => {
resolve([]);
},
};
const _fakeDb = {
db: {
select: () => _selectChain,
insert: () => ({
values: () => ({
returning: async () => [],
then: (r: (v: any) => void) => r(undefined),
}),
}),
update: () => ({
set: () => ({ where: () => Promise.resolve() }),
}),
delete: () => ({ where: () => Promise.resolve() }),
},
getDb: () => _fakeDb.db,
};
mock.module("../db", () => ({ ..._real_db, ..._fakeDb }));
const { default: app } = await import("../app");
const { sessionCache } = await import("../lib/cache");
const USER_ID = "44444444-4444-4444-4444-444444444444";
const SESSION_TOKEN = "test-session-token-layout-userprop";
const TEST_USER = {
id: USER_ID,
username: "regression_user",
displayName: "Regression User",
email: "reg@example.com",
passwordHash: "x",
bio: null,
createdAt: new Date(),
updatedAt: new Date(),
};
const TEST_SESSION = {
userId: USER_ID,
token: SESSION_TOKEN,
expiresAt: new Date(Date.now() + 60 * 60 * 1000),
requires2fa: false,
};
function authedHeaders(): HeadersInit {
return { cookie: `session=${SESSION_TOKEN}` };
}
beforeEach(() => {
_nextSessionRow = TEST_SESSION;
_nextUserRow = TEST_USER;
sessionCache.set(SESSION_TOKEN, TEST_USER as any);
});
afterAll(() => {
sessionCache.invalidate(SESSION_TOKEN);
_nextSessionRow = null;
_nextUserRow = null;
mock.module("../db", () => _real_db);
});
const LOGGED_OUT_NAV_MARKER = `href="/login" class="nav-link"`;
function assertAuthedNav(html: string) {
expect(html).toContain('class="nav-user"');
expect(html).toContain(TEST_USER.displayName);
expect(html).not.toContain(LOGGED_OUT_NAV_MARKER);
}
describe("Layout user= prop is forwarded on authed routes", () => {
it("/settings renders the user nav (was missing user= before fix)", async () => {
const res = await app.request("/settings", { headers: authedHeaders() });
expect(res.status).toBe(200);
const body = await res.text();
assertAuthedNav(body);
});
it("/settings/keys renders the user nav", async () => {
const res = await app.request("/settings/keys", {
headers: authedHeaders(),
});
expect(res.status).toBe(200);
const body = await res.text();
assertAuthedNav(body);
});
it("/new (new repo form) renders the user nav", async () => {
const res = await app.request("/new", { headers: authedHeaders() });
expect(res.status).toBe(200);
const body = await res.text();
assertAuthedNav(body);
});
it("global 404 page renders the user nav when authed", async () => {
const res = await app.request(
"/__nope__/__nope__/__nope__/__nope__/__nope__",
{ headers: authedHeaders() }
);
expect(res.status).toBe(404);
const body = await res.text();
assertAuthedNav(body);
});
});
describe("auth landing pages bounce already-authed users", () => {
it("/login with a live session 302s to /dashboard", async () => {
const res = await app.request("/login", { headers: authedHeaders() });
expect(res.status).toBe(302);
expect(res.headers.get("location")).toBe("/dashboard");
});
it("/login?redirect=/foo honours the redirect target when authed", async () => {
const res = await app.request("/login?redirect=%2Ffoo%2Fbar", {
headers: authedHeaders(),
});
expect(res.status).toBe(302);
expect(res.headers.get("location")).toBe("/foo/bar");
});
it("/register with a live session 302s to /dashboard", async () => {
const res = await app.request("/register", { headers: authedHeaders() });
expect(res.status).toBe(302);
expect(res.headers.get("location")).toBe("/dashboard");
});
it("/login WITHOUT a session still renders the sign-in shell", async () => {
const res = await app.request("/login");
expect(res.status).toBe(200);
const body = await res.text();
expect(body).toContain(LOGGED_OUT_NAV_MARKER);
});
});
|