Skip to content

Commit c417d13

Browse files
committed
feat: require create:user to access /api/v1/users
1 parent d7e20db commit c417d13

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

pages/api/v1/users/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import activation from "models/activation";
55

66
const router = createRouter();
77

8-
router.post(postHandler);
8+
router.use(controller.injectAnonymousOrUser);
9+
router.post(controller.canRequest("create:user"), postHandler);
910

1011
export default router.handler(controller.errorHandlers);
1112

tests/integration/api/v1/users/post.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,34 @@ describe("POST to api/v1/users", () => {
131131
});
132132
});
133133
});
134+
135+
describe("Default user", () => {
136+
test("With unique and valid data", async () => {
137+
const user1 = await orchestrator.createUser();
138+
await orchestrator.activateUser(user1);
139+
const user1SessionObject = await orchestrator.createSession(user1.id);
140+
141+
const user2Response = await fetch("http://localhost:3000/api/v1/users", {
142+
method: "POST",
143+
headers: {
144+
"Content-Type": "application/json",
145+
Cookie: `session_id=${user1SessionObject.token}`,
146+
},
147+
body: JSON.stringify({
148+
username: "loggedUser",
149+
150+
password: "password123",
151+
}),
152+
});
153+
expect(user2Response.status).toBe(403);
154+
155+
const user2ResponseBody = await user2Response.json();
156+
expect(user2ResponseBody).toEqual({
157+
name: "ForbiddenError",
158+
message: "You don't have permission to execute this action.",
159+
action: "You must gain the correct permissions for this.",
160+
status_code: 403,
161+
});
162+
});
163+
});
134164
});

0 commit comments

Comments
 (0)