Skip to content

Commit

Permalink
fix: onAuthUnsuccessful custom server message
Browse files Browse the repository at this point in the history
  • Loading branch information
jaime-ez committed Feb 16, 2024
1 parent bed088a commit a27a759
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
23 changes: 21 additions & 2 deletions src/connection/connection.spec.ts
Expand Up @@ -177,6 +177,25 @@ describe('connection', () => {
assert.calledWithExactly(authCallback, false, { reason: EVENT.INVALID_AUTHENTICATION_DETAILS })
})

it('handles rejected authentication with custom data', async () => {
emitterMock
.expects('emit')
.once()
.withExactArgs(
EVENT.CONNECTION_STATE_CHANGED,
CONNECTION_STATE.AWAITING_AUTHENTICATION
)

await awaitConnectionAck()
await sendChallenge()
await receiveChallengeAccept()
await sendAuth()
await receiveAuthRejectResponse(AUTH_ACTION.INVALID_MESSAGE_DATA)

assert.calledOnce(authCallback)
assert.calledWithExactly(authCallback, false, { reason: AUTH_ACTION.INVALID_MESSAGE_DATA })
})

it('handles authenticating too may times', async () => {
emitterMock
.expects('emit')
Expand Down Expand Up @@ -565,11 +584,11 @@ describe('connection', () => {
await PromiseDelay(0)
}

async function receiveAuthRejectResponse () {
async function receiveAuthRejectResponse (message?: any) {
socket.simulateMessages([{
topic: TOPIC.AUTH,
action: AUTH_ACTION.AUTH_UNSUCCESSFUL,
parsedData: AUTH_ACTION.INVALID_MESSAGE_DATA
parsedData: message
}])

await PromiseDelay(10)
Expand Down
6 changes: 3 additions & 3 deletions src/connection/connection.ts
Expand Up @@ -572,7 +572,7 @@ export class Connection {

if (message.action === AUTH_ACTION.AUTH_UNSUCCESSFUL) {
this.stateMachine.transition(TRANSITIONS.UNSUCCESFUL_LOGIN)
this.onAuthUnSuccessful()
this.onAuthUnSuccessful(message.parsedData)
return
}

Expand Down Expand Up @@ -603,8 +603,8 @@ export class Connection {
this.authCallback = null
}

private onAuthUnSuccessful (): void {
const reason = { reason: EVENT[EVENT.INVALID_AUTHENTICATION_DETAILS] }
private onAuthUnSuccessful (clientData?: any): void {
const reason = { reason: clientData || EVENT[EVENT.INVALID_AUTHENTICATION_DETAILS] }
if (this.resumeCallback) {
this.resumeCallback(reason)
this.resumeCallback = null
Expand Down

0 comments on commit a27a759

Please sign in to comment.