@ -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)
|
Reference in New Issue
Block a user