import axios from 'axios' ; import { io } from 'socket.io-client' ; import { fixtureStart , fixtureStop , getSocket , Profile } from './fixture' ; describe ( 'authorize - with secret as string in options' , ( ) => { let token = '' ; 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 ) => { 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 ) => { 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 ) => { 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 ) => { done ( err ) ; } ) ; } ) ; } ) ; const secretCallback = async ( ) : Promise < string > => { return 'somesecret' ; } ; describe ( 'authorize - with secret as callback in options' , ( ) => { let token = '' ; 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 ) => { 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 ) => { 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 ) => { 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 ) => { done ( err ) ; } ) ; } ) ; } ) ; describe ( 'authorize - with onAuthentication callback in options' , ( ) => { let token = '' ; let wrongToken = '' ; 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 ) => { if ( ! decodedToken . checkField ) { throw new Error ( 'Check Field validation failed' ) ; } return { email : decodedToken . email } ; } } ) . 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 ) => { 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 ) => { 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 ) => { 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 ( ) ; } ) ; } ) ; it ( 'should contain user property' , ( done ) => { const socketServer = getSocket ( ) ; socketServer ?. on ( 'connection' , ( client ) => { expect ( client . user . email ) . toEqual ( 'john@doe.com' ) ; } ) ; const socket = io ( 'http://localhost:9000' , { auth : { token : `Bearer ${ token }` } } ) ; socket . on ( 'connect' , ( ) => { socket . close ( ) ; done ( ) ; } ) ; } ) ; it ( 'should emit error when user validation fails' , ( done ) => { const socket = io ( 'http://localhost:9000' , { auth : { token : `Bearer ${ wrongToken }` } } ) ; socket . on ( 'connect_error' , ( err ) => { try { expect ( err . message ) . toEqual ( 'Check Field validation failed' ) ; } catch ( err ) { socket . close ( ) ; done ( err ) ; } socket . close ( ) ; done ( ) ; } ) ; } ) ; } ) ;