swadhinbiswas Add advanced monorepo path-scope management and enforcement · 2/19/2026
f321d69
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
import { beforeEach, describe, expect, it, vi } from "vitest";
const {
getUserFromRequestMock,
canReadRepoMock,
canAdminRepoMock,
getPathPermissionsMock,
fakeSchema,
} = vi.hoisted(() => ({
getUserFromRequestMock: vi.fn(async () => ({ userId: "user-1", isAdmin: false })),
canReadRepoMock: vi.fn(async () => true),
canAdminRepoMock: vi.fn(async () => true),
getPathPermissionsMock: vi.fn(async () => ([
{
id: "perm-1",
repositoryId: "repo-1",
pathPattern: "packages/web/**",
userId: "user-1",
teamId: null,
permission: "write",
requireApproval: "false",
createdAt: new Date("2026-02-19T00:00:00Z"),
updatedAt: new Date("2026-02-19T00:00:00Z"),
},
])),
fakeSchema: {
users: { username: {} },
repositories: { ownerId: {}, name: {}, id: {} },
repositoryPathPermissions: { id: {}, repositoryId: {} },
} as any,
}));
let mockDb: any;
vi.mock("@/db", () => ({
getDatabase: () => mockDb,
schema: fakeSchema,
}));
vi.mock("@/lib/auth", () => ({
getUserFromRequest: getUserFromRequestMock,
}));
vi.mock("@/lib/permissions", () => ({
canReadRepo: canReadRepoMock,
canAdminRepo: canAdminRepoMock,
}));
vi.mock("@/lib/path-scoping", () => ({
getPathPermissions: getPathPermissionsMock,
}));
import { GET as pathPermissionsGet, POST as pathPermissionsPost } from "@/pages/api/repos/[owner]/[repo]/settings/path-permissions";
import { PUT as pathPermissionsPut, DELETE as pathPermissionsDelete } from "@/pages/api/repos/[owner]/[repo]/settings/path-permissions/[id]";
function makeDb() {
const owner = { id: "owner-1", username: "acme" };
const repo = { id: "repo-1", ownerId: "owner-1", name: "demo" };
const pathPermission = {
id: "perm-1",
repositoryId: "repo-1",
pathPattern: "packages/web/**",
userId: "user-1",
teamId: null,
permission: "write",
requireApproval: "false",
};
const insertCalls: Array<unknown> = [];
const updateCalls: Array<unknown> = [];
const deleteCalls: Array<unknown> = [];
return {
query: {
users: {
findFirst: vi.fn(async () => owner),
},
repositories: {
findFirst: vi.fn(async () => repo),
},
repositoryPathPermissions: {
findFirst: vi.fn(async () => pathPermission),
},
},
insert: vi.fn(() => ({
values: vi.fn(async (value: unknown) => {
insertCalls.push(value);
}),
})),
update: vi.fn(() => ({
set: vi.fn((value: unknown) => {
updateCalls.push(value);
return {
where: vi.fn(async () => undefined),
};
}),
})),
delete: vi.fn(() => ({
where: vi.fn(async (value: unknown) => {
deleteCalls.push(value);
}),
})),
__state: {
insertCalls,
updateCalls,
deleteCalls,
},
};
}
async function readJson(response: Response): Promise<any> {
return response.json();
}
describe("repository path permissions settings routes", () => {
beforeEach(() => {
mockDb = makeDb();
getUserFromRequestMock.mockResolvedValue({ userId: "user-1", isAdmin: false });
canReadRepoMock.mockResolvedValue(true);
canAdminRepoMock.mockResolvedValue(true);
});
it("lists path permission rules for readers", async () => {
const response = await pathPermissionsGet({
params: { owner: "acme", repo: "demo" },
request: new Request("http://localhost/api/repos/acme/demo/settings/path-permissions"),
} as any);
const body = await readJson(response);
expect(response.status).toBe(200);
expect(body?.data?.rules).toHaveLength(1);
expect(body?.data?.summary?.totalRules).toBe(1);
});
it("creates a path permission for repo admins", async () => {
const response = await pathPermissionsPost({
params: { owner: "acme", repo: "demo" },
request: new Request("http://localhost/api/repos/acme/demo/settings/path-permissions", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
pathPattern: "packages/api/**",
teamId: "team-1",
permission: "write",
requireApproval: true,
}),
}),
} as any);
const body = await readJson(response);
expect(response.status).toBe(201);
expect(body?.data?.pathPattern).toBe("packages/api/**");
expect(body?.data?.teamId).toBe("team-1");
expect(body?.data?.requireApproval).toBe("true");
expect(mockDb.__state.insertCalls).toHaveLength(1);
});
it("rejects creation when neither userId nor teamId is provided", async () => {
const response = await pathPermissionsPost({
params: { owner: "acme", repo: "demo" },
request: new Request("http://localhost/api/repos/acme/demo/settings/path-permissions", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
pathPattern: "packages/shared/**",
permission: "read",
}),
}),
} as any);
expect(response.status).toBe(400);
expect(mockDb.__state.insertCalls).toHaveLength(0);
});
it("updates a rule for repo admins", async () => {
const response = await pathPermissionsPut({
params: { owner: "acme", repo: "demo", id: "perm-1" },
request: new Request("http://localhost/api/repos/acme/demo/settings/path-permissions/perm-1", {
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify({
pathPattern: "packages/mobile/**",
permission: "admin",
requireApproval: true,
}),
}),
} as any);
const body = await readJson(response);
expect(response.status).toBe(200);
expect(body?.data?.updated).toBe(true);
expect(mockDb.__state.updateCalls).toHaveLength(1);
});
it("deletes a rule for repo admins", async () => {
const response = await pathPermissionsDelete({
params: { owner: "acme", repo: "demo", id: "perm-1" },
request: new Request("http://localhost/api/repos/acme/demo/settings/path-permissions/perm-1", {
method: "DELETE",
}),
} as any);
const body = await readJson(response);
expect(response.status).toBe(200);
expect(body?.data?.deleted).toBe(true);
expect(mockDb.__state.deleteCalls).toHaveLength(1);
});
});