feat: usage of ESM modules imports (instead of CommonJS) (#5)
Replace `jest` with `tap`.
This commit is contained in:
@ -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)
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user