fix: update dependencies to latest
This commit is contained in:
@ -4,7 +4,7 @@ import dotenv from 'dotenv'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
export const PORT = parseInt(process.env['PORT'] ?? '8080', 10)
|
||||
export const PORT = Number.parseInt(process.env['PORT'] ?? '8080', 10)
|
||||
export const HOST = process.env['HOST'] ?? '0.0.0.0'
|
||||
export const API_URL = process.env['API_URL'] ?? `http://${HOST}:${PORT}`
|
||||
export const FILE_UPLOADS_API_URL =
|
||||
|
@ -3,7 +3,7 @@ import nodemailer from 'nodemailer'
|
||||
import type SMTPTransport from 'nodemailer/lib/smtp-transport/index.js'
|
||||
|
||||
dotenv.config()
|
||||
const EMAIL_PORT = parseInt(process.env['EMAIL_PORT'] ?? '465', 10)
|
||||
const EMAIL_PORT = Number.parseInt(process.env['EMAIL_PORT'] ?? '465', 10)
|
||||
|
||||
export const EMAIL_INFO: SMTPTransport.Options = {
|
||||
host: process.env['EMAIL_HOST'],
|
||||
|
@ -3,9 +3,15 @@ import { URL, fileURLToPath } from 'node:url'
|
||||
|
||||
import ejs from 'ejs'
|
||||
|
||||
import type { Language, Theme } from '../../models/UserSettings.js'
|
||||
import { EMAIL_LOCALES_URL, EMAIL_TEMPLATE_URL } from '../configurations.js'
|
||||
import { emailTransporter, EMAIL_INFO } from './emailTransporter.js'
|
||||
import type { Language, Theme } from '#src/models/UserSettings.js'
|
||||
import {
|
||||
EMAIL_LOCALES_URL,
|
||||
EMAIL_TEMPLATE_URL
|
||||
} from '#src/tools/configurations.js'
|
||||
import {
|
||||
emailTransporter,
|
||||
EMAIL_INFO
|
||||
} from '#src/tools/email/emailTransporter.js'
|
||||
|
||||
interface EmailTranslation {
|
||||
subject: string
|
||||
|
@ -1,98 +1,97 @@
|
||||
import tap from 'tap'
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import sinon from 'sinon'
|
||||
import httpErrors from 'http-errors'
|
||||
import jwt from 'jsonwebtoken'
|
||||
|
||||
import { getUserWithBearerToken } from '../authenticateUser.js'
|
||||
import prisma from '../../database/prisma.js'
|
||||
import { userExample } from '../../../models/User.js'
|
||||
import { getUserWithBearerToken } from '#src/tools/plugins/authenticateUser.js'
|
||||
import prisma from '#src/tools/database/prisma.js'
|
||||
import { userExample } from '#src/models/User.js'
|
||||
|
||||
const { Unauthorized, Forbidden, BadRequest } = httpErrors
|
||||
|
||||
await tap.test(
|
||||
'tools/plugins/authenticateUser - getUserWithBearerToken',
|
||||
async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
await test('tools/plugins/authenticateUser - getUserWithBearerToken', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('shoulds succeeds with the right information', async (t) => {
|
||||
await t.test('shoulds succeeds with the right information', async () => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return userExample
|
||||
}
|
||||
})
|
||||
const currentStrategy = 'Local'
|
||||
sinon.stub(jwt, 'verify').value(() => {
|
||||
return { id: userExample.id, currentStrategy }
|
||||
})
|
||||
const userWithBearerToken = await getUserWithBearerToken('Bearer token')
|
||||
assert.strictEqual(userWithBearerToken.current.id, userExample.id)
|
||||
assert.strictEqual(userWithBearerToken.current.name, userExample.name)
|
||||
assert.strictEqual(userWithBearerToken.accessToken, 'token')
|
||||
assert.strictEqual(userWithBearerToken.currentStrategy, currentStrategy)
|
||||
})
|
||||
|
||||
await t.test(
|
||||
'shoulds throws `Unauthorized` if `bearerToken` is not a string',
|
||||
async () => {
|
||||
await assert.rejects(getUserWithBearerToken(undefined), Unauthorized)
|
||||
}
|
||||
)
|
||||
|
||||
await t.test(
|
||||
'shoulds throws `Unauthorized` if `bearerToken` is not to the right format: `"Bearer token"`',
|
||||
async () => {
|
||||
await assert.rejects(getUserWithBearerToken('Bearer'), Unauthorized)
|
||||
await assert.rejects(getUserWithBearerToken(''), Unauthorized)
|
||||
await assert.rejects(
|
||||
getUserWithBearerToken('Bearer token token2'),
|
||||
Unauthorized
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
await t.test(
|
||||
'shoulds throws `Forbidden` if invalid `bearerToken` by `jwt.verify`',
|
||||
async () => {
|
||||
sinon.stub(jwt, 'verify').value(() => {
|
||||
throw new Error('Invalid token')
|
||||
})
|
||||
await assert.rejects(getUserWithBearerToken('Bearer token'), Forbidden)
|
||||
}
|
||||
)
|
||||
|
||||
await t.test(
|
||||
"shoulds throws `Forbidden` if the user doesn't exist",
|
||||
async () => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return userExample
|
||||
return null
|
||||
}
|
||||
})
|
||||
const currentStrategy = 'Local'
|
||||
sinon.stub(jwt, 'verify').value(() => {
|
||||
return { id: userExample.id, currentStrategy }
|
||||
return { id: userExample.id }
|
||||
})
|
||||
const userWithBearerToken = await getUserWithBearerToken('Bearer token')
|
||||
t.equal(userWithBearerToken.current.id, userExample.id)
|
||||
t.equal(userWithBearerToken.current.name, userExample.name)
|
||||
t.equal(userWithBearerToken.accessToken, 'token')
|
||||
t.equal(userWithBearerToken.currentStrategy, currentStrategy)
|
||||
})
|
||||
await assert.rejects(getUserWithBearerToken('Bearer token'), Forbidden)
|
||||
}
|
||||
)
|
||||
|
||||
await t.test(
|
||||
'shoulds throws `Unauthorized` if `bearerToken` is not a string',
|
||||
async (t) => {
|
||||
await t.rejects(getUserWithBearerToken(undefined), Unauthorized)
|
||||
}
|
||||
)
|
||||
|
||||
await t.test(
|
||||
'shoulds throws `Unauthorized` if `bearerToken` is not to the right format: `"Bearer token"`',
|
||||
async (t) => {
|
||||
await t.rejects(getUserWithBearerToken('Bearer'), Unauthorized)
|
||||
await t.rejects(getUserWithBearerToken(''), Unauthorized)
|
||||
await t.rejects(
|
||||
getUserWithBearerToken('Bearer token token2'),
|
||||
Unauthorized
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
await t.test(
|
||||
'shoulds throws `Forbidden` if invalid `bearerToken` by `jwt.verify`',
|
||||
async (t) => {
|
||||
sinon.stub(jwt, 'verify').value(() => {
|
||||
throw new Error('Invalid token')
|
||||
})
|
||||
await t.rejects(getUserWithBearerToken('Bearer token'), Forbidden)
|
||||
}
|
||||
)
|
||||
|
||||
await t.test(
|
||||
"shoulds throws `Forbidden` if the user doesn't exist",
|
||||
async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return null
|
||||
await t.test(
|
||||
'shoulds throws `BadRequest` if the user account is not confirmed',
|
||||
async () => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return {
|
||||
...userExample,
|
||||
isConfirmed: false
|
||||
}
|
||||
})
|
||||
sinon.stub(jwt, 'verify').value(() => {
|
||||
return { id: userExample.id }
|
||||
})
|
||||
await t.rejects(getUserWithBearerToken('Bearer token'), Forbidden)
|
||||
}
|
||||
)
|
||||
|
||||
await t.test(
|
||||
'shoulds throws `BadRequest` if the user account is not confirmed',
|
||||
async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return {
|
||||
...userExample,
|
||||
isConfirmed: false
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(jwt, 'verify').value(() => {
|
||||
return { id: userExample.id, currentStrategy: 'Local' }
|
||||
})
|
||||
await t.rejects(getUserWithBearerToken('Bearer token'), BadRequest)
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
})
|
||||
sinon.stub(jwt, 'verify').value(() => {
|
||||
return { id: userExample.id, currentStrategy: 'Local' }
|
||||
})
|
||||
await assert.rejects(getUserWithBearerToken('Bearer token'), BadRequest)
|
||||
}
|
||||
)
|
||||
})
|
||||
|
@ -1,17 +1,19 @@
|
||||
import tap from 'tap'
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import fastify from 'fastify'
|
||||
|
||||
import fastifySocketIo from '../socket-io.js'
|
||||
import fastifySocketIo from '#src/tools/plugins/socket-io.js'
|
||||
|
||||
await tap.test('tools/plugins/socket-io', async (t) => {
|
||||
await t.test('should close socket server on fastify close', async (t) => {
|
||||
await test('tools/plugins/socket-io', async (t) => {
|
||||
await t.test('should close socket server on fastify close', async () => {
|
||||
const PORT = 3030
|
||||
const application = fastify()
|
||||
await application.register(fastifySocketIo)
|
||||
await application.listen({
|
||||
port: PORT
|
||||
})
|
||||
t.not(application.io, null)
|
||||
assert.notStrictEqual(application.io, null)
|
||||
await application.close()
|
||||
})
|
||||
})
|
||||
|
@ -2,9 +2,9 @@ import fastifyPlugin from 'fastify-plugin'
|
||||
import httpErrors from 'http-errors'
|
||||
import jwt from 'jsonwebtoken'
|
||||
|
||||
import prisma from '../database/prisma.js'
|
||||
import type { UserJWT, UserRequest } from '../../models/User.js'
|
||||
import { JWT_ACCESS_SECRET } from '../configurations.js'
|
||||
import prisma from '#src/tools/database/prisma.js'
|
||||
import type { UserJWT, UserRequest } from '#src/models/User.js'
|
||||
import { JWT_ACCESS_SECRET } from '#src/tools/configurations.js'
|
||||
|
||||
const { Unauthorized, Forbidden, BadRequest } = httpErrors
|
||||
|
||||
|
@ -3,8 +3,8 @@ import type { ServerOptions } from 'socket.io'
|
||||
import { Server as SocketIoServer } from 'socket.io'
|
||||
import { authorize } from '@thream/socketio-jwt'
|
||||
|
||||
import prisma from '../database/prisma.js'
|
||||
import { JWT_ACCESS_SECRET } from '../configurations.js'
|
||||
import prisma from '#src/tools/database/prisma.js'
|
||||
import { JWT_ACCESS_SECRET } from '#src/tools/configurations.js'
|
||||
|
||||
interface EmitEventOptions {
|
||||
event: string
|
||||
|
@ -3,10 +3,10 @@ import {
|
||||
expiresIn,
|
||||
generateAccessToken,
|
||||
generateRefreshToken
|
||||
} from './jwtToken.js'
|
||||
import prisma from '../database/prisma.js'
|
||||
import type { ProviderOAuth } from '../../models/OAuth.js'
|
||||
import type { UserRequest } from '../../models/User.js'
|
||||
} from '#src/tools/utils/jwtToken.js'
|
||||
import prisma from '#src/tools/database/prisma.js'
|
||||
import type { ProviderOAuth } from '#src/models/OAuth.js'
|
||||
import type { UserRequest } from '#src/models/User.js'
|
||||
|
||||
interface ProviderData {
|
||||
name: string
|
||||
|
@ -1,21 +1,23 @@
|
||||
import tap from 'tap'
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import sinon from 'sinon'
|
||||
|
||||
import { userExample } from '../../../models/User.js'
|
||||
import { userSettingsExample } from '../../../models/UserSettings.js'
|
||||
import { OAuthStrategy } from '../OAuthStrategy.js'
|
||||
import prisma from '../../database/prisma.js'
|
||||
import { refreshTokenExample } from '../../../models/RefreshToken.js'
|
||||
import { userExample } from '#src/models/User.js'
|
||||
import { userSettingsExample } from '#src/models/UserSettings.js'
|
||||
import { OAuthStrategy } from '#src/tools/utils/OAuthStrategy.js'
|
||||
import prisma from '#src/tools/database/prisma.js'
|
||||
import { refreshTokenExample } from '#src/models/RefreshToken.js'
|
||||
|
||||
const oauthStrategy = new OAuthStrategy('Discord')
|
||||
|
||||
await tap.test('tools/utils/OAuthStrategy', async (t) => {
|
||||
await test('tools/utils/OAuthStrategy', async (t) => {
|
||||
await t.test('callbackSignin', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('should signup the user', async (t) => {
|
||||
await t.test('should signup the user', async () => {
|
||||
const name = 'Martin'
|
||||
const id = '12345'
|
||||
sinon.stub(prisma, 'user').value({
|
||||
@ -60,7 +62,7 @@ await tap.test('tools/utils/OAuthStrategy', async (t) => {
|
||||
const userCreateSpy = sinon.spy(prisma.user, 'create')
|
||||
const userSettingCreateSpy = sinon.spy(prisma.userSetting, 'create')
|
||||
await oauthStrategy.callbackSignin({ id, name })
|
||||
t.equal(
|
||||
assert.strictEqual(
|
||||
oAuthCreateSpy.calledWith({
|
||||
data: {
|
||||
userId: userExample.id,
|
||||
@ -70,7 +72,7 @@ await tap.test('tools/utils/OAuthStrategy', async (t) => {
|
||||
}),
|
||||
true
|
||||
)
|
||||
t.equal(
|
||||
assert.strictEqual(
|
||||
oAuthFindFirstSpy.calledWith({
|
||||
where: {
|
||||
provider: 'Discord',
|
||||
@ -79,9 +81,9 @@ await tap.test('tools/utils/OAuthStrategy', async (t) => {
|
||||
}),
|
||||
true
|
||||
)
|
||||
t.equal(userCountSpy.calledWith({ where: { name } }), true)
|
||||
t.equal(userCreateSpy.calledWith({ data: { name } }), true)
|
||||
t.equal(
|
||||
assert.strictEqual(userCountSpy.calledWith({ where: { name } }), true)
|
||||
assert.strictEqual(userCreateSpy.calledWith({ data: { name } }), true)
|
||||
assert.strictEqual(
|
||||
userSettingCreateSpy.calledWith({
|
||||
data: {
|
||||
userId: userExample.id
|
||||
@ -97,7 +99,7 @@ await tap.test('tools/utils/OAuthStrategy', async (t) => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('should add the strategy to the user', async (t) => {
|
||||
await t.test('should add the strategy to the user', async () => {
|
||||
const name = userExample.name
|
||||
const id = '12345'
|
||||
sinon.stub(prisma, 'oAuth').value({
|
||||
@ -121,8 +123,8 @@ await tap.test('tools/utils/OAuthStrategy', async (t) => {
|
||||
{ id, name },
|
||||
{ accessToken: '123', current: userExample, currentStrategy: 'Local' }
|
||||
)
|
||||
t.equal(result, 'success')
|
||||
t.equal(
|
||||
assert.strictEqual(result, 'success')
|
||||
assert.strictEqual(
|
||||
oAuthCreateSpy.calledWith({
|
||||
data: {
|
||||
userId: userExample.id,
|
||||
@ -132,7 +134,7 @@ await tap.test('tools/utils/OAuthStrategy', async (t) => {
|
||||
}),
|
||||
true
|
||||
)
|
||||
t.equal(
|
||||
assert.strictEqual(
|
||||
oAuthFindFirstSpy.calledWith({
|
||||
where: {
|
||||
provider: 'Discord',
|
||||
@ -145,7 +147,7 @@ await tap.test('tools/utils/OAuthStrategy', async (t) => {
|
||||
|
||||
await t.test(
|
||||
'should not add the strategy if the account of the provider is already used',
|
||||
async (t) => {
|
||||
async () => {
|
||||
const name = userExample.name
|
||||
const id = '12345'
|
||||
sinon.stub(prisma, 'oAuth').value({
|
||||
@ -165,8 +167,11 @@ await tap.test('tools/utils/OAuthStrategy', async (t) => {
|
||||
{ id, name },
|
||||
{ accessToken: '123', current: userExample, currentStrategy: 'Local' }
|
||||
)
|
||||
t.equal(result, 'This account is already used by someone else')
|
||||
t.equal(
|
||||
assert.strictEqual(
|
||||
result,
|
||||
'This account is already used by someone else'
|
||||
)
|
||||
assert.strictEqual(
|
||||
oAuthFindFirstSpy.calledWith({
|
||||
where: {
|
||||
provider: 'Discord',
|
||||
@ -180,7 +185,7 @@ await tap.test('tools/utils/OAuthStrategy', async (t) => {
|
||||
|
||||
await t.test(
|
||||
'should not add the strategy if the user is already connected with it',
|
||||
async (t) => {
|
||||
async () => {
|
||||
const name = userExample.name
|
||||
const id = '12345'
|
||||
sinon.stub(prisma, 'oAuth').value({
|
||||
@ -200,8 +205,8 @@ await tap.test('tools/utils/OAuthStrategy', async (t) => {
|
||||
{ id, name },
|
||||
{ accessToken: '123', current: userExample, currentStrategy: 'Local' }
|
||||
)
|
||||
t.equal(result, 'You are already using this account')
|
||||
t.equal(
|
||||
assert.strictEqual(result, 'You are already using this account')
|
||||
assert.strictEqual(
|
||||
oAuthFindFirstSpy.calledWith({
|
||||
where: {
|
||||
provider: 'Discord',
|
||||
|
@ -1,21 +1,22 @@
|
||||
import tap from 'tap'
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import { buildQueryURL } from '../buildQueryURL.js'
|
||||
|
||||
await tap.test('tools/utils/buildQueryUrl', async (t) => {
|
||||
t.equal(
|
||||
await test('tools/utils/buildQueryUrl', async () => {
|
||||
assert.strictEqual(
|
||||
buildQueryURL('http://localhost:8080', {
|
||||
test: 'query'
|
||||
}),
|
||||
'http://localhost:8080/?test=query'
|
||||
)
|
||||
t.equal(
|
||||
assert.strictEqual(
|
||||
buildQueryURL('http://localhost:8080/', {
|
||||
test: 'query'
|
||||
}),
|
||||
'http://localhost:8080/?test=query'
|
||||
)
|
||||
t.equal(
|
||||
assert.strictEqual(
|
||||
buildQueryURL('http://localhost:3000', {
|
||||
test: 'query',
|
||||
code: 'abc'
|
||||
|
@ -1,27 +1,31 @@
|
||||
import tap from 'tap'
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import { parseStringNullish } from '../parseStringNullish.js'
|
||||
|
||||
const defaultString = 'defaultString'
|
||||
|
||||
await tap.test('tools/utils/parseStringNullish', async (t) => {
|
||||
await test('tools/utils/parseStringNullish', async (t) => {
|
||||
await t.test(
|
||||
'returns `defaultString` if `string === undefined`',
|
||||
async (t) => {
|
||||
t.equal(parseStringNullish(defaultString, undefined), defaultString)
|
||||
async () => {
|
||||
assert.strictEqual(
|
||||
parseStringNullish(defaultString, undefined),
|
||||
defaultString
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
await t.test('returns `null` if `string === null`', async (t) => {
|
||||
t.equal(parseStringNullish(defaultString, null), null)
|
||||
await t.test('returns `null` if `string === null`', async () => {
|
||||
assert.strictEqual(parseStringNullish(defaultString, null), null)
|
||||
})
|
||||
|
||||
await t.test('returns `null` if `string.length === 0`', async (t) => {
|
||||
t.equal(parseStringNullish(defaultString, ''), null)
|
||||
await t.test('returns `null` if `string.length === 0`', async () => {
|
||||
assert.strictEqual(parseStringNullish(defaultString, ''), null)
|
||||
})
|
||||
|
||||
await t.test('returns `string` if `string.length > 0`', async (t) => {
|
||||
await t.test('returns `string` if `string.length > 0`', async () => {
|
||||
const string = 'myString'
|
||||
t.equal(parseStringNullish(defaultString, string), string)
|
||||
assert.strictEqual(parseStringNullish(defaultString, string), string)
|
||||
})
|
||||
})
|
||||
|
@ -9,8 +9,8 @@ export const buildQueryURL = (
|
||||
queryObject: ObjectAny
|
||||
): string => {
|
||||
const url = new URL(baseURL)
|
||||
Object.entries(queryObject).forEach(([query, value]) => {
|
||||
for (const [query, value] of Object.entries(queryObject)) {
|
||||
url.searchParams.append(query, value)
|
||||
})
|
||||
}
|
||||
return url.href
|
||||
}
|
||||
|
@ -4,13 +4,13 @@ import { Type } from '@sinclair/typebox'
|
||||
import jwt from 'jsonwebtoken'
|
||||
import ms from 'ms'
|
||||
|
||||
import prisma from '../database/prisma.js'
|
||||
import type { UserJWT } from '../../models/User.js'
|
||||
import prisma from '#src/tools/database/prisma.js'
|
||||
import type { UserJWT } from '#src/models/User.js'
|
||||
import {
|
||||
JWT_ACCESS_EXPIRES_IN,
|
||||
JWT_ACCESS_SECRET,
|
||||
JWT_REFRESH_SECRET
|
||||
} from '../configurations.js'
|
||||
} from '#src/tools/configurations.js'
|
||||
|
||||
export interface ResponseJWT {
|
||||
accessToken: string
|
||||
|
@ -8,7 +8,7 @@ import type { SavedMultipartFile } from '@fastify/multipart'
|
||||
import {
|
||||
FILE_UPLOADS_API_KEY,
|
||||
FILE_UPLOADS_API_URL
|
||||
} from '../configurations.js'
|
||||
} from '#src/tools/configurations.js'
|
||||
|
||||
export const fileUploadAPI = axios.create({
|
||||
baseURL: FILE_UPLOADS_API_URL,
|
||||
@ -44,6 +44,7 @@ export const uploadFile = async (
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
throw fastify.httpErrors.requestHeaderFieldsTooLarge(
|
||||
`File should be less than ${MAXIMUM_FILE_SIZE}mb.`
|
||||
)
|
||||
|
Reference in New Issue
Block a user