refactor: usage of node:test instead of tap

This commit is contained in:
2023-07-22 12:18:28 +02:00
parent b708d66586
commit 50c236ca4d
11 changed files with 1294 additions and 4782 deletions

View File

@@ -1,4 +1,6 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import axios from 'axios'
import type { Socket } from 'socket.io-client'
import { io } from 'socket.io-client'
@@ -24,7 +26,7 @@ const secretCallback = async (): Promise<string> => {
return 'somesecret'
}
await tap.test('authorize', async (t) => {
await test('authorize', async (t) => {
await t.test('with secret as string in options', async (t) => {
let token = ''
let socket: Socket | null = null
@@ -40,71 +42,76 @@ await tap.test('authorize', async (t) => {
await fixtureStop()
})
await t.test('should emit error with no token provided', (t) => {
t.plan(4)
await t.test('should emit error with no token provided', () => {
socket = io(API_URL)
socket.on('connect_error', async (error) => {
t.equal(isUnauthorizedError(error), true)
assert.strictEqual(isUnauthorizedError(error), true)
if (isUnauthorizedError(error)) {
t.equal(error.data.message, 'no token provided')
t.equal(error.data.code, 'credentials_required')
assert.strictEqual(error.data.message, 'no token provided')
assert.strictEqual(error.data.code, 'credentials_required')
assert.ok(true)
} else {
assert.fail('should be unauthorized error')
}
t.pass()
})
socket.on('connect', async () => {
t.fail()
assert.fail('should not connect')
})
})
await t.test('should emit error with bad token format', (t) => {
t.plan(4)
await t.test('should emit error with bad token format', () => {
socket = io(API_URL, {
auth: { token: 'testing' }
})
socket.on('connect_error', async (error) => {
t.equal(isUnauthorizedError(error), true)
assert.strictEqual(isUnauthorizedError(error), true)
if (isUnauthorizedError(error)) {
t.equal(error.data.message, 'Format is Authorization: Bearer [token]')
t.equal(error.data.code, 'credentials_bad_format')
assert.strictEqual(
error.data.message,
'Format is Authorization: Bearer [token]'
)
assert.strictEqual(error.data.code, 'credentials_bad_format')
assert.ok(true)
} else {
assert.fail('should be unauthorized error')
}
t.pass()
})
socket.on('connect', async () => {
t.fail()
assert.fail('should not connect')
})
})
await t.test('should emit error with unauthorized handshake', (t) => {
t.plan(4)
await t.test('should emit error with unauthorized handshake', () => {
socket = io(API_URL, {
auth: { token: 'Bearer testing' }
})
socket.on('connect_error', async (error) => {
t.equal(isUnauthorizedError(error), true)
assert.strictEqual(isUnauthorizedError(error), true)
if (isUnauthorizedError(error)) {
t.equal(
assert.strictEqual(
error.data.message,
'Unauthorized: Token is missing or invalid Bearer'
)
t.equal(error.data.code, 'invalid_token')
assert.strictEqual(error.data.code, 'invalid_token')
assert.ok(true)
} else {
assert.fail('should be unauthorized error')
}
t.pass()
})
socket.on('connect', async () => {
t.fail()
assert.fail('should not connect')
})
})
await t.test('should connect the user', (t) => {
t.plan(1)
await t.test('should connect the user', () => {
socket = io(API_URL, {
auth: { token: `Bearer ${token}` }
})
socket.on('connect', async () => {
t.pass()
assert.ok(true)
})
socket.on('connect_error', async (error) => {
t.fail(error.message)
assert.fail(error.message)
})
})
})
@@ -124,71 +131,76 @@ await tap.test('authorize', async (t) => {
await fixtureStop()
})
await t.test('should emit error with no token provided', (t) => {
t.plan(4)
await t.test('should emit error with no token provided', () => {
socket = io(API_URL)
socket.on('connect_error', async (error) => {
t.equal(isUnauthorizedError(error), true)
assert.strictEqual(isUnauthorizedError(error), true)
if (isUnauthorizedError(error)) {
t.equal(error.data.message, 'no token provided')
t.equal(error.data.code, 'credentials_required')
assert.strictEqual(error.data.message, 'no token provided')
assert.strictEqual(error.data.code, 'credentials_required')
assert.ok(true)
} else {
assert.fail('should be unauthorized error')
}
t.pass()
})
socket.on('connect', async () => {
t.fail()
assert.fail('should not connect')
})
})
await t.test('should emit error with bad token format', (t) => {
t.plan(4)
await t.test('should emit error with bad token format', () => {
socket = io(API_URL, {
auth: { token: 'testing' }
})
socket.on('connect_error', async (error) => {
t.equal(isUnauthorizedError(error), true)
assert.strictEqual(isUnauthorizedError(error), true)
if (isUnauthorizedError(error)) {
t.equal(error.data.message, 'Format is Authorization: Bearer [token]')
t.equal(error.data.code, 'credentials_bad_format')
assert.strictEqual(
error.data.message,
'Format is Authorization: Bearer [token]'
)
assert.strictEqual(error.data.code, 'credentials_bad_format')
assert.ok(true)
} else {
assert.fail('should be unauthorized error')
}
t.pass()
})
socket.on('connect', async () => {
t.fail()
assert.fail('should not connect')
})
})
await t.test('should emit error with unauthorized handshake', (t) => {
t.plan(4)
await t.test('should emit error with unauthorized handshake', () => {
socket = io(API_URL, {
auth: { token: 'Bearer testing' }
})
socket.on('connect_error', async (error) => {
t.equal(isUnauthorizedError(error), true)
assert.strictEqual(isUnauthorizedError(error), true)
if (isUnauthorizedError(error)) {
t.equal(
assert.strictEqual(
error.data.message,
'Unauthorized: Token is missing or invalid Bearer'
)
t.equal(error.data.code, 'invalid_token')
assert.strictEqual(error.data.code, 'invalid_token')
assert.ok(true)
} else {
assert.fail('should be unauthorized error')
}
t.pass()
})
socket.on('connect', async () => {
t.fail()
assert.fail('should not connect')
})
})
await t.test('should connect the user', (t) => {
t.plan(1)
await t.test('should connect the user', () => {
socket = io(API_URL, {
auth: { token: `Bearer ${token}` }
})
socket.on('connect', async () => {
t.pass()
assert.ok(true)
})
socket.on('connect_error', async (error) => {
t.fail(error.message)
assert.fail(error.message)
})
})
})
@@ -221,104 +233,107 @@ await tap.test('authorize', async (t) => {
await fixtureStop()
})
await t.test('should emit error with no token provided', (t) => {
t.plan(4)
await t.test('should emit error with no token provided', () => {
socket = io(API_URL)
socket.on('connect_error', async (error) => {
t.equal(isUnauthorizedError(error), true)
assert.strictEqual(isUnauthorizedError(error), true)
if (isUnauthorizedError(error)) {
t.equal(error.data.message, 'no token provided')
t.equal(error.data.code, 'credentials_required')
assert.strictEqual(error.data.message, 'no token provided')
assert.strictEqual(error.data.code, 'credentials_required')
assert.ok(true)
} else {
assert.fail('should be unauthorized error')
}
t.pass()
})
socket.on('connect', async () => {
t.fail()
assert.fail('should not connect')
})
})
await t.test('should emit error with bad token format', (t) => {
t.plan(4)
await t.test('should emit error with bad token format', () => {
socket = io(API_URL, {
auth: { token: 'testing' }
})
socket.on('connect_error', async (error) => {
t.equal(isUnauthorizedError(error), true)
assert.strictEqual(isUnauthorizedError(error), true)
if (isUnauthorizedError(error)) {
t.equal(error.data.message, 'Format is Authorization: Bearer [token]')
t.equal(error.data.code, 'credentials_bad_format')
assert.strictEqual(
error.data.message,
'Format is Authorization: Bearer [token]'
)
assert.strictEqual(error.data.code, 'credentials_bad_format')
assert.ok(true)
} else {
assert.fail('should be unauthorized error')
}
t.pass()
})
socket.on('connect', async () => {
t.fail()
assert.fail('should not connect')
})
})
await t.test('should emit error with unauthorized handshake', (t) => {
t.plan(4)
await t.test('should emit error with unauthorized handshake', () => {
socket = io(API_URL, {
auth: { token: 'Bearer testing' }
})
socket.on('connect_error', async (error) => {
t.equal(isUnauthorizedError(error), true)
assert.strictEqual(isUnauthorizedError(error), true)
if (isUnauthorizedError(error)) {
t.equal(
assert.strictEqual(
error.data.message,
'Unauthorized: Token is missing or invalid Bearer'
)
t.equal(error.data.code, 'invalid_token')
assert.strictEqual(error.data.code, 'invalid_token')
assert.ok(true)
} else {
assert.fail('should be unauthorized error')
}
t.pass()
})
socket.on('connect', async () => {
t.fail()
assert.fail('should not connect')
})
})
await t.test('should connect the user', (t) => {
t.plan(1)
await t.test('should connect the user', () => {
socket = io(API_URL, {
auth: { token: `Bearer ${token}` }
})
socket.on('connect', async () => {
t.pass()
assert.ok(true)
})
socket.on('connect_error', async (error) => {
t.fail(error.message)
assert.fail(error.message)
})
})
await t.test('should contains user properties', (t) => {
t.plan(2)
await t.test('should contains user properties', () => {
const socketServer = getSocket()
socketServer?.on('connection', (client: any) => {
t.equal(client.user.email, basicProfile.email)
t.pass()
assert.strictEqual(client.user.email, basicProfile.email)
assert.ok(true)
})
socket = io(API_URL, {
auth: { token: `Bearer ${token}` }
})
socket.on('connect_error', async (error) => {
t.fail(error.message)
assert.fail(error.message)
})
})
await t.test('should emit error when user validation fails', (t) => {
t.plan(2)
await t.test('should emit error when user validation fails', () => {
socket = io(API_URL, {
auth: { token: `Bearer ${wrongToken}` }
})
socket.on('connect_error', async (error) => {
try {
t.equal(error.message, 'Check Field validation failed')
t.pass()
assert.strictEqual(error.message, 'Check Field validation failed')
assert.ok(true)
} catch {
t.fail()
assert.fail(error.message)
}
})
socket.on('connect', async () => {
t.fail()
assert.fail('should not connect')
})
})
})