feat: usage of ESM modules imports (instead of CommonJS) (#5)
Replace `jest` with `tap`.
This commit is contained in:
@ -1,21 +1,42 @@
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
|
||||
import { application } from '../../../../application.js'
|
||||
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||
import { prismaMock } from '../../../../__test__/setup.js'
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { channelExample } from '../../../../models/Channel.js'
|
||||
import { memberExample } from '../../../../models/Member.js'
|
||||
|
||||
describe('DELETE /channels/[channelId]', () => {
|
||||
it('succeeds', async () => {
|
||||
await tap.test('DELETE /channels/[channelId]', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const defaultChannelId = 5
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||
prismaMock.channel.count.mockResolvedValue(2)
|
||||
prismaMock.channel.delete.mockResolvedValue(channelExample)
|
||||
prismaMock.channel.findFirst.mockResolvedValue({
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
})
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
},
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
}
|
||||
},
|
||||
count: async () => {
|
||||
return 2
|
||||
},
|
||||
delete: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return memberExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/channels/${channelExample.id}`,
|
||||
@ -24,18 +45,28 @@ describe('DELETE /channels/[channelId]', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.id).toEqual(channelExample.id)
|
||||
expect(responseJson.name).toEqual(channelExample.name)
|
||||
expect(responseJson.guildId).toEqual(channelExample.guildId)
|
||||
expect(responseJson.defaultChannelId).toEqual(defaultChannelId)
|
||||
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)
|
||||
})
|
||||
|
||||
it('fails if there is only one channel', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||
prismaMock.channel.count.mockResolvedValue(1)
|
||||
await t.test('fails if there is only one channel', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
},
|
||||
count: async () => {
|
||||
return 1
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return memberExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/channels/${channelExample.id}`,
|
||||
@ -43,12 +74,16 @@ describe('DELETE /channels/[channelId]', () => {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
it('fails if the channel is not found', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(null)
|
||||
await t.test('fails if the channel is not found', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/channels/${channelExample.id}`,
|
||||
@ -56,13 +91,21 @@ describe('DELETE /channels/[channelId]', () => {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
t.equal(response.statusCode, 404)
|
||||
})
|
||||
|
||||
it('fails if the member is not found', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails if the member is not found', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/channels/${channelExample.id}`,
|
||||
@ -70,17 +113,24 @@ describe('DELETE /channels/[channelId]', () => {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
t.equal(response.statusCode, 404)
|
||||
})
|
||||
|
||||
it('fails if the member is not owner', async () => {
|
||||
const member = {
|
||||
...memberExample,
|
||||
isOwner: false
|
||||
}
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue(member)
|
||||
await t.test('fails if the member is not owner', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
isOwner: false
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/channels/${channelExample.id}`,
|
||||
@ -88,6 +138,6 @@ describe('DELETE /channels/[channelId]', () => {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
})
|
||||
|
@ -1,14 +1,29 @@
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
|
||||
import { application } from '../../../../application.js'
|
||||
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||
import { prismaMock } from '../../../../__test__/setup.js'
|
||||
import { memberExample } from '../../../../models/Member.js'
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { channelExample } from '../../../../models/Channel.js'
|
||||
import { memberExample } from '../../../../models/Member.js'
|
||||
|
||||
describe('GET /channels/[channelId]', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||
await tap.test('GET /channels/[channelId]', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return memberExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: `/channels/${channelExample.id}`,
|
||||
@ -17,31 +32,24 @@ describe('GET /channels/[channelId]', () => {
|
||||
}
|
||||
})
|
||||
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)
|
||||
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)
|
||||
})
|
||||
|
||||
it('fails with not found member', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(null)
|
||||
await t.test('fails with not found member', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/channels/1',
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
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}`,
|
||||
@ -50,15 +58,39 @@ describe('GET /channels/[channelId]', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(404)
|
||||
expect(responseJson.message).toEqual('Channel not found')
|
||||
t.equal(response.statusCode, 404)
|
||||
t.equal(responseJson.message, 'Channel not found')
|
||||
})
|
||||
|
||||
it('fails with unauthenticated user', async () => {
|
||||
await t.test('fails with not found channel', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return memberExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: `/channels/${channelExample.id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
t.equal(response.statusCode, 404)
|
||||
t.equal(responseJson.message, 'Channel not found')
|
||||
})
|
||||
|
||||
await t.test('fails with unauthenticated user', async (t) => {
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/channels/1'
|
||||
})
|
||||
expect(response.statusCode).toEqual(401)
|
||||
t.equal(response.statusCode, 401)
|
||||
})
|
||||
})
|
||||
|
@ -1,81 +1,129 @@
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
|
||||
import { application } from '../../../../application.js'
|
||||
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||
import { prismaMock } from '../../../../__test__/setup.js'
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { channelExample } from '../../../../models/Channel.js'
|
||||
import { memberExample } from '../../../../models/Member.js'
|
||||
|
||||
describe('PUT /channels/[channelId]', () => {
|
||||
it('succeeds', async () => {
|
||||
const newName = 'new channel name'
|
||||
|
||||
await tap.test('PUT /channels/[channelId]', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const defaultChannelId = 5
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||
prismaMock.channel.update.mockResolvedValue(channelExample)
|
||||
prismaMock.channel.findFirst.mockResolvedValue({
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
})
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
},
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
}
|
||||
},
|
||||
update: async () => {
|
||||
return {
|
||||
...channelExample,
|
||||
name: newName
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return memberExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: `/channels/${channelExample.id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: { name: channelExample.name }
|
||||
payload: { name: newName }
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.id).toEqual(channelExample.id)
|
||||
expect(responseJson.name).toEqual(channelExample.name)
|
||||
expect(responseJson.guildId).toEqual(channelExample.guildId)
|
||||
expect(responseJson.defaultChannelId).toEqual(defaultChannelId)
|
||||
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)
|
||||
})
|
||||
|
||||
it('fails if the channel is not found', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(null)
|
||||
await t.test('fails if the channel is not found', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return memberExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: `/channels/${channelExample.id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: { name: channelExample.name }
|
||||
payload: { name: newName }
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
t.equal(response.statusCode, 404)
|
||||
})
|
||||
|
||||
it('fails if the member is not found', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails if the member is not found', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: `/channels/${channelExample.id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: { name: channelExample.name }
|
||||
payload: { name: newName }
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
t.equal(response.statusCode, 404)
|
||||
})
|
||||
|
||||
it('fails if the member is not owner', async () => {
|
||||
const member = {
|
||||
...memberExample,
|
||||
isOwner: false
|
||||
}
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue(member)
|
||||
await t.test('fails if the member is not owner', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
isOwner: false
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: `/channels/${channelExample.id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: { name: channelExample.name }
|
||||
payload: { name: newName }
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
})
|
||||
|
@ -1,20 +1,39 @@
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
|
||||
import { application } from '../../../../../application.js'
|
||||
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.js'
|
||||
import { prismaMock } from '../../../../../__test__/setup.js'
|
||||
import prisma from '../../../../../tools/database/prisma.js'
|
||||
import { channelExample } from '../../../../../models/Channel.js'
|
||||
import { userExample } from '../../../../../models/User.js'
|
||||
import { memberExample } from '../../../../../models/Member.js'
|
||||
import { userExample } from '../../../../../models/User.js'
|
||||
import { messageExample } from '../../../../../models/Message.js'
|
||||
|
||||
describe('GET /channels/[channelId]/messages', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
user: userExample
|
||||
} as any)
|
||||
prismaMock.message.findMany.mockResolvedValue([messageExample])
|
||||
await tap.test('GET /channels/[channelId]/messages', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
user: userExample
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'message').value({
|
||||
findMany: async () => {
|
||||
return [messageExample]
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: `/channels/${channelExample.id}/messages`,
|
||||
@ -23,21 +42,33 @@ describe('GET /channels/[channelId]/messages', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.length).toEqual(1)
|
||||
expect(responseJson[0].id).toEqual(messageExample.id)
|
||||
expect(responseJson[0].value).toEqual(messageExample.value)
|
||||
expect(responseJson[0].type).toEqual(messageExample.type)
|
||||
expect(responseJson[0].mimetype).toEqual(messageExample.mimetype)
|
||||
expect(responseJson[0].member.id).toEqual(memberExample.id)
|
||||
expect(responseJson[0].member.isOwner).toEqual(memberExample.isOwner)
|
||||
expect(responseJson[0].member.user.id).toEqual(userExample.id)
|
||||
expect(responseJson[0].member.user.name).toEqual(userExample.name)
|
||||
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)
|
||||
})
|
||||
|
||||
it('fails with not found channel', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(null)
|
||||
await t.test('fails with not found channel', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
user: userExample
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: `/channels/${channelExample.id}/messages`,
|
||||
@ -46,14 +77,22 @@ describe('GET /channels/[channelId]/messages', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(404)
|
||||
expect(responseJson.message).toEqual('Channel not found')
|
||||
t.equal(response.statusCode, 404)
|
||||
t.equal(responseJson.message, 'Channel not found')
|
||||
})
|
||||
|
||||
it('fails with not found member', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails with not found member', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: `/channels/${channelExample.id}/messages`,
|
||||
@ -62,15 +101,15 @@ describe('GET /channels/[channelId]/messages', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(404)
|
||||
expect(responseJson.message).toEqual('Channel not found')
|
||||
t.equal(response.statusCode, 404)
|
||||
t.equal(responseJson.message, 'Channel not found')
|
||||
})
|
||||
|
||||
it('fails with unauthenticated user', async () => {
|
||||
await t.test('fails with unauthenticated user', async (t) => {
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/channels/1/messages'
|
||||
url: `/channels/1/messages`
|
||||
})
|
||||
expect(response.statusCode).toEqual(401)
|
||||
t.equal(response.statusCode, 401)
|
||||
})
|
||||
})
|
||||
|
@ -1,49 +1,74 @@
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
|
||||
import { application } from '../../../../../application.js'
|
||||
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.js'
|
||||
import { prismaMock } from '../../../../../__test__/setup.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'
|
||||
|
||||
describe('POST /channels/[channelId]/messages', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
user: userExample
|
||||
} as any)
|
||||
prismaMock.message.create.mockResolvedValue(messageExample)
|
||||
await tap.test('POST /channels/[channelId]/messages', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
user: userExample
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'message').value({
|
||||
create: async () => {
|
||||
return messageExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/channels/${channelExample.id}/messages`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: {
|
||||
value: messageExample.value
|
||||
}
|
||||
payload: { value: messageExample.value }
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(201)
|
||||
expect(responseJson.id).toEqual(messageExample.id)
|
||||
expect(responseJson.value).toEqual(messageExample.value)
|
||||
expect(responseJson.type).toEqual(messageExample.type)
|
||||
expect(responseJson.mimetype).toEqual(messageExample.mimetype)
|
||||
expect(responseJson.member.id).toEqual(memberExample.id)
|
||||
expect(responseJson.member.isOwner).toEqual(memberExample.isOwner)
|
||||
expect(responseJson.member.user.id).toEqual(userExample.id)
|
||||
expect(responseJson.member.user.name).toEqual(userExample.name)
|
||||
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)
|
||||
})
|
||||
|
||||
it('fails with no message value', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
user: userExample
|
||||
} as any)
|
||||
await t.test('fails with no message value', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
user: userExample
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/channels/${channelExample.id}/messages`,
|
||||
@ -52,43 +77,59 @@ describe('POST /channels/[channelId]/messages', () => {
|
||||
},
|
||||
payload: {}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
it('fails with not found channel', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(null)
|
||||
await t.test('fails with not found channel', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
user: userExample
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/channels/5/messages',
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: {
|
||||
value: messageExample.value
|
||||
}
|
||||
payload: { value: messageExample.value }
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(404)
|
||||
expect(responseJson.message).toEqual('Channel not found')
|
||||
t.equal(response.statusCode, 404)
|
||||
t.equal(responseJson.message, 'Channel not found')
|
||||
})
|
||||
|
||||
it('fails with not found member', async () => {
|
||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
||||
prismaMock.member.findUnique.mockResolvedValue(null)
|
||||
await t.test('fails with not found member', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findUnique: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/channels/${channelExample.id}/messages`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: {
|
||||
value: messageExample.value
|
||||
}
|
||||
payload: { value: messageExample.value }
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(404)
|
||||
expect(responseJson.message).toEqual('Channel not found')
|
||||
t.equal(response.statusCode, 404)
|
||||
t.equal(responseJson.message, 'Channel not found')
|
||||
})
|
||||
})
|
||||
|
@ -10,7 +10,7 @@ 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 { maximumFileSize } from '../../../../../tools/configurations/index.js'
|
||||
import { MAXIMUM_FILE_SIZE } from '../../../../../tools/configurations/index.js'
|
||||
|
||||
const parametersSchema = Type.Object({
|
||||
channelId: channelSchema.id
|
||||
@ -95,7 +95,7 @@ export const postMessageUploadsByChannelIdService: FastifyPluginAsync = async (
|
||||
fastify,
|
||||
request,
|
||||
folderInUploadsFolder: 'messages',
|
||||
maximumFileSize
|
||||
maximumFileSize: MAXIMUM_FILE_SIZE
|
||||
})
|
||||
const message = await prisma.message.create({
|
||||
data: {
|
||||
|
Reference in New Issue
Block a user