2
1
mirror of https://github.com/Thream/socketio-jwt.git synced 2024-07-21 09:38:31 +02:00

build(deps): bump jest from 26.6.3 to 27.0.6

close #128
This commit is contained in:
Divlo 2021-07-23 23:00:25 +02:00
parent a41881744a
commit dff86f7bda
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
7 changed files with 2900 additions and 6058 deletions

8
jest.config.js Normal file
View File

@ -0,0 +1,8 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
rootDir: './src',
setupFilesAfterEnv: ['<rootDir>/__test__/setup.ts'],
collectCoverage: true,
coverageDirectory: '../coverage/'
}

8647
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@
"build" "build"
], ],
"engines": { "engines": {
"node": ">=12" "node": ">=12.0.0"
}, },
"keywords": [ "keywords": [
"socket", "socket",
@ -94,7 +94,7 @@
"@commitlint/config-conventional": "12.1.4", "@commitlint/config-conventional": "12.1.4",
"@release-it/conventional-changelog": "3.0.1", "@release-it/conventional-changelog": "3.0.1",
"@types/express": "4.17.13", "@types/express": "4.17.13",
"@types/jest": "26.0.23", "@types/jest": "26.0.24",
"@types/jsonwebtoken": "8.5.4", "@types/jsonwebtoken": "8.5.4",
"@types/node": "16.4.1", "@types/node": "16.4.1",
"@types/server-destroy": "1.0.1", "@types/server-destroy": "1.0.1",
@ -102,7 +102,7 @@
"editorconfig-checker": "4.0.2", "editorconfig-checker": "4.0.2",
"express": "4.17.1", "express": "4.17.1",
"husky": "7.0.1", "husky": "7.0.1",
"jest": "26.6.3", "jest": "27.0.6",
"markdownlint-cli": "0.28.1", "markdownlint-cli": "0.28.1",
"pinst": "2.1.6", "pinst": "2.1.6",
"release-it": "14.10.0", "release-it": "14.10.0",
@ -110,7 +110,7 @@
"server-destroy": "1.0.1", "server-destroy": "1.0.1",
"socket.io": "4.1.3", "socket.io": "4.1.3",
"socket.io-client": "4.1.3", "socket.io-client": "4.1.3",
"ts-jest": "26.5.6", "ts-jest": "27.0.4",
"ts-standard": "10.0.0", "ts-standard": "10.0.0",
"typescript": "4.3.5" "typescript": "4.3.5"
} }

View File

