parent
a41881744a
commit
dff86f7bda
8
jest.config.js
Normal file
8
jest.config.js
Normal 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
8647
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -9,7 +9,7 @@
|
||||
"build"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
"node": ">=12.0.0"
|
||||
},
|
||||
"keywords": [
|
||||
"socket",
|
||||
@ -94,7 +94,7 @@
|
||||
"@commitlint/config-conventional": "12.1.4",
|
||||
"@release-it/conventional-changelog": "3.0.1",
|
||||
"@types/express": "4.17.13",
|
||||
"@types/jest": "26.0.23",
|
||||
"@types/jest": "26.0.24",
|
||||
"@types/jsonwebtoken": "8.5.4",
|
||||
"@types/node": "16.4.1",
|
||||
"@types/server-destroy": "1.0.1",
|
||||
@ -102,7 +102,7 @@
|
||||
"editorconfig-checker": "4.0.2",
|
||||
"express": "4.17.1",
|
||||
"husky": "7.0.1",
|
||||
"jest": "26.6.3",
|
||||
"jest": "27.0.6",
|
||||
"markdownlint-cli": "0.28.1",
|
||||
"pinst": "2.1.6",
|
||||
"release-it": "14.10.0",
|
||||
@ -110,7 +110,7 @@
|
||||
"server-destroy": "1.0.1",
|
||||
"socket.io": "4.1.3",
|
||||
"socket.io-client": "4.1.3",
|
||||
"ts-jest": "26.5.6",
|
||||
"ts-jest": "27.0.4",
|
||||
"ts-standard": "10.0.0",
|
||||
"typescript": "4.3.5"
|
||||
}
|
||||
|
@ -6,15 +6,182 @@ import { fixtureStart, fixtureStop, getSocket, Profile } from './fixture'
|
||||
describe('authorize - with secret as string in options', () => {
|
||||
let token: string = ''
|
||||
|
||||
beforeEach(async (done) => {
|
||||
jest.setTimeout(15_000)
|
||||
await fixtureStart(async () => {
|
||||
beforeEach((done) => {
|
||||
fixtureStart(async () => {
|
||||
const response = await axios.post('http://localhost:9000/login')
|
||||
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()
|
||||
})
|
||||
})
|
||||
|
||||
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) => {
|
||||
fixtureStop(done)
|
||||
})
|
||||
@ -66,83 +233,6 @@ describe('authorize - with secret as string in options', () => {
|
||||
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) => {
|
||||
const socketServer = getSocket()
|
||||
|
@ -29,33 +29,29 @@ let server: HttpServer | null = null
|
||||
|
||||
export const fixtureStart = async (
|
||||
done: any,
|
||||
options: AuthorizeOptions = { secret: 'aaafoo super sercret' }
|
||||
options: AuthorizeOptions = { secret: 'super secret' }
|
||||
): Promise<void> => {
|
||||
const app = express()
|
||||
app.use(express.json())
|
||||
let keySecret = 'secret'
|
||||
const profile: Profile = {
|
||||
email: 'john@doe.com',
|
||||
id: 123,
|
||||
checkField: true
|
||||
}
|
||||
let keySecret = ''
|
||||
if (typeof options.secret === 'string') {
|
||||
keySecret = options.secret
|
||||
} 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) => {
|
||||
const profile: Profile = {
|
||||
email: 'john@doe.com',
|
||||
id: 123,
|
||||
checkField: true
|
||||
}
|
||||
const token = jwt.sign(profile, keySecret, {
|
||||
expiresIn: 60 * 60 * 5
|
||||
})
|
||||
return res.json({ token })
|
||||
})
|
||||
app.post('/login-wrong', (_req, res) => {
|
||||
const profile: Profile = {
|
||||
email: 'john@doe.com',
|
||||
id: 123,
|
||||
checkField: false
|
||||
}
|
||||
profile.checkField = false
|
||||
const token = jwt.sign(profile, keySecret, {
|
||||
expiresIn: 60 * 60 * 5
|
||||
})
|
||||
@ -71,7 +67,7 @@ export const fixtureStop = (callback: Function): void => {
|
||||
socket.io?.close()
|
||||
try {
|
||||
server?.destroy()
|
||||
} catch (err) {}
|
||||
} catch {}
|
||||
callback()
|
||||
}
|
||||
|
||||
|
1
src/__test__/setup.ts
Normal file
1
src/__test__/setup.ts
Normal file
@ -0,0 +1 @@
|
||||
jest.setTimeout(15_000)
|
@ -1,23 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2019",
|
||||
"target": "ESNext",
|
||||
"module": "commonjs",
|
||||
"lib": ["ES2019"],
|
||||
"lib": ["ESNext"],
|
||||
"moduleResolution": "node",
|
||||
"allowJs": false,
|
||||
"checkJs": false,
|
||||
"declaration": true,
|
||||
"sourceMap": true,
|
||||
"outDir": "./build",
|
||||
"rootDir": "./src",
|
||||
"removeComments": false,
|
||||
"noEmitOnError": true,
|
||||
"importHelpers": false,
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"incremental": false
|
||||
"declaration": true,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user