feat: usage of ESM modules imports (instead of CommonJS) (#5)

Replace `jest` with `tap`.
This commit is contained in:
Divlo 2022-03-20 11:49:27 +01:00 committed by GitHub
parent 91a0e2a76f
commit 19b6f96ecf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
70 changed files with 8017 additions and 6318 deletions

View File

@ -10,7 +10,7 @@ services:
command: 'sleep infinity'
thream-database:
image: 'postgres:14.0'
image: 'postgres:14.2'
environment:
POSTGRES_USER: 'user'
POSTGRES_PASSWORD: 'password'

View File

@ -3,6 +3,7 @@
.env
build
coverage
.nyc_output
node_modules
tmp
temp

View File

@ -5,14 +5,12 @@
"project": "./tsconfig.json"
},
"env": {
"node": true,
"jest": true
"node": true
},
"rules": {
"prettier/prettier": "error",
"import/extensions": ["error", "always"],
"unicorn/prevent-abbreviations": "error",
"@typescript-eslint/await-thenable": "off",
"@typescript-eslint/no-misused-promises": "off"
"unicorn/prefer-node-protocol": "error"
}
}

View File

@ -16,7 +16,7 @@ jobs:
language: ['javascript']
steps:
- uses: 'actions/checkout@v2.3.4'
- uses: 'actions/checkout@v3.0.0'
- name: 'Initialize CodeQL'
uses: 'github/codeql-action/init@v1'

View File

@ -10,10 +10,10 @@ jobs:
build:
runs-on: 'ubuntu-latest'
steps:
- uses: 'actions/checkout@v2'
- uses: 'actions/checkout@v3.0.0'
- name: 'Use Node.js'
uses: 'actions/setup-node@v2.4.1'
uses: 'actions/setup-node@v3.0.0'
with:
node-version: '16.x'
cache: 'npm'

View File

@ -10,10 +10,10 @@ jobs:
lint:
runs-on: 'ubuntu-latest'
steps:
- uses: 'actions/checkout@v2'
- uses: 'actions/checkout@v3.0.0'
- name: 'Use Node.js'
uses: 'actions/setup-node@v2.4.1'
uses: 'actions/setup-node@v3.0.0'
with:
node-version: '16.x'
cache: 'npm'

View File

@ -8,7 +8,7 @@ jobs:
release:
runs-on: 'ubuntu-latest'
steps:
- uses: 'actions/checkout@v2.3.4'
- uses: 'actions/checkout@v3.0.0'
with:
fetch-depth: 0
persist-credentials: false
@ -21,7 +21,7 @@ jobs:
git-commit-gpgsign: true
- name: 'Use Node.js'
uses: 'actions/setup-node@v2.4.1'
uses: 'actions/setup-node@v3.0.0'
with:
node-version: '16.x'
cache: 'npm'

View File

@ -8,12 +8,12 @@ on:
jobs:
test:
runs-on: 'ubuntu-latest'
runs-on: 'macos-latest'
steps:
- uses: 'actions/checkout@v2'
- uses: 'actions/checkout@v3.0.0'
- name: 'Use Node.js'
uses: 'actions/setup-node@v2.4.1'
uses: 'actions/setup-node@v3.0.0'
with:
node-version: '16.x'
cache: 'npm'
@ -21,5 +21,10 @@ jobs:
- name: 'Install'
run: 'npm install'
- name: 'Build'
run: 'npm run build'
- run: 'cp .env.example .env'
- name: 'Test'
run: 'npm run test'

1
.gitignore vendored
View File

@ -8,6 +8,7 @@ build
# testing
coverage
.nyc_output
# envs
.env

View File

@ -1,10 +1,6 @@
{
"*": ["editorconfig-checker"],
"*.{js,jsx,ts,tsx}": [
"prettier --write",
"eslint --fix",
"jest --findRelatedTests"
],
"*.{js,jsx,ts,tsx}": ["prettier --write", "eslint --fix"],
"*.{json,jsonc,yml,yaml}": ["prettier --write"],
"*.md": ["prettier --write", "markdownlint --dot --fix"]
}

2
.swcrc
View File

@ -13,7 +13,7 @@
"loose": true
},
"module": {
"type": "commonjs",
"type": "es6",
"strict": false,
"strictMode": true,
"lazy": false,

8
.taprc Normal file
View File

@ -0,0 +1,8 @@
ts: false
jsx: false
flow: false
check-coverage: false
coverage: false
files:
- 'build/**/*.test.js'

25
.vscode/settings.json vendored
View File

@ -2,34 +2,11 @@
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.preferences.importModuleSpecifierEnding": "js",
"prettier.configPath": ".prettierrc.json",
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true
},
"[markdown]": {
"editor.autoClosingBrackets": "always",
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[yaml]": {
"editor.autoClosingBrackets": "always",
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.autoClosingBrackets": "always",
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[jsonc]": {
"editor.autoClosingBrackets": "always",
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.autoClosingBrackets": "always",
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascript]": {
"editor.autoClosingBrackets": "always",
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[prisma]": {
"editor.defaultFormatter": "Prisma.prisma"
}

