-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(keys/service.ts): enhance error message in KeyService for missin…
…g ratelimit
- Loading branch information
Showing
2 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { describe, expect, test } from "vitest"; | ||
|
||
import type { ErrorResponse } from "@/pkg/errors"; | ||
import { schema } from "@unkey/db"; | ||
import { newId } from "@unkey/id"; | ||
import { IntegrationHarness } from "src/pkg/testutil/integration-harness"; | ||
|
||
describe("with identity", () => { | ||
describe("with ratelimits", () => { | ||
describe("missing ratelimit", () => { | ||
test("returns 400 and a useful error message", async (t) => { | ||
const h = await IntegrationHarness.init(t); | ||
|
||
const identity = { | ||
id: newId("test"), | ||
workspaceId: h.resources.userWorkspace.id, | ||
externalId: newId("test"), | ||
}; | ||
await h.db.primary.insert(schema.identities).values(identity); | ||
await h.db.primary.insert(schema.ratelimits).values({ | ||
id: newId("test"), | ||
workspaceId: h.resources.userWorkspace.id, | ||
name: "existing-ratelimit", | ||
identityId: identity.id, | ||
limit: 100, | ||
duration: 60_000, | ||
}); | ||
|
||
const key = await h.createKey({ identityId: identity.id }); | ||
|
||
const res = await h.post<any, ErrorResponse>({ | ||
url: "/v1/keys.verifyKey", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: { | ||
key: key.key, | ||
ratelimits: [ | ||
{ | ||
name: "does-not-exist", | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
expect(res.status).toEqual(400); | ||
expect(res.body.error.message).toMatchInlineSnapshot( | ||
`"ratelimit "does-not-exist" was requested but does not exist for key "test_2BC2hsZxB7P9WLR37L8K4jSwMugv" nor identity { id: test_2BC2hZebF4du9PYnijrmSryNP9xe, externalId: test_2BC2hZjbrfj1Zb1HgudRYbonQzpx}"`, | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("without identity", () => { | ||
describe("with ratelimits", () => { | ||
describe("missing ratelimit", () => { | ||
test("returns 400 and a useful error message", async (t) => { | ||
const h = await IntegrationHarness.init(t); | ||
|
||
const key = await h.createKey(); | ||
|
||
await h.db.primary.insert(schema.ratelimits).values({ | ||
id: newId("test"), | ||
workspaceId: h.resources.userWorkspace.id, | ||
name: "existing-ratelimit", | ||
keyId: key.keyId, | ||
limit: 100, | ||
duration: 60_000, | ||
}); | ||
|
||
const res = await h.post<any, ErrorResponse>({ | ||
url: "/v1/keys.verifyKey", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: { | ||
key: key.key, | ||
ratelimits: [ | ||
{ | ||
name: "does-not-exist", | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
expect(res.status).toEqual(400); | ||
expect(res.body.error.message).toMatchInlineSnapshot( | ||
`"ratelimit "does-not-exist" was requested but does not exist for key "test_2BC2mdMQYnubdoAsBb2PD7MhooPs" and there is no identity connected"`, | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |