fix: update dependencies to latest

This commit is contained in:
2023-07-22 16:26:27 +02:00
parent 23d2a9da71
commit 9a1684e22b
123 changed files with 2322 additions and 5765 deletions

View File

@ -1,45 +1,46 @@
import type { User } from '@prisma/client'
import sinon from 'sinon'
import { refreshTokenExample } from '../../models/RefreshToken.js'
import type { UserJWT } from '../../models/User.js'
import { userExample } from '../../models/User.js'
import { userSettingsExample } from '../../models/UserSettings.js'
import { refreshTokenExample } from '#src/models/RefreshToken.js'
import type { UserJWT } from '#src/models/User.js'
import { userExample } from '#src/models/User.js'
import { userSettingsExample } from '#src/models/UserSettings.js'
import {
generateAccessToken,
generateRefreshToken
} from '../../tools/utils/jwtToken.js'
import prisma from '../../tools/database/prisma.js'
} from '#src/tools/utils/jwtToken.js'
import prisma from '#src/tools/database/prisma.js'
const userStubValue = {
findUnique: async () => {
return userExample
}
}
const userSettingStubValue = {
findFirst: async () => {
return userSettingsExample
}
}
const oAuthStubValue = {
findMany: async () => {
return []
}
}
const refreshTokenStubValue = {
create: async () => {
return refreshTokenExample
}
}
export const authenticateUserTest = async (): Promise<{
accessToken: string
refreshToken: string
user: User
userStubValue: any
userSettingStubValue: any
oAuthStubValue: any
refreshTokenStubValue: any
userStubValue: typeof userStubValue
userSettingStubValue: typeof userSettingStubValue
oAuthStubValue: typeof oAuthStubValue
refreshTokenStubValue: typeof refreshTokenStubValue
}> => {
const userStubValue = {
findUnique: async () => {
return userExample
}
}
const userSettingStubValue = {
findFirst: async () => {
return userSettingsExample
}
}
const oAuthStubValue = {
findMany: async () => {
return []
}
}
const refreshTokenStubValue = {
create: async () => {
return refreshTokenExample
}
}
sinon.stub(prisma, 'user').value(userStubValue)
sinon.stub(prisma, 'userSetting').value(userSettingStubValue)
sinon.stub(prisma, 'oAuth').value(oAuthStubValue)

View File

@ -8,8 +8,8 @@ import fastifyRateLimit from '@fastify/rate-limit'
import fastifySensible from '@fastify/sensible'
import { readPackage } from 'read-pkg'
import { services } from './services/index.js'
import fastifySocketIo from './tools/plugins/socket-io.js'
import { services } from '#src/services/index.js'
import fastifySocketIo from '#src/tools/plugins/socket-io.js'
dotenv.config()
const packageJSON = await readPackage()

View File

@ -1,8 +1,8 @@
import { application } from './application.js'
import { application } from '#src/application.js'
import { HOST, PORT } from '#src/tools/configurations.js'
const address = await application.listen({
port: PORT,
host: HOST
})
console.log('\u001B[36m%s\u001B[0m', `🚀 Server listening at ${address}`)
console.log(`Server listening at ${address}`)

View File

@ -1,8 +1,8 @@
import { Type } from '@sinclair/typebox'
import type { Channel } from '@prisma/client'
import { date, id } from './utils.js'
import { guildExample } from './Guild.js'
import { date, id } from '#src/models/utils.js'
import { guildExample } from '#src/models/Guild.js'
export const types = [Type.Literal('text')]

View File

@ -1,7 +1,7 @@
import type { Guild } from '@prisma/client'
import { Type } from '@sinclair/typebox'
import { date, id } from './utils.js'
import { date, id } from '#src/models/utils.js'
export const guildSchema = {
id,

View File

@ -1,9 +1,9 @@
import { Type } from '@sinclair/typebox'
import type { Member } from '@prisma/client'
import { date, id } from './utils.js'
import { guildExample } from './Guild.js'
import { userExample } from './User.js'
import { date, id } from '#src/models/utils.js'
import { guildExample } from '#src/models/Guild.js'
import { userExample } from '#src/models/User.js'
export const memberSchema = {
id,

View File

@ -1,7 +1,7 @@
import type { Message } from '@prisma/client'
import { Type } from '@sinclair/typebox'
import { date, id } from './utils.js'
import { date, id } from '#src/models/utils.js'
export const types = [Type.Literal('text'), Type.Literal('file')]

View File

@ -1,6 +1,6 @@
import { Type } from '@sinclair/typebox'
import { date, id } from './utils.js'
import { date, id } from '#src/models/utils.js'
export const providers = ['Google', 'GitHub', 'Discord'] as const
export const strategies = [...providers, 'Local'] as const

View File

@ -1,8 +1,8 @@
import type { RefreshToken } from '@prisma/client'
import { Type } from '@sinclair/typebox'
import { userExample } from './User.js'
import { date, id } from './utils.js'
import { userExample } from '#src/models/User.js'
import { date, id } from '#src/models/utils.js'
export const refreshTokensSchema = {
id,

View File

@ -2,10 +2,10 @@ import type { User } from '@prisma/client'
import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { AuthenticationStrategy } from './OAuth.js'
import { strategiesTypebox } from './OAuth.js'
import { userSettingsSchema } from './UserSettings.js'
import { date, id } from './utils.js'
import type { AuthenticationStrategy } from '#src/models/OAuth.js'
import { strategiesTypebox } from '#src/models/OAuth.js'
import { userSettingsSchema } from '#src/models/UserSettings.js'
import { date, id } from '#src/models/utils.js'
export interface UserJWT {
id: number

View File

@ -2,7 +2,7 @@ import type { UserSetting } from '@prisma/client'
import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import { date, id } from './utils.js'
import { date, id } from '#src/models/utils.js'
export const languages = [Type.Literal('fr'), Type.Literal('en')]
export const themes = [Type.Literal('light'), Type.Literal('dark')]

View File

@ -1,18 +1,20 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../tools/database/prisma.js'
import { channelExample } from '../../../../models/Channel.js'
import { memberExample } from '../../../../models/Member.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { channelExample } from '#src/models/Channel.js'
import { memberExample } from '#src/models/Member.js'
await tap.test('DELETE /channels/[channelId]', async (t) => {
await test('DELETE /channels/[channelId]', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const defaultChannelId = 5
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
@ -45,14 +47,14 @@ await tap.test('DELETE /channels/[channelId]', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.id, channelExample.id)
t.equal(responseJson.name, channelExample.name)
t.equal(responseJson.guildId, channelExample.guildId)
t.equal(responseJson.defaultChannelId, defaultChannelId)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.id, channelExample.id)
assert.strictEqual(responseJson.name, channelExample.name)
assert.strictEqual(responseJson.guildId, channelExample.guildId)
assert.strictEqual(responseJson.defaultChannelId, defaultChannelId)
})
await t.test('fails if there is only one channel', async (t) => {
await t.test('fails if there is only one channel', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -74,10 +76,10 @@ await tap.test('DELETE /channels/[channelId]', async (t) => {
authorization: `Bearer ${accessToken}`
}
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
await t.test('fails if the channel is not found', async (t) => {
await t.test('fails if the channel is not found', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -91,10 +93,10 @@ await tap.test('DELETE /channels/[channelId]', async (t) => {
authorization: `Bearer ${accessToken}`
}
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the member is not found', async (t) => {
await t.test('fails if the member is not found', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -113,10 +115,10 @@ await tap.test('DELETE /channels/[channelId]', async (t) => {
authorization: `Bearer ${accessToken}`
}
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the member is not owner', async (t) => {
await t.test('fails if the member is not owner', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -138,6 +140,6 @@ await tap.test('DELETE /channels/[channelId]', async (t) => {
authorization: `Bearer ${accessToken}`
}
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
})

View File

@ -1,18 +1,20 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../tools/database/prisma.js'
import { channelExample } from '../../../../models/Channel.js'
import { memberExample } from '../../../../models/Member.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { channelExample } from '#src/models/Channel.js'
import { memberExample } from '#src/models/Member.js'
await tap.test('GET /channels/[channelId]', async (t) => {
await test('GET /channels/[channelId]', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -32,13 +34,13 @@ await tap.test('GET /channels/[channelId]', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.channel.id, channelExample.id)
t.equal(responseJson.channel.name, channelExample.name)
t.equal(responseJson.channel.guildId, channelExample.guildId)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.channel.id, channelExample.id)
assert.strictEqual(responseJson.channel.name, channelExample.name)
assert.strictEqual(responseJson.channel.guildId, channelExample.guildId)
})
await t.test('fails with not found member', async (t) => {
await t.test('fails with not found member', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -58,11 +60,11 @@ await tap.test('GET /channels/[channelId]', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 404)
t.equal(responseJson.message, 'Channel not found')
assert.strictEqual(response.statusCode, 404)
assert.strictEqual(responseJson.message, 'Channel not found')
})
await t.test('fails with not found channel', async (t) => {
await t.test('fails with not found channel', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -82,15 +84,15 @@ await tap.test('GET /channels/[channelId]', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 404)
t.equal(responseJson.message, 'Channel not found')
assert.strictEqual(response.statusCode, 404)
assert.strictEqual(responseJson.message, 'Channel not found')
})
await t.test('fails with unauthenticated user', async (t) => {
await t.test('fails with unauthenticated user', async () => {
const response = await application.inject({
method: 'GET',
url: '/channels/1'
})
t.equal(response.statusCode, 401)
assert.strictEqual(response.statusCode, 401)
})
})

View File

@ -1,20 +1,22 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../tools/database/prisma.js'
import { channelExample } from '../../../../models/Channel.js'
import { memberExample } from '../../../../models/Member.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { channelExample } from '#src/models/Channel.js'
import { memberExample } from '#src/models/Member.js'
const newName = 'new channel name'
await tap.test('PUT /channels/[channelId]', async (t) => {
await test('PUT /channels/[channelId]', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const defaultChannelId = 5
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
@ -48,14 +50,14 @@ await tap.test('PUT /channels/[channelId]', async (t) => {
payload: { name: newName }
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.id, channelExample.id)
t.equal(responseJson.name, newName)
t.equal(responseJson.guildId, channelExample.guildId)
t.equal(responseJson.defaultChannelId, defaultChannelId)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.id, channelExample.id)
assert.strictEqual(responseJson.name, newName)
assert.strictEqual(responseJson.guildId, channelExample.guildId)
assert.strictEqual(responseJson.defaultChannelId, defaultChannelId)
})
await t.test('fails if the channel is not found', async (t) => {
await t.test('fails if the channel is not found', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -75,10 +77,10 @@ await tap.test('PUT /channels/[channelId]', async (t) => {
},
payload: { name: newName }
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the member is not found', async (t) => {
await t.test('fails if the member is not found', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -98,10 +100,10 @@ await tap.test('PUT /channels/[channelId]', async (t) => {
},
payload: { name: newName }
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the member is not owner', async (t) => {
await t.test('fails if the member is not owner', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -124,6 +126,6 @@ await tap.test('PUT /channels/[channelId]', async (t) => {
},
payload: { name: newName }
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
})

View File

@ -2,10 +2,10 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
import { channelSchema } from '../../../models/Channel.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { channelSchema } from '#src/models/Channel.js'
const parametersSchema = Type.Object({
channelId: channelSchema.id
@ -48,8 +48,8 @@ export const deleteChannelService: FastifyPluginAsync = async (fastify) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { user } = request
const { channelId } = request.params
const { user, params } = request
const { channelId } = params
const channelCheck = await prisma.channel.findUnique({
where: { id: channelId }
})

View File

@ -2,10 +2,10 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
import { channelSchema } from '../../../models/Channel.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { channelSchema } from '#src/models/Channel.js'
const parametersSchema = Type.Object({
channelId: channelSchema.id

View File

@ -1,20 +1,22 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../../application.js'
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../../tools/database/prisma.js'
import { channelExample } from '../../../../../models/Channel.js'
import { memberExample } from '../../../../../models/Member.js'
import { userExample } from '../../../../../models/User.js'
import { messageExample } from '../../../../../models/Message.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { channelExample } from '#src/models/Channel.js'
import { memberExample } from '#src/models/Member.js'
import { userExample } from '#src/models/User.js'
import { messageExample } from '#src/models/Message.js'
await tap.test('GET /channels/[channelId]/messages', async (t) => {
await test('GET /channels/[channelId]/messages', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -42,19 +44,19 @@ await tap.test('GET /channels/[channelId]/messages', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.length, 1)
t.equal(responseJson[0].id, messageExample.id)
t.equal(responseJson[0].value, messageExample.value)
t.equal(responseJson[0].type, messageExample.type)
t.equal(responseJson[0].mimetype, messageExample.mimetype)
t.equal(responseJson[0].member.id, memberExample.id)
t.equal(responseJson[0].member.isOwner, memberExample.isOwner)
t.equal(responseJson[0].member.user.id, userExample.id)
t.equal(responseJson[0].member.user.name, userExample.name)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.length, 1)
assert.strictEqual(responseJson[0].id, messageExample.id)
assert.strictEqual(responseJson[0].value, messageExample.value)
assert.strictEqual(responseJson[0].type, messageExample.type)
assert.strictEqual(responseJson[0].mimetype, messageExample.mimetype)
assert.strictEqual(responseJson[0].member.id, memberExample.id)
assert.strictEqual(responseJson[0].member.isOwner, memberExample.isOwner)
assert.strictEqual(responseJson[0].member.user.id, userExample.id)
assert.strictEqual(responseJson[0].member.user.name, userExample.name)
})
await t.test('fails with not found channel', async (t) => {
await t.test('fails with not found channel', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -77,11 +79,11 @@ await tap.test('GET /channels/[channelId]/messages', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 404)
t.equal(responseJson.message, 'Channel not found')
assert.strictEqual(response.statusCode, 404)
assert.strictEqual(responseJson.message, 'Channel not found')
})
await t.test('fails with not found member', async (t) => {
await t.test('fails with not found member', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -101,15 +103,15 @@ await tap.test('GET /channels/[channelId]/messages', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 404)
t.equal(responseJson.message, 'Channel not found')
assert.strictEqual(response.statusCode, 404)
assert.strictEqual(responseJson.message, 'Channel not found')
})
await t.test('fails with unauthenticated user', async (t) => {
await t.test('fails with unauthenticated user', async () => {
const response = await application.inject({
method: 'GET',
url: `/channels/1/messages`
})
t.equal(response.statusCode, 401)
assert.strictEqual(response.statusCode, 401)
})
})

View File

@ -1,20 +1,22 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../../application.js'
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../../tools/database/prisma.js'
import { channelExample } from '../../../../../models/Channel.js'
import { memberExample } from '../../../../../models/Member.js'
import { userExample } from '../../../../../models/User.js'
import { messageExample } from '../../../../../models/Message.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { channelExample } from '#src/models/Channel.js'
import { memberExample } from '#src/models/Member.js'
import { userExample } from '#src/models/User.js'
import { messageExample } from '#src/models/Message.js'
await tap.test('POST /channels/[channelId]/messages', async (t) => {
await test('POST /channels/[channelId]/messages', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -43,18 +45,18 @@ await tap.test('POST /channels/[channelId]/messages', async (t) => {
payload: { value: messageExample.value }
})
const responseJson = response.json()
t.equal(response.statusCode, 201)
t.equal(responseJson.id, messageExample.id)
t.equal(responseJson.value, messageExample.value)
t.equal(responseJson.type, messageExample.type)
t.equal(responseJson.mimetype, messageExample.mimetype)
t.equal(responseJson.member.id, memberExample.id)
t.equal(responseJson.member.isOwner, memberExample.isOwner)
t.equal(responseJson.member.user.id, userExample.id)
t.equal(responseJson.member.user.name, userExample.name)
assert.strictEqual(response.statusCode, 201)
assert.strictEqual(responseJson.id, messageExample.id)
assert.strictEqual(responseJson.value, messageExample.value)
assert.strictEqual(responseJson.type, messageExample.type)
assert.strictEqual(responseJson.mimetype, messageExample.mimetype)
assert.strictEqual(responseJson.member.id, memberExample.id)
assert.strictEqual(responseJson.member.isOwner, memberExample.isOwner)
assert.strictEqual(responseJson.member.user.id, userExample.id)
assert.strictEqual(responseJson.member.user.name, userExample.name)
})
await t.test('fails with no message value', async (t) => {
await t.test('fails with no message value', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -77,10 +79,10 @@ await tap.test('POST /channels/[channelId]/messages', async (t) => {
},
payload: {}
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
await t.test('fails with not found channel', async (t) => {
await t.test('fails with not found channel', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -104,11 +106,11 @@ await tap.test('POST /channels/[channelId]/messages', async (t) => {
payload: { value: messageExample.value }
})
const responseJson = response.json()
t.equal(response.statusCode, 404)
t.equal(responseJson.message, 'Channel not found')
assert.strictEqual(response.statusCode, 404)
assert.strictEqual(responseJson.message, 'Channel not found')
})
await t.test('fails with not found member', async (t) => {
await t.test('fails with not found member', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
@ -129,7 +131,7 @@ await tap.test('POST /channels/[channelId]/messages', async (t) => {
payload: { value: messageExample.value }
})
const responseJson = response.json()
t.equal(response.statusCode, 404)
t.equal(responseJson.message, 'Channel not found')
assert.strictEqual(response.statusCode, 404)
assert.strictEqual(responseJson.message, 'Channel not found')
})
})

View File

@ -2,17 +2,17 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../../models/utils.js'
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
import { messageSchema } from '../../../../models/Message.js'
import { memberSchema } from '../../../../models/Member.js'
import { userPublicWithoutSettingsSchema } from '../../../../models/User.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { messageSchema } from '#src/models/Message.js'
import { memberSchema } from '#src/models/Member.js'
import { userPublicWithoutSettingsSchema } from '#src/models/User.js'
import {
getPaginationOptions,
queryPaginationObjectSchema
} from '../../../../tools/database/pagination.js'
import { channelSchema } from '../../../../models/Channel.js'
} from '#src/tools/database/pagination.js'
import { channelSchema } from '#src/models/Channel.js'
type QuerySchemaType = Static<typeof queryPaginationObjectSchema>

View File

@ -2,13 +2,13 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../../models/utils.js'
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
import { messageSchema } from '../../../../models/Message.js'
import { channelSchema } from '../../../../models/Channel.js'
import { memberSchema } from '../../../../models/Member.js'
import { userPublicWithoutSettingsSchema } from '../../../../models/User.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { messageSchema } from '#src/models/Message.js'
import { channelSchema } from '#src/models/Channel.js'
import { memberSchema } from '#src/models/Member.js'
import { userPublicWithoutSettingsSchema } from '#src/models/User.js'
const parametersSchema = Type.Object({
channelId: channelSchema.id

View File

@ -3,14 +3,14 @@ import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import fastifyMultipart from '@fastify/multipart'
import prisma from '../../../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
import { messageSchema } from '../../../../../models/Message.js'
import { memberSchema } from '../../../../../models/Member.js'
import { userPublicWithoutSettingsSchema } from '../../../../../models/User.js'
import { channelSchema } from '../../../../../models/Channel.js'
import { uploadFile } from '../../../../../tools/utils/uploadFile.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { messageSchema } from '#src/models/Message.js'
import { memberSchema } from '#src/models/Member.js'
import { userPublicWithoutSettingsSchema } from '#src/models/User.js'
import { channelSchema } from '#src/models/Channel.js'
import { uploadFile } from '#src/tools/utils/uploadFile.js'
const parametersSchema = Type.Object({
channelId: channelSchema.id

View File

@ -2,10 +2,10 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
import { channelSchema } from '../../../models/Channel.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { channelSchema } from '#src/models/Channel.js'
const bodyPutServiceSchema = Type.Object({
name: channelSchema.name
@ -56,9 +56,9 @@ export const putChannelService: FastifyPluginAsync = async (fastify) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { user } = request
const { channelId } = request.params
const { name } = request.body
const { user, params, body } = request
const { channelId } = params
const { name } = body
const channelCheck = await prisma.channel.findUnique({
where: { id: channelId }
})

View File

@ -1,18 +1,20 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../tools/database/prisma.js'
import { memberExample } from '../../../../models/Member.js'
import { guildExample } from '../../../../models/Guild.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { memberExample } from '#src/models/Member.js'
import { guildExample } from '#src/models/Guild.js'
await tap.test('DELETE /guilds/[guildId]', async (t) => {
await test('DELETE /guilds/[guildId]', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds and delete the guild', async (t) => {
await t.test('succeeds and delete the guild', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -36,13 +38,13 @@ await tap.test('DELETE /guilds/[guildId]', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.id, guildExample.id)
t.equal(responseJson.name, guildExample.name)
t.equal(responseJson.description, guildExample.description)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.id, guildExample.id)
assert.strictEqual(responseJson.name, guildExample.name)
assert.strictEqual(responseJson.description, guildExample.description)
})
await t.test("fails if the guild doesn't exist", async (t) => {
await t.test("fails if the guild doesn't exist", async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -56,10 +58,10 @@ await tap.test('DELETE /guilds/[guildId]', async (t) => {
authorization: `Bearer ${accessToken}`
}
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the user is not the owner', async (t) => {
await t.test('fails if the user is not the owner', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -78,7 +80,10 @@ await tap.test('DELETE /guilds/[guildId]', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 400)
t.equal(responseJson.message, 'You should be an owner of the guild')
assert.strictEqual(response.statusCode, 400)
assert.strictEqual(
responseJson.message,
'You should be an owner of the guild'
)
})
})

View File

@ -1,22 +1,24 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../tools/database/prisma.js'
import { memberExample } from '../../../../models/Member.js'
import { guildExample } from '../../../../models/Guild.js'
import { userExample } from '../../../../models/User.js'
import { channelExample } from '../../../../models/Channel.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { memberExample } from '#src/models/Member.js'
import { guildExample } from '#src/models/Guild.js'
import { userExample } from '#src/models/User.js'
import { channelExample } from '#src/models/Channel.js'
const defaultChannelId = 5
await tap.test('GET /guilds/[guildId]', async (t) => {
await test('GET /guilds/[guildId]', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken, user } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -43,17 +45,17 @@ await tap.test('GET /guilds/[guildId]', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.member.id, memberExample.id)
t.equal(responseJson.member.isOwner, memberExample.isOwner)
t.equal(responseJson.member.user.name, user.name)
t.equal(responseJson.member.user.email, null)
t.equal(responseJson.guild.id, guildExample.id)
t.equal(responseJson.guild.name, guildExample.name)
t.equal(responseJson.guild.defaultChannelId, defaultChannelId)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.member.id, memberExample.id)
assert.strictEqual(responseJson.member.isOwner, memberExample.isOwner)
assert.strictEqual(responseJson.member.user.name, user.name)
assert.strictEqual(responseJson.member.user.email, null)
assert.strictEqual(responseJson.guild.id, guildExample.id)
assert.strictEqual(responseJson.guild.name, guildExample.name)
assert.strictEqual(responseJson.guild.defaultChannelId, defaultChannelId)
})
await t.test('fails with not found member/guild', async (t) => {
await t.test('fails with not found member/guild', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -68,15 +70,15 @@ await tap.test('GET /guilds/[guildId]', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 404)
t.equal(responseJson.message, 'Member not found')
assert.strictEqual(response.statusCode, 404)
assert.strictEqual(responseJson.message, 'Member not found')
})
await t.test('fails with unauthenticated user', async (t) => {
await t.test('fails with unauthenticated user', async () => {
const response = await application.inject({
method: 'GET',
url: '/guilds/1'
})
t.equal(response.statusCode, 401)
assert.strictEqual(response.statusCode, 401)
})
})

View File

@ -1,23 +1,25 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../tools/database/prisma.js'
import { memberExample } from '../../../../models/Member.js'
import { guildExample } from '../../../../models/Guild.js'
import { channelExample } from '../../../../models/Channel.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { memberExample } from '#src/models/Member.js'
import { guildExample } from '#src/models/Guild.js'
import { channelExample } from '#src/models/Channel.js'
const defaultChannelId = 5
const newName = 'New guild name'
const newDescription = 'New guild description'
await tap.test('PUT /guilds/[guildId]', async (t) => {
await test('PUT /guilds/[guildId]', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds and edit the guild', async (t) => {
await t.test('succeeds and edit the guild', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -57,13 +59,13 @@ await tap.test('PUT /guilds/[guildId]', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.name, newName)
t.equal(responseJson.description, newDescription)
t.equal(responseJson.defaultChannelId, defaultChannelId)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.name, newName)
assert.strictEqual(responseJson.description, newDescription)
assert.strictEqual(responseJson.defaultChannelId, defaultChannelId)
})
await t.test("fails if the guild doesn't exist", async (t) => {
await t.test("fails if the guild doesn't exist", async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -81,10 +83,10 @@ await tap.test('PUT /guilds/[guildId]', async (t) => {
description: newDescription
}
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the user is not the owner', async (t) => {
await t.test('fails if the user is not the owner', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -107,7 +109,10 @@ await tap.test('PUT /guilds/[guildId]', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 400)
t.equal(responseJson.message, 'You should be an owner of the guild')
assert.strictEqual(response.statusCode, 400)
assert.strictEqual(
responseJson.message,
'You should be an owner of the guild'
)
})
})

View File

@ -1,19 +1,21 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../../application.js'
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../../tools/database/prisma.js'
import { memberExample } from '../../../../../models/Member.js'
import { guildExample } from '../../../../../models/Guild.js'
import { channelExample } from '../../../../../models/Channel.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { memberExample } from '#src/models/Member.js'
import { guildExample } from '#src/models/Guild.js'
import { channelExample } from '#src/models/Channel.js'
await tap.test('GET /guilds/[guildId]/channels', async (t) => {
await test('GET /guilds/[guildId]/channels', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -33,14 +35,14 @@ await tap.test('GET /guilds/[guildId]/channels', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.length, 1)
t.equal(responseJson[0].id, channelExample.id)
t.equal(responseJson[0].name, channelExample.name)
t.equal(responseJson[0].guildId, channelExample.guildId)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.length, 1)
assert.strictEqual(responseJson[0].id, channelExample.id)
assert.strictEqual(responseJson[0].name, channelExample.name)
assert.strictEqual(responseJson[0].guildId, channelExample.guildId)
})
await t.test('fails with not found member/guild', async (t) => {
await t.test('fails with not found member/guild', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -55,15 +57,15 @@ await tap.test('GET /guilds/[guildId]/channels', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 404)
t.equal(responseJson.message, 'Member not found')
assert.strictEqual(response.statusCode, 404)
assert.strictEqual(responseJson.message, 'Member not found')
})
await t.test('fails with unauthenticated user', async (t) => {
await t.test('fails with unauthenticated user', async () => {
const response = await application.inject({
method: 'GET',
url: '/guilds/1/channels'
})
t.equal(response.statusCode, 401)
assert.strictEqual(response.statusCode, 401)
})
})

View File

@ -1,21 +1,23 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../../application.js'
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../../tools/database/prisma.js'
import { memberExample } from '../../../../../models/Member.js'
import { guildExample } from '../../../../../models/Guild.js'
import { channelExample } from '../../../../../models/Channel.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { memberExample } from '#src/models/Member.js'
import { guildExample } from '#src/models/Guild.js'
import { channelExample } from '#src/models/Channel.js'
const defaultChannelId = 5
await tap.test('POST /guilds/[guildId]/channels', async (t) => {
await test('POST /guilds/[guildId]/channels', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -42,14 +44,14 @@ await tap.test('POST /guilds/[guildId]/channels', async (t) => {
payload: { name: channelExample.name }
})
const responseJson = response.json()
t.equal(response.statusCode, 201)
t.equal(responseJson.id, channelExample.id)
t.equal(responseJson.name, channelExample.name)
t.equal(responseJson.guildId, channelExample.guildId)
t.equal(responseJson.defaultChannelId, defaultChannelId)
assert.strictEqual(response.statusCode, 201)
assert.strictEqual(responseJson.id, channelExample.id)
assert.strictEqual(responseJson.name, channelExample.name)
assert.strictEqual(responseJson.guildId, channelExample.guildId)
assert.strictEqual(responseJson.defaultChannelId, defaultChannelId)
})
await t.test('fails if the member is not found', async (t) => {
await t.test('fails if the member is not found', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -64,10 +66,10 @@ await tap.test('POST /guilds/[guildId]/channels', async (t) => {
},
payload: { name: channelExample.name }
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the member is not owner', async (t) => {
await t.test('fails if the member is not owner', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -85,6 +87,6 @@ await tap.test('POST /guilds/[guildId]/channels', async (t) => {
},
payload: { name: channelExample.name }
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
})

View File

@ -2,15 +2,15 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../../models/utils.js'
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
import { guildSchema } from '../../../../models/Guild.js'
import { channelSchema } from '../../../../models/Channel.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { guildSchema } from '#src/models/Guild.js'
import { channelSchema } from '#src/models/Channel.js'
import {
getPaginationOptions,
queryPaginationObjectSchema
} from '../../../../tools/database/pagination.js'
} from '#src/tools/database/pagination.js'
type QuerySchemaType = Static<typeof queryPaginationObjectSchema>

View File

@ -2,11 +2,11 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../../models/utils.js'
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
import { channelSchema } from '../../../../models/Channel.js'
import { guildSchema } from '../../../../models/Guild.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { channelSchema } from '#src/models/Channel.js'
import { guildSchema } from '#src/models/Guild.js'
const bodyPostServiceSchema = Type.Object({
name: channelSchema.name
@ -57,9 +57,9 @@ export const postChannelService: FastifyPluginAsync = async (fastify) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { user } = request
const { guildId } = request.params
const { name } = request.body
const { user, params, body } = request
const { guildId } = params
const { name } = body
const member = await prisma.member.findFirst({
where: { guildId, userId: user.current.id }
})

View File

@ -2,10 +2,10 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
import { guildSchema } from '../../../models/Guild.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { guildSchema } from '#src/models/Guild.js'
const parametersSchema = Type.Object({
guildId: guildSchema.id

View File

@ -2,13 +2,13 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
import { guildSchema } from '../../../models/Guild.js'
import { memberSchema } from '../../../models/Member.js'
import { userPublicWithoutSettingsSchema } from '../../../models/User.js'
import { channelSchema } from '../../../models/Channel.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { guildSchema } from '#src/models/Guild.js'
import { memberSchema } from '#src/models/Member.js'
import { userPublicWithoutSettingsSchema } from '#src/models/User.js'
import { channelSchema } from '#src/models/Channel.js'
const parametersSchema = Type.Object({
guildId: guildSchema.id

View File

@ -3,12 +3,12 @@ import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import fastifyMultipart from '@fastify/multipart'
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
import { fastifyErrors } from '../../../../models/utils.js'
import prisma from '../../../../tools/database/prisma.js'
import { uploadFile } from '../../../../tools/utils/uploadFile.js'
import { guildSchema } from '../../../../models/Guild.js'
import { channelSchema } from '../../../../models/Channel.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { fastifyErrors } from '#src/models/utils.js'
import prisma from '#src/tools/database/prisma.js'
import { uploadFile } from '#src/tools/utils/uploadFile.js'
import { guildSchema } from '#src/models/Guild.js'
import { channelSchema } from '#src/models/Channel.js'
const parametersSchema = Type.Object({
guildId: guildSchema.id

View File

@ -1,19 +1,21 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../../application.js'
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../../tools/database/prisma.js'
import { memberExample } from '../../../../../models/Member.js'
import { guildExample } from '../../../../../models/Guild.js'
import { userExample } from '../../../../../models/User.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { memberExample } from '#src/models/Member.js'
import { guildExample } from '#src/models/Guild.js'
import { userExample } from '#src/models/User.js'
await tap.test('GET /guilds/[guildId]/members', async (t) => {
await test('GET /guilds/[guildId]/members', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -31,16 +33,16 @@ await tap.test('GET /guilds/[guildId]/members', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.length, 1)
t.equal(responseJson[0].id, memberExample.id)
t.equal(responseJson[0].isOwner, memberExample.isOwner)
t.equal(responseJson[0].user.id, userExample.id)
t.equal(responseJson[0].user.name, userExample.name)
t.equal(responseJson[0].user.email, null)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.length, 1)
assert.strictEqual(responseJson[0].id, memberExample.id)
assert.strictEqual(responseJson[0].isOwner, memberExample.isOwner)
assert.strictEqual(responseJson[0].user.id, userExample.id)
assert.strictEqual(responseJson[0].user.name, userExample.name)
assert.strictEqual(responseJson[0].user.email, null)
})
await t.test('fails with not found member/guild', async (t) => {
await t.test('fails with not found member/guild', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -55,15 +57,15 @@ await tap.test('GET /guilds/[guildId]/members', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 404)
t.equal(responseJson.message, 'Member not found')
assert.strictEqual(response.statusCode, 404)
assert.strictEqual(responseJson.message, 'Member not found')
})
await t.test('fails with unauthenticated user', async (t) => {
await t.test('fails with unauthenticated user', async () => {
const response = await application.inject({
method: 'GET',
url: '/guilds/1/members'
})
t.equal(response.statusCode, 401)
assert.strictEqual(response.statusCode, 401)
})
})

View File

@ -2,16 +2,16 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../../models/utils.js'
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
import { guildSchema } from '../../../../models/Guild.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { guildSchema } from '#src/models/Guild.js'
import {
getPaginationOptions,
queryPaginationObjectSchema
} from '../../../../tools/database/pagination.js'
import { memberSchema } from '../../../../models/Member.js'
import { userPublicWithoutSettingsSchema } from '../../../../models/User.js'
} from '#src/tools/database/pagination.js'
import { memberSchema } from '#src/models/Member.js'
import { userPublicWithoutSettingsSchema } from '#src/models/User.js'
type QuerySchemaType = Static<typeof queryPaginationObjectSchema>

View File

@ -1,22 +1,24 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../../../application.js'
import { authenticateUserTest } from '../../../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../../../tools/database/prisma.js'
import { memberExample } from '../../../../../../models/Member.js'
import { guildExample } from '../../../../../../models/Guild.js'
import { userExample } from '../../../../../../models/User.js'
import { channelExample } from '../../../../../../models/Channel.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { memberExample } from '#src/models/Member.js'
import { guildExample } from '#src/models/Guild.js'
import { userExample } from '#src/models/User.js'
import { channelExample } from '#src/models/Channel.js'
const defaultChannelId = 5
await tap.test('POST /guilds/[guildId]/members/join', async (t) => {
await test('POST /guilds/[guildId]/members/join', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -44,17 +46,17 @@ await tap.test('POST /guilds/[guildId]/members/join', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 201)
t.equal(responseJson.id, memberExample.id)
t.equal(responseJson.userId, memberExample.userId)
t.equal(responseJson.user.name, userExample.name)
t.equal(responseJson.user.email, null)
t.equal(responseJson.guild.id, guildExample.id)
t.equal(responseJson.guild.name, guildExample.name)
t.equal(responseJson.guild.defaultChannelId, channelExample.id)
assert.strictEqual(response.statusCode, 201)
assert.strictEqual(responseJson.id, memberExample.id)
assert.strictEqual(responseJson.userId, memberExample.userId)
assert.strictEqual(responseJson.user.name, userExample.name)
assert.strictEqual(responseJson.user.email, null)
assert.strictEqual(responseJson.guild.id, guildExample.id)
assert.strictEqual(responseJson.guild.name, guildExample.name)
assert.strictEqual(responseJson.guild.defaultChannelId, channelExample.id)
})
await t.test('fails if the guild is not found', async (t) => {
await t.test('fails if the guild is not found', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -78,10 +80,10 @@ await tap.test('POST /guilds/[guildId]/members/join', async (t) => {
authorization: `Bearer ${accessToken}`
}
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the user is already in the guild', async (t) => {
await t.test('fails if the user is already in the guild', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -109,7 +111,7 @@ await tap.test('POST /guilds/[guildId]/members/join', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 400)
t.equal(responseJson.defaultChannelId, defaultChannelId)
assert.strictEqual(response.statusCode, 400)
assert.strictEqual(responseJson.defaultChannelId, defaultChannelId)
})
})

View File

@ -2,17 +2,13 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../../../tools/database/prisma.js'
import {
fastifyErrors,
fastifyErrorsSchema,
id
} from '../../../../../models/utils.js'
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
import { guildSchema } from '../../../../../models/Guild.js'
import { memberSchema } from '../../../../../models/Member.js'
import { userPublicWithoutSettingsSchema } from '../../../../../models/User.js'
import { channelSchema } from '../../../../../models/Channel.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors, fastifyErrorsSchema, id } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { guildSchema } from '#src/models/Guild.js'
import { memberSchema } from '#src/models/Member.js'
import { userPublicWithoutSettingsSchema } from '#src/models/User.js'
import { channelSchema } from '#src/models/Channel.js'
const parametersSchema = Type.Object({
guildId: guildSchema.id
@ -62,8 +58,8 @@ export const postMemberService: FastifyPluginAsync = async (fastify) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { user } = request
const { guildId } = request.params
const { user, params } = request
const { guildId } = params
const guild = await prisma.guild.findUnique({
where: {
id: guildId

View File

@ -1,18 +1,20 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../../../application.js'
import { authenticateUserTest } from '../../../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../../../tools/database/prisma.js'
import { memberExample } from '../../../../../../models/Member.js'
import { guildExample } from '../../../../../../models/Guild.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { memberExample } from '#src/models/Member.js'
import { guildExample } from '#src/models/Guild.js'
await tap.test('DELETE /guilds/[guildId]/members/leave', async (t) => {
await test('DELETE /guilds/[guildId]/members/leave', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken } = await authenticateUserTest()
const member = {
...memberExample,
@ -34,13 +36,13 @@ await tap.test('DELETE /guilds/[guildId]/members/leave', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.id, member.id)
t.equal(responseJson.isOwner, member.isOwner)
t.equal(responseJson.userId, member.userId)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.id, member.id)
assert.strictEqual(responseJson.isOwner, member.isOwner)
assert.strictEqual(responseJson.userId, member.userId)
})
await t.test('fails if the member is not found', async (t) => {
await t.test('fails if the member is not found', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
@ -54,10 +56,10 @@ await tap.test('DELETE /guilds/[guildId]/members/leave', async (t) => {
authorization: `Bearer ${accessToken}`
}
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the member is owner', async (t) => {
await t.test('fails if the member is owner', async () => {
const { accessToken } = await authenticateUserTest()
const member = {
...memberExample,
@ -75,6 +77,6 @@ await tap.test('DELETE /guilds/[guildId]/members/leave', async (t) => {
authorization: `Bearer ${accessToken}`
}
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
})

View File

@ -2,11 +2,11 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
import { guildSchema } from '../../../../../models/Guild.js'
import { memberSchema } from '../../../../../models/Member.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { guildSchema } from '#src/models/Guild.js'
import { memberSchema } from '#src/models/Member.js'
const parametersSchema = Type.Object({
guildId: guildSchema.id
@ -46,8 +46,8 @@ export const deleteMemberService: FastifyPluginAsync = async (fastify) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { user } = request
const { guildId } = request.params
const { user, params } = request
const { guildId } = params
const member = await prisma.member.findFirst({
where: { guildId, userId: user.current.id }
})

View File

@ -2,12 +2,12 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
import { guildSchema } from '../../../models/Guild.js'
import { parseStringNullish } from '../../../tools/utils/parseStringNullish.js'
import { channelSchema } from '../../../models/Channel.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { guildSchema } from '#src/models/Guild.js'
import { parseStringNullish } from '#src/tools/utils/parseStringNullish.js'
import { channelSchema } from '#src/models/Channel.js'
const parametersSchema = Type.Object({
guildId: guildSchema.id

View File

@ -1,19 +1,21 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../application.js'
import { authenticateUserTest } from '../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../tools/database/prisma.js'
import { memberExample } from '../../../models/Member.js'
import { guildExample } from '../../../models/Guild.js'
import { channelExample } from '../../../models/Channel.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { memberExample } from '#src/models/Member.js'
import { guildExample } from '#src/models/Guild.js'
import { channelExample } from '#src/models/Channel.js'
await tap.test('GET /guilds', async (t) => {
await test('GET /guilds', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'guild').value({
findUnique: async () => {
@ -38,10 +40,10 @@ await tap.test('GET /guilds', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.length, 1)
t.equal(responseJson[0].name, guildExample.name)
t.equal(responseJson[0].description, guildExample.description)
t.equal(responseJson[0].defaultChannelId, channelExample.id)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.length, 1)
assert.strictEqual(responseJson[0].name, guildExample.name)
assert.strictEqual(responseJson[0].description, guildExample.description)
assert.strictEqual(responseJson[0].defaultChannelId, channelExample.id)
})
})

View File

@ -1,20 +1,22 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../application.js'
import { authenticateUserTest } from '../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../tools/database/prisma.js'
import { memberExample } from '../../../models/Member.js'
import { guildExample } from '../../../models/Guild.js'
import { channelExample } from '../../../models/Channel.js'
import { userExample } from '../../../models/User.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { memberExample } from '#src/models/Member.js'
import { guildExample } from '#src/models/Guild.js'
import { channelExample } from '#src/models/Channel.js'
import { userExample } from '#src/models/User.js'
await tap.test('POST /guilds', async (t) => {
await test('POST /guilds', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken, user } = await authenticateUserTest()
sinon.stub(prisma, 'guild').value({
create: async () => {
@ -49,21 +51,24 @@ await tap.test('POST /guilds', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 201)
t.equal(responseJson.guild.id, guildExample.id)
t.equal(responseJson.guild.name, guildExample.name)
t.equal(responseJson.guild.description, guildExample.description)
t.equal(responseJson.guild.members.length, 1)
t.equal(responseJson.guild.members[0].userId, user.id)
t.equal(responseJson.guild.members[0].user.name, user.name)
t.equal(responseJson.guild.members[0].guildId, guildExample.id)
t.equal(responseJson.guild.members[0].isOwner, memberExample.isOwner)
t.equal(responseJson.guild.channels.length, 1)
t.equal(responseJson.guild.channels[0].id, channelExample.id)
t.equal(responseJson.guild.channels[0].guildId, guildExample.id)
assert.strictEqual(response.statusCode, 201)
assert.strictEqual(responseJson.guild.id, guildExample.id)
assert.strictEqual(responseJson.guild.name, guildExample.name)
assert.strictEqual(responseJson.guild.description, guildExample.description)
assert.strictEqual(responseJson.guild.members.length, 1)
assert.strictEqual(responseJson.guild.members[0].userId, user.id)
assert.strictEqual(responseJson.guild.members[0].user.name, user.name)
assert.strictEqual(responseJson.guild.members[0].guildId, guildExample.id)
assert.strictEqual(
responseJson.guild.members[0].isOwner,
memberExample.isOwner
)
assert.strictEqual(responseJson.guild.channels.length, 1)
assert.strictEqual(responseJson.guild.channels[0].id, channelExample.id)
assert.strictEqual(responseJson.guild.channels[0].guildId, guildExample.id)
})
await t.test('fails with empty name and description', async (t) => {
await t.test('fails with empty name and description', async () => {
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'POST',
@ -72,6 +77,6 @@ await tap.test('POST /guilds', async (t) => {
authorization: `Bearer ${accessToken}`
}
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
})

View File

@ -2,14 +2,14 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../tools/database/prisma.js'
import { fastifyErrors, id } from '../../models/utils.js'
import authenticateUser from '../../tools/plugins/authenticateUser.js'
import { guildSchema } from '../../models/Guild.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors, id } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { guildSchema } from '#src/models/Guild.js'
import {
getPaginationOptions,
queryPaginationObjectSchema
} from '../../tools/database/pagination.js'
} from '#src/tools/database/pagination.js'
type QuerySchemaType = Static<typeof queryPaginationObjectSchema>

View File

@ -2,14 +2,14 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../tools/database/prisma.js'
import { fastifyErrors } from '../../models/utils.js'
import authenticateUser from '../../tools/plugins/authenticateUser.js'
import { guildSchema } from '../../models/Guild.js'
import { channelSchema } from '../../models/Channel.js'
import { memberSchema } from '../../models/Member.js'
import { userPublicWithoutSettingsSchema } from '../../models/User.js'
import { parseStringNullish } from '../../tools/utils/parseStringNullish.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { guildSchema } from '#src/models/Guild.js'
import { channelSchema } from '#src/models/Channel.js'
import { memberSchema } from '#src/models/Member.js'
import { userPublicWithoutSettingsSchema } from '#src/models/User.js'
import { parseStringNullish } from '#src/tools/utils/parseStringNullish.js'
const bodyPostServiceSchema = Type.Object({
name: guildSchema.name,

View File

@ -1,17 +1,19 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../tools/database/prisma.js'
import { guildExample } from '../../../../models/Guild.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { guildExample } from '#src/models/Guild.js'
await tap.test('GET /guilds/public', async (t) => {
await test('GET /guilds/public', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'guild').value({
findMany: async () => {
@ -31,9 +33,9 @@ await tap.test('GET /guilds/public', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.length, 1)
t.equal(responseJson[0].name, guildExample.name)
t.equal(responseJson[0].membersCount, 2)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.length, 1)
assert.strictEqual(responseJson[0].name, guildExample.name)
assert.strictEqual(responseJson[0].membersCount, 2)
})
})

View File

@ -2,14 +2,14 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
import { guildSchema } from '../../../models/Guild.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { guildSchema } from '#src/models/Guild.js'
import {
getPaginationOptions,
queryPaginationSchema
} from '../../../tools/database/pagination.js'
} from '#src/tools/database/pagination.js'
const querySchema = Type.Object({
search: Type.Optional(Type.String()),

View File

@ -1,20 +1,22 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../tools/database/prisma.js'
import { messageExample } from '../../../../models/Message.js'
import { memberExample } from '../../../../models/Member.js'
import { userExample } from '../../../../models/User.js'
import { channelExample } from '../../../../models/Channel.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { messageExample } from '#src/models/Message.js'
import { memberExample } from '#src/models/Member.js'
import { userExample } from '#src/models/User.js'
import { channelExample } from '#src/models/Channel.js'
await tap.test('DELETE /messsages/[messageId]', async (t) => {
await test('DELETE /messsages/[messageId]', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'message').value({
findFirst: async () => {
@ -43,18 +45,18 @@ await tap.test('DELETE /messsages/[messageId]', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.id, messageExample.id)
t.equal(responseJson.value, messageExample.value)
t.equal(responseJson.type, messageExample.type)
t.equal(responseJson.mimetype, messageExample.mimetype)
t.equal(responseJson.member.id, memberExample.id)
t.equal(responseJson.member.isOwner, memberExample.isOwner)
t.equal(responseJson.member.user.id, userExample.id)
t.equal(responseJson.member.user.name, userExample.name)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.id, messageExample.id)
assert.strictEqual(responseJson.value, messageExample.value)
assert.strictEqual(responseJson.type, messageExample.type)
assert.strictEqual(responseJson.mimetype, messageExample.mimetype)
assert.strictEqual(responseJson.member.id, memberExample.id)
assert.strictEqual(responseJson.member.isOwner, memberExample.isOwner)
assert.strictEqual(responseJson.member.user.id, userExample.id)
assert.strictEqual(responseJson.member.user.name, userExample.name)
})
await t.test('fails if the message is not found', async (t) => {
await t.test('fails if the message is not found', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'message').value({
findFirst: async () => {
@ -68,10 +70,10 @@ await tap.test('DELETE /messsages/[messageId]', async (t) => {
authorization: `Bearer ${accessToken}`
}
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the member is not found', async (t) => {
await t.test('fails if the member is not found', async () => {
const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'message').value({
findFirst: async () => {
@ -93,10 +95,10 @@ await tap.test('DELETE /messsages/[messageId]', async (t) => {
authorization: `Bearer ${accessToken}`
}
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the member is not owner of the message', async (t) => {
await t.test('fails if the member is not owner of the message', async () => {
const { accessToken } = await authenticateUserTest()
const randomUserIdOwnerOfMessage = 14
sinon.stub(prisma, 'message').value({
@ -122,6 +124,6 @@ await tap.test('DELETE /messsages/[messageId]', async (t) => {
authorization: `Bearer ${accessToken}`
}
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
})

View File

@ -1,20 +1,22 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../tools/database/prisma.js'
import { messageExample } from '../../../../models/Message.js'
import { memberExample } from '../../../../models/Member.js'
import { userExample } from '../../../../models/User.js'
import { channelExample } from '../../../../models/Channel.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { messageExample } from '#src/models/Message.js'
import { memberExample } from '#src/models/Member.js'
import { userExample } from '#src/models/User.js'
import { channelExample } from '#src/models/Channel.js'
await tap.test('PUT /messsages/[messageId]', async (t) => {
await test('PUT /messsages/[messageId]', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken } = await authenticateUserTest()
const newValue = 'some message'
sinon.stub(prisma, 'message').value({
@ -48,18 +50,18 @@ await tap.test('PUT /messsages/[messageId]', async (t) => {
payload: { value: newValue }
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.id, messageExample.id)
t.equal(responseJson.value, newValue)
t.equal(responseJson.type, messageExample.type)
t.equal(responseJson.mimetype, messageExample.mimetype)
t.equal(responseJson.member.id, memberExample.id)
t.equal(responseJson.member.isOwner, memberExample.isOwner)
t.equal(responseJson.member.user.id, userExample.id)
t.equal(responseJson.member.user.name, userExample.name)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.id, messageExample.id)
assert.strictEqual(responseJson.value, newValue)
assert.strictEqual(responseJson.type, messageExample.type)
assert.strictEqual(responseJson.mimetype, messageExample.mimetype)
assert.strictEqual(responseJson.member.id, memberExample.id)
assert.strictEqual(responseJson.member.isOwner, memberExample.isOwner)
assert.strictEqual(responseJson.member.user.id, userExample.id)
assert.strictEqual(responseJson.member.user.name, userExample.name)
})
await t.test('fails if the message is not found', async (t) => {
await t.test('fails if the message is not found', async () => {
const { accessToken } = await authenticateUserTest()
const newValue = 'some message'
sinon.stub(prisma, 'message').value({
@ -75,10 +77,10 @@ await tap.test('PUT /messsages/[messageId]', async (t) => {
},
payload: { value: newValue }
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
await t.test('fails if the member is not found', async (t) => {
await t.test('fails if the member is not found', async () => {
const { accessToken } = await authenticateUserTest()
const newValue = 'some message'
sinon.stub(prisma, 'message').value({
@ -102,12 +104,12 @@ await tap.test('PUT /messsages/[messageId]', async (t) => {
},
payload: { value: newValue }
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
await t.test(
'fails if the member is not the owner of the message',
async (t) => {
async () => {
const { accessToken } = await authenticateUserTest()
const newValue = 'some message'
const randomUserIdOwnerOfMessage = 14
@ -135,7 +137,7 @@ await tap.test('PUT /messsages/[messageId]', async (t) => {
},
payload: { value: newValue }
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
}
)
})

View File

@ -2,12 +2,12 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
import { messageSchema } from '../../../models/Message.js'
import { memberSchema } from '../../../models/Member.js'
import { userPublicWithoutSettingsSchema } from '../../../models/User.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { messageSchema } from '#src/models/Message.js'
import { memberSchema } from '#src/models/Member.js'
import { userPublicWithoutSettingsSchema } from '#src/models/User.js'
const parametersSchema = Type.Object({
messageId: messageSchema.id
@ -53,8 +53,8 @@ export const deleteMessageService: FastifyPluginAsync = async (fastify) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { user } = request
const { messageId } = request.params
const { user, params } = request
const { messageId } = params
const messageCheck = await prisma.message.findFirst({
where: { id: messageId },
include: {

View File

@ -2,12 +2,12 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
import { messageSchema } from '../../../models/Message.js'
import { memberSchema } from '../../../models/Member.js'
import { userPublicWithoutSettingsSchema } from '../../../models/User.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { messageSchema } from '#src/models/Message.js'
import { memberSchema } from '#src/models/Member.js'
import { userPublicWithoutSettingsSchema } from '#src/models/User.js'
const bodyPutServiceSchema = Type.Object({
value: messageSchema.value
@ -61,9 +61,9 @@ export const putMessageService: FastifyPluginAsync = async (fastify) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { user } = request
const { messageId } = request.params
const { value } = request.body
const { user, params, body } = request
const { messageId } = params
const { value } = body
const messageCheck = await prisma.message.findFirst({
where: { id: messageId, type: 'text' },
include: {

View File

@ -1,17 +1,19 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import prisma from '../../../../tools/database/prisma.js'
import { userExample } from '../../../../models/User.js'
import { userSettingsExample } from '../../../../models/UserSettings.js'
import { application } from '#src/application.js'
import prisma from '#src/tools/database/prisma.js'
import { userExample } from '#src/models/User.js'
import { userSettingsExample } from '#src/models/UserSettings.js'
await tap.test('GET /users/[userId]', async (t) => {
await test('GET /users/[userId]', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
sinon.stub(prisma, 'guild').value({
findMany: async () => {
return []
@ -32,12 +34,12 @@ await tap.test('GET /users/[userId]', async (t) => {
url: `/users/${userExample.id}`
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.user.id, userExample.id)
t.equal(responseJson.user.name, userExample.name)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.user.id, userExample.id)
assert.strictEqual(responseJson.user.name, userExample.name)
})
await t.test('fails with not found user', async (t) => {
await t.test('fails with not found user', async () => {
sinon.stub(prisma, 'userSetting').value({
findFirst: async () => {
return null
@ -48,7 +50,7 @@ await tap.test('GET /users/[userId]', async (t) => {
url: `/users/1`
})
const responseJson = response.json()
t.equal(response.statusCode, 404)
t.equal(responseJson.message, 'User not found')
assert.strictEqual(response.statusCode, 404)
assert.strictEqual(responseJson.message, 'User not found')
})
})

View File

@ -2,10 +2,10 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import { userPublicSchema } from '../../../models/User.js'
import { guildSchema } from '../../../models/Guild.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import { userPublicSchema } from '#src/models/User.js'
import { guildSchema } from '#src/models/Guild.js'
const parametersGetUserSchema = Type.Object({
userId: userPublicSchema.id

View File

@ -1,16 +1,18 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import prisma from '../../../../tools/database/prisma.js'
import { userExample } from '../../../../models/User.js'
import { application } from '#src/application.js'
import prisma from '#src/tools/database/prisma.js'
import { userExample } from '#src/models/User.js'
await tap.test('GET /users/confirm-email', async (t) => {
await test('GET /users/confirm-email', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
sinon.stub(prisma, 'user').value({
findFirst: async () => {
return userExample
@ -26,10 +28,10 @@ await tap.test('GET /users/confirm-email', async (t) => {
temporaryToken: userExample.temporaryToken ?? ''
}
})
t.equal(response.statusCode, 200)
assert.strictEqual(response.statusCode, 200)
})
await t.test('should fails with invalid `temporaryToken`', async (t) => {
await t.test('should fails with invalid `temporaryToken`', async () => {
sinon.stub(prisma, 'user').value({
findFirst: async () => {
return null
@ -45,6 +47,6 @@ await tap.test('GET /users/confirm-email', async (t) => {
temporaryToken: userExample.temporaryToken ?? ''
}
})
t.equal(response.statusCode, 403)
assert.strictEqual(response.statusCode, 403)
})
})

View File

@ -2,9 +2,9 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import { userSchema } from '../../../models/User.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import { userSchema } from '#src/models/User.js'
const queryGetConfirmEmailSchema = Type.Object({
redirectURI: Type.Optional(Type.String({ format: 'uri-reference' })),

View File

@ -1,15 +1,17 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
await tap.test('GET /users/current', async (t) => {
await test('GET /users/current', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken, user } = await authenticateUserTest()
const response = await application.inject({
method: 'GET',
@ -19,16 +21,16 @@ await tap.test('GET /users/current', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.user.name, user.name)
t.strictSame(responseJson.user.strategies, ['Local'])
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.user.name, user.name)
assert.deepStrictEqual(responseJson.user.strategies, ['Local'])
})
await t.test('fails with unauthenticated user', async (t) => {
await t.test('fails with unauthenticated user', async () => {
const response = await application.inject({
method: 'GET',
url: '/users/current'
})
t.equal(response.statusCode, 401)
assert.strictEqual(response.statusCode, 401)
})
})

View File

@ -1,16 +1,18 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import prisma from '../../../../tools/database/prisma.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import { application } from '#src/application.js'
import prisma from '#src/tools/database/prisma.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
await tap.test('PUT /users/current', async (t) => {
await test('PUT /users/current', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds with valid accessToken and valid name', async (t) => {
await t.test('succeeds with valid accessToken and valid name', async () => {
const newName = 'John Doe'
const { accessToken, user, userStubValue } = await authenticateUserTest()
sinon.stub(prisma, 'user').value({
@ -36,11 +38,11 @@ await tap.test('PUT /users/current', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.user.name, newName)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.user.name, newName)
})
await t.test('succeeds and only update the status', async (t) => {
await t.test('succeeds and only update the status', async () => {
const newStatus = '👀 Working on secret projects...'
const { accessToken, user, userStubValue } = await authenticateUserTest()
sinon.stub(prisma, 'user').value({
@ -66,12 +68,12 @@ await tap.test('PUT /users/current', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.user.name, user.name)
t.equal(responseJson.user.status, newStatus)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.user.name, user.name)
assert.strictEqual(responseJson.user.status, newStatus)
})
await t.test('fails with name already used', async (t) => {
await t.test('fails with name already used', async () => {
const newName = 'John Doe'
const { accessToken, user, userStubValue } = await authenticateUserTest()
sinon.stub(prisma, 'user').value({
@ -90,10 +92,10 @@ await tap.test('PUT /users/current', async (t) => {
name: newName
}
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
await t.test('fails with invalid website url', async (t) => {
await t.test('fails with invalid website url', async () => {
const newWebsite = 'invalid website url'
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
@ -106,10 +108,10 @@ await tap.test('PUT /users/current', async (t) => {
website: newWebsite
}
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
await t.test('succeeds with valid website url', async (t) => {
await t.test('succeeds with valid website url', async () => {
const newWebsite = 'https://somerandomwebsite.com'
const { accessToken, user, userStubValue } = await authenticateUserTest()
sinon.stub(prisma, 'user').value({
@ -135,8 +137,8 @@ await tap.test('PUT /users/current', async (t) => {
}
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.user.name, user.name)
t.equal(responseJson.user.website, newWebsite)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.user.name, user.name)
assert.strictEqual(responseJson.user.website, newWebsite)
})
})

View File

@ -1,9 +1,9 @@
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
import { userCurrentSchema } from '../../../models/User.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { userCurrentSchema } from '#src/models/User.js'
const getCurrentUserSchema: FastifySchema = {
description: 'GET the current connected user',

View File

@ -2,10 +2,10 @@ import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import fastifyMultipart from '@fastify/multipart'
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
import { fastifyErrors } from '../../../../models/utils.js'
import prisma from '../../../../tools/database/prisma.js'
import { uploadFile } from '../../../../tools/utils/uploadFile.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { fastifyErrors } from '#src/models/utils.js'
import prisma from '#src/tools/database/prisma.js'
import { uploadFile } from '#src/tools/utils/uploadFile.js'
const putServiceSchema: FastifySchema = {
description: 'Edit the current connected user logo',

View File

@ -4,14 +4,14 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
import { userCurrentSchema, userSchema } from '../../../models/User.js'
import { sendEmail } from '../../../tools/email/sendEmail.js'
import { API_URL } from '../../../tools/configurations.js'
import type { Language, Theme } from '../../../models/UserSettings.js'
import { parseStringNullish } from '../../../tools/utils/parseStringNullish.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { userCurrentSchema, userSchema } from '#src/models/User.js'
import { sendEmail } from '#src/tools/email/sendEmail.js'
import { API_URL } from '#src/tools/configurations.js'
import type { Language, Theme } from '#src/models/UserSettings.js'
import { parseStringNullish } from '#src/tools/utils/parseStringNullish.js'
const bodyPutServiceSchema = Type.Object({
name: Type.Optional(userSchema.name),

View File

@ -1,19 +1,21 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../../application.js'
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../../tools/database/prisma.js'
import { userSettingsExample } from '../../../../../models/UserSettings.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { userSettingsExample } from '#src/models/UserSettings.js'
await tap.test('PUT /users/current/settings', async (t) => {
await test('PUT /users/current/settings', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test(
'succeeds and edit the theme, language, isPublicEmail and isPublicGuilds',
async (t) => {
async () => {
const newSettings = {
theme: 'light',
language: 'fr',
@ -42,15 +44,21 @@ await tap.test('PUT /users/current/settings', async (t) => {
payload: newSettings
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.settings.theme, newSettings.theme)
t.equal(responseJson.settings.language, newSettings.language)
t.equal(responseJson.settings.isPublicEmail, newSettings.isPublicEmail)
t.equal(responseJson.settings.isPublicGuilds, newSettings.isPublicGuilds)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.settings.theme, newSettings.theme)
assert.strictEqual(responseJson.settings.language, newSettings.language)
assert.strictEqual(
responseJson.settings.isPublicEmail,
newSettings.isPublicEmail
)
assert.strictEqual(
responseJson.settings.isPublicGuilds,
newSettings.isPublicGuilds
)
}
)
await t.test('fails with invalid language', async (t) => {
await t.test('fails with invalid language', async () => {
const newSettings = {
language: 'somerandomlanguage'
}
@ -75,6 +83,6 @@ await tap.test('PUT /users/current/settings', async (t) => {
},
payload: newSettings
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
})

View File

@ -2,10 +2,10 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../../models/utils.js'
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
import { userSettingsSchema } from '../../../../models/UserSettings.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { userSettingsSchema } from '#src/models/UserSettings.js'
const bodyPutServiceSchema = Type.Object({
theme: Type.Optional(userSettingsSchema.theme),

View File

@ -2,10 +2,10 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../../models/utils.js'
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
import { oauthSchema } from '../../../../models/OAuth.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
import { oauthSchema } from '#src/models/OAuth.js'
const parametersSchema = Type.Object({
provider: oauthSchema.provider
@ -45,8 +45,8 @@ export const deleteProviderService: FastifyPluginAsync = async (fastify) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { user } = request
const { provider } = request.params
const { user, params } = request
const { provider } = params
const OAuths = await prisma.oAuth.findMany({
where: { userId: user.current.id }
})

View File

@ -2,7 +2,7 @@ import querystring from 'node:querystring'
import axios from 'axios'
import { OAuthStrategy } from '../../../../../tools/utils/OAuthStrategy.js'
import { OAuthStrategy } from '#src/tools/utils/OAuthStrategy.js'
export const DISCORD_PROVIDER = 'Discord'
export const DISCORD_BASE_URL = 'https://discord.com/api/v10'

View File

@ -2,10 +2,10 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { API_URL } from '#src/tools/configurations.js'
import { fastifyErrors } from '#src/models/utils.js'
import { DISCORD_BASE_URL, DISCORD_CLIENT_ID } from '../__utils__/utils.js'
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
const querySchema = Type.Object({
redirectURI: Type.String({ format: 'uri-reference' })

View File

@ -2,11 +2,11 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { API_URL } from '#src/tools/configurations.js'
import { fastifyErrors } from '#src/models/utils.js'
import { discordStrategy, getDiscordUserData } from '../__utils__/utils.js'
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
import { getUserWithBearerToken } from '../../../../../tools/plugins/authenticateUser.js'
import { buildQueryURL } from '#src/tools/utils/buildQueryURL.js'
import { getUserWithBearerToken } from '#src/tools/plugins/authenticateUser.js'
const querySchema = Type.Object({
code: Type.String(),

View File

@ -2,10 +2,10 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { API_URL } from '#src/tools/configurations.js'
import { fastifyErrors } from '#src/models/utils.js'
import { discordStrategy, getDiscordUserData } from '../__utils__/utils.js'
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
import { buildQueryURL } from '#src/tools/utils/buildQueryURL.js'
const querySchema = Type.Object({
code: Type.String(),

View File

@ -2,8 +2,8 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { API_URL } from '#src/tools/configurations.js'
import { fastifyErrors } from '#src/models/utils.js'
import { DISCORD_BASE_URL, DISCORD_CLIENT_ID } from '../__utils__/utils.js'
const querySchema = Type.Object({

View File

@ -2,7 +2,7 @@ import querystring from 'node:querystring'
import axios from 'axios'
import { OAuthStrategy } from '../../../../../tools/utils/OAuthStrategy.js'
import { OAuthStrategy } from '#src/tools/utils/OAuthStrategy.js'
export const GITHUB_PROVIDER = 'GitHub'
export const GITHUB_BASE_URL = 'https://github.com'

View File

@ -2,10 +2,10 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { API_URL } from '#src/tools/configurations.js'
import { fastifyErrors } from '#src/models/utils.js'
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from '../__utils__/utils.js'
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
const querySchema = Type.Object({
redirectURI: Type.String({ format: 'uri-reference' })

View File

@ -2,11 +2,11 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { API_URL } from '#src/tools/configurations.js'
import { fastifyErrors } from '#src/models/utils.js'
import { githubStrategy, getGitHubUserData } from '../__utils__/utils.js'
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
import { getUserWithBearerToken } from '../../../../../tools/plugins/authenticateUser.js'
import { buildQueryURL } from '#src/tools/utils/buildQueryURL.js'
import { getUserWithBearerToken } from '#src/tools/plugins/authenticateUser.js'
const querySchema = Type.Object({
code: Type.String(),

View File

@ -2,10 +2,10 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { API_URL } from '#src/tools/configurations.js'
import { fastifyErrors } from '#src/models/utils.js'
import { githubStrategy, getGitHubUserData } from '../__utils__/utils.js'
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
import { buildQueryURL } from '#src/tools/utils/buildQueryURL.js'
const querySchema = Type.Object({
code: Type.String(),

View File

@ -2,8 +2,8 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { API_URL } from '#src/tools/configurations.js'
import { fastifyErrors } from '#src/models/utils.js'
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from '../__utils__/utils.js'
const querySchema = Type.Object({

View File

@ -2,7 +2,7 @@ import querystring from 'node:querystring'
import axios from 'axios'
import { OAuthStrategy } from '../../../../../tools/utils/OAuthStrategy.js'
import { OAuthStrategy } from '#src/tools/utils/OAuthStrategy.js'
export const GOOGLE_PROVIDER = 'Google'
export const GOOGLE_BASE_URL = 'https://accounts.google.com/o/oauth2/v2/auth'

View File

@ -2,10 +2,10 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { API_URL } from '#src/tools/configurations.js'
import { fastifyErrors } from '#src/models/utils.js'
import { GOOGLE_BASE_URL, GOOGLE_CLIENT_ID } from '../__utils__/utils.js'
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
const querySchema = Type.Object({
redirectURI: Type.String({ format: 'uri-reference' })

View File

@ -2,11 +2,11 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { API_URL } from '#src/tools/configurations.js'
import { fastifyErrors } from '#src/models/utils.js'
import { googleStrategy, getGoogleUserData } from '../__utils__/utils.js'
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
import { getUserWithBearerToken } from '../../../../../tools/plugins/authenticateUser.js'
import { buildQueryURL } from '#src/tools/utils/buildQueryURL.js'
import { getUserWithBearerToken } from '#src/tools/plugins/authenticateUser.js'
const querySchema = Type.Object({
code: Type.String(),

View File

@ -2,10 +2,10 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { API_URL } from '#src/tools/configurations.js'
import { fastifyErrors } from '#src/models/utils.js'
import { googleStrategy, getGoogleUserData } from '../__utils__/utils.js'
import { buildQueryURL } from '../../../../../tools/utils/buildQueryURL.js'
import { buildQueryURL } from '#src/tools/utils/buildQueryURL.js'
const querySchema = Type.Object({
code: Type.String(),

View File

@ -2,8 +2,8 @@ import type { Static } from '@sinclair/typebox'
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import { API_URL } from '../../../../../tools/configurations.js'
import { fastifyErrors } from '../../../../../models/utils.js'
import { API_URL } from '#src/tools/configurations.js'
import { fastifyErrors } from '#src/models/utils.js'
import { GOOGLE_BASE_URL, GOOGLE_CLIENT_ID } from '../__utils__/utils.js'
const querySchema = Type.Object({

View File

@ -1,19 +1,21 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import jwt from 'jsonwebtoken'
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../tools/database/prisma.js'
import { refreshTokenExample } from '../../../../models/RefreshToken.js'
import { expiresIn } from '../../../../tools/utils/jwtToken.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
import { refreshTokenExample } from '#src/models/RefreshToken.js'
import { expiresIn } from '#src/tools/utils/jwtToken.js'
await tap.test('POST /users/refresh-token', async (t) => {
await test('POST /users/refresh-token', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { refreshToken, refreshTokenStubValue } = await authenticateUserTest()
sinon.stub(prisma, 'refreshToken').value({
...refreshTokenStubValue,
@ -31,13 +33,13 @@ await tap.test('POST /users/refresh-token', async (t) => {
payload: { refreshToken }
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.type, 'Bearer')
t.equal(responseJson.expiresIn, expiresIn)
t.type(responseJson.accessToken, 'string')
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.type, 'Bearer')
assert.strictEqual(responseJson.expiresIn, expiresIn)
assert.strictEqual(typeof responseJson.accessToken, 'string')
})
await t.test('fails with refreshToken not saved in database', async (t) => {
await t.test('fails with refreshToken not saved in database', async () => {
sinon.stub(prisma, 'refreshToken').value({
findFirst: async () => {
return null
@ -48,10 +50,10 @@ await tap.test('POST /users/refresh-token', async (t) => {
url: '/users/refresh-token',
payload: { refreshToken: 'somerandomtoken' }
})
t.equal(response.statusCode, 403)
assert.strictEqual(response.statusCode, 403)
})
await t.test('fails with invalid jwt refreshToken', async (t) => {
await t.test('fails with invalid jwt refreshToken', async () => {
const { refreshToken, refreshTokenStubValue } = await authenticateUserTest()
sinon.stub(prisma, 'refreshToken').value({
...refreshTokenStubValue,
@ -67,6 +69,6 @@ await tap.test('POST /users/refresh-token', async (t) => {
url: '/users/refresh-token',
payload: { refreshToken }
})
t.equal(response.statusCode, 403)
assert.strictEqual(response.statusCode, 403)
})
})

View File

@ -3,15 +3,15 @@ import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import jwt from 'jsonwebtoken'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import {
generateAccessToken,
jwtSchema,
expiresIn
} from '../../../tools/utils/jwtToken.js'
import type { UserRefreshJWT } from '../../../models/User.js'
import { JWT_REFRESH_SECRET } from '../../../tools/configurations.js'
} from '#src/tools/utils/jwtToken.js'
import type { UserRefreshJWT } from '#src/models/User.js'
import { JWT_REFRESH_SECRET } from '#src/tools/configurations.js'
const bodyPostRefreshTokenSchema = Type.Object({
refreshToken: jwtSchema.refreshToken

View File

@ -1,19 +1,21 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import ms from 'ms'
import { application } from '../../../../application.js'
import prisma from '../../../../tools/database/prisma.js'
import { userExample } from '../../../../models/User.js'
import { userSettingsExample } from '../../../../models/UserSettings.js'
import { emailTransporter } from '../../../../tools/email/emailTransporter.js'
import { application } from '#src/application.js'
import prisma from '#src/tools/database/prisma.js'
import { userExample } from '#src/models/User.js'
import { userSettingsExample } from '#src/models/UserSettings.js'
import { emailTransporter } from '#src/tools/email/emailTransporter.js'
await tap.test('POST /users/reset-password', async (t) => {
await test('POST /users/reset-password', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
sinon.stub(prisma, 'user').value({
findUnique: async () => {
return userExample
@ -37,10 +39,10 @@ await tap.test('POST /users/reset-password', async (t) => {
url: '/users/reset-password?redirectURI=https://redirecturi.com',
payload: { email: userExample.email }
})
t.equal(response.statusCode, 200)
assert.strictEqual(response.statusCode, 200)
})
await t.test("fails with email that doesn't exist", async (t) => {
await t.test("fails with email that doesn't exist", async () => {
sinon.stub(prisma, 'user').value({
findUnique: async () => {
return null
@ -51,10 +53,10 @@ await tap.test('POST /users/reset-password', async (t) => {
url: '/users/reset-password?redirectURI=https://redirecturi.com',
payload: { email: userExample.email }
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
await t.test('fails with unconfirmed account', async (t) => {
await t.test('fails with unconfirmed account', async () => {
sinon.stub(prisma, 'user').value({
findUnique: async () => {
return {
@ -68,10 +70,10 @@ await tap.test('POST /users/reset-password', async (t) => {
url: '/users/reset-password?redirectURI=https://redirecturi.com',
payload: { email: userExample.email }
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
await t.test("fails if userSettings doesn't exist", async (t) => {
await t.test("fails if userSettings doesn't exist", async () => {
sinon.stub(prisma, 'user').value({
findUnique: async () => {
return userExample
@ -87,10 +89,10 @@ await tap.test('POST /users/reset-password', async (t) => {
url: '/users/reset-password?redirectURI=https://redirecturi.com',
payload: { email: userExample.email }
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
await t.test('fails with a request already in progress', async (t) => {
await t.test('fails with a request already in progress', async () => {
sinon.stub(prisma, 'user').value({
findUnique: async () => {
return {
@ -110,6 +112,6 @@ await tap.test('POST /users/reset-password', async (t) => {
url: '/users/reset-password?redirectURI=https://redirecturi.com',
payload: { email: userExample.email }
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
})

View File

@ -1,17 +1,19 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import ms from 'ms'
import { application } from '../../../../application.js'
import prisma from '../../../../tools/database/prisma.js'
import { userExample } from '../../../../models/User.js'
import { application } from '#src/application.js'
import prisma from '#src/tools/database/prisma.js'
import { userExample } from '#src/models/User.js'
await tap.test('PUT /users/reset-password', async (t) => {
await test('PUT /users/reset-password', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const temporaryToken = 'random-token'
sinon.stub(prisma, 'user').value({
findFirst: async () => {
@ -38,10 +40,10 @@ await tap.test('PUT /users/reset-password', async (t) => {
temporaryToken: userExample.temporaryToken
}
})
t.equal(response.statusCode, 200)
assert.strictEqual(response.statusCode, 200)
})
await t.test('fails with expired temporaryToken', async (t) => {
await t.test('fails with expired temporaryToken', async () => {
const temporaryToken = 'random-token'
sinon.stub(prisma, 'user').value({
findFirst: async () => {
@ -63,6 +65,6 @@ await tap.test('PUT /users/reset-password', async (t) => {
temporaryToken: userExample.temporaryToken
}
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
})

View File

@ -5,11 +5,11 @@ import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import ms from 'ms'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import { userSchema } from '../../../models/User.js'
import { sendEmail } from '../../../tools/email/sendEmail.js'
import type { Language, Theme } from '../../../models/UserSettings.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import { userSchema } from '#src/models/User.js'
import { sendEmail } from '#src/tools/email/sendEmail.js'
import type { Language, Theme } from '#src/models/UserSettings.js'
const queryPostResetPasswordSchema = Type.Object({
redirectURI: Type.String({ format: 'uri-reference' })

View File

@ -3,9 +3,9 @@ import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import bcrypt from 'bcryptjs'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import { userSchema } from '../../../models/User.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import { userSchema } from '#src/models/User.js'
const bodyPutResetPasswordSchema = Type.Object({
password: userSchema.password,

View File

@ -1,24 +1,26 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import bcrypt from 'bcryptjs'
import { application } from '../../../../application.js'
import prisma from '../../../../tools/database/prisma.js'
import { userExample } from '../../../../models/User.js'
import { refreshTokenExample } from '../../../../models/RefreshToken.js'
import { expiresIn } from '../../../../tools/utils/jwtToken.js'
import { application } from '#src/application.js'
import prisma from '#src/tools/database/prisma.js'
import { userExample } from '#src/models/User.js'
import { refreshTokenExample } from '#src/models/RefreshToken.js'
import { expiresIn } from '#src/tools/utils/jwtToken.js'
const payload = {
email: userExample.email,
password: userExample.password
}
await tap.test('POST /users/signin', async (t) => {
await test('POST /users/signin', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
sinon.stub(prisma, 'user').value({
findUnique: async () => {
return {
@ -38,12 +40,12 @@ await tap.test('POST /users/signin', async (t) => {
payload
})
const responseJson = response.json()
t.equal(response.statusCode, 200)
t.equal(responseJson.type, 'Bearer')
t.equal(responseJson.expiresIn, expiresIn)
assert.strictEqual(response.statusCode, 200)
assert.strictEqual(responseJson.type, 'Bearer')
assert.strictEqual(responseJson.expiresIn, expiresIn)
})
await t.test('fails with invalid user', async (t) => {
await t.test('fails with invalid user', async () => {
sinon.stub(prisma, 'user').value({
findUnique: () => {
return null
@ -54,10 +56,10 @@ await tap.test('POST /users/signin', async (t) => {
url: '/users/signin',
payload
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
await t.test('fails with invalid email', async (t) => {
await t.test('fails with invalid email', async () => {
sinon.stub(prisma, 'user').value({
findUnique: () => {
return null
@ -71,10 +73,10 @@ await tap.test('POST /users/signin', async (t) => {
email: 'incorrect-email'
}
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
await t.test("fails if user hasn't got a password", async (t) => {
await t.test("fails if user hasn't got a password", async () => {
sinon.stub(prisma, 'user').value({
findUnique: () => {
return {
@ -88,10 +90,10 @@ await tap.test('POST /users/signin', async (t) => {
url: '/users/signin',
payload
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
await t.test('fails with incorrect password', async (t) => {
await t.test('fails with incorrect password', async () => {
sinon.stub(prisma, 'user').value({
findUnique: async () => {
return userExample
@ -105,6 +107,6 @@ await tap.test('POST /users/signin', async (t) => {
password: 'incorrect-password'
}
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
})

View File

@ -3,15 +3,15 @@ import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import bcrypt from 'bcryptjs'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import { userSchema } from '../../../models/User.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import { userSchema } from '#src/models/User.js'
import {
generateAccessToken,
generateRefreshToken,
jwtSchema,
expiresIn
} from '../../../tools/utils/jwtToken.js'
} from '#src/tools/utils/jwtToken.js'
const bodyPostSigninSchema = Type.Object({
email: userSchema.email,

View File

@ -1,16 +1,18 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import prisma from '../../../../tools/database/prisma.js'
import { application } from '#src/application.js'
import { authenticateUserTest } from '#src/__test__/utils/authenticateUserTest.js'
import prisma from '#src/tools/database/prisma.js'
await tap.test('DELETE /users/signout', async (t) => {
await test('DELETE /users/signout', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
const { accessToken, refreshTokenStubValue } = await authenticateUserTest()
sinon.stub(prisma, 'refreshToken').value({
...refreshTokenStubValue,
@ -25,14 +27,14 @@ await tap.test('DELETE /users/signout', async (t) => {
authorization: `Bearer ${accessToken}`
}
})
t.equal(response.statusCode, 200)
assert.strictEqual(response.statusCode, 200)
})
await t.test('fails with empty authorized header', async (t) => {
await t.test('fails with empty authorized header', async () => {
const response = await application.inject({
method: 'DELETE',
url: '/users/signout'
})
t.equal(response.statusCode, 401)
assert.strictEqual(response.statusCode, 401)
})
})

View File

@ -1,18 +1,20 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import jwt from 'jsonwebtoken'
import { application } from '../../../../application.js'
import prisma from '../../../../tools/database/prisma.js'
import { refreshTokenExample } from '../../../../models/RefreshToken.js'
import type { UserRefreshJWT } from '../../../../models/User.js'
import { application } from '#src/application.js'
import prisma from '#src/tools/database/prisma.js'
import { refreshTokenExample } from '#src/models/RefreshToken.js'
import type { UserRefreshJWT } from '#src/models/User.js'
await tap.test('POST /users/signout', async (t) => {
await test('POST /users/signout', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
sinon.stub(prisma, 'refreshToken').value({
findFirst: async () => {
return refreshTokenExample
@ -32,10 +34,10 @@ await tap.test('POST /users/signout', async (t) => {
url: '/users/signout',
payload: { refreshToken: 'jwt token' }
})
t.equal(response.statusCode, 200)
assert.strictEqual(response.statusCode, 200)
})
await t.test('fails with invalid refreshToken', async (t) => {
await t.test('fails with invalid refreshToken', async () => {
sinon.stub(prisma, 'refreshToken').value({
findFirst: async () => {
return null
@ -46,6 +48,6 @@ await tap.test('POST /users/signout', async (t) => {
url: '/users/signout',
payload: { refreshToken: 'somerandomtoken' }
})
t.equal(response.statusCode, 404)
assert.strictEqual(response.statusCode, 404)
})
})

View File

@ -1,9 +1,9 @@
import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import authenticateUser from '../../../tools/plugins/authenticateUser.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
const deleteSignoutSchema: FastifySchema = {
description: 'Signout the user to every connected devices',

View File

@ -3,11 +3,11 @@ import { Type } from '@sinclair/typebox'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import jwt from 'jsonwebtoken'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import { JWT_REFRESH_SECRET } from '../../../tools/configurations.js'
import type { UserRefreshJWT } from '../../../models/User.js'
import { jwtSchema } from '../../../tools/utils/jwtToken.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import { JWT_REFRESH_SECRET } from '#src/tools/configurations.js'
import type { UserRefreshJWT } from '#src/models/User.js'
import { jwtSchema } from '#src/tools/utils/jwtToken.js'
const bodyPostSignoutSchema = Type.Object({
refreshToken: jwtSchema.refreshToken

View File

@ -1,11 +1,13 @@
import tap from 'tap'
import test from 'node:test'
import assert from 'node:assert/strict'
import sinon from 'sinon'
import { application } from '../../../../application.js'
import prisma from '../../../../tools/database/prisma.js'
import { userExample } from '../../../../models/User.js'
import { userSettingsExample } from '../../../../models/UserSettings.js'
import { emailTransporter } from '../../../../tools/email/emailTransporter.js'
import { application } from '#src/application.js'
import prisma from '#src/tools/database/prisma.js'
import { userExample } from '#src/models/User.js'
import { userSettingsExample } from '#src/models/UserSettings.js'
import { emailTransporter } from '#src/tools/email/emailTransporter.js'
const payload = {
name: userExample.name,
@ -15,12 +17,12 @@ const payload = {
language: userSettingsExample.language
}
await tap.test('POST /users/signup', async (t) => {
await test('POST /users/signup', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
await t.test('succeeds', async () => {
sinon.stub(prisma, 'user').value({
findFirst: async () => {
return null
@ -41,12 +43,12 @@ await tap.test('POST /users/signup', async (t) => {
payload
})
const responseJson = response.json()
t.equal(response.statusCode, 201)
t.equal(responseJson.user.name, userExample.name)
t.equal(responseJson.user.email, userExample.email)
assert.strictEqual(response.statusCode, 201)
assert.strictEqual(responseJson.user.name, userExample.name)
assert.strictEqual(responseJson.user.email, userExample.email)
})
await t.test('fails with invalid email', async (t) => {
await t.test('fails with invalid email', async () => {
sinon.stub(prisma, 'user').value({
findFirst: async () => {
return null
@ -61,10 +63,10 @@ await tap.test('POST /users/signup', async (t) => {
email: 'incorrect-email@abc'
}
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
await t.test('fails with already taken `name` or `email`', async (t) => {
await t.test('fails with already taken `name` or `email`', async () => {
sinon.stub(prisma, 'user').value({
findFirst: async () => {
return userExample
@ -76,6 +78,6 @@ await tap.test('POST /users/signup', async (t) => {
url: '/users/signup',
payload
})
t.equal(response.statusCode, 400)
assert.strictEqual(response.statusCode, 400)
})
})

View File

@ -5,12 +5,12 @@ import { Type } from '@sinclair/typebox'
import bcrypt from 'bcryptjs'
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
import prisma from '../../../tools/database/prisma.js'
import { fastifyErrors } from '../../../models/utils.js'
import type { BodyUserSchemaType } from '../../../models/User.js'
import { bodyUserSchema, userPublicSchema } from '../../../models/User.js'
import { sendEmail } from '../../../tools/email/sendEmail.js'
import { API_URL } from '../../../tools/configurations.js'
import prisma from '#src/tools/database/prisma.js'
import { fastifyErrors } from '#src/models/utils.js'
import type { BodyUserSchemaType } from '#src/models/User.js'
import { bodyUserSchema, userPublicSchema } from '#src/models/User.js'
import { sendEmail } from '#src/tools/email/sendEmail.js'
import { API_URL } from '#src/tools/configurations.js'
const queryPostSignupSchema = Type.Object({
redirectURI: Type.Optional(Type.String({ format: 'uri-reference' }))

View File

@ -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 =

View File

@ -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'],

View File

@ -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

View File

@ -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)
}
)
})

View File

@ -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()
})
})

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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',

View File

@ -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'

Some files were not shown because too many files have changed in this diff Show More