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: {
|
||||
|
@ -1,18 +1,33 @@
|
||||
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 { memberExample } from '../../../../models/Member.js'
|
||||
import { guildExample } from '../../../../models/Guild.js'
|
||||
|
||||
describe('DELETE /guilds/[guildId]', () => {
|
||||
it('succeeds and delete the guild', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
isOwner: true,
|
||||
guild: guildExample
|
||||
} as any)
|
||||
prismaMock.guild.delete.mockResolvedValue(guildExample)
|
||||
await tap.test('DELETE /guilds/[guildId]', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds and delete the guild', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
isOwner: true,
|
||||
guild: guildExample
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'guild').value({
|
||||
delete: async () => {
|
||||
return guildExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/guilds/${guildExample.id}`,
|
||||
@ -21,14 +36,19 @@ describe('DELETE /guilds/[guildId]', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.name).toEqual(guildExample.name)
|
||||
expect(responseJson.description).toEqual(guildExample.description)
|
||||
t.equal(response.statusCode, 200)
|
||||
t.equal(responseJson.id, guildExample.id)
|
||||
t.equal(responseJson.name, guildExample.name)
|
||||
t.equal(responseJson.description, guildExample.description)
|
||||
})
|
||||
|
||||
it("fails if the guild doesn't exist", async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
await t.test("fails if the guild doesn't exist", async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/guilds/${guildExample.id}`,
|
||||
@ -36,16 +56,20 @@ describe('DELETE /guilds/[guildId]', () => {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
t.equal(response.statusCode, 404)
|
||||
})
|
||||
|
||||
it('fails if the user is not the owner', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
isOwner: false,
|
||||
guild: guildExample
|
||||
} as any)
|
||||
await t.test('fails if the user is not the owner', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
isOwner: false,
|
||||
guild: guildExample
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/guilds/${guildExample.id}`,
|
||||
@ -54,7 +78,7 @@ describe('DELETE /guilds/[guildId]', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(400)
|
||||
expect(responseJson.message).toEqual('You should be an owner of the guild')
|
||||
t.equal(response.statusCode, 400)
|
||||
t.equal(responseJson.message, 'You should be an owner of the guild')
|
||||
})
|
||||
})
|
||||
|
@ -1,24 +1,40 @@
|
||||
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 { memberExample } from '../../../../models/Member.js'
|
||||
import { guildExample } from '../../../../models/Guild.js'
|
||||
import { userExample } from '../../../../models/User.js'
|
||||
import { channelExample } from '../../../../models/Channel.js'
|
||||
|
||||
describe('GET /guilds/[guildId]', () => {
|
||||
it('succeeds', async () => {
|
||||
const defaultChannelId = 5
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
guild: guildExample,
|
||||
user: userExample
|
||||
} as any)
|
||||
prismaMock.channel.findFirst.mockResolvedValue({
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
})
|
||||
const defaultChannelId = 5
|
||||
|
||||
await tap.test('GET /guilds/[guildId]', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken, user } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
guild: guildExample,
|
||||
user: userExample
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: `/guilds/${guildExample.id}`,
|
||||
@ -27,18 +43,23 @@ describe('GET /guilds/[guildId]', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.member.isOwner).toEqual(memberExample.isOwner)
|
||||
expect(responseJson.member.user.name).toEqual(user.name)
|
||||
expect(responseJson.member.user.email).toBeNull()
|
||||
expect(responseJson.guild.id).toEqual(guildExample.id)
|
||||
expect(responseJson.guild.name).toEqual(guildExample.name)
|
||||
expect(responseJson.guild.defaultChannelId).toEqual(defaultChannelId)
|
||||
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)
|
||||
})
|
||||
|
||||
it('fails with not found guild', async () => {
|
||||
await t.test('fails with not found member/guild', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/guilds/1',
|
||||
@ -47,15 +68,15 @@ describe('GET /guilds/[guildId]', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(404)
|
||||
expect(responseJson.message).toEqual('Member not found')
|
||||
t.equal(response.statusCode, 404)
|
||||
t.equal(responseJson.message, 'Member 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: '/guilds/1'
|
||||
})
|
||||
expect(response.statusCode).toEqual(401)
|
||||
t.equal(response.statusCode, 401)
|
||||
})
|
||||
})
|
||||
|
@ -1,30 +1,50 @@
|
||||
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 { guildExample } from '../../../../models/Guild.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'
|
||||
|
||||
describe('PUT /guilds/[guildId]', () => {
|
||||
it('succeeds and edit the guild', async () => {
|
||||
const defaultChannelId = 5
|
||||
const newName = 'New guild name'
|
||||
const newDescription = 'New guild description'
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
isOwner: true,
|
||||
guild: guildExample
|
||||
} as any)
|
||||
prismaMock.guild.update.mockResolvedValue({
|
||||
...guildExample,
|
||||
name: newName,
|
||||
description: newDescription
|
||||
})
|
||||
prismaMock.channel.findFirst.mockResolvedValue({
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
})
|
||||
const defaultChannelId = 5
|
||||
const newName = 'New guild name'
|
||||
const newDescription = 'New guild description'
|
||||
|
||||
await tap.test('PUT /guilds/[guildId]', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds and edit the guild', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
isOwner: true,
|
||||
guild: guildExample
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'guild').value({
|
||||
update: async () => {
|
||||
return {
|
||||
...guildExample,
|
||||
name: newName,
|
||||
description: newDescription
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: `/guilds/${guildExample.id}`,
|
||||
@ -37,17 +57,19 @@ describe('PUT /guilds/[guildId]', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.name).toEqual(newName)
|
||||
expect(responseJson.description).toEqual(newDescription)
|
||||
expect(responseJson.defaultChannelId).toEqual(defaultChannelId)
|
||||
t.equal(response.statusCode, 200)
|
||||
t.equal(responseJson.name, newName)
|
||||
t.equal(responseJson.description, newDescription)
|
||||
t.equal(responseJson.defaultChannelId, defaultChannelId)
|
||||
})
|
||||
|
||||
it("fails if the guild doesn't exist", async () => {
|
||||
const newName = 'New guild name'
|
||||
const newDescription = 'New guild description'
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
await t.test("fails if the guild doesn't exist", async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: `/guilds/${guildExample.id}`,
|
||||
@ -59,18 +81,20 @@ describe('PUT /guilds/[guildId]', () => {
|
||||
description: newDescription
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
t.equal(response.statusCode, 404)
|
||||
})
|
||||
|
||||
it('fails if the user is not the owner', async () => {
|
||||
const newName = 'New guild name'
|
||||
const newDescription = 'New guild description'
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
isOwner: false,
|
||||
guild: guildExample
|
||||
} as any)
|
||||
await t.test('fails if the user is not the owner', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
isOwner: false,
|
||||
guild: guildExample
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: `/guilds/${guildExample.id}`,
|
||||
@ -83,7 +107,7 @@ describe('PUT /guilds/[guildId]', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(400)
|
||||
expect(responseJson.message).toEqual('You should be an owner of the guild')
|
||||
t.equal(response.statusCode, 400)
|
||||
t.equal(responseJson.message, 'You should be an owner of the guild')
|
||||
})
|
||||
})
|
||||
|
@ -1,15 +1,30 @@
|
||||
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 { memberExample } from '../../../../../models/Member.js'
|
||||
import { guildExample } from '../../../../../models/Guild.js'
|
||||
import { channelExample } from '../../../../../models/Channel.js'
|
||||
|
||||
describe('GET /guilds/[guildId]/channels', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||
prismaMock.channel.findMany.mockResolvedValue([channelExample])
|
||||
await tap.test('GET /guilds/[guildId]/channels', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return memberExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findMany: async () => {
|
||||
return [channelExample]
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: `/guilds/${guildExample.id}/channels`,
|
||||
@ -18,16 +33,20 @@ describe('GET /guilds/[guildId]/channels', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.length).toEqual(1)
|
||||
expect(responseJson[0].id).toEqual(channelExample.id)
|
||||
expect(responseJson[0].name).toEqual(channelExample.name)
|
||||
expect(responseJson[0].guildId).toEqual(channelExample.guildId)
|
||||
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)
|
||||
})
|
||||
|
||||
it('fails with not found guild', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails with not found member/guild', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/guilds/1/channels',
|
||||
@ -36,15 +55,15 @@ describe('GET /guilds/[guildId]/channels', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(404)
|
||||
expect(responseJson.message).toEqual('Member not found')
|
||||
t.equal(response.statusCode, 404)
|
||||
t.equal(responseJson.message, 'Member 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: '/guilds/1/channels'
|
||||
})
|
||||
expect(response.statusCode).toEqual(401)
|
||||
t.equal(response.statusCode, 401)
|
||||
})
|
||||
})
|
||||
|
@ -1,20 +1,38 @@
|
||||
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 { channelExample } from '../../../../../models/Channel.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'
|
||||
|
||||
describe('POST /guilds/[guildId]/channels', () => {
|
||||
it('succeeds', async () => {
|
||||
const defaultChannelId = 5
|
||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||
prismaMock.channel.create.mockResolvedValue(channelExample)
|
||||
prismaMock.channel.findFirst.mockResolvedValue({
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
})
|
||||
const defaultChannelId = 5
|
||||
|
||||
await tap.test('POST /guilds/[guildId]/channels', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return memberExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
}
|
||||
},
|
||||
create: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/guilds/${guildExample.id}/channels`,
|
||||
@ -24,16 +42,20 @@ describe('POST /guilds/[guildId]/channels', () => {
|
||||
payload: { name: channelExample.name }
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(201)
|
||||
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, 201)
|
||||
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 the member is not found', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails if the member is not found', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/guilds/${guildExample.id}/channels`,
|
||||
@ -42,16 +64,19 @@ describe('POST /guilds/[guildId]/channels', () => {
|
||||
},
|
||||
payload: { name: channelExample.name }
|
||||
})
|
||||
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.member.findFirst.mockResolvedValue(member)
|
||||
await t.test('fails if the member is not owner', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
isOwner: false
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/guilds/${guildExample.id}/channels`,
|
||||
@ -60,6 +85,6 @@ describe('POST /guilds/[guildId]/channels', () => {
|
||||
},
|
||||
payload: { name: channelExample.name }
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
})
|
||||
|
@ -8,8 +8,8 @@ import prisma from '../../../../tools/database/prisma.js'
|
||||
import { uploadFile } from '../../../../tools/utils/uploadFile.js'
|
||||
import { guildSchema } from '../../../../models/Guild.js'
|
||||
import {
|
||||
maximumImageSize,
|
||||
supportedImageMimetype
|
||||
MAXIMUM_IMAGE_SIZE,
|
||||
SUPPORTED_IMAGE_MIMETYPE
|
||||
} from '../../../../tools/configurations/index.js'
|
||||
import { channelSchema } from '../../../../models/Channel.js'
|
||||
|
||||
@ -68,8 +68,8 @@ export const putGuildIconById: FastifyPluginAsync = async (fastify) => {
|
||||
fastify,
|
||||
request,
|
||||
folderInUploadsFolder: 'guilds',
|
||||
maximumFileSize: maximumImageSize,
|
||||
supportedFileMimetype: supportedImageMimetype
|
||||
maximumFileSize: MAXIMUM_IMAGE_SIZE,
|
||||
supportedFileMimetype: SUPPORTED_IMAGE_MIMETYPE
|
||||
})
|
||||
await prisma.guild.update({
|
||||
where: { id: guildId },
|
||||
|
@ -1,36 +1,52 @@
|
||||
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 { memberExample } from '../../../../../models/Member.js'
|
||||
import { guildExample } from '../../../../../models/Guild.js'
|
||||
import { userExample } from '../../../../../models/User.js'
|
||||
|
||||
describe('GET /guilds/[guildId]/members', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||
prismaMock.member.findMany.mockResolvedValue([
|
||||
{ ...memberExample, user: userExample }
|
||||
] as any)
|
||||
await tap.test('GET /guilds/[guildId]/members', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return memberExample
|
||||
},
|
||||
findMany: async () => {
|
||||
return [{ ...memberExample, user: userExample }]
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: `/guilds/${memberExample.guildId}/members`,
|
||||
url: `/guilds/${guildExample.id}/members`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.length).toEqual(1)
|
||||
expect(responseJson[0].id).toEqual(memberExample.id)
|
||||
expect(responseJson[0].isOwner).toEqual(memberExample.isOwner)
|
||||
expect(responseJson[0].user.id).toEqual(userExample.id)
|
||||
expect(responseJson[0].user.name).toEqual(userExample.name)
|
||||
expect(responseJson[0].user.email).toEqual(null)
|
||||
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)
|
||||
})
|
||||
|
||||
it('fails with not found member', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails with not found member/guild', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/guilds/1/members',
|
||||
@ -39,15 +55,15 @@ describe('GET /guilds/[guildId]/members', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(404)
|
||||
expect(responseJson.message).toEqual('Member not found')
|
||||
t.equal(response.statusCode, 404)
|
||||
t.equal(responseJson.message, 'Member 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: '/guilds/1/members'
|
||||
})
|
||||
expect(response.statusCode).toEqual(401)
|
||||
t.equal(response.statusCode, 401)
|
||||
})
|
||||
})
|
||||
|
@ -1,21 +1,41 @@
|
||||
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 { guildExample } from '../../../../../../models/Guild.js'
|
||||
import { channelExample } from '../../../../../../models/Channel.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'
|
||||
|
||||
describe('POST /guilds/[guildId]/members/join', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.guild.findUnique.mockResolvedValue(guildExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
prismaMock.member.create.mockResolvedValue({
|
||||
...memberExample,
|
||||
user: userExample
|
||||
} as any)
|
||||
prismaMock.channel.findFirst.mockResolvedValue(channelExample)
|
||||
const { accessToken, user } = await authenticateUserTest()
|
||||
const defaultChannelId = 5
|
||||
|
||||
await tap.test('POST /guilds/[guildId]/members/join', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
},
|
||||
create: async () => {
|
||||
return { ...memberExample, user: userExample }
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findFirst: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'guild').value({
|
||||
findUnique: async () => {
|
||||
return guildExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/guilds/${guildExample.id}/members/join`,
|
||||
@ -24,21 +44,33 @@ describe('POST /guilds/[guildId]/members/join', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(201)
|
||||
expect(responseJson.id).toEqual(memberExample.id)
|
||||
expect(responseJson.userId).toEqual(memberExample.userId)
|
||||
expect(responseJson.user.name).toEqual(user.name)
|
||||
expect(responseJson.user.email).toEqual(null)
|
||||
expect(responseJson.guild.id).toEqual(guildExample.id)
|
||||
expect(responseJson.guild.name).toEqual(guildExample.name)
|
||||
expect(responseJson.guild.defaultChannelId).toEqual(channelExample.id)
|
||||
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)
|
||||
})
|
||||
|
||||
it('fails if the guild is not found', async () => {
|
||||
prismaMock.guild.findUnique.mockResolvedValue(null)
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
prismaMock.channel.findFirst.mockResolvedValue(channelExample)
|
||||
await t.test('fails if the guild is not found', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'guild').value({
|
||||
findUnique: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/guilds/${guildExample.id}/members/join`,
|
||||
@ -46,18 +78,29 @@ describe('POST /guilds/[guildId]/members/join', () => {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
t.equal(response.statusCode, 404)
|
||||
})
|
||||
|
||||
it('fails if the user is already in the guild', async () => {
|
||||
const defaultChannelId = 5
|
||||
prismaMock.guild.findUnique.mockResolvedValue(guildExample)
|
||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
||||
prismaMock.channel.findFirst.mockResolvedValue({
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
})
|
||||
await t.test('fails if the user is already in the guild', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return memberExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...channelExample,
|
||||
id: defaultChannelId
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'guild').value({
|
||||
findUnique: async () => {
|
||||
return guildExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: `/guilds/${guildExample.id}/members/join`,
|
||||
@ -66,7 +109,7 @@ describe('POST /guilds/[guildId]/members/join', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(400)
|
||||
expect(responseJson.defaultChannelId).toEqual(defaultChannelId)
|
||||
t.equal(response.statusCode, 400)
|
||||
t.equal(responseJson.defaultChannelId, defaultChannelId)
|
||||
})
|
||||
})
|
||||
|
@ -1,18 +1,31 @@
|
||||
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 { guildExample } from '../../../../../../models/Guild.js'
|
||||
import prisma from '../../../../../../tools/database/prisma.js'
|
||||
import { memberExample } from '../../../../../../models/Member.js'
|
||||
import { guildExample } from '../../../../../../models/Guild.js'
|
||||
|
||||
describe('DELETE /guilds/[guildId]/members/leave', () => {
|
||||
it('succeeds', async () => {
|
||||
await tap.test('DELETE /guilds/[guildId]/members/leave', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const member = {
|
||||
...memberExample,
|
||||
isOwner: false
|
||||
}
|
||||
prismaMock.member.findFirst.mockResolvedValue(member)
|
||||
prismaMock.member.delete.mockResolvedValue(member)
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return member
|
||||
},
|
||||
delete: async () => {
|
||||
return member
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/guilds/${guildExample.id}/members/leave`,
|
||||
@ -21,15 +34,19 @@ describe('DELETE /guilds/[guildId]/members/leave', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.id).toEqual(member.id)
|
||||
expect(responseJson.isOwner).toEqual(member.isOwner)
|
||||
expect(responseJson.userId).toEqual(member.userId)
|
||||
t.equal(response.statusCode, 200)
|
||||
t.equal(responseJson.id, member.id)
|
||||
t.equal(responseJson.isOwner, member.isOwner)
|
||||
t.equal(responseJson.userId, member.userId)
|
||||
})
|
||||
|
||||
it('fails if the member is not found', async () => {
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails if the member is not found', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/guilds/${guildExample.id}/members/leave`,
|
||||
@ -37,16 +54,20 @@ describe('DELETE /guilds/[guildId]/members/leave', () => {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
t.equal(response.statusCode, 404)
|
||||
})
|
||||
|
||||
it('fails if the member is owner', async () => {
|
||||
await t.test('fails if the member is owner', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const member = {
|
||||
...memberExample,
|
||||
isOwner: true
|
||||
}
|
||||
prismaMock.member.findFirst.mockResolvedValue(member)
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return member
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/guilds/${guildExample.id}/members/leave`,
|
||||
@ -54,6 +75,6 @@ describe('DELETE /guilds/[guildId]/members/leave', () => {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
})
|
||||
|
@ -1,16 +1,35 @@
|
||||
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 { guildExample } from '../../../models/Guild.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'
|
||||
|
||||
describe('GET /guilds', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.guild.findUnique.mockResolvedValue(guildExample)
|
||||
prismaMock.member.findMany.mockResolvedValue([memberExample])
|
||||
prismaMock.channel.findFirst.mockResolvedValue(channelExample)
|
||||
await tap.test('GET /guilds', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'guild').value({
|
||||
findUnique: async () => {
|
||||
return guildExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findMany: async () => {
|
||||
return [memberExample]
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
findFirst: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/guilds',
|
||||
@ -19,10 +38,10 @@ describe('GET /guilds', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.length).toEqual(1)
|
||||
expect(responseJson[0].name).toEqual(guildExample.name)
|
||||
expect(responseJson[0].description).toEqual(guildExample.description)
|
||||
expect(responseJson[0].defaultChannelId).toEqual(channelExample.id)
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
@ -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 { guildExample } from '../../../models/Guild.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'
|
||||
|
||||
describe('POST /guilds', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.guild.create.mockResolvedValue(guildExample)
|
||||
prismaMock.member.create.mockResolvedValue(memberExample)
|
||||
prismaMock.member.findUnique.mockResolvedValue({
|
||||
...memberExample,
|
||||
...userExample
|
||||
})
|
||||
prismaMock.channel.create.mockResolvedValue(channelExample)
|
||||
await tap.test('POST /guilds', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken, user } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'guild').value({
|
||||
create: async () => {
|
||||
return guildExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
create: async () => {
|
||||
return memberExample
|
||||
},
|
||||
findUnique: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
...userExample
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'channel').value({
|
||||
create: async () => {
|
||||
return channelExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/guilds',
|
||||
@ -28,23 +49,21 @@ describe('POST /guilds', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(201)
|
||||
expect(responseJson.guild.id).toEqual(guildExample.id)
|
||||
expect(responseJson.guild.name).toEqual(guildExample.name)
|
||||
expect(responseJson.guild.members.length).toEqual(1)
|
||||
expect(responseJson.guild.members[0].userId).toEqual(user.id)
|
||||
expect(responseJson.guild.members[0].user.name).toEqual(user.name)
|
||||
expect(responseJson.guild.members[0].guildId).toEqual(guildExample.id)
|
||||
expect(responseJson.guild.members[0].isOwner).toEqual(memberExample.isOwner)
|
||||
expect(responseJson.guild.channels.length).toEqual(1)
|
||||
expect(responseJson.guild.channels[0].id).toEqual(channelExample.id)
|
||||
expect(responseJson.guild.channels[0].guildId).toEqual(guildExample.id)
|
||||
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)
|
||||
})
|
||||
|
||||
it('fails with empty name and description', async () => {
|
||||
prismaMock.guild.create.mockResolvedValue(guildExample)
|
||||
prismaMock.member.create.mockResolvedValue(memberExample)
|
||||
prismaMock.channel.create.mockResolvedValue(channelExample)
|
||||
await t.test('fails with empty name and description', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
@ -53,6 +72,6 @@ describe('POST /guilds', () => {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
})
|
||||
|
@ -1,13 +1,28 @@
|
||||
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 { guildExample } from '../../../../models/Guild.js'
|
||||
|
||||
describe('GET /guilds/public', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.guild.findMany.mockResolvedValue([guildExample])
|
||||
prismaMock.member.count.mockResolvedValue(2)
|
||||
await tap.test('GET /guilds/public', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'guild').value({
|
||||
findMany: async () => {
|
||||
return [guildExample]
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
count: async () => {
|
||||
return 2
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/guilds/public',
|
||||
@ -16,9 +31,9 @@ describe('GET /guilds/public', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.length).toEqual(1)
|
||||
expect(responseJson[0].name).toEqual(guildExample.name)
|
||||
expect(responseJson[0].membersCount).toEqual(2)
|
||||
t.equal(response.statusCode, 200)
|
||||
t.equal(responseJson.length, 1)
|
||||
t.equal(responseJson[0].name, guildExample.name)
|
||||
t.equal(responseJson[0].membersCount, 2)
|
||||
})
|
||||
})
|
||||
|
@ -1,23 +1,40 @@
|
||||
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 { messageExample } from '../../../../models/Message.js'
|
||||
import { memberExample } from '../../../../models/Member.js'
|
||||
import { userExample } from '../../../../models/User.js'
|
||||
import { channelExample } from '../../../../models/Channel.js'
|
||||
|
||||
describe('DELETE /messsages/[messageId]', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.message.findFirst.mockResolvedValue({
|
||||
...messageExample,
|
||||
channel: channelExample
|
||||
} as any)
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
user: userExample
|
||||
} as any)
|
||||
prismaMock.message.delete.mockResolvedValue(messageExample)
|
||||
await tap.test('DELETE /messsages/[messageId]', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'message').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...messageExample,
|
||||
channel: channelExample
|
||||
}
|
||||
},
|
||||
delete: async () => {
|
||||
return messageExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
user: userExample
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/messages/${messageExample.id}`,
|
||||
@ -26,20 +43,24 @@ describe('DELETE /messsages/[messageId]', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
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, 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)
|
||||
})
|
||||
|
||||
it('fails if the message is not found', async () => {
|
||||
prismaMock.message.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails if the message is not found', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'message').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/messages/${messageExample.id}`,
|
||||
@ -47,16 +68,24 @@ describe('DELETE /messsages/[messageId]', () => {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
t.equal(response.statusCode, 404)
|
||||
})
|
||||
|
||||
it('fails if the member is not found', async () => {
|
||||
prismaMock.message.findFirst.mockResolvedValue({
|
||||
...messageExample,
|
||||
channel: channelExample
|
||||
} as any)
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails if the member is not found', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'message').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...messageExample,
|
||||
channel: channelExample
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/messages/${messageExample.id}`,
|
||||
@ -64,20 +93,28 @@ describe('DELETE /messsages/[messageId]', () => {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
t.equal(response.statusCode, 404)
|
||||
})
|
||||
|
||||
it('fails if the member is not owner of the message', async () => {
|
||||
await t.test('fails if the member is not owner of the message', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const randomUserIdOwnerOfMessage = 14
|
||||
prismaMock.message.findFirst.mockResolvedValue({
|
||||
...messageExample,
|
||||
channel: channelExample
|
||||
} as any)
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
userId: randomUserIdOwnerOfMessage
|
||||
sinon.stub(prisma, 'message').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...messageExample,
|
||||
channel: channelExample
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
userId: randomUserIdOwnerOfMessage
|
||||
}
|
||||
}
|
||||
})
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: `/messages/${messageExample.id}`,
|
||||
@ -85,6 +122,6 @@ describe('DELETE /messsages/[messageId]', () => {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
})
|
||||
|
@ -1,109 +1,141 @@
|
||||
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 { messageExample } from '../../../../models/Message.js'
|
||||
import { memberExample } from '../../../../models/Member.js'
|
||||
import { userExample } from '../../../../models/User.js'
|
||||
import { channelExample } from '../../../../models/Channel.js'
|
||||
|
||||
describe('PUT /messsages/[messageId]', () => {
|
||||
it('succeeds', async () => {
|
||||
const newValue = 'some message'
|
||||
prismaMock.message.findFirst.mockResolvedValue({
|
||||
...messageExample,
|
||||
channel: channelExample
|
||||
} as any)
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
user: userExample
|
||||
} as any)
|
||||
prismaMock.message.update.mockResolvedValue({
|
||||
...messageExample,
|
||||
value: newValue
|
||||
})
|
||||
await tap.test('PUT /messsages/[messageId]', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const newValue = 'some message'
|
||||
sinon.stub(prisma, 'message').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...messageExample,
|
||||
channel: channelExample
|
||||
}
|
||||
},
|
||||
update: async () => {
|
||||
return {
|
||||
...messageExample,
|
||||
value: newValue
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
user: userExample
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: `/messages/${messageExample.id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: {
|
||||
value: newValue
|
||||
}
|
||||
payload: { value: newValue }
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.id).toEqual(messageExample.id)
|
||||
expect(responseJson.value).toEqual(newValue)
|
||||
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, 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)
|
||||
})
|
||||
|
||||
it('fails if the message is not found', async () => {
|
||||
const newValue = 'some message'
|
||||
prismaMock.message.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails if the message is not found', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const newValue = 'some message'
|
||||
sinon.stub(prisma, 'message').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: `/messages/${messageExample.id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: {
|
||||
value: newValue
|
||||
}
|
||||
payload: { value: newValue }
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
t.equal(response.statusCode, 404)
|
||||
})
|
||||
|
||||
it('fails if the member is not found', async () => {
|
||||
const newValue = 'some message'
|
||||
prismaMock.message.findFirst.mockResolvedValue({
|
||||
...messageExample,
|
||||
channel: channelExample
|
||||
} as any)
|
||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails if the member is not found', async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const newValue = 'some message'
|
||||
sinon.stub(prisma, 'message').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...messageExample,
|
||||
channel: channelExample
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: `/messages/${messageExample.id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: {
|
||||
value: newValue
|
||||
}
|
||||
payload: { value: newValue }
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
t.equal(response.statusCode, 404)
|
||||
})
|
||||
|
||||
it('fails if the member is not owner of the message', async () => {
|
||||
const newValue = 'some message'
|
||||
const randomUserIdOwnerOfMessage = 14
|
||||
prismaMock.message.findFirst.mockResolvedValue({
|
||||
...messageExample,
|
||||
channel: channelExample
|
||||
} as any)
|
||||
prismaMock.member.findFirst.mockResolvedValue({
|
||||
...memberExample,
|
||||
userId: randomUserIdOwnerOfMessage
|
||||
})
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: `/messages/${messageExample.id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: {
|
||||
value: newValue
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
})
|
||||
await t.test(
|
||||
'fails if the member is not the owner of the message',
|
||||
async (t) => {
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const newValue = 'some message'
|
||||
const randomUserIdOwnerOfMessage = 14
|
||||
sinon.stub(prisma, 'message').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...messageExample,
|
||||
channel: channelExample
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'member').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...memberExample,
|
||||
userId: randomUserIdOwnerOfMessage
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: `/messages/${messageExample.id}`,
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: { value: newValue }
|
||||
})
|
||||
t.equal(response.statusCode, 400)
|
||||
}
|
||||
)
|
||||
})
|
||||
|
@ -1,31 +1,54 @@
|
||||
import tap from 'tap'
|
||||
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 { prismaMock } from '../../../../__test__/setup.js'
|
||||
|
||||
describe('GET /users/[userId]', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.guild.findMany.mockResolvedValue([])
|
||||
prismaMock.user.findUnique.mockResolvedValue(userExample)
|
||||
prismaMock.userSetting.findFirst.mockResolvedValue(userSettingsExample)
|
||||
await tap.test('GET /users/[userId]', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
sinon.stub(prisma, 'guild').value({
|
||||
findMany: async () => {
|
||||
return []
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return userExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'userSetting').value({
|
||||
findFirst: async () => {
|
||||
return userSettingsExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: `/users/${userExample.id}`
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.user.id).toEqual(userExample.id)
|
||||
expect(responseJson.user.name).toEqual(userExample.name)
|
||||
t.equal(response.statusCode, 200)
|
||||
t.equal(responseJson.user.id, userExample.id)
|
||||
t.equal(responseJson.user.name, userExample.name)
|
||||
})
|
||||
|
||||
it('fails with not found user', async () => {
|
||||
prismaMock.userSetting.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails with not found user', async (t) => {
|
||||
sinon.stub(prisma, 'userSetting').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: `/users/1`
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(404)
|
||||
expect(responseJson.message).toEqual('User not found')
|
||||
t.equal(response.statusCode, 404)
|
||||
t.equal(responseJson.message, 'User not found')
|
||||
})
|
||||
})
|
||||
|
@ -1,31 +1,23 @@
|
||||
import { application } from '../../../../application.js'
|
||||
import { userExample } from '../../../../models/User.js'
|
||||
import { prismaMock } from '../../../../__test__/setup.js'
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
|
||||
describe('GET /users/confirm-email', () => {
|
||||
it('should succeeds', async () => {
|
||||
prismaMock.user.findFirst.mockResolvedValue(userExample)
|
||||
prismaMock.user.update.mockResolvedValue({
|
||||
...userExample,
|
||||
isConfirmed: true,
|
||||
temporaryToken: null
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/users/confirm-email',
|
||||
query: {
|
||||
temporaryToken: userExample.temporaryToken ?? ''
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(200)
|
||||
import { application } from '../../../../application.js'
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { userExample } from '../../../../models/User.js'
|
||||
|
||||
await tap.test('GET /users/confirm-email', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('should fails with invalid `temporaryToken`', async () => {
|
||||
prismaMock.user.findFirst.mockResolvedValue(null)
|
||||
prismaMock.user.update.mockResolvedValue({
|
||||
...userExample,
|
||||
isConfirmed: true,
|
||||
temporaryToken: null
|
||||
await t.test('succeeds', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findFirst: async () => {
|
||||
return userExample
|
||||
},
|
||||
update: async () => {
|
||||
return { ...userExample, isConfirmed: true, temporaryToken: null }
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
@ -34,6 +26,25 @@ describe('GET /users/confirm-email', () => {
|
||||
temporaryToken: userExample.temporaryToken ?? ''
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(403)
|
||||
t.equal(response.statusCode, 200)
|
||||
})
|
||||
|
||||
await t.test('should fails with invalid `temporaryToken`', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
},
|
||||
update: async () => {
|
||||
return { ...userExample, isConfirmed: true, temporaryToken: null }
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/users/confirm-email',
|
||||
query: {
|
||||
temporaryToken: userExample.temporaryToken ?? ''
|
||||
}
|
||||
})
|
||||
t.equal(response.statusCode, 403)
|
||||
})
|
||||
})
|
||||
|
@ -1,8 +1,15 @@
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
|
||||
import { application } from '../../../../application.js'
|
||||
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||
|
||||
describe('GET /users/current', () => {
|
||||
it('succeeds', async () => {
|
||||
await tap.test('GET /users/current', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken, user } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
@ -12,18 +19,16 @@ describe('GET /users/current', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.user.name).toEqual(user.name)
|
||||
expect(responseJson.user.strategies).toEqual(
|
||||
expect.arrayContaining(['local'])
|
||||
)
|
||||
t.equal(response.statusCode, 200)
|
||||
t.equal(responseJson.user.name, user.name)
|
||||
t.strictSame(responseJson.user.strategies, ['local'])
|
||||
})
|
||||
|
||||
it('fails with unauthenticated user', async () => {
|
||||
await t.test('fails with unauthenticated user', async (t) => {
|
||||
const response = await application.inject({
|
||||
method: 'GET',
|
||||
url: '/users/current'
|
||||
})
|
||||
expect(response.statusCode).toEqual(401)
|
||||
t.equal(response.statusCode, 401)
|
||||
})
|
||||
})
|
||||
|
@ -1,15 +1,29 @@
|
||||
import { application } from '../../../../application.js'
|
||||
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||
import { prismaMock } from '../../../../__test__/setup.js'
|
||||
import { userExample } from '../../../../models/User.js'
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
|
||||
describe('PUT /users/current', () => {
|
||||
it('succeeds with valid accessToken and valid name', async () => {
|
||||
import { application } from '../../../../application.js'
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||
|
||||
await tap.test('PUT /users/current', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds with valid accessToken and valid name', async (t) => {
|
||||
const newName = 'John Doe'
|
||||
const { accessToken, user } = await authenticateUserTest()
|
||||
prismaMock.user.update.mockResolvedValue({
|
||||
...user,
|
||||
name: newName
|
||||
const { accessToken, user, userStubValue } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'user').value({
|
||||
...userStubValue,
|
||||
findFirst: async () => {
|
||||
return null
|
||||
},
|
||||
update: async () => {
|
||||
return {
|
||||
...user,
|
||||
name: newName
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
@ -22,16 +36,24 @@ describe('PUT /users/current', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.user.name).toEqual(newName)
|
||||
t.equal(response.statusCode, 200)
|
||||
t.equal(responseJson.user.name, newName)
|
||||
})
|
||||
|
||||
it('succeeds and only update the status', async () => {
|
||||
await t.test('succeeds and only update the status', async (t) => {
|
||||
const newStatus = '👀 Working on secret projects...'
|
||||
const { accessToken, user } = await authenticateUserTest()
|
||||
prismaMock.user.update.mockResolvedValue({
|
||||
...user,
|
||||
status: newStatus
|
||||
const { accessToken, user, userStubValue } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'user').value({
|
||||
...userStubValue,
|
||||
findFirst: async () => {
|
||||
return null
|
||||
},
|
||||
update: async () => {
|
||||
return {
|
||||
...user,
|
||||
status: newStatus
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
@ -44,15 +66,20 @@ describe('PUT /users/current', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.user.name).toEqual(user.name)
|
||||
expect(responseJson.user.status).toEqual(newStatus)
|
||||
t.equal(response.statusCode, 200)
|
||||
t.equal(responseJson.user.name, user.name)
|
||||
t.equal(responseJson.user.status, newStatus)
|
||||
})
|
||||
|
||||
it('fails with name already used', async () => {
|
||||
await t.test('fails with name already used', async (t) => {
|
||||
const newName = 'John Doe'
|
||||
prismaMock.user.findFirst.mockResolvedValue(userExample)
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const { accessToken, user, userStubValue } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'user').value({
|
||||
...userStubValue,
|
||||
findFirst: async () => {
|
||||
return user
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: '/users/current',
|
||||
@ -63,10 +90,10 @@ describe('PUT /users/current', () => {
|
||||
name: newName
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
it('fails with invalid website url', async () => {
|
||||
await t.test('fails with invalid website url', async (t) => {
|
||||
const newWebsite = 'invalid website url'
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
@ -79,15 +106,23 @@ describe('PUT /users/current', () => {
|
||||
website: newWebsite
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
it('succeeds with valid website url', async () => {
|
||||
await t.test('succeeds with valid website url', async (t) => {
|
||||
const newWebsite = 'https://somerandomwebsite.com'
|
||||
const { accessToken, user } = await authenticateUserTest()
|
||||
prismaMock.user.update.mockResolvedValue({
|
||||
...user,
|
||||
website: newWebsite
|
||||
const { accessToken, user, userStubValue } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'user').value({
|
||||
...userStubValue,
|
||||
findFirst: async () => {
|
||||
return null
|
||||
},
|
||||
update: async () => {
|
||||
return {
|
||||
...user,
|
||||
website: newWebsite
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
@ -100,8 +135,8 @@ describe('PUT /users/current', () => {
|
||||
}
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.user.name).toEqual(user.name)
|
||||
expect(responseJson.user.website).toEqual(newWebsite)
|
||||
t.equal(response.statusCode, 200)
|
||||
t.equal(responseJson.user.name, user.name)
|
||||
t.equal(responseJson.user.website, newWebsite)
|
||||
})
|
||||
})
|
||||
|
@ -7,8 +7,8 @@ import { fastifyErrors } from '../../../../models/utils.js'
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { uploadFile } from '../../../../tools/utils/uploadFile.js'
|
||||
import {
|
||||
maximumImageSize,
|
||||
supportedImageMimetype
|
||||
MAXIMUM_IMAGE_SIZE,
|
||||
SUPPORTED_IMAGE_MIMETYPE
|
||||
} from '../../../../tools/configurations/index.js'
|
||||
|
||||
const putServiceSchema: FastifySchema = {
|
||||
@ -52,8 +52,8 @@ export const putCurrentUserLogo: FastifyPluginAsync = async (fastify) => {
|
||||
fastify,
|
||||
request,
|
||||
folderInUploadsFolder: 'users',
|
||||
maximumFileSize: maximumImageSize,
|
||||
supportedFileMimetype: supportedImageMimetype
|
||||
maximumFileSize: MAXIMUM_IMAGE_SIZE,
|
||||
supportedFileMimetype: SUPPORTED_IMAGE_MIMETYPE
|
||||
})
|
||||
await prisma.user.update({
|
||||
where: { id: request.user.current.id },
|
||||
|
@ -1,48 +1,72 @@
|
||||
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 { userSettingsExample } from '../../../../../models/UserSettings.js'
|
||||
|
||||
describe('PUT /users/current/settings', () => {
|
||||
it('succeeds and edit the theme, language, isPublicEmail and isPublicGuilds', async () => {
|
||||
const newSettings = {
|
||||
theme: 'light',
|
||||
language: 'fr',
|
||||
isPublicEmail: true,
|
||||
isPublicGuilds: true
|
||||
}
|
||||
prismaMock.userSetting.findFirst.mockResolvedValue(userSettingsExample)
|
||||
prismaMock.userSetting.update.mockResolvedValue({
|
||||
...userSettingsExample,
|
||||
...newSettings
|
||||
})
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: '/users/current/settings',
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: newSettings
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.settings.theme).toEqual(newSettings.theme)
|
||||
expect(responseJson.settings.language).toEqual(newSettings.language)
|
||||
expect(responseJson.settings.isPublicEmail).toEqual(
|
||||
newSettings.isPublicEmail
|
||||
)
|
||||
expect(responseJson.settings.isPublicGuilds).toEqual(
|
||||
newSettings.isPublicGuilds
|
||||
)
|
||||
await tap.test('PUT /users/current/settings', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('fails with invalid language', async () => {
|
||||
await t.test(
|
||||
'succeeds and edit the theme, language, isPublicEmail and isPublicGuilds',
|
||||
async (t) => {
|
||||
const newSettings = {
|
||||
theme: 'light',
|
||||
language: 'fr',
|
||||
isPublicEmail: true,
|
||||
isPublicGuilds: true
|
||||
}
|
||||
const { accessToken, userSettingStubValue } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'userSetting').value({
|
||||
...userSettingStubValue,
|
||||
findFirst: async () => {
|
||||
return userSettingsExample
|
||||
},
|
||||
update: async () => {
|
||||
return {
|
||||
...userSettingsExample,
|
||||
...newSettings
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: '/users/current/settings',
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
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)
|
||||
}
|
||||
)
|
||||
|
||||
await t.test('fails with invalid language', async (t) => {
|
||||
const newSettings = {
|
||||
language: 'somerandomlanguage'
|
||||
}
|
||||
prismaMock.userSetting.findFirst.mockResolvedValue(userSettingsExample)
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const { accessToken, userSettingStubValue } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'userSetting').value({
|
||||
...userSettingStubValue,
|
||||
findFirst: async () => {
|
||||
return userSettingsExample
|
||||
},
|
||||
update: async () => {
|
||||
return {
|
||||
...userSettingsExample,
|
||||
...newSettings
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: '/users/current/settings',
|
||||
@ -51,6 +75,6 @@ describe('PUT /users/current/settings', () => {
|
||||
},
|
||||
payload: newSettings
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
})
|
||||
|
@ -1,46 +1,72 @@
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
|
||||
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 { prismaMock } from '../../../../__test__/setup.js'
|
||||
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||
|
||||
describe('POST /users/refresh-token', () => {
|
||||
it('succeeds', async () => {
|
||||
const { refreshToken } = await authenticateUserTest()
|
||||
prismaMock.refreshToken.findFirst.mockResolvedValue({
|
||||
...refreshTokenExample,
|
||||
id: 1,
|
||||
token: refreshToken
|
||||
await tap.test('POST /users/refresh-token', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken, refreshToken, refreshTokenStubValue } =
|
||||
await authenticateUserTest()
|
||||
sinon.stub(prisma, 'refreshToken').value({
|
||||
...refreshTokenStubValue,
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...refreshTokenExample,
|
||||
id: 1,
|
||||
token: refreshToken
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/refresh-token',
|
||||
headers: {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
},
|
||||
payload: { refreshToken }
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.type).toEqual('Bearer')
|
||||
expect(responseJson.expiresIn).toEqual(expiresIn)
|
||||
expect(typeof responseJson.accessToken).toEqual('string')
|
||||
t.equal(response.statusCode, 200)
|
||||
t.equal(responseJson.type, 'Bearer')
|
||||
t.equal(responseJson.expiresIn, expiresIn)
|
||||
t.type(responseJson.accessToken, 'string')
|
||||
})
|
||||
|
||||
it('fails with refreshToken noty saved in database', async () => {
|
||||
await t.test('fails with refreshToken not saved in database', async (t) => {
|
||||
sinon.stub(prisma, 'refreshToken').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/refresh-token',
|
||||
payload: { refreshToken: 'somerandomtoken' }
|
||||
})
|
||||
expect(response.statusCode).toEqual(403)
|
||||
t.equal(response.statusCode, 403)
|
||||
})
|
||||
|
||||
it('fails with invalid jwt refreshToken', async () => {
|
||||
const { refreshToken } = await authenticateUserTest()
|
||||
prismaMock.refreshToken.findFirst.mockResolvedValue(refreshTokenExample)
|
||||
await t.test('fails with invalid jwt refreshToken', async (t) => {
|
||||
const { refreshToken, refreshTokenStubValue } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'refreshToken').value({
|
||||
...refreshTokenStubValue,
|
||||
findFirst: async () => {
|
||||
return refreshTokenExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/refresh-token',
|
||||
payload: { refreshToken }
|
||||
})
|
||||
expect(response.statusCode).toEqual(403)
|
||||
t.equal(response.statusCode, 403)
|
||||
})
|
||||
})
|
||||
|
@ -1,66 +1,115 @@
|
||||
import tap from 'tap'
|
||||
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 { prismaMock } from '../../../../__test__/setup.js'
|
||||
import { emailTransporter } from '../../../../tools/email/emailTransporter.js'
|
||||
|
||||
describe('POST /users/reset-password', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue(userExample)
|
||||
prismaMock.userSetting.findFirst.mockResolvedValue(userSettingsExample)
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/reset-password?redirectURI=https://redirecturi.com',
|
||||
payload: { email: userExample.email }
|
||||
})
|
||||
expect(response.statusCode).toEqual(200)
|
||||
await tap.test('POST /users/reset-password', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it("fails with email that doesn't exist", async () => {
|
||||
await t.test('succeeds', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return userExample
|
||||
},
|
||||
update: async () => {
|
||||
return {
|
||||
...userExample,
|
||||
temporaryExpirationToken: new Date(Date.now() + ms('1 hour')),
|
||||
temporaryToken: 'random-token'
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'userSetting').value({
|
||||
findFirst: async () => {
|
||||
return userSettingsExample
|
||||
}
|
||||
})
|
||||
sinon.stub(emailTransporter, 'sendMail').value(() => {})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/reset-password?redirectURI=https://redirecturi.com',
|
||||
payload: { email: userExample.email }
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 200)
|
||||
})
|
||||
|
||||
it('fails with unconfirmed account', async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue({
|
||||
...userExample,
|
||||
isConfirmed: false
|
||||
await t.test("fails with email that doesn't exist", async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/reset-password?redirectURI=https://redirecturi.com',
|
||||
payload: { email: userExample.email }
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
it("fails if userSettings doenst' exist", async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue(userExample)
|
||||
prismaMock.userSetting.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails with unconfirmed account', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return {
|
||||
...userExample,
|
||||
isConfirmed: false
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/reset-password?redirectURI=https://redirecturi.com',
|
||||
payload: { email: userExample.email }
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
it('fails with a request already in progress', async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue({
|
||||
...userExample,
|
||||
temporaryExpirationToken: new Date(Date.now() + ms('1 hour'))
|
||||
await t.test("fails if userSettings doesn't exist", async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return userExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'userSetting').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
prismaMock.userSetting.findFirst.mockResolvedValue(userSettingsExample)
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/reset-password?redirectURI=https://redirecturi.com',
|
||||
payload: { email: userExample.email }
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
await t.test('fails with a request already in progress', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return {
|
||||
...userExample,
|
||||
temporaryToken: 'random-token',
|
||||
temporaryExpirationToken: new Date(Date.now() + ms('1 hour'))
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'userSetting').value({
|
||||
findFirst: async () => {
|
||||
return userSettingsExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/reset-password?redirectURI=https://redirecturi.com',
|
||||
payload: { email: userExample.email }
|
||||
})
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
})
|
||||
|
@ -1,28 +1,30 @@
|
||||
import tap from 'tap'
|
||||
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 { prismaMock } from '../../../../__test__/setup.js'
|
||||
|
||||
describe('PUT /users/reset-password', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.user.findFirst.mockResolvedValue({
|
||||
...userExample,
|
||||
temporaryExpirationToken: new Date(Date.now() + ms('1 hour'))
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: '/users/reset-password',
|
||||
payload: {
|
||||
password: 'new password',
|
||||
temporaryToken: userExample.temporaryToken
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(200)
|
||||
await tap.test('PUT /users/reset-password', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('fails with expired temporaryToken', async () => {
|
||||
prismaMock.user.findFirst.mockResolvedValue(userExample)
|
||||
await t.test('succeeds', async (t) => {
|
||||
const temporaryToken = 'random-token'
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...userExample,
|
||||
temporaryToken,
|
||||
temporaryExpirationToken: new Date(Date.now() + ms('1 hour'))
|
||||
}
|
||||
},
|
||||
update: async () => {
|
||||
return userExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: '/users/reset-password',
|
||||
@ -31,6 +33,31 @@ describe('PUT /users/reset-password', () => {
|
||||
temporaryToken: userExample.temporaryToken
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 200)
|
||||
})
|
||||
|
||||
await t.test('fails with expired temporaryToken', async (t) => {
|
||||
const temporaryToken = 'random-token'
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findFirst: async () => {
|
||||
return {
|
||||
...userExample,
|
||||
temporaryToken,
|
||||
temporaryExpirationToken: new Date(Date.now() - ms('1 hour'))
|
||||
}
|
||||
},
|
||||
update: async () => {
|
||||
return userExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'PUT',
|
||||
url: '/users/reset-password',
|
||||
payload: {
|
||||
password: 'new password',
|
||||
temporaryToken: userExample.temporaryToken
|
||||
}
|
||||
})
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
})
|
||||
|
@ -70,6 +70,12 @@ export const postResetPasswordUser: FastifyPluginAsync = async (fastify) => {
|
||||
'A request to reset-password is already in progress'
|
||||
)
|
||||
}
|
||||
const userSettings = await prisma.userSetting.findFirst({
|
||||
where: { userId: user.id }
|
||||
})
|
||||
if (userSettings == null) {
|
||||
throw fastify.httpErrors.badRequest()
|
||||
}
|
||||
const temporaryToken = randomUUID()
|
||||
await prisma.user.update({
|
||||
where: {
|
||||
@ -80,12 +86,6 @@ export const postResetPasswordUser: FastifyPluginAsync = async (fastify) => {
|
||||
temporaryToken
|
||||
}
|
||||
})
|
||||
const userSettings = await prisma.userSetting.findFirst({
|
||||
where: { userId: user.id }
|
||||
})
|
||||
if (userSettings == null) {
|
||||
throw fastify.httpErrors.badRequest()
|
||||
}
|
||||
await sendEmail({
|
||||
type: 'reset-password',
|
||||
email,
|
||||
|
@ -1,46 +1,68 @@
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
import bcrypt from 'bcryptjs'
|
||||
|
||||
import { application } from '../../../../application.js'
|
||||
import { refreshTokenExample } from '../../../../models/RefreshToken.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 { prismaMock } from '../../../../__test__/setup.js'
|
||||
|
||||
const payload = {
|
||||
email: userExample.email,
|
||||
password: userExample.password
|
||||
}
|
||||
|
||||
describe('POST /users/signin', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue({
|
||||
...userExample,
|
||||
password: await bcrypt.hash(userExample.password as string, 12)
|
||||
await tap.test('POST /users/signin', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return {
|
||||
...userExample,
|
||||
password: await bcrypt.hash(payload.password as string, 12)
|
||||
}
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'refreshToken').value({
|
||||
create: async () => {
|
||||
return refreshTokenExample
|
||||
}
|
||||
})
|
||||
prismaMock.refreshToken.create.mockResolvedValue(refreshTokenExample)
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/signin',
|
||||
payload
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(200)
|
||||
expect(responseJson.type).toEqual('Bearer')
|
||||
expect(responseJson.expiresIn).toEqual(expiresIn)
|
||||
t.equal(response.statusCode, 200)
|
||||
t.equal(responseJson.type, 'Bearer')
|
||||
t.equal(responseJson.expiresIn, expiresIn)
|
||||
})
|
||||
|
||||
it('fails with invalid user', async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue(null)
|
||||
await t.test('fails with invalid user', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/signin',
|
||||
payload
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
it('fails with invalid email', async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue(null)
|
||||
await t.test('fails with invalid email', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/signin',
|
||||
@ -49,32 +71,40 @@ describe('POST /users/signin', () => {
|
||||
email: 'incorrect-email'
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
it("fails if user hasn't got a password", async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue({
|
||||
...userExample,
|
||||
password: null
|
||||
await t.test("fails if user hasn't got a password", async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: () => {
|
||||
return {
|
||||
...userExample,
|
||||
password: null
|
||||
}
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/signin',
|
||||
payload
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
it('fails with incorrect password', async () => {
|
||||
prismaMock.user.findUnique.mockResolvedValue(userExample)
|
||||
await t.test('fails with incorrect password', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findUnique: async () => {
|
||||
return userExample
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/signin',
|
||||
payload: {
|
||||
...payload,
|
||||
password: userExample.password
|
||||
password: 'incorrect-password'
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
})
|
||||
|
@ -1,13 +1,23 @@
|
||||
import { application } from '../../../../application.js'
|
||||
import { prismaMock } from '../../../../__test__/setup.js'
|
||||
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
|
||||
describe('DELETE /users/signout', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.refreshToken.deleteMany.mockResolvedValue({
|
||||
count: 1
|
||||
import { application } from '../../../../application.js'
|
||||
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
|
||||
await tap.test('DELETE /users/signout', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
const { accessToken, refreshTokenStubValue } = await authenticateUserTest()
|
||||
sinon.stub(prisma, 'refreshToken').value({
|
||||
...refreshTokenStubValue,
|
||||
deleteMany: async () => {
|
||||
return { count: 1 }
|
||||
}
|
||||
})
|
||||
const { accessToken } = await authenticateUserTest()
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: '/users/signout',
|
||||
@ -15,14 +25,14 @@ describe('DELETE /users/signout', () => {
|
||||
authorization: `Bearer ${accessToken}`
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(200)
|
||||
t.equal(response.statusCode, 200)
|
||||
})
|
||||
|
||||
it('fails with empty authorization header', async () => {
|
||||
await t.test('fails with empty authorized header', async (t) => {
|
||||
const response = await application.inject({
|
||||
method: 'DELETE',
|
||||
url: '/users/signout'
|
||||
})
|
||||
expect(response.statusCode).toEqual(401)
|
||||
t.equal(response.statusCode, 401)
|
||||
})
|
||||
})
|
||||
|
@ -1,25 +1,41 @@
|
||||
import { application } from '../../../../application.js'
|
||||
import { refreshTokenExample } from '../../../../models/RefreshToken.js'
|
||||
import { prismaMock } from '../../../../__test__/setup.js'
|
||||
import tap from 'tap'
|
||||
import sinon from 'sinon'
|
||||
|
||||
describe('POST /users/signout', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.refreshToken.findFirst.mockResolvedValue(refreshTokenExample)
|
||||
import { application } from '../../../../application.js'
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { refreshTokenExample } from '../../../../models/RefreshToken.js'
|
||||
|
||||
await tap.test('POST /users/signout', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
sinon.stub(prisma, 'refreshToken').value({
|
||||
findFirst: async () => {
|
||||
return refreshTokenExample
|
||||
},
|
||||
delete: async () => {}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/signout',
|
||||
payload: { refreshToken: refreshTokenExample.token }
|
||||
})
|
||||
expect(response.statusCode).toEqual(200)
|
||||
t.equal(response.statusCode, 200)
|
||||
})
|
||||
|
||||
it('fails with invalid refreshToken', async () => {
|
||||
prismaMock.refreshToken.findFirst.mockResolvedValue(null)
|
||||
await t.test('fails with invalid refreshToken', async (t) => {
|
||||
sinon.stub(prisma, 'refreshToken').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/signout',
|
||||
payload: { refreshToken: 'somerandomtoken' }
|
||||
})
|
||||
expect(response.statusCode).toEqual(404)
|
||||
t.equal(response.statusCode, 404)
|
||||
})
|
||||
})
|
||||
|
@ -1,7 +1,11 @@
|
||||
import tap from 'tap'
|
||||
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 { prismaMock } from '../../../../__test__/setup.js'
|
||||
import { emailTransporter } from '../../../../tools/email/emailTransporter.js'
|
||||
|
||||
const payload = {
|
||||
name: userExample.name,
|
||||
@ -11,26 +15,44 @@ const payload = {
|
||||
language: userSettingsExample.language
|
||||
}
|
||||
|
||||
describe('POST /users/signup', () => {
|
||||
it('succeeds', async () => {
|
||||
prismaMock.user.findFirst.mockResolvedValue(null)
|
||||
prismaMock.user.create.mockResolvedValue(userExample)
|
||||
prismaMock.userSetting.create.mockResolvedValue(userSettingsExample)
|
||||
await tap.test('POST /users/signup', async (t) => {
|
||||
t.afterEach(() => {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
await t.test('succeeds', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
},
|
||||
create: async () => {
|
||||
return userExample
|
||||
}
|
||||
})
|
||||
sinon.stub(prisma, 'userSetting').value({
|
||||
create: async () => {
|
||||
return userSettingsExample
|
||||
}
|
||||
})
|
||||
sinon.stub(emailTransporter, 'sendMail').value(() => {})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/signup',
|
||||
payload
|
||||
})
|
||||
const responseJson = response.json()
|
||||
expect(response.statusCode).toEqual(201)
|
||||
expect(responseJson.user.name).toEqual(userExample.name)
|
||||
expect(responseJson.user.email).toEqual(userExample.email)
|
||||
t.equal(response.statusCode, 201)
|
||||
t.equal(responseJson.user.name, userExample.name)
|
||||
t.equal(responseJson.user.email, userExample.email)
|
||||
})
|
||||
|
||||
it('fails with invalid email', async () => {
|
||||
prismaMock.user.findFirst.mockResolvedValue(null)
|
||||
prismaMock.user.create.mockResolvedValue(userExample)
|
||||
prismaMock.userSetting.create.mockResolvedValue(userSettingsExample)
|
||||
await t.test('fails with invalid email', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findFirst: async () => {
|
||||
return null
|
||||
}
|
||||
})
|
||||
sinon.stub(emailTransporter, 'sendMail').value(() => {})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/signup',
|
||||
@ -39,16 +61,21 @@ describe('POST /users/signup', () => {
|
||||
email: 'incorrect-email'
|
||||
}
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
|
||||
it('fails with already taken `name` or `email`', async () => {
|
||||
prismaMock.user.findFirst.mockResolvedValue(userExample)
|
||||
await t.test('fails with already taken `name` or `email`', async (t) => {
|
||||
sinon.stub(prisma, 'user').value({
|
||||
findFirst: async () => {
|
||||
return userExample
|
||||
}
|
||||
})
|
||||
sinon.stub(emailTransporter, 'sendMail').value(() => {})
|
||||
const response = await application.inject({
|
||||
method: 'POST',
|
||||
url: '/users/signup',
|
||||
payload
|
||||
})
|
||||
expect(response.statusCode).toEqual(400)
|
||||
t.equal(response.statusCode, 400)
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user