View File

@ -1,15 +1,15 @@
FROM node:16.14.0 AS dependencies
FROM node:16.14.2 AS dependencies
WORKDIR /usr/src/app
COPY ./package*.json ./
RUN npm install
FROM node:16.14.0 AS builder
FROM node:16.14.2 AS builder
WORKDIR /usr/src/app
COPY --from=dependencies /usr/src/app/node_modules ./node_modules
COPY ./ ./
RUN npm run prisma:generate && npm run build
FROM node:16.14.0 AS runner
FROM node:16.14.2 AS runner
WORKDIR /usr/src/app
ENV NODE_ENV=production
COPY --from=builder /usr/src/app/node_modules ./node_modules

View File

@ -16,7 +16,7 @@ services:
thream-database:
container_name: 'thream-database'
image: 'postgres:14.0'
image: 'postgres:14.2'
environment:
POSTGRES_USER: 'user'
POSTGRES_PASSWORD: 'password'

View File

@ -1,5 +1,5 @@
/** @type {import('node-plop').PlopGeneratorConfig} */
exports.serviceGenerator = {
export const serviceGenerator = {
description: 'REST API endpoint',
prompts: [
{

View File

@ -1,15 +1,26 @@
import tap from 'tap'
import sinon from 'sinon'
import { application } from 'application.js'
{{#if shouldBeAuthenticated}}
import { authenticateUserTest } from '__test__/utils/authenticateUserTest.js'
{{/if}}
import { prismaMock } from '__test__/setup.js'
import prisma from 'tools/database/prisma.js'
describe('{{httpMethod}} {{url}}', () => {
it('succeeds', async () => {
// prismaMock.service.findUnique.mockResolvedValue(null)
await tap.test('{{httpMethod}} {{url}}', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
{{#if shouldBeAuthenticated}}
const { accessToken, user } = await authenticateUserTest()
const { accessToken } = await authenticateUserTest()
{{/if}}
sinon.stub(prisma, 'channel').value({
findFirst: async () => {
return null
}
})
const response = await application.inject({
method: '{{httpMethod}}',
url: '{{url}}',
@ -21,6 +32,6 @@ describe('{{httpMethod}} {{url}}', () => {
payload: {}
})
// const responseJson = response.json()
expect(response.statusCode).toEqual(200)
t.equal(response.statusCode, 200)
})
})

View File

@ -1,13 +0,0 @@
{
"testEnvironment": "node",
"resolver": "jest-ts-webcompat-resolver",
"transform": {
"^.+\\.(t|j)sx?$": ["@swc/jest"]
},
"setupFiles": ["./__test__/setEnvironmentsVariables.ts"],
"setupFilesAfterEnv": ["./__test__/setup.ts"],
"rootDir": "./src",
"collectCoverage": true,
"coverageDirectory": "../coverage/",
"coverageReporters": ["text", "cobertura"]
}

11008
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,7 @@
"version": "0.0.1",
"description": "Thream's application programming interface to stay close with your friends and communities.",
"private": true,
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/Thream/api"
@ -13,8 +14,9 @@
},
"scripts": {
"build": "rimraf ./build && swc ./src --out-dir ./build && tsc",
"build:dev": "swc ./src --out-dir ./build --watch",
"start": "cross-env NODE_ENV=production node build/index.js",
"dev": "concurrently -k -n \"TypeScript,Node\" -p \"[{name}]\" -c \"blue,green\" \"swc ./src --out-dir ./build --watch\" \"cross-env NODE_ENV=development nodemon build/index.js\"",
"dev": "concurrently -k -n \"TypeScript,Node\" -p \"[{name}]\" -c \"blue,green\" \"npm run build:dev\" \"cross-env NODE_ENV=development nodemon build/index.js\"",
"generate": "plop",
"scripts:delete-dead-uploaded-files": "node build/scripts/delete-dead-uploaded-files.js",
"lint:commit": "commitlint",
@ -23,7 +25,7 @@
"lint:typescript": "eslint \"**/*.{js,jsx,ts,tsx}\" --ignore-path \".gitignore\"",
"lint:prettier": "prettier \".\" --check",
"lint:staged": "lint-staged",
"test": "jest",
"test": "tap",
"prisma:generate": "prisma generate",
"prisma:studio": "prisma studio",
"prisma:migrate:dev": "prisma migrate dev",
@ -32,69 +34,68 @@
"postinstall": "husky install"
},
"dependencies": {
"@prisma/client": "3.10.0",
"@prisma/client": "3.11.0",
"@sinclair/typebox": "0.23.4",
"@thream/socketio-jwt": "2.2.1",
"axios": "0.26.0",
"axios": "0.26.1",
"bcryptjs": "2.4.3",
"dotenv": "16.0.0",
"ejs": "3.1.6",
"fastify": "3.27.2",
"fastify": "3.27.4",
"fastify-cors": "6.0.3",
"fastify-helmet": "7.0.1",
"fastify-multipart": "5.3.1",
"fastify-plugin": "3.0.1",
"fastify-rate-limit": "5.7.2",
"fastify-rate-limit": "5.8.0",
"fastify-sensible": "3.1.2",
"fastify-static": "4.5.0",
"fastify-swagger": "4.17.0",
"fastify-static": "4.6.1",
"fastify-swagger": "5.0.0",
"fastify-url-data": "3.0.3",
"http-errors": "2.0.0",
"jsonwebtoken": "8.5.1",
"ms": "2.1.3",
"nodemailer": "6.7.2",
"read-pkg": "5.2.0",
"read-pkg": "7.1.0",
"socket.io": "4.4.1"
},
"devDependencies": {
"@commitlint/cli": "16.2.1",
"@commitlint/cli": "16.2.3",
"@commitlint/config-conventional": "16.2.1",
"@saithodev/semantic-release-backmerge": "2.1.2",
"@swc/cli": "0.1.55",
"@swc/core": "1.2.148",
"@swc/jest": "0.2.20",
"@swc/core": "1.2.159",
"@types/bcryptjs": "2.4.2",
"@types/busboy": "1.3.0",
"@types/ejs": "3.1.0",
"@types/http-errors": "1.8.2",
"@types/jest": "27.4.1",
"@types/jsonwebtoken": "8.5.8",
"@types/ms": "0.7.31",
"@types/node": "17.0.21",
"@types/nodemailer": "6.4.4",
"@typescript-eslint/eslint-plugin": "5.13.0",
"@types/sinon": "10.0.11",
"@types/tap": "15.0.6",
"@typescript-eslint/eslint-plugin": "5.15.0",
"concurrently": "7.0.0",
"cross-env": "7.0.3",
"editorconfig-checker": "4.0.2",
"eslint": "8.10.0",
"eslint": "8.11.0",
"eslint-config-conventions": "1.1.2",
"eslint-config-prettier": "8.5.0",
"eslint-config-conventions": "1.1.0",
"eslint-plugin-import": "2.25.4",
"eslint-plugin-prettier": "4.0.0",
"eslint-plugin-promise": "6.0.0",
"eslint-plugin-unicorn": "41.0.0",
"eslint-plugin-unicorn": "41.0.1",
"husky": "7.0.4",
"jest": "27.5.1",
"jest-mock-extended": "2.0.4",
"jest-ts-webcompat-resolver": "1.0.0",
"lint-staged": "12.3.5",
"lint-staged": "12.3.7",
"markdownlint-cli": "0.31.1",
"nodemon": "2.0.15",
"plop": "3.0.5",
"prettier": "2.5.1",
"prisma": "3.10.0",
"prettier": "2.6.0",
"prisma": "3.11.0",
"rimraf": "3.0.2",
"semantic-release": "19.0.2",
"sinon": "13.0.1",
"tap": "16.0.0",
"typescript": "4.6.2"
}
}

View File

@ -1,6 +1,6 @@
const { serviceGenerator } = require('./generators/service/index.js')
import { serviceGenerator } from './generators/service/index.js'
module.exports = (
export default (
/** @type {import('plop').NodePlopAPI} */
plop
) => {

View File

@ -1,3 +0,0 @@
process.env.JWT_ACCESS_EXPIRES_IN = '15 minutes'
process.env.JWT_ACCESS_SECRET = 'accessTokenSecret'
process.env.JWT_REFRESH_SECRET = 'refreshTokenSecret'

View File

@ -1,25 +0,0 @@
import { PrismaClient } from '@prisma/client'
import { mockDeep, mockReset, DeepMockProxy } from 'jest-mock-extended'
import prisma from '../tools/database/prisma.js'
jest.mock('nodemailer', () => {
return {
createTransport: () => {
return {
sendMail: jest.fn(async () => {})
}
}
}
})
jest.mock('../tools/database/prisma.js', () => ({
__esModule: true,
default: mockDeep<PrismaClient>()
}))
beforeEach(() => {
mockReset(prismaMock)
})
export const prismaMock = prisma as unknown as DeepMockProxy<PrismaClient>

View File

@ -1,4 +1,5 @@
import { User } from '@prisma/client'
import sinon from 'sinon'
import { refreshTokenExample } from '../../models/RefreshToken.js'
import { userExample, UserJWT } from '../../models/User.js'
@ -7,22 +8,54 @@ import {
generateAccessToken,
generateRefreshToken
} from '../../tools/utils/jwtToken.js'
import { prismaMock } from '../setup.js'
import prisma from '../../tools/database/prisma.js'
export const authenticateUserTest = async (): Promise<{
accessToken: string
refreshToken: string
user: User
userStubValue: any
userSettingStubValue: any
oAuthStubValue: any
refreshTokenStubValue: any
}> => {
prismaMock.user.findUnique.mockResolvedValue(userExample)
prismaMock.userSetting.findFirst.mockResolvedValue(userSettingsExample)
prismaMock.oAuth.findMany.mockResolvedValue([])
prismaMock.refreshToken.create.mockResolvedValue(refreshTokenExample)
const userStubValue = {
findUnique: async () => {
return userExample
}
}
const userSettingStubValue = {
findFirst: async () => {
return userSettingsExample
}
}
const oAuthStubValue = {
findMany: async () => {
return []
}
}
const refreshTokenStubValue = {
create: async () => {
return refreshTokenExample
}
}
sinon.stub(prisma, 'user').value(userStubValue)
sinon.stub(prisma, 'userSetting').value(userSettingStubValue)
sinon.stub(prisma, 'oAuth').value(oAuthStubValue)
sinon.stub(prisma, 'refreshToken').value(refreshTokenStubValue)
const userJWT: UserJWT = {
currentStrategy: 'local',
id: 1
}
const accessToken = generateAccessToken(userJWT)
const refreshToken = await generateRefreshToken(userJWT)
return { accessToken, refreshToken, user: userExample }
return {
accessToken,
refreshToken,
user: userExample,
userStubValue,
userSettingStubValue,
oAuthStubValue,
refreshTokenStubValue
}
}

View File

@ -20,32 +20,25 @@ export const application = fastify({
logger: process.env.NODE_ENV === 'development'
})
const main = async (): Promise<void> => {
await application.register(fastifyCors)
await application.register(fastifySensible)
await application.register(fastifyUrlData)
await application.register(fastifySocketIo, {
cors: {
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204
}
})
await application.register(fastifyHelmet)
await application.register(fastifyRateLimit, {
max: 150,
timeWindow: '1 minute'
})
await application.register(fastifyStatic, {
root: fileURLToPath(UPLOADS_URL),
prefix: '/uploads/'
})
await application.register(fastifySwagger, swaggerOptions)
await application.register(services)
}
main().catch((error) => {
console.error(error)
process.exit(1)
await application.register(fastifyCors)
await application.register(fastifySensible)
await application.register(fastifyUrlData)
await application.register(fastifySocketIo, {
cors: {
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204
}
})
await application.register(fastifyHelmet)
await application.register(fastifyRateLimit, {
max: 150,
timeWindow: '1 minute'
})
await application.register(fastifyStatic, {
root: fileURLToPath(UPLOADS_URL),
prefix: '/uploads/'
})
await application.register(fastifySwagger, swaggerOptions)
await application.register(services)

View File

@ -1,12 +1,5 @@
import { application } from './application.js'
import { HOST, PORT } from './tools/configurations/index.js'
const main = async (): Promise<void> => {
const address = await application.listen(PORT, HOST)
console.log('\u001B[36m%s\u001B[0m', `🚀 Server listening at ${address}`)
}
main().catch((error) => {
console.error(error)
process.exit(1)
})
const address = await application.listen(PORT, HOST)
console.log('\u001B[36m%s\u001B[0m', `🚀 Server listening at ${address}`)

View File

@ -34,25 +34,18 @@ const deleteDeadUploadedFiles = async (
}
}
const main = async (): Promise<void> => {
await deleteDeadUploadedFiles('guilds', async (icon: string) => {
return await prisma.guild.findFirst({
where: { icon }
})
await deleteDeadUploadedFiles('guilds', async (icon: string) => {
return await prisma.guild.findFirst({
where: { icon }
})
})
await deleteDeadUploadedFiles('messages', async (value: string) => {
return await prisma.message.findFirst({
where: { type: 'file', value }
})
})
await deleteDeadUploadedFiles('users', async (logo: string) => {
return await prisma.user.findFirst({
where: { logo }
})
await deleteDeadUploadedFiles('messages', async (value: string) => {
return await prisma.message.findFirst({
where: { type: 'file', value }
})
})
await deleteDeadUploadedFiles('users', async (logo: string) => {
return await prisma.user.findFirst({
where: { logo }
})
})
}
main().catch((error) => {
console.error(error)
process.exit(1)
})

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,4 @@
import { URL, pathToFileURL } from 'node:url'
import path from 'node:path'
import { URL } from 'node:url'
import dotenv from 'dotenv'
@ -14,16 +13,14 @@ export const JWT_REFRESH_SECRET =
export const JWT_ACCESS_EXPIRES_IN =
process.env.JWT_ACCESS_EXPIRES_IN ?? '15 minutes'
const importMetaURL = pathToFileURL(path.join(__dirname, 'app.js'))
export const SRC_URL = new URL('../../', importMetaURL)
export const SRC_URL = new URL('../../', import.meta.url)
export const ROOT_URL = new URL('../', SRC_URL)
export const EMAIL_URL = new URL('./email/', ROOT_URL)
export const EMAIL_TEMPLATE_URL = new URL('./email-template.ejs', EMAIL_URL)
export const EMAIL_LOCALES_URL = new URL('./locales/', EMAIL_URL)
export const UPLOADS_URL = new URL('./uploads/', ROOT_URL)
export const supportedImageMimetype = [
export const SUPPORTED_IMAGE_MIMETYPE = [
'image/png',
'image/jpg',
'image/jpeg',
@ -31,5 +28,7 @@ export const supportedImageMimetype = [
]
/** in megabytes */
export const maximumImageSize = 10
export const maximumFileSize = 100
export const MAXIMUM_IMAGE_SIZE = 10
/** in megabytes */
export const MAXIMUM_FILE_SIZE = 100

View File

@ -1,10 +1,10 @@
import dotenv from 'dotenv'
import readPackageJSON from 'read-pkg'
import { readPackage } from 'read-pkg'
import { FastifyDynamicSwaggerOptions } from 'fastify-swagger'
dotenv.config()
const packageJSON = readPackageJSON.sync()
const packageJSON = await readPackage()
export const swaggerOptions: FastifyDynamicSwaggerOptions = {
routePrefix: '/documentation',

View File

@ -1,4 +1,4 @@
import * as Prisma from '@prisma/client'
import Prisma from '@prisma/client'
const { PrismaClient } = Prisma

View File

@ -1,77 +1,98 @@
import tap from 'tap'
import sinon from 'sinon'
import httpErrors from 'http-errors'
import jwt from 'jsonwebtoken'
import { userExample } from '../../../models/User.js'
import { prismaMock } from '../../../__test__/setup.js'
import { getUserWithBearerToken } from '../authenticateUser.js'
import prisma from '../../database/prisma.js'
import { userExample } from '../../../models/User.js'
const { Unauthorized, Forbidden, BadRequest } = httpErrors
describe('tools/plugins/authenticateUser - getUserWithBearerToken', () => {
afterEach(() => {
jest.clearAllMocks()
})
it('shoulds succeeds with the right information', async () => {
prismaMock.user.findUnique.mockResolvedValue(userExample)
const currentStrategy = 'local'
jwt.verify = jest.fn<any, any[]>((() => {
return { id: userExample.id, currentStrategy }
}) as any)
const userWithBearerToken = await getUserWithBearerToken('Bearer token')
expect(userWithBearerToken.current.id).toEqual(userExample.id)
expect(userWithBearerToken.current.name).toEqual(userExample.name)
expect(userWithBearerToken.accessToken).toEqual('token')
expect(userWithBearerToken.currentStrategy).toEqual(currentStrategy)
})
it('shoulds throws `Unauthorized` if `bearerToken` is not a string', async () => {
await expect(
async () => await getUserWithBearerToken(undefined)
).rejects.toThrow(Unauthorized)
})
it('shoulds throws `Unauthorized` if `bearerToken` is not to the right format: `"Bearer token"`', async () => {
await expect(
async () => await getUserWithBearerToken('Bearer')
).rejects.toThrow(Unauthorized)
await expect(async () => await getUserWithBearerToken('')).rejects.toThrow(
Unauthorized
)
await expect(
async () => await getUserWithBearerToken('Bearer token token2')
).rejects.toThrow(Unauthorized)
})
it('shoulds throws `Forbidden` if invalid `bearerToken` by `jwt.verify`', async () => {
jwt.verify = jest.fn<any, any[]>((() => {
throw new Error('Invalid token')
}) as any)
await expect(
async () => await getUserWithBearerToken('Bearer token')
).rejects.toThrow(Forbidden)
})
it("shoulds throws `Forbidden` if the user doesn't exist", async () => {
prismaMock.user.findUnique.mockResolvedValue(null)
jwt.verify = jest.fn<any, any[]>((() => {
return { id: userExample.id }
}) as any)
await expect(
async () => await getUserWithBearerToken('Bearer token')
).rejects.toThrow(Forbidden)
})
it('shoulds throws `BadRequest` if the user account is not confirmed', async () => {
prismaMock.user.findUnique.mockResolvedValue({
...userExample,
isConfirmed: false
await tap.test(
'tools/plugins/authenticateUser - getUserWithBearerToken',
async (t) => {
t.afterEach(() => {
sinon.restore()
})
jwt.verify = jest.fn<any, any[]>((() => {
return { id: userExample.id, currentStrategy: 'local' }
}) as any)
await expect(
async () => await getUserWithBearerToken('Bearer token')
).rejects.toThrow(BadRequest)
})
})
await t.test('shoulds succeeds with the right information', async (t) => {
sinon.stub(prisma, 'user').value({
findUnique: async () => {
return userExample
}
})
const currentStrategy = 'local'
sinon.stub(jwt, 'verify').value(() => {
return { id: userExample.id, currentStrategy }
})
const userWithBearerToken = await getUserWithBearerToken('Bearer token')
t.equal(userWithBearerToken.current.id, userExample.id)
t.equal(userWithBearerToken.current.name, userExample.name)
t.equal(userWithBearerToken.accessToken, 'token')
t.equal(userWithBearerToken.currentStrategy, currentStrategy)
})
await t.test(
'shoulds throws `Unauthorized` if `bearerToken` is not a string',
async (t) => {
await t.rejects(getUserWithBearerToken(undefined), Unauthorized)
}
)
await t.test(
'shoulds throws `Unauthorized` if `bearerToken` is not to the right format: `"Bearer token"`',
async (t) => {
await t.rejects(getUserWithBearerToken('Bearer'), Unauthorized)
await t.rejects(getUserWithBearerToken(''), Unauthorized)
await t.rejects(
getUserWithBearerToken('Bearer token token2'),
Unauthorized
)
}
)
await t.test(
'shoulds throws `Forbidden` if invalid `bearerToken` by `jwt.verify`',
async (t) => {
sinon.stub(jwt, 'verify').value(() => {
throw new Error('Invalid token')
})
await t.rejects(getUserWithBearerToken('Bearer token'), Forbidden)
}
)
await t.test(
"shoulds throws `Forbidden` if the user doesn't exist",
async (t) => {
sinon.stub(prisma, 'user').value({
findUnique: async () => {
return null
}
})
sinon.stub(jwt, 'verify').value(() => {
return { id: userExample.id }
})
await t.rejects(getUserWithBearerToken('Bearer token'), Forbidden)
}
)
await t.test(
'shoulds throws `BadRequest` if the user account is not confirmed',
async (t) => {
sinon.stub(prisma, 'user').value({
findUnique: async () => {
return {
...userExample,
isConfirmed: false
}
}
})
sinon.stub(jwt, 'verify').value(() => {
return { id: userExample.id, currentStrategy: 'local' }
})
await t.rejects(getUserWithBearerToken('Bearer token'), BadRequest)
}
)
}
)

View File

@ -1,14 +1,15 @@
import tap from 'tap'
import fastify from 'fastify'
import fastifySocketIo from '../socket-io.js'
describe('tools/plugins/socket-io', () => {
it('should close socket server on fastify close', async () => {
await tap.test('tools/plugins/socket-io', async (t) => {
await t.test('should close socket server on fastify close', async (t) => {
const PORT = 3030
const application = fastify()
await application.register(fastifySocketIo)
await application.listen(PORT)
expect(application.io).not.toBeNull()
t.not(application.io, null)
await application.close()
})
})

View File

@ -1,137 +1,216 @@
import tap from 'tap'
import sinon from 'sinon'
import { userExample } from '../../../models/User.js'
import { userSettingsExample } from '../../../models/UserSettings.js'
import { prismaMock } from '../../../__test__/setup.js'
import { OAuthStrategy } from '../OAuthStrategy.js'
import prisma from '../../database/prisma.js'
import { refreshTokenExample } from '../../../models/RefreshToken.js'
const oauthStrategy = new OAuthStrategy('discord')
describe('/tools/utils/OAuthStrategy - callbackSignin', () => {
it('should signup the user', async () => {
const name = 'Martin'
const id = '12345'
prismaMock.oAuth.findFirst.mockResolvedValue(null)
prismaMock.user.count.mockResolvedValue(0)
prismaMock.user.create.mockResolvedValue({
...userExample,
name
await tap.test('tools/utils/OAuthStrategy', async (t) => {
await t.test('callbackSignin', async (t) => {
t.afterEach(() => {
sinon.restore()
})
prismaMock.userSetting.create.mockResolvedValue(userSettingsExample)
prismaMock.oAuth.create.mockResolvedValue({
id: 1,
userId: userExample.id,
provider: 'discord',
providerId: id,
updatedAt: new Date(),
createdAt: new Date()
await t.test('should signup the user', async (t) => {
const name = 'Martin'
const id = '12345'
sinon.stub(prisma, 'user').value({
count: async () => {
return 0
},
create: async () => {
return {
...userExample,
name
}
}
})
sinon.stub(prisma, 'refreshToken').value({
create: async () => {
return refreshTokenExample
}
})
sinon.stub(prisma, 'userSetting').value({
create: async () => {
return userSettingsExample
}
})
sinon.stub(prisma, 'oAuth').value({
findFirst: async () => {
return null
},
create: async () => {
return {
id: 1,
userId: userExample.id,
provider: 'discord',
providerId: id,
updatedAt: new Date(),
createdAt: new Date()
}
}
})
const oAuthCreateSpy = sinon.spy(prisma.oAuth, 'create')
const oAuthFindFirstSpy = sinon.spy(prisma.oAuth, 'findFirst')
const userCountSpy = sinon.spy(prisma.user, 'count')
const userCreateSpy = sinon.spy(prisma.user, 'create')
const userSettingCreateSpy = sinon.spy(prisma.userSetting, 'create')
await oauthStrategy.callbackSignin({ id, name })
t.equal(
oAuthCreateSpy.calledWith({
data: {
userId: userExample.id,
provider: 'discord',
providerId: id
}
}),
true
)
t.equal(
oAuthFindFirstSpy.calledWith({
where: {
provider: 'discord',
providerId: id
}
}),
true
)
t.equal(userCountSpy.calledWith({ where: { name } }), true)
t.equal(userCreateSpy.calledWith({ data: { name } }), true)
t.equal(
userSettingCreateSpy.calledWith({
data: {
userId: userExample.id
}
}),
true
)
})
await oauthStrategy.callbackSignin({ id, name })
expect(prismaMock.oAuth.findFirst).toHaveBeenCalledWith({
where: {
provider: 'discord',
providerId: id
})
await t.test('callbackAddStrategy', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('should add the strategy to the user', async (t) => {
const name = userExample.name
const id = '12345'
sinon.stub(prisma, 'oAuth').value({
findFirst: async () => {
return null
},
create: async () => {
return {
id: 1,
userId: userExample.id,
provider: 'discord',
providerId: id,
updatedAt: new Date(),
createdAt: new Date()
}
}
})
const oAuthCreateSpy = sinon.spy(prisma.oAuth, 'create')
const oAuthFindFirstSpy = sinon.spy(prisma.oAuth, 'findFirst')
const result = await oauthStrategy.callbackAddStrategy(
{ id, name },
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
)
t.equal(result, 'success')
t.equal(
oAuthCreateSpy.calledWith({
data: {
userId: userExample.id,
provider: 'discord',
providerId: id
}
}),
true
)
t.equal(
oAuthFindFirstSpy.calledWith({
where: {
provider: 'discord',
providerId: id
}
}),
true
)
})
await t.test(
'should not add the strategy if the account of the provider is already used',
async (t) => {
const name = userExample.name
const id = '12345'
sinon.stub(prisma, 'oAuth').value({
findFirst: async () => {
return {
id: 1,
userId: 2,
provider: 'discord',
providerId: id,
updatedAt: new Date(),
createdAt: new Date()
}
}
})
const oAuthFindFirstSpy = sinon.spy(prisma.oAuth, 'findFirst')
const result = await oauthStrategy.callbackAddStrategy(
{ id, name },
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
)
t.equal(result, 'This account is already used by someone else')
t.equal(
oAuthFindFirstSpy.calledWith({
where: {
provider: 'discord',
providerId: id
}
}),
true
)
}
})
expect(prismaMock.user.count).toHaveBeenCalledWith({
where: { name }
})
expect(prismaMock.user.create).toHaveBeenCalledWith({
data: { name }
})
expect(prismaMock.userSetting.create).toHaveBeenCalledWith({
data: {
userId: userExample.id
)
await t.test(
'should not add the strategy if the user is already connected with it',
async (t) => {
const name = userExample.name
const id = '12345'
sinon.stub(prisma, 'oAuth').value({
findFirst: async () => {
return {
id: 1,
userId: userExample.id,
provider: 'discord',
providerId: id,
updatedAt: new Date(),
createdAt: new Date()
}
}
})
const oAuthFindFirstSpy = sinon.spy(prisma.oAuth, 'findFirst')
const result = await oauthStrategy.callbackAddStrategy(
{ id, name },
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
)
t.equal(result, 'You are already using this account')
t.equal(
oAuthFindFirstSpy.calledWith({
where: {
provider: 'discord',
providerId: id
}
}),
true
)
}
})
expect(prismaMock.oAuth.create).toHaveBeenCalledWith({
data: {
userId: userExample.id,
provider: 'discord',
providerId: id
}
})
})
})
describe('/tools/utils/OAuthStrategy - callbackAddStrategy', () => {
it('should add the strategy to the user', async () => {
const name = userExample.name
const id = '12345'
prismaMock.oAuth.findFirst.mockResolvedValue(null)
prismaMock.oAuth.create.mockResolvedValue({
id: 1,
userId: userExample.id,
provider: 'discord',
providerId: id,
updatedAt: new Date(),
createdAt: new Date()
})
const result = await oauthStrategy.callbackAddStrategy(
{ id, name },
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
)
expect(prismaMock.oAuth.findFirst).toHaveBeenCalledWith({
where: {
provider: 'discord',
providerId: id
}
})
expect(prismaMock.oAuth.create).toHaveBeenCalledWith({
data: {
userId: userExample.id,
provider: 'discord',
providerId: id
}
})
expect(result).toEqual('success')
})
it('should not add the strategy if the account of the provider is already used', async () => {
const name = userExample.name
const id = '12345'
prismaMock.oAuth.findFirst.mockResolvedValue({
id: 1,
userId: 2,
provider: 'discord',
providerId: id,
updatedAt: new Date(),
createdAt: new Date()
})
const result = await oauthStrategy.callbackAddStrategy(
{ id, name },
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
)
expect(prismaMock.oAuth.findFirst).toHaveBeenCalledWith({
where: {
provider: 'discord',
providerId: id
}
})
expect(prismaMock.oAuth.create).not.toHaveBeenCalled()
expect(result).toEqual('This account is already used by someone else')
})
it('should not add the strategy if the user is already connected with it', async () => {
const name = userExample.name
const id = '12345'
prismaMock.oAuth.findFirst.mockResolvedValue({
id: 1,
userId: userExample.id,
provider: 'discord',
providerId: id,
updatedAt: new Date(),
createdAt: new Date()
})
const result = await oauthStrategy.callbackAddStrategy(
{ id, name },
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
)
expect(prismaMock.oAuth.findFirst).toHaveBeenCalledWith({
where: {
provider: 'discord',
providerId: id
}
})
expect(prismaMock.oAuth.create).not.toHaveBeenCalled()
expect(result).toEqual('You are already using this account')
)
})
})

View File

@ -1,20 +1,25 @@
import tap from 'tap'
import { buildQueryURL } from '../buildQueryURL.js'
test('/tools/utils/buildQueryUrl', () => {
expect(
await tap.test('tools/utils/buildQueryUrl', async (t) => {
t.equal(
buildQueryURL('http://localhost:8080', {
test: 'query'
})
).toEqual('http://localhost:8080/?test=query')
expect(
}),
'http://localhost:8080/?test=query'
)
t.equal(
buildQueryURL('http://localhost:8080/', {
test: 'query'
})
).toEqual('http://localhost:8080/?test=query')
expect(
}),
'http://localhost:8080/?test=query'
)
t.equal(
buildQueryURL('http://localhost:3000', {
test: 'query',
code: 'abc'
})
).toEqual('http://localhost:3000/?test=query&code=abc')
}),
'http://localhost:3000/?test=query&code=abc'
)
})

View File

@ -1,22 +1,27 @@
import tap from 'tap'
import { parseStringNullish } from '../parseStringNullish.js'
const defaultString = 'defaultString'
describe('/tools/utils/parseStringNullish', () => {
it('returns `defaultString` if `string === undefined`', () => {
expect(parseStringNullish(defaultString, undefined)).toEqual(defaultString)
await tap.test('tools/utils/parseStringNullish', async (t) => {
await t.test(
'returns `defaultString` if `string === undefined`',
async (t) => {
t.equal(parseStringNullish(defaultString, undefined), defaultString)
}
)
await t.test('returns `null` if `string === null`', async (t) => {
t.equal(parseStringNullish(defaultString, null), null)
})
it('returns `null` if `string === null`', () => {
expect(parseStringNullish(defaultString, null)).toEqual(null)
await t.test('returns `null` if `string.length === 0`', async (t) => {
t.equal(parseStringNullish(defaultString, ''), null)
})
it('returns `null` if `string.length === 0`', () => {
expect(parseStringNullish(defaultString, '')).toEqual(null)
})
it('returns `string` if `string.length > 0`', () => {
await t.test('returns `string` if `string.length > 0`', async (t) => {
const string = 'myString'
expect(parseStringNullish(defaultString, string)).toEqual(string)
t.equal(parseStringNullish(defaultString, string), string)
})
})

View File

@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "commonjs",
"module": "ESNext",
"lib": ["ESNext"],
"moduleResolution": "node",
"outDir": "./build",