From 99aed63f671426c135e39bbd3416a63c273fb55b Mon Sep 17 00:00:00 2001 From: Lyra Naeseth Date: Sat, 8 Jun 2024 19:49:05 -0700 Subject: [PATCH] Fix tests --- test/index.test.ts | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index 12faa59..170cb99 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -3,7 +3,6 @@ import { jwt } from '../src' import { describe, expect, it } from 'bun:test' -const req = (path: string) => new Request(`http://localhost${path}`) const post = (path: string, body = {}) => new Request(`http://localhost${path}`, { method: 'POST', @@ -13,8 +12,8 @@ const post = (path: string, body = {}) => body: JSON.stringify(body) }) -describe('Static Plugin', () => { - it('sign JWT', async () => { +describe('JWT Plugin', () => { + it('signs and verifies JWTs', async () => { const app = new Elysia() .use( jwt({ @@ -22,25 +21,27 @@ describe('Static Plugin', () => { secret: 'A' }) ) - .post('/validate', ({ jwt, body }) => jwt.sign(body), { - body: t.Object({ - name: t.String() - }) - }) - .post('/validate', ({ jwt, body: { name } }) => jwt.verify(name), { + .post('/sign', ({ jwt, body }) => jwt.sign(body), { body: t.Object({ name: t.String() }) }) + .post('/verify', ({ jwt, body: { token } }) => jwt.verify(token), { + body: t.Object({ token: t.String() }) + }) const name = 'Shirokami' - const _sign = post('/sign', { name }) - const token = await _sign.text() + const sign = await app.handle(post('/sign', { name })) + const token = await sign.text() + expect(token).toMatch(/^[A-Za-z0-9_-]{2,}(?:\.[A-Za-z0-9_-]{2,}){2}$/) - const _verified = post('/verify', { name }) - const signed = (await _verified.json()) as { - name: string - } + const verify = await app.handle(post('/verify', { token })) + const signed = (await verify.json()) as { name: string } | false + expect(signed).toEqual({ name }); - expect(name).toBe(signed.name) + const verifyForged = await app.handle(post("/verify", { + token: "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiU2hpcm9rYW1pIn0.SNUb0H5gYTJYPznYxN36glyJAubUelp8zxy53hYeUt4" + })); + const forged = (await verifyForged.json()) as { name: string } | false + expect(forged).toBe(false); }) })