Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue92 unit tests #1

Merged
merged 4 commits into from
Nov 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions tests/jwt.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { suite } from 'uvu'
import { ServerResponse as Response } from 'http'
import fs from 'fs'
import jsonwebtoken from 'jsonwebtoken'
import jsonwebtoken, { Algorithm } from 'jsonwebtoken'
import expect from 'expect'
import { jwt, Request } from '../src/index'

Expand All @@ -15,14 +15,28 @@ it.before.each(() => {
req.user = {}
})

it('should work if authorization header is valid jsonwebtoken', () => {
it('should work using default algorithm if authorization header is valid jsonwebtoken', () => {
const secret = 'shhhhhh'
const token = jsonwebtoken.sign({ foo: 'bar' }, secret)

req.headers.authorization = 'Bearer ' + token

jwt({ secret: secret })(req, res, () => {
expect('bar').toBe(req.user.foo)
expect(req.user.foo).toBe('bar')
})
})

it('should work with different HMAC algorithms', () => {
const algorithms: Algorithm[] = ['HS512', 'HS256', 'HS384']
const secret = 'shhhhhh'

algorithms.forEach((algorithm) => {
const token = jsonwebtoken.sign({ foo: 'bar' }, secret, { algorithm })
req.headers.authorization = 'Bearer ' + token

jwt({ secret: secret, algorithm })(req, res, () => {
expect(req.user.foo).toBe('bar')
})
})
})

Expand All @@ -33,7 +47,7 @@ it('should work if authorization header is valid with a buffer secret', () => {
req.headers.authorization = 'Bearer ' + token

jwt({ secret: secret.toString(), algorithm: 'HS256' })(req, res, () => {
expect('bar').toBe(req.user.foo)
expect(req.user.foo).toBe('bar')
})
})

Expand All @@ -44,7 +58,21 @@ it('should handle private key encryption', () => {
req.headers.authorization = 'Bearer ' + jsonwebtoken.sign({ foo: 'bar' }, privateKey, { algorithm: 'RS256' })

jwt({ secret: [privateKey, publicKey], algorithm: 'RS256' })(req, res, () => {
expect('bar').toBe(req.user.foo)
expect(req.user.foo).toBe('bar')
})
})

it('should work with different RSA algorithms', () => {
const algorithms: Algorithm[] = ['RS256', 'RS384', 'RS512']
const privateKey = fs.readFileSync('tests/fixtures/private', { encoding: 'utf-8' })
const publicKey = fs.readFileSync('tests/fixtures/public', { encoding: 'utf-8' })

algorithms.forEach((algorithm) => {
req.headers.authorization = 'Bearer ' + jsonwebtoken.sign({ foo: 'bar' }, privateKey, { algorithm })

jwt({ secret: [privateKey, publicKey], algorithm })(req, res, () => {
expect(req.user.foo).toBe('bar')
})
})
})

Expand All @@ -60,4 +88,12 @@ it('should not work with malformed input', () => {
})
})

it('should not work if authorization header is missing', () => {
const secret = 'shhhhhh'

jwt({ secret: secret })(req, res, () => {
expect(req.user.foo).toBeUndefined()
})
})

it.run()