@ -6,15 +6,182 @@ import { fixtureStart, fixtureStop, getSocket, Profile } from './fixture'
describe('authorize - with secret as string in options', () => { describe('authorize - with secret as string in options', () => {
let token: string = '' let token: string = ''
beforeEach(async (done) => { beforeEach((done) => {
jest.setTimeout(15_000) fixtureStart(async () => {
await fixtureStart(async () => {
const response = await axios.post('http://localhost:9000/login') const response = await axios.post('http://localhost:9000/login')
token = response.data.token token = response.data.token
})
.then(done)
.catch((error) => {
done(error)
})
})
afterEach((done) => {
fixtureStop(done)
})
it('should emit error with no token provided', (done) => {
const socket = io('http://localhost:9000')
socket.on('connect_error', (err: any) => {
expect(err.data.message).toEqual('no token provided')
expect(err.data.code).toEqual('credentials_required')
socket.close()
done() done()
}) })
}) })
it('should emit error with bad token format', (done) => {
const socket = io('http://localhost:9000', {
auth: { token: 'testing' }
})
socket.on('connect_error', (err: any) => {
expect(err.data.message).toEqual(
'Format is Authorization: Bearer [token]'
)
expect(err.data.code).toEqual('credentials_bad_format')
socket.close()
done()
})
})
it('should emit error with unauthorized handshake', (done) => {
const socket = io('http://localhost:9000', {
auth: { token: 'Bearer testing' }
})
socket.on('connect_error', (err: any) => {
expect(err.data.message).toEqual(
'Unauthorized: Token is missing or invalid Bearer'
)
expect(err.data.code).toEqual('invalid_token')
socket.close()
done()
})
})
it('should connect the user', (done) => {
const socket = io('http://localhost:9000', {
auth: { token: `Bearer ${token}` }
})
socket.on('connect', () => {
socket.close()
done()
})
socket.on('connect_error', (err: any) => {
done(err)
})
})
})
const secretCallback = async (): Promise<string> => {
return 'somesecret'
}
describe('authorize - with secret as callback in options', () => {
let token: string = ''
beforeEach((done) => {
fixtureStart(
async () => {
const response = await axios.post('http://localhost:9000/login')
token = response.data.token
},
{ secret: secretCallback }
)
.then(done)
.catch((error) => {
done(error)
})
})
afterEach((done) => {
fixtureStop(done)
})
it('should emit error with no token provided', (done) => {
const socket = io('http://localhost:9000')
socket.on('connect_error', (err: any) => {
expect(err.data.message).toEqual('no token provided')
expect(err.data.code).toEqual('credentials_required')
socket.close()
done()
})
})
it('should emit error with bad token format', (done) => {
const socket = io('http://localhost:9000', {
auth: { token: 'testing' }
})
socket.on('connect_error', (err: any) => {
expect(err.data.message).toEqual(
'Format is Authorization: Bearer [token]'
)
expect(err.data.code).toEqual('credentials_bad_format')
socket.close()
done()
})
})
it('should emit error with unauthorized handshake', (done) => {
const socket = io('http://localhost:9000', {
auth: { token: 'Bearer testing' }
})
socket.on('connect_error', (err: any) => {
expect(err.data.message).toEqual(
'Unauthorized: Token is missing or invalid Bearer'
)
expect(err.data.code).toEqual('invalid_token')
socket.close()
done()
})
})
it('should connect the user', (done) => {
const socket = io('http://localhost:9000', {
auth: { token: `Bearer ${token}` }
})
socket.on('connect', () => {
socket.close()
done()
})
socket.on('connect_error', (err: any) => {
done(err)
})
})
})
describe('authorize - with onAuthentication callback in options', () => {
let token: string = ''
let wrongToken: string = ''
beforeEach((done) => {
fixtureStart(
async () => {
const response = await axios.post('http://localhost:9000/login')
token = response.data.token
const responseWrong = await axios.post(
'http://localhost:9000/login-wrong'
)
wrongToken = responseWrong.data.token
},
{
secret: secretCallback,
onAuthentication: (decodedToken: Profile) => {
if (!decodedToken.checkField) {
throw new Error('Check Field validation failed')
}
return {
email: decodedToken.email
}
}
}
)
.then(done)
.catch((error) => {
done(error)
})
})
afterEach((done) => { afterEach((done) => {
fixtureStop(done) fixtureStop(done)
}) })
@ -66,83 +233,6 @@ describe('authorize - with secret as string in options', () => {
done() done()
}) })
}) })
})
const secretCallback = async (): Promise<string> => {
return 'somesecret'
}
describe('authorize - with secret as callback in options', () => {
let token: string = ''
beforeEach(async (done) => {
jest.setTimeout(15_000)
await fixtureStart(
async () => {
const response = await axios.post('http://localhost:9000/login')
token = response.data.token
done()
},
{ secret: secretCallback }
)
})
afterEach((done) => {
fixtureStop(done)
})
it('should connect the user', (done) => {
const socket = io('http://localhost:9000', {
auth: { token: `Bearer ${token}` }
})
socket.on('connect', () => {
socket.close()
done()
})
})
})
describe('authorize - with onAuthentication callback in options', () => {
let token: string = ''
let wrongToken: string = ''
beforeEach(async (done) => {
jest.setTimeout(15_000)
await fixtureStart(
async () => {
const response = await axios.post('http://localhost:9000/login')
token = response.data.token
const responseWrong = await axios.post('http://localhost:9000/login-wrong')
wrongToken = responseWrong.data.token
done()
},
{
secret: secretCallback,
onAuthentication: (decodedToken: Profile) => {
if (!decodedToken.checkField) {
throw new Error('Check Field validation failed')
}
return {
email: decodedToken.email
}
}
}
)
})
afterEach((done) => {
fixtureStop(done)
})
it('should connect the user', (done) => {
const socket = io('http://localhost:9000', {
auth: { token: `Bearer ${token}` }
})
socket.on('connect', () => {
socket.close()
done()
})
})
it('should contain user property', (done) => { it('should contain user property', (done) => {
const socketServer = getSocket() const socketServer = getSocket()

View File

@ -29,33 +29,29 @@ let server: HttpServer | null = null
export const fixtureStart = async ( export const fixtureStart = async (
done: any, done: any,
options: AuthorizeOptions = { secret: 'aaafoo super sercret' } options: AuthorizeOptions = { secret: 'super secret' }
): Promise<void> => { ): Promise<void> => {
const app = express() const profile: Profile = {
app.use(express.json()) email: 'john@doe.com',
let keySecret = 'secret' id: 123,
checkField: true
}
let keySecret = ''
if (typeof options.secret === 'string') { if (typeof options.secret === 'string') {
keySecret = options.secret keySecret = options.secret
} else { } else {
keySecret = await options.secret({ header: { alg: 'RS256' }, payload: '' }) keySecret = await options.secret({ header: { alg: 'HS256' }, payload: profile })
} }
const app = express()
app.use(express.json())
app.post('/login', (_req, res) => { app.post('/login', (_req, res) => {
const profile: Profile = {
email: 'john@doe.com',
id: 123,
checkField: true
}
const token = jwt.sign(profile, keySecret, { const token = jwt.sign(profile, keySecret, {
expiresIn: 60 * 60 * 5 expiresIn: 60 * 60 * 5
}) })
return res.json({ token }) return res.json({ token })
}) })
app.post('/login-wrong', (_req, res) => { app.post('/login-wrong', (_req, res) => {
const profile: Profile = { profile.checkField = false
email: 'john@doe.com',
id: 123,
checkField: false
}
const token = jwt.sign(profile, keySecret, { const token = jwt.sign(profile, keySecret, {
expiresIn: 60 * 60 * 5 expiresIn: 60 * 60 * 5
}) })
@ -71,7 +67,7 @@ export const fixtureStop = (callback: Function): void => {
socket.io?.close() socket.io?.close()
try { try {
server?.destroy() server?.destroy()
} catch (err) {} } catch {}
callback() callback()
} }

1
src/__test__/setup.ts Normal file
View File

@ -0,0 +1 @@
jest.setTimeout(15_000)

View File

@ -1,23 +1,13 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "ES2019", "target": "ESNext",
"module": "commonjs", "module": "commonjs",
"lib": ["ES2019"], "lib": ["ESNext"],
"moduleResolution": "node", "moduleResolution": "node",
"allowJs": false,
"checkJs": false,
"declaration": true,
"sourceMap": true,
"outDir": "./build", "outDir": "./build",
"rootDir": "./src", "rootDir": "./src",
"removeComments": false,
"noEmitOnError": true,
"importHelpers": false,
"strict": true, "strict": true,
"esModuleInterop": true, "esModuleInterop": true,
"experimentalDecorators": true, "declaration": true,
"emitDecoratorMetadata": true,
"forceConsistentCasingInFileNames": true,
"incremental": false
} }
} }