|
1 | 1 | import { describe, expect, test } from "vitest";
|
2 | 2 |
|
| 3 | +import { randomUUID } from "node:crypto"; |
3 | 4 | import { IntegrationHarness } from "src/pkg/testutil/integration-harness";
|
4 | 5 |
|
5 | 6 | import type {
|
@@ -39,3 +40,46 @@ describe.each([
|
39 | 40 | });
|
40 | 41 | });
|
41 | 42 | });
|
| 43 | + |
| 44 | +test("creating the same permission twice returns conflict", async (t) => { |
| 45 | + const h = await IntegrationHarness.init(t); |
| 46 | + const root = await h.createRootKey(["rbac.*.create_permission"]); |
| 47 | + |
| 48 | + const createPermissionRequest = { |
| 49 | + url: "/v1/permissions.createPermission", |
| 50 | + headers: { |
| 51 | + "Content-Type": "application/json", |
| 52 | + Authorization: `Bearer ${root.key}`, |
| 53 | + }, |
| 54 | + body: { |
| 55 | + name: randomUUID(), |
| 56 | + }, |
| 57 | + }; |
| 58 | + |
| 59 | + const successResponse = await h.post< |
| 60 | + V1PermissionsCreatePermissionRequest, |
| 61 | + V1PermissionsCreatePermissionResponse |
| 62 | + >(createPermissionRequest); |
| 63 | + |
| 64 | + expect( |
| 65 | + successResponse.status, |
| 66 | + `expected 200, received: ${JSON.stringify(successResponse, null, 2)}`, |
| 67 | + ).toBe(200); |
| 68 | + |
| 69 | + const errorResponse = await h.post< |
| 70 | + V1PermissionsCreatePermissionRequest, |
| 71 | + V1PermissionsCreatePermissionResponse |
| 72 | + >(createPermissionRequest); |
| 73 | + |
| 74 | + expect( |
| 75 | + errorResponse.status, |
| 76 | + `expected 409, received: ${JSON.stringify(errorResponse, null, 2)}`, |
| 77 | + ).toBe(409); |
| 78 | + expect(errorResponse.body).toMatchObject({ |
| 79 | + error: { |
| 80 | + code: "CONFLICT", |
| 81 | + docs: "https://unkey.dev/docs/api-reference/errors/code/CONFLICT", |
| 82 | + message: `Permission with name "${createPermissionRequest.body.name}" already exists in this workspace`, |
| 83 | + }, |
| 84 | + }); |
| 85 | +}); |
0 commit comments