2
1
mirror of https://github.com/Thream/api.git synced 2024-07-21 03:38:31 +02:00

feat(services): add GET /channels/[channelId]

This commit is contained in:
Divlo 2021-12-28 21:53:44 +00:00
parent 2871c4c9e0
commit 567b5aa2d8
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
5 changed files with 141 additions and 2 deletions

View File

@ -0,0 +1,64 @@
import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
import { prismaMock } from '../../../../__test__/setup.js'
import { memberExample } from '../../../../models/Member'
import { channelExample } from '../../../../models/Channel.js'
describe('GET /channels/[channelId]', () => {
it('succeeds', async () => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(memberExample)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'GET',
url: `/channels/${channelExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
}
})
const responseJson = response.json()
expect(response.statusCode).toEqual(200)
expect(responseJson.channel.id).toEqual(channelExample.id)
expect(responseJson.channel.name).toEqual(channelExample.name)
expect(responseJson.channel.guildId).toEqual(channelExample.guildId)
})
it('fails with not found member', async () => {
prismaMock.channel.findUnique.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'GET',
url: '/channels/1',
headers: {
authorization: `Bearer ${accessToken}`
}
})
const responseJson = response.json()
expect(response.statusCode).toEqual(404)
expect(responseJson.message).toEqual('Channel not found')
})
it('fails with not found member', async () => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({
method: 'GET',
url: `/channels/${channelExample.id}`,
headers: {
authorization: `Bearer ${accessToken}`
}
})
const responseJson = response.json()
expect(response.statusCode).toEqual(404)
expect(responseJson.message).toEqual('Channel not found')
})
it('fails with unauthenticated user', async () => {
const response = await application.inject({
method: 'GET',
url: '/channels/1'
})
expect(response.statusCode).toEqual(401)
})
})

View File

@ -0,0 +1,66 @@
import { Static, Type } from '@sinclair/typebox'
import { 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'
const parametersSchema = Type.Object({
channelId: channelSchema.id
})
type Parameters = Static<typeof parametersSchema>
const getServiceSchema: FastifySchema = {
description: 'GET a channel with its id.',
tags: ['channels'] as string[],
security: [
{
bearerAuth: []
}
] as Array<{ [key: string]: [] }>,
params: parametersSchema,
response: {
200: Type.Object({
channel: Type.Object(channelSchema)
}),
400: fastifyErrors[400],
401: fastifyErrors[401],
403: fastifyErrors[403],
404: fastifyErrors[404],
500: fastifyErrors[500]
}
} as const
export const getChannelByIdService: FastifyPluginAsync = async (fastify) => {
await fastify.register(authenticateUser)
fastify.route<{
Params: Parameters
}>({
method: 'GET',
url: '/channels/:channelId',
schema: getServiceSchema,
handler: async (request, reply) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
const { channelId } = request.params
const channel = await prisma.channel.findUnique({
where: { id: channelId }
})
if (channel == null) {
throw fastify.httpErrors.notFound('Channel not found')
}
const member = await prisma.member.findFirst({
where: { guildId: channel.guildId, userId: request.user.current.id }
})
if (member == null) {
throw fastify.httpErrors.notFound('Channel not found')
}
reply.statusCode = 200
return { channel }
}
})
}

View File

@ -0,0 +1,7 @@
import { FastifyPluginAsync } from 'fastify'
import { getChannelByIdService } from './[channelId]/get'
export const channelsService: FastifyPluginAsync = async (fastify) => {
await fastify.register(getChannelByIdService)
}

View File

@ -26,8 +26,8 @@ describe('GET /guilds/[guildId]/channels', () => {
}) })
it('fails with not found guild', async () => { it('fails with not found guild', async () => {
const { accessToken } = await authenticateUserTest()
prismaMock.member.findFirst.mockResolvedValue(null) prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest()
const response = await application.inject({ const response = await application.inject({
method: 'GET', method: 'GET',
url: '/guilds/1/channels', url: '/guilds/1/channels',

View File

@ -3,9 +3,11 @@ import { FastifyPluginAsync } from 'fastify'
import { usersService } from './users/index.js' import { usersService } from './users/index.js'
import { guildsService } from './guilds/index.js' import { guildsService } from './guilds/index.js'
import { uploadsService } from './uploads/index.js' import { uploadsService } from './uploads/index.js'
import { channelsService } from './channels/index.js'
export const services: FastifyPluginAsync = async (fastify) => { export const services: FastifyPluginAsync = async (fastify) => {
await fastify.register(usersService) await fastify.register(channelsService)
await fastify.register(guildsService) await fastify.register(guildsService)
await fastify.register(uploadsService) await fastify.register(uploadsService)
await fastify.register(usersService)
} }