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' command: 'sleep infinity'
thream-database: thream-database:
image: 'postgres:14.0' image: 'postgres:14.2'
environment: environment:
POSTGRES_USER: 'user' POSTGRES_USER: 'user'
POSTGRES_PASSWORD: 'password' POSTGRES_PASSWORD: 'password'

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

1
.gitignore vendored
View File

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

View File

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

2
.swcrc
View File

@ -13,7 +13,7 @@
"loose": true "loose": true
}, },
"module": { "module": {
"type": "commonjs", "type": "es6",
"strict": false, "strict": false,
"strictMode": true, "strictMode": true,
"lazy": false, "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.tsdk": "node_modules/typescript/lib",
"typescript.preferences.importModuleSpecifierEnding": "js", "typescript.preferences.importModuleSpecifierEnding": "js",
"prettier.configPath": ".prettierrc.json", "prettier.configPath": ".prettierrc.json",
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true, "editor.formatOnSave": true,
"editor.codeActionsOnSave": { "editor.codeActionsOnSave": {
"source.fixAll": true "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]": { "[prisma]": {
"editor.defaultFormatter": "Prisma.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 WORKDIR /usr/src/app
COPY ./package*.json ./ COPY ./package*.json ./
RUN npm install RUN npm install
FROM node:16.14.0 AS builder FROM node:16.14.2 AS builder
WORKDIR /usr/src/app WORKDIR /usr/src/app
COPY --from=dependencies /usr/src/app/node_modules ./node_modules COPY --from=dependencies /usr/src/app/node_modules ./node_modules
COPY ./ ./ COPY ./ ./
RUN npm run prisma:generate && npm run build 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 WORKDIR /usr/src/app
ENV NODE_ENV=production ENV NODE_ENV=production
COPY --from=builder /usr/src/app/node_modules ./node_modules COPY --from=builder /usr/src/app/node_modules ./node_modules

View File

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

View File

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

View File

@ -1,15 +1,26 @@
import tap from 'tap'
import sinon from 'sinon'
import { application } from 'application.js' import { application } from 'application.js'
{{#if shouldBeAuthenticated}} {{#if shouldBeAuthenticated}}
import { authenticateUserTest } from '__test__/utils/authenticateUserTest.js' import { authenticateUserTest } from '__test__/utils/authenticateUserTest.js'
{{/if}} {{/if}}
import { prismaMock } from '__test__/setup.js' import prisma from 'tools/database/prisma.js'
describe('{{httpMethod}} {{url}}', () => { await tap.test('{{httpMethod}} {{url}}', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
// prismaMock.service.findUnique.mockResolvedValue(null) sinon.restore()
})
await t.test('succeeds', async (t) => {
{{#if shouldBeAuthenticated}} {{#if shouldBeAuthenticated}}
const { accessToken, user } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
{{/if}} {{/if}}
sinon.stub(prisma, 'channel').value({
findFirst: async () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: '{{httpMethod}}', method: '{{httpMethod}}',
url: '{{url}}', url: '{{url}}',
@ -21,6 +32,6 @@ describe('{{httpMethod}} {{url}}', () => {
payload: {} payload: {}
}) })
// const responseJson = response.json() // 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", "version": "0.0.1",
"description": "Thream's application programming interface to stay close with your friends and communities.", "description": "Thream's application programming interface to stay close with your friends and communities.",
"private": true, "private": true,
"type": "module",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/Thream/api" "url": "https://github.com/Thream/api"
@ -13,8 +14,9 @@
}, },
"scripts": { "scripts": {
"build": "rimraf ./build && swc ./src --out-dir ./build && tsc", "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", "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", "generate": "plop",
"scripts:delete-dead-uploaded-files": "node build/scripts/delete-dead-uploaded-files.js", "scripts:delete-dead-uploaded-files": "node build/scripts/delete-dead-uploaded-files.js",
"lint:commit": "commitlint", "lint:commit": "commitlint",
@ -23,7 +25,7 @@
"lint:typescript": "eslint \"**/*.{js,jsx,ts,tsx}\" --ignore-path \".gitignore\"", "lint:typescript": "eslint \"**/*.{js,jsx,ts,tsx}\" --ignore-path \".gitignore\"",
"lint:prettier": "prettier \".\" --check", "lint:prettier": "prettier \".\" --check",
"lint:staged": "lint-staged", "lint:staged": "lint-staged",
"test": "jest", "test": "tap",
"prisma:generate": "prisma generate", "prisma:generate": "prisma generate",
"prisma:studio": "prisma studio", "prisma:studio": "prisma studio",
"prisma:migrate:dev": "prisma migrate dev", "prisma:migrate:dev": "prisma migrate dev",
@ -32,69 +34,68 @@
"postinstall": "husky install" "postinstall": "husky install"
}, },
"dependencies": { "dependencies": {
"@prisma/client": "3.10.0", "@prisma/client": "3.11.0",
"@sinclair/typebox": "0.23.4", "@sinclair/typebox": "0.23.4",
"@thream/socketio-jwt": "2.2.1", "@thream/socketio-jwt": "2.2.1",
"axios": "0.26.0", "axios": "0.26.1",
"bcryptjs": "2.4.3", "bcryptjs": "2.4.3",
"dotenv": "16.0.0", "dotenv": "16.0.0",
"ejs": "3.1.6", "ejs": "3.1.6",
"fastify": "3.27.2", "fastify": "3.27.4",
"fastify-cors": "6.0.3", "fastify-cors": "6.0.3",
"fastify-helmet": "7.0.1", "fastify-helmet": "7.0.1",
"fastify-multipart": "5.3.1", "fastify-multipart": "5.3.1",
"fastify-plugin": "3.0.1", "fastify-plugin": "3.0.1",
"fastify-rate-limit": "5.7.2", "fastify-rate-limit": "5.8.0",
"fastify-sensible": "3.1.2", "fastify-sensible": "3.1.2",
"fastify-static": "4.5.0", "fastify-static": "4.6.1",
"fastify-swagger": "4.17.0", "fastify-swagger": "5.0.0",
"fastify-url-data": "3.0.3", "fastify-url-data": "3.0.3",
"http-errors": "2.0.0", "http-errors": "2.0.0",
"jsonwebtoken": "8.5.1", "jsonwebtoken": "8.5.1",
"ms": "2.1.3", "ms": "2.1.3",
"nodemailer": "6.7.2", "nodemailer": "6.7.2",
"read-pkg": "5.2.0", "read-pkg": "7.1.0",
"socket.io": "4.4.1" "socket.io": "4.4.1"
}, },
"devDependencies": { "devDependencies": {
"@commitlint/cli": "16.2.1", "@commitlint/cli": "16.2.3",
"@commitlint/config-conventional": "16.2.1", "@commitlint/config-conventional": "16.2.1",
"@saithodev/semantic-release-backmerge": "2.1.2", "@saithodev/semantic-release-backmerge": "2.1.2",
"@swc/cli": "0.1.55", "@swc/cli": "0.1.55",
"@swc/core": "1.2.148", "@swc/core": "1.2.159",
"@swc/jest": "0.2.20",
"@types/bcryptjs": "2.4.2", "@types/bcryptjs": "2.4.2",
"@types/busboy": "1.3.0", "@types/busboy": "1.3.0",
"@types/ejs": "3.1.0", "@types/ejs": "3.1.0",
"@types/http-errors": "1.8.2", "@types/http-errors": "1.8.2",
"@types/jest": "27.4.1",
"@types/jsonwebtoken": "8.5.8", "@types/jsonwebtoken": "8.5.8",
"@types/ms": "0.7.31", "@types/ms": "0.7.31",
"@types/node": "17.0.21", "@types/node": "17.0.21",
"@types/nodemailer": "6.4.4", "@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", "concurrently": "7.0.0",
"cross-env": "7.0.3", "cross-env": "7.0.3",
"editorconfig-checker": "4.0.2", "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-prettier": "8.5.0",
"eslint-config-conventions": "1.1.0",
"eslint-plugin-import": "2.25.4", "eslint-plugin-import": "2.25.4",
"eslint-plugin-prettier": "4.0.0", "eslint-plugin-prettier": "4.0.0",
"eslint-plugin-promise": "6.0.0", "eslint-plugin-promise": "6.0.0",
"eslint-plugin-unicorn": "41.0.0", "eslint-plugin-unicorn": "41.0.1",
"husky": "7.0.4", "husky": "7.0.4",
"jest": "27.5.1", "lint-staged": "12.3.7",
"jest-mock-extended": "2.0.4",
"jest-ts-webcompat-resolver": "1.0.0",
"lint-staged": "12.3.5",
"markdownlint-cli": "0.31.1", "markdownlint-cli": "0.31.1",
"nodemon": "2.0.15", "nodemon": "2.0.15",
"plop": "3.0.5", "plop": "3.0.5",
"prettier": "2.5.1", "prettier": "2.6.0",
"prisma": "3.10.0", "prisma": "3.11.0",
"rimraf": "3.0.2", "rimraf": "3.0.2",
"semantic-release": "19.0.2", "semantic-release": "19.0.2",
"sinon": "13.0.1",
"tap": "16.0.0",
"typescript": "4.6.2" "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} */ /** @type {import('plop').NodePlopAPI} */
plop 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 { User } from '@prisma/client'
import sinon from 'sinon'
import { refreshTokenExample } from '../../models/RefreshToken.js' import { refreshTokenExample } from '../../models/RefreshToken.js'
import { userExample, UserJWT } from '../../models/User.js' import { userExample, UserJWT } from '../../models/User.js'
@ -7,22 +8,54 @@ import {
generateAccessToken, generateAccessToken,
generateRefreshToken generateRefreshToken
} from '../../tools/utils/jwtToken.js' } from '../../tools/utils/jwtToken.js'
import { prismaMock } from '../setup.js' import prisma from '../../tools/database/prisma.js'
export const authenticateUserTest = async (): Promise<{ export const authenticateUserTest = async (): Promise<{
accessToken: string accessToken: string
refreshToken: string refreshToken: string
user: User user: User
userStubValue: any
userSettingStubValue: any
oAuthStubValue: any
refreshTokenStubValue: any
}> => { }> => {
prismaMock.user.findUnique.mockResolvedValue(userExample) const userStubValue = {
prismaMock.userSetting.findFirst.mockResolvedValue(userSettingsExample) findUnique: async () => {
prismaMock.oAuth.findMany.mockResolvedValue([]) return userExample
prismaMock.refreshToken.create.mockResolvedValue(refreshTokenExample) }
}
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 = { const userJWT: UserJWT = {
currentStrategy: 'local', currentStrategy: 'local',
id: 1 id: 1
} }
const accessToken = generateAccessToken(userJWT) const accessToken = generateAccessToken(userJWT)
const refreshToken = await generateRefreshToken(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' logger: process.env.NODE_ENV === 'development'
}) })
const main = async (): Promise<void> => { await application.register(fastifyCors)
await application.register(fastifyCors) await application.register(fastifySensible)
await application.register(fastifySensible) await application.register(fastifyUrlData)
await application.register(fastifyUrlData) await application.register(fastifySocketIo, {
await application.register(fastifySocketIo, { cors: {
cors: { origin: '*',
origin: '*', methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', preflightContinue: false,
preflightContinue: false, optionsSuccessStatus: 204
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(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 { application } from './application.js'
import { HOST, PORT } from './tools/configurations/index.js' import { HOST, PORT } from './tools/configurations/index.js'
const main = async (): Promise<void> => { const address = await application.listen(PORT, HOST)
const address = await application.listen(PORT, HOST) console.log('\u001B[36m%s\u001B[0m', `🚀 Server listening at ${address}`)
console.log('\u001B[36m%s\u001B[0m', `🚀 Server listening at ${address}`)
}
main().catch((error) => {
console.error(error)
process.exit(1)
})

View File

@ -34,25 +34,18 @@ const deleteDeadUploadedFiles = async (
} }
} }
const main = async (): Promise<void> => { await deleteDeadUploadedFiles('guilds', async (icon: string) => {
await deleteDeadUploadedFiles('guilds', async (icon: string) => { return await prisma.guild.findFirst({
return await prisma.guild.findFirst({ where: { icon }
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 { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.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 { channelExample } from '../../../../models/Channel.js'
import { memberExample } from '../../../../models/Member.js' import { memberExample } from '../../../../models/Member.js'
describe('DELETE /channels/[channelId]', () => { await tap.test('DELETE /channels/[channelId]', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
const defaultChannelId = 5 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() 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({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/channels/${channelExample.id}`, url: `/channels/${channelExample.id}`,
@ -24,18 +45,28 @@ describe('DELETE /channels/[channelId]', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.id).toEqual(channelExample.id) t.equal(responseJson.id, channelExample.id)
expect(responseJson.name).toEqual(channelExample.name) t.equal(responseJson.name, channelExample.name)
expect(responseJson.guildId).toEqual(channelExample.guildId) t.equal(responseJson.guildId, channelExample.guildId)
expect(responseJson.defaultChannelId).toEqual(defaultChannelId) t.equal(responseJson.defaultChannelId, defaultChannelId)
}) })
it('fails if there is only one channel', async () => { await t.test('fails if there is only one channel', async (t) => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(memberExample)
prismaMock.channel.count.mockResolvedValue(1)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/channels/${channelExample.id}`, url: `/channels/${channelExample.id}`,
@ -43,12 +74,16 @@ describe('DELETE /channels/[channelId]', () => {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
} }
}) })
expect(response.statusCode).toEqual(400) t.equal(response.statusCode, 400)
}) })
it('fails if the channel is not found', async () => { await t.test('fails if the channel is not found', async (t) => {
prismaMock.channel.findUnique.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'channel').value({
findUnique: async () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/channels/${channelExample.id}`, url: `/channels/${channelExample.id}`,
@ -56,13 +91,21 @@ describe('DELETE /channels/[channelId]', () => {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
} }
}) })
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
}) })
it('fails if the member is not found', async () => { await t.test('fails if the member is not found', async (t) => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/channels/${channelExample.id}`, url: `/channels/${channelExample.id}`,
@ -70,17 +113,24 @@ describe('DELETE /channels/[channelId]', () => {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
} }
}) })
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
}) })
it('fails if the member is not owner', async () => { await t.test('fails if the member is not owner', async (t) => {
const member = {
...memberExample,
isOwner: false
}
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(member)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/channels/${channelExample.id}`, url: `/channels/${channelExample.id}`,
@ -88,6 +138,6 @@ describe('DELETE /channels/[channelId]', () => {
authorization: `Bearer ${accessToken}` 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 { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.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 { channelExample } from '../../../../models/Channel.js' import { channelExample } from '../../../../models/Channel.js'
import { memberExample } from '../../../../models/Member.js'
describe('GET /channels/[channelId]', () => { await tap.test('GET /channels/[channelId]', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample) sinon.restore()
prismaMock.member.findFirst.mockResolvedValue(memberExample) })
await t.test('succeeds', async (t) => {
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: `/channels/${channelExample.id}`, url: `/channels/${channelExample.id}`,
@ -17,31 +32,24 @@ describe('GET /channels/[channelId]', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.channel.id).toEqual(channelExample.id) t.equal(responseJson.channel.id, channelExample.id)
expect(responseJson.channel.name).toEqual(channelExample.name) t.equal(responseJson.channel.name, channelExample.name)
expect(responseJson.channel.guildId).toEqual(channelExample.guildId) t.equal(responseJson.channel.guildId, channelExample.guildId)
}) })
it('fails with not found member', async () => { await t.test('fails with not found member', async (t) => {
prismaMock.channel.findUnique.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
const response = await application.inject({ sinon.stub(prisma, 'channel').value({
method: 'GET', findUnique: async () => {
url: '/channels/1', return channelExample
headers: { }
authorization: `Bearer ${accessToken}` })
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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: `/channels/${channelExample.id}`, url: `/channels/${channelExample.id}`,
@ -50,15 +58,39 @@ describe('GET /channels/[channelId]', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
expect(responseJson.message).toEqual('Channel not found') 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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: '/channels/1' 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 { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.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 { channelExample } from '../../../../models/Channel.js'
import { memberExample } from '../../../../models/Member.js' import { memberExample } from '../../../../models/Member.js'
describe('PUT /channels/[channelId]', () => { const newName = 'new channel name'
it('succeeds', async () => {
await tap.test('PUT /channels/[channelId]', async (t) => {
t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
const defaultChannelId = 5 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() 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({ const response = await application.inject({
method: 'PUT', method: 'PUT',
url: `/channels/${channelExample.id}`, url: `/channels/${channelExample.id}`,
headers: { headers: {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
}, },
payload: { name: channelExample.name } payload: { name: newName }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.id).toEqual(channelExample.id) t.equal(responseJson.id, channelExample.id)
expect(responseJson.name).toEqual(channelExample.name) t.equal(responseJson.name, newName)
expect(responseJson.guildId).toEqual(channelExample.guildId) t.equal(responseJson.guildId, channelExample.guildId)
expect(responseJson.defaultChannelId).toEqual(defaultChannelId) t.equal(responseJson.defaultChannelId, defaultChannelId)
}) })
it('fails if the channel is not found', async () => { await t.test('fails if the channel is not found', async (t) => {
prismaMock.channel.findUnique.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'PUT', method: 'PUT',
url: `/channels/${channelExample.id}`, url: `/channels/${channelExample.id}`,
headers: { headers: {
authorization: `Bearer ${accessToken}` 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 () => { await t.test('fails if the member is not found', async (t) => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'PUT', method: 'PUT',
url: `/channels/${channelExample.id}`, url: `/channels/${channelExample.id}`,
headers: { headers: {
authorization: `Bearer ${accessToken}` 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 () => { await t.test('fails if the member is not owner', async (t) => {
const member = {
...memberExample,
isOwner: false
}
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(member)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'PUT', method: 'PUT',
url: `/channels/${channelExample.id}`, url: `/channels/${channelExample.id}`,
headers: { headers: {
authorization: `Bearer ${accessToken}` 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 { application } from '../../../../../application.js'
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.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 { channelExample } from '../../../../../models/Channel.js'
import { userExample } from '../../../../../models/User.js'
import { memberExample } from '../../../../../models/Member.js' import { memberExample } from '../../../../../models/Member.js'
import { userExample } from '../../../../../models/User.js'
import { messageExample } from '../../../../../models/Message.js' import { messageExample } from '../../../../../models/Message.js'
describe('GET /channels/[channelId]/messages', () => { await tap.test('GET /channels/[channelId]/messages', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample) sinon.restore()
prismaMock.member.findFirst.mockResolvedValue({ })
...memberExample,
user: userExample await t.test('succeeds', async (t) => {
} as any)
prismaMock.message.findMany.mockResolvedValue([messageExample])
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: `/channels/${channelExample.id}/messages`, url: `/channels/${channelExample.id}/messages`,
@ -23,21 +42,33 @@ describe('GET /channels/[channelId]/messages', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.length).toEqual(1) t.equal(responseJson.length, 1)
expect(responseJson[0].id).toEqual(messageExample.id) t.equal(responseJson[0].id, messageExample.id)
expect(responseJson[0].value).toEqual(messageExample.value) t.equal(responseJson[0].value, messageExample.value)
expect(responseJson[0].type).toEqual(messageExample.type) t.equal(responseJson[0].type, messageExample.type)
expect(responseJson[0].mimetype).toEqual(messageExample.mimetype) t.equal(responseJson[0].mimetype, messageExample.mimetype)
expect(responseJson[0].member.id).toEqual(memberExample.id) t.equal(responseJson[0].member.id, memberExample.id)
expect(responseJson[0].member.isOwner).toEqual(memberExample.isOwner) t.equal(responseJson[0].member.isOwner, memberExample.isOwner)
expect(responseJson[0].member.user.id).toEqual(userExample.id) t.equal(responseJson[0].member.user.id, userExample.id)
expect(responseJson[0].member.user.name).toEqual(userExample.name) t.equal(responseJson[0].member.user.name, userExample.name)
}) })
it('fails with not found channel', async () => { await t.test('fails with not found channel', async (t) => {
prismaMock.channel.findUnique.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: `/channels/${channelExample.id}/messages`, url: `/channels/${channelExample.id}/messages`,
@ -46,14 +77,22 @@ describe('GET /channels/[channelId]/messages', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
expect(responseJson.message).toEqual('Channel not found') t.equal(responseJson.message, 'Channel not found')
}) })
it('fails with not found member', async () => { await t.test('fails with not found member', async (t) => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: `/channels/${channelExample.id}/messages`, url: `/channels/${channelExample.id}/messages`,
@ -62,15 +101,15 @@ describe('GET /channels/[channelId]/messages', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
expect(responseJson.message).toEqual('Channel not found') 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({ const response = await application.inject({
method: 'GET', 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 { application } from '../../../../../application.js'
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.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 { channelExample } from '../../../../../models/Channel.js'
import { memberExample } from '../../../../../models/Member.js' import { memberExample } from '../../../../../models/Member.js'
import { userExample } from '../../../../../models/User.js' import { userExample } from '../../../../../models/User.js'
import { messageExample } from '../../../../../models/Message.js' import { messageExample } from '../../../../../models/Message.js'
describe('POST /channels/[channelId]/messages', () => { await tap.test('POST /channels/[channelId]/messages', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample) sinon.restore()
prismaMock.member.findFirst.mockResolvedValue({ })
...memberExample,
user: userExample await t.test('succeeds', async (t) => {
} as any)
prismaMock.message.create.mockResolvedValue(messageExample)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: `/channels/${channelExample.id}/messages`, url: `/channels/${channelExample.id}/messages`,
headers: { headers: {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
}, },
payload: { payload: { value: messageExample.value }
value: messageExample.value
}
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(201) t.equal(response.statusCode, 201)
expect(responseJson.id).toEqual(messageExample.id) t.equal(responseJson.id, messageExample.id)
expect(responseJson.value).toEqual(messageExample.value) t.equal(responseJson.value, messageExample.value)
expect(responseJson.type).toEqual(messageExample.type) t.equal(responseJson.type, messageExample.type)
expect(responseJson.mimetype).toEqual(messageExample.mimetype) t.equal(responseJson.mimetype, messageExample.mimetype)
expect(responseJson.member.id).toEqual(memberExample.id) t.equal(responseJson.member.id, memberExample.id)
expect(responseJson.member.isOwner).toEqual(memberExample.isOwner) t.equal(responseJson.member.isOwner, memberExample.isOwner)
expect(responseJson.member.user.id).toEqual(userExample.id) t.equal(responseJson.member.user.id, userExample.id)
expect(responseJson.member.user.name).toEqual(userExample.name) t.equal(responseJson.member.user.name, userExample.name)
}) })
it('fails with no message value', async () => { await t.test('fails with no message value', async (t) => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findFirst.mockResolvedValue({
...memberExample,
user: userExample
} as any)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: `/channels/${channelExample.id}/messages`, url: `/channels/${channelExample.id}/messages`,
@ -52,43 +77,59 @@ describe('POST /channels/[channelId]/messages', () => {
}, },
payload: {} payload: {}
}) })
expect(response.statusCode).toEqual(400) t.equal(response.statusCode, 400)
}) })
it('fails with not found channel', async () => { await t.test('fails with not found channel', async (t) => {
prismaMock.channel.findUnique.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/channels/5/messages', url: '/channels/5/messages',
headers: { headers: {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
}, },
payload: { payload: { value: messageExample.value }
value: messageExample.value
}
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
expect(responseJson.message).toEqual('Channel not found') t.equal(responseJson.message, 'Channel not found')
}) })
it('fails with not found member', async () => { await t.test('fails with not found member', async (t) => {
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
prismaMock.member.findUnique.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: `/channels/${channelExample.id}/messages`, url: `/channels/${channelExample.id}/messages`,
headers: { headers: {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
}, },
payload: { payload: { value: messageExample.value }
value: messageExample.value
}
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
expect(responseJson.message).toEqual('Channel not found') 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 { userPublicWithoutSettingsSchema } from '../../../../../models/User.js'
import { channelSchema } from '../../../../../models/Channel.js' import { channelSchema } from '../../../../../models/Channel.js'
import { uploadFile } from '../../../../../tools/utils/uploadFile.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({ const parametersSchema = Type.Object({
channelId: channelSchema.id channelId: channelSchema.id
@ -95,7 +95,7 @@ export const postMessageUploadsByChannelIdService: FastifyPluginAsync = async (
fastify, fastify,
request, request,
folderInUploadsFolder: 'messages', folderInUploadsFolder: 'messages',
maximumFileSize maximumFileSize: MAXIMUM_FILE_SIZE
}) })
const message = await prisma.message.create({ const message = await prisma.message.create({
data: { data: {

View File

@ -1,18 +1,33 @@
import tap from 'tap'
import sinon from 'sinon'
import { application } from '../../../../application.js' import { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.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 { memberExample } from '../../../../models/Member.js'
import { guildExample } from '../../../../models/Guild.js' import { guildExample } from '../../../../models/Guild.js'
describe('DELETE /guilds/[guildId]', () => { await tap.test('DELETE /guilds/[guildId]', async (t) => {
it('succeeds and delete the guild', async () => { t.afterEach(() => {
prismaMock.member.findFirst.mockResolvedValue({ sinon.restore()
...memberExample, })
isOwner: true,
guild: guildExample await t.test('succeeds and delete the guild', async (t) => {
} as any)
prismaMock.guild.delete.mockResolvedValue(guildExample)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/guilds/${guildExample.id}`, url: `/guilds/${guildExample.id}`,
@ -21,14 +36,19 @@ describe('DELETE /guilds/[guildId]', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.name).toEqual(guildExample.name) t.equal(responseJson.id, guildExample.id)
expect(responseJson.description).toEqual(guildExample.description) t.equal(responseJson.name, guildExample.name)
t.equal(responseJson.description, guildExample.description)
}) })
it("fails if the guild doesn't exist", async () => { await t.test("fails if the guild doesn't exist", async (t) => {
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/guilds/${guildExample.id}`, url: `/guilds/${guildExample.id}`,
@ -36,16 +56,20 @@ describe('DELETE /guilds/[guildId]', () => {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
} }
}) })
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
}) })
it('fails if the user is not the owner', async () => { await t.test('fails if the user is not the owner', async (t) => {
prismaMock.member.findFirst.mockResolvedValue({
...memberExample,
isOwner: false,
guild: guildExample
} as any)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
return {
...memberExample,
isOwner: false,
guild: guildExample
}
}
})
const response = await application.inject({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/guilds/${guildExample.id}`, url: `/guilds/${guildExample.id}`,
@ -54,7 +78,7 @@ describe('DELETE /guilds/[guildId]', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(400) t.equal(response.statusCode, 400)
expect(responseJson.message).toEqual('You should be an owner of the guild') 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 { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.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 { memberExample } from '../../../../models/Member.js'
import { guildExample } from '../../../../models/Guild.js' import { guildExample } from '../../../../models/Guild.js'
import { userExample } from '../../../../models/User.js' import { userExample } from '../../../../models/User.js'
import { channelExample } from '../../../../models/Channel.js' import { channelExample } from '../../../../models/Channel.js'
describe('GET /guilds/[guildId]', () => { const defaultChannelId = 5
it('succeeds', async () => {
const defaultChannelId = 5 await tap.test('GET /guilds/[guildId]', async (t) => {
prismaMock.member.findFirst.mockResolvedValue({ t.afterEach(() => {
...memberExample, sinon.restore()
guild: guildExample, })
user: userExample
} as any) await t.test('succeeds', async (t) => {
prismaMock.channel.findFirst.mockResolvedValue({
...channelExample,
id: defaultChannelId
})
const { accessToken, user } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: `/guilds/${guildExample.id}`, url: `/guilds/${guildExample.id}`,
@ -27,18 +43,23 @@ describe('GET /guilds/[guildId]', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.member.isOwner).toEqual(memberExample.isOwner) t.equal(responseJson.member.id, memberExample.id)
expect(responseJson.member.user.name).toEqual(user.name) t.equal(responseJson.member.isOwner, memberExample.isOwner)
expect(responseJson.member.user.email).toBeNull() t.equal(responseJson.member.user.name, user.name)
expect(responseJson.guild.id).toEqual(guildExample.id) t.equal(responseJson.member.user.email, null)
expect(responseJson.guild.name).toEqual(guildExample.name) t.equal(responseJson.guild.id, guildExample.id)
expect(responseJson.guild.defaultChannelId).toEqual(defaultChannelId) 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() const { accessToken } = await authenticateUserTest()
prismaMock.member.findFirst.mockResolvedValue(null) sinon.stub(prisma, 'member').value({
findFirst: async () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'GET', method: 'GET',
url: '/guilds/1', url: '/guilds/1',
@ -47,15 +68,15 @@ describe('GET /guilds/[guildId]', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
expect(responseJson.message).toEqual('Member not found') 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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: '/guilds/1' 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 { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.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'
import { memberExample } from '../../../../models/Member.js' import { memberExample } from '../../../../models/Member.js'
import { guildExample } from '../../../../models/Guild.js'
import { channelExample } from '../../../../models/Channel.js' import { channelExample } from '../../../../models/Channel.js'
describe('PUT /guilds/[guildId]', () => { const defaultChannelId = 5
it('succeeds and edit the guild', async () => { const newName = 'New guild name'
const defaultChannelId = 5 const newDescription = 'New guild description'
const newName = 'New guild name'
const newDescription = 'New guild description' await tap.test('PUT /guilds/[guildId]', async (t) => {
prismaMock.member.findFirst.mockResolvedValue({ t.afterEach(() => {
...memberExample, sinon.restore()
isOwner: true, })
guild: guildExample
} as any) await t.test('succeeds and edit the guild', async (t) => {
prismaMock.guild.update.mockResolvedValue({
...guildExample,
name: newName,
description: newDescription
})
prismaMock.channel.findFirst.mockResolvedValue({
...channelExample,
id: defaultChannelId
})
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'PUT', method: 'PUT',
url: `/guilds/${guildExample.id}`, url: `/guilds/${guildExample.id}`,
@ -37,17 +57,19 @@ describe('PUT /guilds/[guildId]', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.name).toEqual(newName) t.equal(responseJson.name, newName)
expect(responseJson.description).toEqual(newDescription) t.equal(responseJson.description, newDescription)
expect(responseJson.defaultChannelId).toEqual(defaultChannelId) t.equal(responseJson.defaultChannelId, defaultChannelId)
}) })
it("fails if the guild doesn't exist", async () => { await t.test("fails if the guild doesn't exist", async (t) => {
const newName = 'New guild name'
const newDescription = 'New guild description'
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'PUT', method: 'PUT',
url: `/guilds/${guildExample.id}`, url: `/guilds/${guildExample.id}`,
@ -59,18 +81,20 @@ describe('PUT /guilds/[guildId]', () => {
description: newDescription description: newDescription
} }
}) })
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
}) })
it('fails if the user is not the owner', async () => { await t.test('fails if the user is not the owner', async (t) => {
const newName = 'New guild name'
const newDescription = 'New guild description'
prismaMock.member.findFirst.mockResolvedValue({
...memberExample,
isOwner: false,
guild: guildExample
} as any)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
return {
...memberExample,
isOwner: false,
guild: guildExample
}
}
})
const response = await application.inject({ const response = await application.inject({
method: 'PUT', method: 'PUT',
url: `/guilds/${guildExample.id}`, url: `/guilds/${guildExample.id}`,
@ -83,7 +107,7 @@ describe('PUT /guilds/[guildId]', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(400) t.equal(response.statusCode, 400)
expect(responseJson.message).toEqual('You should be an owner of the guild') 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 { application } from '../../../../../application.js'
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.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 { memberExample } from '../../../../../models/Member.js'
import { guildExample } from '../../../../../models/Guild.js' import { guildExample } from '../../../../../models/Guild.js'
import { channelExample } from '../../../../../models/Channel.js' import { channelExample } from '../../../../../models/Channel.js'
describe('GET /guilds/[guildId]/channels', () => { await tap.test('GET /guilds/[guildId]/channels', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.member.findFirst.mockResolvedValue(memberExample) sinon.restore()
prismaMock.channel.findMany.mockResolvedValue([channelExample]) })
await t.test('succeeds', async (t) => {
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: `/guilds/${guildExample.id}/channels`, url: `/guilds/${guildExample.id}/channels`,
@ -18,16 +33,20 @@ describe('GET /guilds/[guildId]/channels', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.length).toEqual(1) t.equal(responseJson.length, 1)
expect(responseJson[0].id).toEqual(channelExample.id) t.equal(responseJson[0].id, channelExample.id)
expect(responseJson[0].name).toEqual(channelExample.name) t.equal(responseJson[0].name, channelExample.name)
expect(responseJson[0].guildId).toEqual(channelExample.guildId) t.equal(responseJson[0].guildId, channelExample.guildId)
}) })
it('fails with not found guild', async () => { await t.test('fails with not found member/guild', async (t) => {
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'GET', method: 'GET',
url: '/guilds/1/channels', url: '/guilds/1/channels',
@ -36,15 +55,15 @@ describe('GET /guilds/[guildId]/channels', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
expect(responseJson.message).toEqual('Member not found') 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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: '/guilds/1/channels' 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 { application } from '../../../../../application.js'
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.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 { memberExample } from '../../../../../models/Member.js'
import { guildExample } from '../../../../../models/Guild.js' import { guildExample } from '../../../../../models/Guild.js'
import { channelExample } from '../../../../../models/Channel.js'
describe('POST /guilds/[guildId]/channels', () => { const defaultChannelId = 5
it('succeeds', async () => {
const defaultChannelId = 5 await tap.test('POST /guilds/[guildId]/channels', async (t) => {
prismaMock.member.findFirst.mockResolvedValue(memberExample) t.afterEach(() => {
prismaMock.channel.create.mockResolvedValue(channelExample) sinon.restore()
prismaMock.channel.findFirst.mockResolvedValue({ })
...channelExample,
id: defaultChannelId await t.test('succeeds', async (t) => {
})
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: `/guilds/${guildExample.id}/channels`, url: `/guilds/${guildExample.id}/channels`,
@ -24,16 +42,20 @@ describe('POST /guilds/[guildId]/channels', () => {
payload: { name: channelExample.name } payload: { name: channelExample.name }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(201) t.equal(response.statusCode, 201)
expect(responseJson.id).toEqual(channelExample.id) t.equal(responseJson.id, channelExample.id)
expect(responseJson.name).toEqual(channelExample.name) t.equal(responseJson.name, channelExample.name)
expect(responseJson.guildId).toEqual(channelExample.guildId) t.equal(responseJson.guildId, channelExample.guildId)
expect(responseJson.defaultChannelId).toEqual(defaultChannelId) t.equal(responseJson.defaultChannelId, defaultChannelId)
}) })
it('fails if the member is not found', async () => { await t.test('fails if the member is not found', async (t) => {
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
url: `/guilds/${guildExample.id}/channels`, url: `/guilds/${guildExample.id}/channels`,
@ -42,16 +64,19 @@ describe('POST /guilds/[guildId]/channels', () => {
}, },
payload: { name: channelExample.name } payload: { name: channelExample.name }
}) })
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
}) })
it('fails if the member is not owner', async () => { await t.test('fails if the member is not owner', async (t) => {
const member = {
...memberExample,
isOwner: false
}
prismaMock.member.findFirst.mockResolvedValue(member)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
return {
...memberExample,
isOwner: false
}
}
})
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
url: `/guilds/${guildExample.id}/channels`, url: `/guilds/${guildExample.id}/channels`,
@ -60,6 +85,6 @@ describe('POST /guilds/[guildId]/channels', () => {
}, },
payload: { name: channelExample.name } 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 { uploadFile } from '../../../../tools/utils/uploadFile.js'
import { guildSchema } from '../../../../models/Guild.js' import { guildSchema } from '../../../../models/Guild.js'
import { import {
maximumImageSize, MAXIMUM_IMAGE_SIZE,
supportedImageMimetype SUPPORTED_IMAGE_MIMETYPE
} from '../../../../tools/configurations/index.js' } from '../../../../tools/configurations/index.js'
import { channelSchema } from '../../../../models/Channel.js' import { channelSchema } from '../../../../models/Channel.js'
@ -68,8 +68,8 @@ export const putGuildIconById: FastifyPluginAsync = async (fastify) => {
fastify, fastify,
request, request,
folderInUploadsFolder: 'guilds', folderInUploadsFolder: 'guilds',
maximumFileSize: maximumImageSize, maximumFileSize: MAXIMUM_IMAGE_SIZE,
supportedFileMimetype: supportedImageMimetype supportedFileMimetype: SUPPORTED_IMAGE_MIMETYPE
}) })
await prisma.guild.update({ await prisma.guild.update({
where: { id: guildId }, where: { id: guildId },

View File

@ -1,36 +1,52 @@
import tap from 'tap'
import sinon from 'sinon'
import { application } from '../../../../../application.js' import { application } from '../../../../../application.js'
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.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 { memberExample } from '../../../../../models/Member.js'
import { guildExample } from '../../../../../models/Guild.js'
import { userExample } from '../../../../../models/User.js' import { userExample } from '../../../../../models/User.js'
describe('GET /guilds/[guildId]/members', () => { await tap.test('GET /guilds/[guildId]/members', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.member.findFirst.mockResolvedValue(memberExample) sinon.restore()
prismaMock.member.findMany.mockResolvedValue([ })
{ ...memberExample, user: userExample }
] as any) await t.test('succeeds', async (t) => {
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
return memberExample
},
findMany: async () => {
return [{ ...memberExample, user: userExample }]
}
})
const response = await application.inject({ const response = await application.inject({
method: 'GET', method: 'GET',
url: `/guilds/${memberExample.guildId}/members`, url: `/guilds/${guildExample.id}/members`,
headers: { headers: {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.length).toEqual(1) t.equal(responseJson.length, 1)
expect(responseJson[0].id).toEqual(memberExample.id) t.equal(responseJson[0].id, memberExample.id)
expect(responseJson[0].isOwner).toEqual(memberExample.isOwner) t.equal(responseJson[0].isOwner, memberExample.isOwner)
expect(responseJson[0].user.id).toEqual(userExample.id) t.equal(responseJson[0].user.id, userExample.id)
expect(responseJson[0].user.name).toEqual(userExample.name) t.equal(responseJson[0].user.name, userExample.name)
expect(responseJson[0].user.email).toEqual(null) t.equal(responseJson[0].user.email, null)
}) })
it('fails with not found member', async () => { await t.test('fails with not found member/guild', async (t) => {
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'GET', method: 'GET',
url: '/guilds/1/members', url: '/guilds/1/members',
@ -39,15 +55,15 @@ describe('GET /guilds/[guildId]/members', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
expect(responseJson.message).toEqual('Member not found') 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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: '/guilds/1/members' 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 { application } from '../../../../../../application.js'
import { authenticateUserTest } from '../../../../../../__test__/utils/authenticateUserTest.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'
import { channelExample } from '../../../../../../models/Channel.js'
import { memberExample } from '../../../../../../models/Member.js' import { memberExample } from '../../../../../../models/Member.js'
import { guildExample } from '../../../../../../models/Guild.js'
import { userExample } from '../../../../../../models/User.js' import { userExample } from '../../../../../../models/User.js'
import { channelExample } from '../../../../../../models/Channel.js'
describe('POST /guilds/[guildId]/members/join', () => { const defaultChannelId = 5
it('succeeds', async () => {
prismaMock.guild.findUnique.mockResolvedValue(guildExample) await tap.test('POST /guilds/[guildId]/members/join', async (t) => {
prismaMock.member.findFirst.mockResolvedValue(null) t.afterEach(() => {
prismaMock.member.create.mockResolvedValue({ sinon.restore()
...memberExample, })
user: userExample
} as any) await t.test('succeeds', async (t) => {
prismaMock.channel.findFirst.mockResolvedValue(channelExample) const { accessToken } = await authenticateUserTest()
const { accessToken, user } = 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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: `/guilds/${guildExample.id}/members/join`, url: `/guilds/${guildExample.id}/members/join`,
@ -24,21 +44,33 @@ describe('POST /guilds/[guildId]/members/join', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(201) t.equal(response.statusCode, 201)
expect(responseJson.id).toEqual(memberExample.id) t.equal(responseJson.id, memberExample.id)
expect(responseJson.userId).toEqual(memberExample.userId) t.equal(responseJson.userId, memberExample.userId)
expect(responseJson.user.name).toEqual(user.name) t.equal(responseJson.user.name, userExample.name)
expect(responseJson.user.email).toEqual(null) t.equal(responseJson.user.email, null)
expect(responseJson.guild.id).toEqual(guildExample.id) t.equal(responseJson.guild.id, guildExample.id)
expect(responseJson.guild.name).toEqual(guildExample.name) t.equal(responseJson.guild.name, guildExample.name)
expect(responseJson.guild.defaultChannelId).toEqual(channelExample.id) t.equal(responseJson.guild.defaultChannelId, channelExample.id)
}) })
it('fails if the guild is not found', async () => { await t.test('fails if the guild is not found', async (t) => {
prismaMock.guild.findUnique.mockResolvedValue(null)
prismaMock.member.findFirst.mockResolvedValue(null)
prismaMock.channel.findFirst.mockResolvedValue(channelExample)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: `/guilds/${guildExample.id}/members/join`, url: `/guilds/${guildExample.id}/members/join`,
@ -46,18 +78,29 @@ describe('POST /guilds/[guildId]/members/join', () => {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
} }
}) })
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
}) })
it('fails if the user is already in the guild', async () => { await t.test('fails if the user is already in the guild', async (t) => {
const defaultChannelId = 5
prismaMock.guild.findUnique.mockResolvedValue(guildExample)
prismaMock.member.findFirst.mockResolvedValue(memberExample)
prismaMock.channel.findFirst.mockResolvedValue({
...channelExample,
id: defaultChannelId
})
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: `/guilds/${guildExample.id}/members/join`, url: `/guilds/${guildExample.id}/members/join`,
@ -66,7 +109,7 @@ describe('POST /guilds/[guildId]/members/join', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(400) t.equal(response.statusCode, 400)
expect(responseJson.defaultChannelId).toEqual(defaultChannelId) 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 { application } from '../../../../../../application.js'
import { authenticateUserTest } from '../../../../../../__test__/utils/authenticateUserTest.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'
import { memberExample } from '../../../../../../models/Member.js' import { memberExample } from '../../../../../../models/Member.js'
import { guildExample } from '../../../../../../models/Guild.js'
describe('DELETE /guilds/[guildId]/members/leave', () => { await tap.test('DELETE /guilds/[guildId]/members/leave', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
const { accessToken } = await authenticateUserTest()
const member = { const member = {
...memberExample, ...memberExample,
isOwner: false isOwner: false
} }
prismaMock.member.findFirst.mockResolvedValue(member) sinon.stub(prisma, 'member').value({
prismaMock.member.delete.mockResolvedValue(member) findFirst: async () => {
const { accessToken } = await authenticateUserTest() return member
},
delete: async () => {
return member
}
})
const response = await application.inject({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/guilds/${guildExample.id}/members/leave`, url: `/guilds/${guildExample.id}/members/leave`,
@ -21,15 +34,19 @@ describe('DELETE /guilds/[guildId]/members/leave', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.id).toEqual(member.id) t.equal(responseJson.id, member.id)
expect(responseJson.isOwner).toEqual(member.isOwner) t.equal(responseJson.isOwner, member.isOwner)
expect(responseJson.userId).toEqual(member.userId) t.equal(responseJson.userId, member.userId)
}) })
it('fails if the member is not found', async () => { await t.test('fails if the member is not found', async (t) => {
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'member').value({
findFirst: async () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/guilds/${guildExample.id}/members/leave`, url: `/guilds/${guildExample.id}/members/leave`,
@ -37,16 +54,20 @@ describe('DELETE /guilds/[guildId]/members/leave', () => {
authorization: `Bearer ${accessToken}` 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 = { const member = {
...memberExample, ...memberExample,
isOwner: true isOwner: true
} }
prismaMock.member.findFirst.mockResolvedValue(member) sinon.stub(prisma, 'member').value({
const { accessToken } = await authenticateUserTest() findFirst: async () => {
return member
}
})
const response = await application.inject({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/guilds/${guildExample.id}/members/leave`, url: `/guilds/${guildExample.id}/members/leave`,
@ -54,6 +75,6 @@ describe('DELETE /guilds/[guildId]/members/leave', () => {
authorization: `Bearer ${accessToken}` 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 { application } from '../../../application.js'
import { authenticateUserTest } from '../../../__test__/utils/authenticateUserTest.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'
import { memberExample } from '../../../models/Member.js' import { memberExample } from '../../../models/Member.js'
import { guildExample } from '../../../models/Guild.js'
import { channelExample } from '../../../models/Channel.js' import { channelExample } from '../../../models/Channel.js'
describe('GET /guilds', () => { await tap.test('GET /guilds', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.guild.findUnique.mockResolvedValue(guildExample) sinon.restore()
prismaMock.member.findMany.mockResolvedValue([memberExample]) })
prismaMock.channel.findFirst.mockResolvedValue(channelExample)
await t.test('succeeds', async (t) => {
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: '/guilds', url: '/guilds',
@ -19,10 +38,10 @@ describe('GET /guilds', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.length).toEqual(1) t.equal(responseJson.length, 1)
expect(responseJson[0].name).toEqual(guildExample.name) t.equal(responseJson[0].name, guildExample.name)
expect(responseJson[0].description).toEqual(guildExample.description) t.equal(responseJson[0].description, guildExample.description)
expect(responseJson[0].defaultChannelId).toEqual(channelExample.id) 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 { application } from '../../../application.js'
import { authenticateUserTest } from '../../../__test__/utils/authenticateUserTest.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'
import { memberExample } from '../../../models/Member.js' import { memberExample } from '../../../models/Member.js'
import { guildExample } from '../../../models/Guild.js'
import { channelExample } from '../../../models/Channel.js' import { channelExample } from '../../../models/Channel.js'
import { userExample } from '../../../models/User.js' import { userExample } from '../../../models/User.js'
describe('POST /guilds', () => { await tap.test('POST /guilds', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.guild.create.mockResolvedValue(guildExample) sinon.restore()
prismaMock.member.create.mockResolvedValue(memberExample) })
prismaMock.member.findUnique.mockResolvedValue({
...memberExample, await t.test('succeeds', async (t) => {
...userExample
})
prismaMock.channel.create.mockResolvedValue(channelExample)
const { accessToken, user } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/guilds', url: '/guilds',
@ -28,23 +49,21 @@ describe('POST /guilds', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(201) t.equal(response.statusCode, 201)
expect(responseJson.guild.id).toEqual(guildExample.id) t.equal(responseJson.guild.id, guildExample.id)
expect(responseJson.guild.name).toEqual(guildExample.name) t.equal(responseJson.guild.name, guildExample.name)
expect(responseJson.guild.members.length).toEqual(1) t.equal(responseJson.guild.description, guildExample.description)
expect(responseJson.guild.members[0].userId).toEqual(user.id) t.equal(responseJson.guild.members.length, 1)
expect(responseJson.guild.members[0].user.name).toEqual(user.name) t.equal(responseJson.guild.members[0].userId, user.id)
expect(responseJson.guild.members[0].guildId).toEqual(guildExample.id) t.equal(responseJson.guild.members[0].user.name, user.name)
expect(responseJson.guild.members[0].isOwner).toEqual(memberExample.isOwner) t.equal(responseJson.guild.members[0].guildId, guildExample.id)
expect(responseJson.guild.channels.length).toEqual(1) t.equal(responseJson.guild.members[0].isOwner, memberExample.isOwner)
expect(responseJson.guild.channels[0].id).toEqual(channelExample.id) t.equal(responseJson.guild.channels.length, 1)
expect(responseJson.guild.channels[0].guildId).toEqual(guildExample.id) 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 () => { await t.test('fails with empty name and description', async (t) => {
prismaMock.guild.create.mockResolvedValue(guildExample)
prismaMock.member.create.mockResolvedValue(memberExample)
prismaMock.channel.create.mockResolvedValue(channelExample)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
@ -53,6 +72,6 @@ describe('POST /guilds', () => {
authorization: `Bearer ${accessToken}` 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 { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.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' import { guildExample } from '../../../../models/Guild.js'
describe('GET /guilds/public', () => { await tap.test('GET /guilds/public', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.guild.findMany.mockResolvedValue([guildExample]) sinon.restore()
prismaMock.member.count.mockResolvedValue(2) })
await t.test('succeeds', async (t) => {
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: '/guilds/public', url: '/guilds/public',
@ -16,9 +31,9 @@ describe('GET /guilds/public', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.length).toEqual(1) t.equal(responseJson.length, 1)
expect(responseJson[0].name).toEqual(guildExample.name) t.equal(responseJson[0].name, guildExample.name)
expect(responseJson[0].membersCount).toEqual(2) 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 { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.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 { messageExample } from '../../../../models/Message.js'
import { memberExample } from '../../../../models/Member.js' import { memberExample } from '../../../../models/Member.js'
import { userExample } from '../../../../models/User.js' import { userExample } from '../../../../models/User.js'
import { channelExample } from '../../../../models/Channel.js' import { channelExample } from '../../../../models/Channel.js'
describe('DELETE /messsages/[messageId]', () => { await tap.test('DELETE /messsages/[messageId]', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.message.findFirst.mockResolvedValue({ sinon.restore()
...messageExample, })
channel: channelExample
} as any) await t.test('succeeds', async (t) => {
prismaMock.member.findFirst.mockResolvedValue({
...memberExample,
user: userExample
} as any)
prismaMock.message.delete.mockResolvedValue(messageExample)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/messages/${messageExample.id}`, url: `/messages/${messageExample.id}`,
@ -26,20 +43,24 @@ describe('DELETE /messsages/[messageId]', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.id).toEqual(messageExample.id) t.equal(responseJson.id, messageExample.id)
expect(responseJson.value).toEqual(messageExample.value) t.equal(responseJson.value, messageExample.value)
expect(responseJson.type).toEqual(messageExample.type) t.equal(responseJson.type, messageExample.type)
expect(responseJson.mimetype).toEqual(messageExample.mimetype) t.equal(responseJson.mimetype, messageExample.mimetype)
expect(responseJson.member.id).toEqual(memberExample.id) t.equal(responseJson.member.id, memberExample.id)
expect(responseJson.member.isOwner).toEqual(memberExample.isOwner) t.equal(responseJson.member.isOwner, memberExample.isOwner)
expect(responseJson.member.user.id).toEqual(userExample.id) t.equal(responseJson.member.user.id, userExample.id)
expect(responseJson.member.user.name).toEqual(userExample.name) t.equal(responseJson.member.user.name, userExample.name)
}) })
it('fails if the message is not found', async () => { await t.test('fails if the message is not found', async (t) => {
prismaMock.message.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
sinon.stub(prisma, 'message').value({
findFirst: async () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/messages/${messageExample.id}`, url: `/messages/${messageExample.id}`,
@ -47,16 +68,24 @@ describe('DELETE /messsages/[messageId]', () => {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
} }
}) })
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
}) })
it('fails if the member is not found', async () => { await t.test('fails if the member is not found', async (t) => {
prismaMock.message.findFirst.mockResolvedValue({
...messageExample,
channel: channelExample
} as any)
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/messages/${messageExample.id}`, url: `/messages/${messageExample.id}`,
@ -64,20 +93,28 @@ describe('DELETE /messsages/[messageId]', () => {
authorization: `Bearer ${accessToken}` 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 const randomUserIdOwnerOfMessage = 14
prismaMock.message.findFirst.mockResolvedValue({ sinon.stub(prisma, 'message').value({
...messageExample, findFirst: async () => {
channel: channelExample return {
} as any) ...messageExample,
prismaMock.member.findFirst.mockResolvedValue({ channel: channelExample
...memberExample, }
userId: randomUserIdOwnerOfMessage }
})
sinon.stub(prisma, 'member').value({
findFirst: async () => {
return {
...memberExample,
userId: randomUserIdOwnerOfMessage
}
}
}) })
const { accessToken } = await authenticateUserTest()
const response = await application.inject({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: `/messages/${messageExample.id}`, url: `/messages/${messageExample.id}`,
@ -85,6 +122,6 @@ describe('DELETE /messsages/[messageId]', () => {
authorization: `Bearer ${accessToken}` 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 { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.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 { messageExample } from '../../../../models/Message.js'
import { memberExample } from '../../../../models/Member.js' import { memberExample } from '../../../../models/Member.js'
import { userExample } from '../../../../models/User.js' import { userExample } from '../../../../models/User.js'
import { channelExample } from '../../../../models/Channel.js' import { channelExample } from '../../../../models/Channel.js'
describe('PUT /messsages/[messageId]', () => { await tap.test('PUT /messsages/[messageId]', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
const newValue = 'some message' sinon.restore()
prismaMock.message.findFirst.mockResolvedValue({ })
...messageExample,
channel: channelExample await t.test('succeeds', async (t) => {
} as any)
prismaMock.member.findFirst.mockResolvedValue({
...memberExample,
user: userExample
} as any)
prismaMock.message.update.mockResolvedValue({
...messageExample,
value: newValue
})
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'PUT', method: 'PUT',
url: `/messages/${messageExample.id}`, url: `/messages/${messageExample.id}`,
headers: { headers: {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
}, },
payload: { payload: { value: newValue }
value: newValue
}
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.id).toEqual(messageExample.id) t.equal(responseJson.id, messageExample.id)
expect(responseJson.value).toEqual(newValue) t.equal(responseJson.value, newValue)
expect(responseJson.type).toEqual(messageExample.type) t.equal(responseJson.type, messageExample.type)
expect(responseJson.mimetype).toEqual(messageExample.mimetype) t.equal(responseJson.mimetype, messageExample.mimetype)
expect(responseJson.member.id).toEqual(memberExample.id) t.equal(responseJson.member.id, memberExample.id)
expect(responseJson.member.isOwner).toEqual(memberExample.isOwner) t.equal(responseJson.member.isOwner, memberExample.isOwner)
expect(responseJson.member.user.id).toEqual(userExample.id) t.equal(responseJson.member.user.id, userExample.id)
expect(responseJson.member.user.name).toEqual(userExample.name) t.equal(responseJson.member.user.name, userExample.name)
}) })
it('fails if the message is not found', async () => { await t.test('fails if the message is not found', async (t) => {
const newValue = 'some message'
prismaMock.message.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
const newValue = 'some message'
sinon.stub(prisma, 'message').value({
findFirst: async () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'PUT', method: 'PUT',
url: `/messages/${messageExample.id}`, url: `/messages/${messageExample.id}`,
headers: { headers: {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
}, },
payload: { payload: { value: newValue }
value: newValue
}
}) })
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
}) })
it('fails if the member is not found', async () => { await t.test('fails if the member is not found', async (t) => {
const newValue = 'some message'
prismaMock.message.findFirst.mockResolvedValue({
...messageExample,
channel: channelExample
} as any)
prismaMock.member.findFirst.mockResolvedValue(null)
const { accessToken } = await authenticateUserTest() 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({ const response = await application.inject({
method: 'PUT', method: 'PUT',
url: `/messages/${messageExample.id}`, url: `/messages/${messageExample.id}`,
headers: { headers: {
authorization: `Bearer ${accessToken}` authorization: `Bearer ${accessToken}`
}, },
payload: { payload: { value: newValue }
value: newValue
}
}) })
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(
const newValue = 'some message' 'fails if the member is not the owner of the message',
const randomUserIdOwnerOfMessage = 14 async (t) => {
prismaMock.message.findFirst.mockResolvedValue({ const { accessToken } = await authenticateUserTest()
...messageExample, const newValue = 'some message'
channel: channelExample const randomUserIdOwnerOfMessage = 14
} as any) sinon.stub(prisma, 'message').value({
prismaMock.member.findFirst.mockResolvedValue({ findFirst: async () => {
...memberExample, return {
userId: randomUserIdOwnerOfMessage ...messageExample,
}) channel: channelExample
const { accessToken } = await authenticateUserTest() }
const response = await application.inject({ }
method: 'PUT', })
url: `/messages/${messageExample.id}`, sinon.stub(prisma, 'member').value({
headers: { findFirst: async () => {
authorization: `Bearer ${accessToken}` return {
}, ...memberExample,
payload: { userId: randomUserIdOwnerOfMessage
value: newValue }
} }
}) })
expect(response.statusCode).toEqual(400) 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 { application } from '../../../../application.js'
import prisma from '../../../../tools/database/prisma.js'
import { userExample } from '../../../../models/User.js' import { userExample } from '../../../../models/User.js'
import { userSettingsExample } from '../../../../models/UserSettings.js' import { userSettingsExample } from '../../../../models/UserSettings.js'
import { prismaMock } from '../../../../__test__/setup.js'
describe('GET /users/[userId]', () => { await tap.test('GET /users/[userId]', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.guild.findMany.mockResolvedValue([]) sinon.restore()
prismaMock.user.findUnique.mockResolvedValue(userExample) })
prismaMock.userSetting.findFirst.mockResolvedValue(userSettingsExample)
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({ const response = await application.inject({
method: 'GET', method: 'GET',
url: `/users/${userExample.id}` url: `/users/${userExample.id}`
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.user.id).toEqual(userExample.id) t.equal(responseJson.user.id, userExample.id)
expect(responseJson.user.name).toEqual(userExample.name) t.equal(responseJson.user.name, userExample.name)
}) })
it('fails with not found user', async () => { await t.test('fails with not found user', async (t) => {
prismaMock.userSetting.findFirst.mockResolvedValue(null) sinon.stub(prisma, 'userSetting').value({
findFirst: async () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'GET', method: 'GET',
url: `/users/1` url: `/users/1`
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(404) t.equal(response.statusCode, 404)
expect(responseJson.message).toEqual('User not found') t.equal(responseJson.message, 'User not found')
}) })
}) })

View File

@ -1,31 +1,23 @@
import { application } from '../../../../application.js' import tap from 'tap'
import { userExample } from '../../../../models/User.js' import sinon from 'sinon'
import { prismaMock } from '../../../../__test__/setup.js'
describe('GET /users/confirm-email', () => { import { application } from '../../../../application.js'
it('should succeeds', async () => { import prisma from '../../../../tools/database/prisma.js'
prismaMock.user.findFirst.mockResolvedValue(userExample) import { userExample } from '../../../../models/User.js'
prismaMock.user.update.mockResolvedValue({
...userExample, await tap.test('GET /users/confirm-email', async (t) => {
isConfirmed: true, t.afterEach(() => {
temporaryToken: null sinon.restore()
})
const response = await application.inject({
method: 'GET',
url: '/users/confirm-email',
query: {
temporaryToken: userExample.temporaryToken ?? ''
}
})
expect(response.statusCode).toEqual(200)
}) })
it('should fails with invalid `temporaryToken`', async () => { await t.test('succeeds', async (t) => {
prismaMock.user.findFirst.mockResolvedValue(null) sinon.stub(prisma, 'user').value({
prismaMock.user.update.mockResolvedValue({ findFirst: async () => {
...userExample, return userExample
isConfirmed: true, },
temporaryToken: null update: async () => {
return { ...userExample, isConfirmed: true, temporaryToken: null }
}
}) })
const response = await application.inject({ const response = await application.inject({
method: 'GET', method: 'GET',
@ -34,6 +26,25 @@ describe('GET /users/confirm-email', () => {
temporaryToken: userExample.temporaryToken ?? '' 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 { application } from '../../../../application.js'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js' import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
describe('GET /users/current', () => { await tap.test('GET /users/current', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
sinon.restore()
})
await t.test('succeeds', async (t) => {
const { accessToken, user } = await authenticateUserTest() const { accessToken, user } = await authenticateUserTest()
const response = await application.inject({ const response = await application.inject({
method: 'GET', method: 'GET',
@ -12,18 +19,16 @@ describe('GET /users/current', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.user.name).toEqual(user.name) t.equal(responseJson.user.name, user.name)
expect(responseJson.user.strategies).toEqual( t.strictSame(responseJson.user.strategies, ['local'])
expect.arrayContaining(['local'])
)
}) })
it('fails with unauthenticated user', async () => { await t.test('fails with unauthenticated user', async (t) => {
const response = await application.inject({ const response = await application.inject({
method: 'GET', method: 'GET',
url: '/users/current' 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 tap from 'tap'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js' import sinon from 'sinon'
import { prismaMock } from '../../../../__test__/setup.js'
import { userExample } from '../../../../models/User.js'
describe('PUT /users/current', () => { import { application } from '../../../../application.js'
it('succeeds with valid accessToken and valid name', async () => { 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 newName = 'John Doe'
const { accessToken, user } = await authenticateUserTest() const { accessToken, user, userStubValue } = await authenticateUserTest()
prismaMock.user.update.mockResolvedValue({ sinon.stub(prisma, 'user').value({
...user, ...userStubValue,
name: newName findFirst: async () => {
return null
},
update: async () => {
return {
...user,
name: newName
}
}
}) })
const response = await application.inject({ const response = await application.inject({
method: 'PUT', method: 'PUT',
@ -22,16 +36,24 @@ describe('PUT /users/current', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.user.name).toEqual(newName) 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 newStatus = '👀 Working on secret projects...'
const { accessToken, user } = await authenticateUserTest() const { accessToken, user, userStubValue } = await authenticateUserTest()
prismaMock.user.update.mockResolvedValue({ sinon.stub(prisma, 'user').value({
...user, ...userStubValue,
status: newStatus findFirst: async () => {
return null
},
update: async () => {
return {
...user,
status: newStatus
}
}
}) })
const response = await application.inject({ const response = await application.inject({
method: 'PUT', method: 'PUT',
@ -44,15 +66,20 @@ describe('PUT /users/current', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.user.name).toEqual(user.name) t.equal(responseJson.user.name, user.name)
expect(responseJson.user.status).toEqual(newStatus) 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' const newName = 'John Doe'
prismaMock.user.findFirst.mockResolvedValue(userExample) const { accessToken, user, userStubValue } = await authenticateUserTest()
const { accessToken } = await authenticateUserTest() sinon.stub(prisma, 'user').value({
...userStubValue,
findFirst: async () => {
return user
}
})
const response = await application.inject({ const response = await application.inject({
method: 'PUT', method: 'PUT',
url: '/users/current', url: '/users/current',
@ -63,10 +90,10 @@ describe('PUT /users/current', () => {
name: newName 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 newWebsite = 'invalid website url'
const { accessToken } = await authenticateUserTest() const { accessToken } = await authenticateUserTest()
const response = await application.inject({ const response = await application.inject({
@ -79,15 +106,23 @@ describe('PUT /users/current', () => {
website: newWebsite 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 newWebsite = 'https://somerandomwebsite.com'
const { accessToken, user } = await authenticateUserTest() const { accessToken, user, userStubValue } = await authenticateUserTest()
prismaMock.user.update.mockResolvedValue({ sinon.stub(prisma, 'user').value({
...user, ...userStubValue,
website: newWebsite findFirst: async () => {
return null
},
update: async () => {
return {
...user,
website: newWebsite
}
}
}) })
const response = await application.inject({ const response = await application.inject({
method: 'PUT', method: 'PUT',
@ -100,8 +135,8 @@ describe('PUT /users/current', () => {
} }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.user.name).toEqual(user.name) t.equal(responseJson.user.name, user.name)
expect(responseJson.user.website).toEqual(newWebsite) 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 prisma from '../../../../tools/database/prisma.js'
import { uploadFile } from '../../../../tools/utils/uploadFile.js' import { uploadFile } from '../../../../tools/utils/uploadFile.js'
import { import {
maximumImageSize, MAXIMUM_IMAGE_SIZE,
supportedImageMimetype SUPPORTED_IMAGE_MIMETYPE
} from '../../../../tools/configurations/index.js' } from '../../../../tools/configurations/index.js'
const putServiceSchema: FastifySchema = { const putServiceSchema: FastifySchema = {
@ -52,8 +52,8 @@ export const putCurrentUserLogo: FastifyPluginAsync = async (fastify) => {
fastify, fastify,
request, request,
folderInUploadsFolder: 'users', folderInUploadsFolder: 'users',
maximumFileSize: maximumImageSize, maximumFileSize: MAXIMUM_IMAGE_SIZE,
supportedFileMimetype: supportedImageMimetype supportedFileMimetype: SUPPORTED_IMAGE_MIMETYPE
}) })
await prisma.user.update({ await prisma.user.update({
where: { id: request.user.current.id }, 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 { application } from '../../../../../application.js'
import { authenticateUserTest } from '../../../../../__test__/utils/authenticateUserTest.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' import { userSettingsExample } from '../../../../../models/UserSettings.js'
describe('PUT /users/current/settings', () => { await tap.test('PUT /users/current/settings', async (t) => {
it('succeeds and edit the theme, language, isPublicEmail and isPublicGuilds', async () => { t.afterEach(() => {
const newSettings = { sinon.restore()
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
)
}) })
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 = { const newSettings = {
language: 'somerandomlanguage' language: 'somerandomlanguage'
} }
prismaMock.userSetting.findFirst.mockResolvedValue(userSettingsExample) const { accessToken, userSettingStubValue } = await authenticateUserTest()
const { accessToken } = await authenticateUserTest() sinon.stub(prisma, 'userSetting').value({
...userSettingStubValue,
findFirst: async () => {
return userSettingsExample
},
update: async () => {
return {
...userSettingsExample,
...newSettings
}
}
})
const response = await application.inject({ const response = await application.inject({
method: 'PUT', method: 'PUT',
url: '/users/current/settings', url: '/users/current/settings',
@ -51,6 +75,6 @@ describe('PUT /users/current/settings', () => {
}, },
payload: newSettings 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 { 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 { refreshTokenExample } from '../../../../models/RefreshToken.js'
import { expiresIn } from '../../../../tools/utils/jwtToken.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', () => { await tap.test('POST /users/refresh-token', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
const { refreshToken } = await authenticateUserTest() sinon.restore()
prismaMock.refreshToken.findFirst.mockResolvedValue({ })
...refreshTokenExample,
id: 1, await t.test('succeeds', async (t) => {
token: refreshToken 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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/refresh-token', url: '/users/refresh-token',
headers: {
authorization: `Bearer ${accessToken}`
},
payload: { refreshToken } payload: { refreshToken }
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.type).toEqual('Bearer') t.equal(responseJson.type, 'Bearer')
expect(responseJson.expiresIn).toEqual(expiresIn) t.equal(responseJson.expiresIn, expiresIn)
expect(typeof responseJson.accessToken).toEqual('string') 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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/refresh-token', url: '/users/refresh-token',
payload: { refreshToken: 'somerandomtoken' } payload: { refreshToken: 'somerandomtoken' }
}) })
expect(response.statusCode).toEqual(403) t.equal(response.statusCode, 403)
}) })
it('fails with invalid jwt refreshToken', async () => { await t.test('fails with invalid jwt refreshToken', async (t) => {
const { refreshToken } = await authenticateUserTest() const { refreshToken, refreshTokenStubValue } = await authenticateUserTest()
prismaMock.refreshToken.findFirst.mockResolvedValue(refreshTokenExample) sinon.stub(prisma, 'refreshToken').value({
...refreshTokenStubValue,
findFirst: async () => {
return refreshTokenExample
}
})
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/refresh-token', url: '/users/refresh-token',
payload: { refreshToken } 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 ms from 'ms'
import { application } from '../../../../application.js' import { application } from '../../../../application.js'
import prisma from '../../../../tools/database/prisma.js'
import { userExample } from '../../../../models/User.js' import { userExample } from '../../../../models/User.js'
import { userSettingsExample } from '../../../../models/UserSettings.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', () => { await tap.test('POST /users/reset-password', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.user.findUnique.mockResolvedValue(userExample) sinon.restore()
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)
}) })
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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/reset-password?redirectURI=https://redirecturi.com', url: '/users/reset-password?redirectURI=https://redirecturi.com',
payload: { email: userExample.email } payload: { email: userExample.email }
}) })
expect(response.statusCode).toEqual(400) t.equal(response.statusCode, 200)
}) })
it('fails with unconfirmed account', async () => { await t.test("fails with email that doesn't exist", async (t) => {
prismaMock.user.findUnique.mockResolvedValue({ sinon.stub(prisma, 'user').value({
...userExample, findUnique: async () => {
isConfirmed: false return null
}
}) })
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/reset-password?redirectURI=https://redirecturi.com', url: '/users/reset-password?redirectURI=https://redirecturi.com',
payload: { email: userExample.email } payload: { email: userExample.email }
}) })
expect(response.statusCode).toEqual(400) t.equal(response.statusCode, 400)
}) })
it("fails if userSettings doenst' exist", async () => { await t.test('fails with unconfirmed account', async (t) => {
prismaMock.user.findUnique.mockResolvedValue(userExample) sinon.stub(prisma, 'user').value({
prismaMock.userSetting.findFirst.mockResolvedValue(null) findUnique: async () => {
return {
...userExample,
isConfirmed: false
}
}
})
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/reset-password?redirectURI=https://redirecturi.com', url: '/users/reset-password?redirectURI=https://redirecturi.com',
payload: { email: userExample.email } payload: { email: userExample.email }
}) })
expect(response.statusCode).toEqual(400) t.equal(response.statusCode, 400)
}) })
it('fails with a request already in progress', async () => { await t.test("fails if userSettings doesn't exist", async (t) => {
prismaMock.user.findUnique.mockResolvedValue({ sinon.stub(prisma, 'user').value({
...userExample, findUnique: async () => {
temporaryExpirationToken: new Date(Date.now() + ms('1 hour')) return userExample
}
})
sinon.stub(prisma, 'userSetting').value({
findFirst: async () => {
return null
}
}) })
prismaMock.userSetting.findFirst.mockResolvedValue(userSettingsExample)
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/reset-password?redirectURI=https://redirecturi.com', url: '/users/reset-password?redirectURI=https://redirecturi.com',
payload: { email: userExample.email } 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 ms from 'ms'
import { application } from '../../../../application.js' import { application } from '../../../../application.js'
import prisma from '../../../../tools/database/prisma.js'
import { userExample } from '../../../../models/User.js' import { userExample } from '../../../../models/User.js'
import { prismaMock } from '../../../../__test__/setup.js'
describe('PUT /users/reset-password', () => { await tap.test('PUT /users/reset-password', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.user.findFirst.mockResolvedValue({ sinon.restore()
...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)
}) })
it('fails with expired temporaryToken', async () => { await t.test('succeeds', async (t) => {
prismaMock.user.findFirst.mockResolvedValue(userExample) 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({ const response = await application.inject({
method: 'PUT', method: 'PUT',
url: '/users/reset-password', url: '/users/reset-password',
@ -31,6 +33,31 @@ describe('PUT /users/reset-password', () => {
temporaryToken: userExample.temporaryToken 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' '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() const temporaryToken = randomUUID()
await prisma.user.update({ await prisma.user.update({
where: { where: {
@ -80,12 +86,6 @@ export const postResetPasswordUser: FastifyPluginAsync = async (fastify) => {
temporaryToken temporaryToken
} }
}) })
const userSettings = await prisma.userSetting.findFirst({
where: { userId: user.id }
})
if (userSettings == null) {
throw fastify.httpErrors.badRequest()
}
await sendEmail({ await sendEmail({
type: 'reset-password', type: 'reset-password',
email, email,

View File

@ -1,46 +1,68 @@
import tap from 'tap'
import sinon from 'sinon'
import bcrypt from 'bcryptjs' import bcrypt from 'bcryptjs'
import { application } from '../../../../application.js' 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 { userExample } from '../../../../models/User.js'
import { refreshTokenExample } from '../../../../models/RefreshToken.js'
import { expiresIn } from '../../../../tools/utils/jwtToken.js' import { expiresIn } from '../../../../tools/utils/jwtToken.js'
import { prismaMock } from '../../../../__test__/setup.js'
const payload = { const payload = {
email: userExample.email, email: userExample.email,
password: userExample.password password: userExample.password
} }
describe('POST /users/signin', () => { await tap.test('POST /users/signin', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.user.findUnique.mockResolvedValue({ sinon.restore()
...userExample, })
password: await bcrypt.hash(userExample.password as string, 12)
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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/signin', url: '/users/signin',
payload payload
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
expect(responseJson.type).toEqual('Bearer') t.equal(responseJson.type, 'Bearer')
expect(responseJson.expiresIn).toEqual(expiresIn) t.equal(responseJson.expiresIn, expiresIn)
}) })
it('fails with invalid user', async () => { await t.test('fails with invalid user', async (t) => {
prismaMock.user.findUnique.mockResolvedValue(null) sinon.stub(prisma, 'user').value({
findUnique: () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/signin', url: '/users/signin',
payload payload
}) })
expect(response.statusCode).toEqual(400) t.equal(response.statusCode, 400)
}) })
it('fails with invalid email', async () => { await t.test('fails with invalid email', async (t) => {
prismaMock.user.findUnique.mockResolvedValue(null) sinon.stub(prisma, 'user').value({
findUnique: () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/signin', url: '/users/signin',
@ -49,32 +71,40 @@ describe('POST /users/signin', () => {
email: 'incorrect-email' email: 'incorrect-email'
} }
}) })
expect(response.statusCode).toEqual(400) t.equal(response.statusCode, 400)
}) })
it("fails if user hasn't got a password", async () => { await t.test("fails if user hasn't got a password", async (t) => {
prismaMock.user.findUnique.mockResolvedValue({ sinon.stub(prisma, 'user').value({
...userExample, findUnique: () => {
password: null return {
...userExample,
password: null
}
}
}) })
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/signin', url: '/users/signin',
payload payload
}) })
expect(response.statusCode).toEqual(400) t.equal(response.statusCode, 400)
}) })
it('fails with incorrect password', async () => { await t.test('fails with incorrect password', async (t) => {
prismaMock.user.findUnique.mockResolvedValue(userExample) sinon.stub(prisma, 'user').value({
findUnique: async () => {
return userExample
}
})
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/signin', url: '/users/signin',
payload: { payload: {
...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 tap from 'tap'
import { prismaMock } from '../../../../__test__/setup.js' import sinon from 'sinon'
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
describe('DELETE /users/signout', () => { import { application } from '../../../../application.js'
it('succeeds', async () => { import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
prismaMock.refreshToken.deleteMany.mockResolvedValue({ import prisma from '../../../../tools/database/prisma.js'
count: 1
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({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: '/users/signout', url: '/users/signout',
@ -15,14 +25,14 @@ describe('DELETE /users/signout', () => {
authorization: `Bearer ${accessToken}` 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({ const response = await application.inject({
method: 'DELETE', method: 'DELETE',
url: '/users/signout' 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 tap from 'tap'
import { refreshTokenExample } from '../../../../models/RefreshToken.js' import sinon from 'sinon'
import { prismaMock } from '../../../../__test__/setup.js'
describe('POST /users/signout', () => { import { application } from '../../../../application.js'
it('succeeds', async () => { import prisma from '../../../../tools/database/prisma.js'
prismaMock.refreshToken.findFirst.mockResolvedValue(refreshTokenExample) 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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/signout', url: '/users/signout',
payload: { refreshToken: refreshTokenExample.token } payload: { refreshToken: refreshTokenExample.token }
}) })
expect(response.statusCode).toEqual(200) t.equal(response.statusCode, 200)
}) })
it('fails with invalid refreshToken', async () => { await t.test('fails with invalid refreshToken', async (t) => {
prismaMock.refreshToken.findFirst.mockResolvedValue(null) sinon.stub(prisma, 'refreshToken').value({
findFirst: async () => {
return null
}
})
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/signout', url: '/users/signout',
payload: { refreshToken: 'somerandomtoken' } 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 { application } from '../../../../application.js'
import prisma from '../../../../tools/database/prisma.js'
import { userExample } from '../../../../models/User.js' import { userExample } from '../../../../models/User.js'
import { userSettingsExample } from '../../../../models/UserSettings.js' import { userSettingsExample } from '../../../../models/UserSettings.js'
import { prismaMock } from '../../../../__test__/setup.js' import { emailTransporter } from '../../../../tools/email/emailTransporter.js'
const payload = { const payload = {
name: userExample.name, name: userExample.name,
@ -11,26 +15,44 @@ const payload = {
language: userSettingsExample.language language: userSettingsExample.language
} }
describe('POST /users/signup', () => { await tap.test('POST /users/signup', async (t) => {
it('succeeds', async () => { t.afterEach(() => {
prismaMock.user.findFirst.mockResolvedValue(null) sinon.restore()
prismaMock.user.create.mockResolvedValue(userExample) })
prismaMock.userSetting.create.mockResolvedValue(userSettingsExample)
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({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/signup', url: '/users/signup',
payload payload
}) })
const responseJson = response.json() const responseJson = response.json()
expect(response.statusCode).toEqual(201) t.equal(response.statusCode, 201)
expect(responseJson.user.name).toEqual(userExample.name) t.equal(responseJson.user.name, userExample.name)
expect(responseJson.user.email).toEqual(userExample.email) t.equal(responseJson.user.email, userExample.email)
}) })
it('fails with invalid email', async () => { await t.test('fails with invalid email', async (t) => {
prismaMock.user.findFirst.mockResolvedValue(null) sinon.stub(prisma, 'user').value({
prismaMock.user.create.mockResolvedValue(userExample) findFirst: async () => {
prismaMock.userSetting.create.mockResolvedValue(userSettingsExample) return null
}
})
sinon.stub(emailTransporter, 'sendMail').value(() => {})
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/signup', url: '/users/signup',
@ -39,16 +61,21 @@ describe('POST /users/signup', () => {
email: 'incorrect-email' email: 'incorrect-email'
} }
}) })
expect(response.statusCode).toEqual(400) t.equal(response.statusCode, 400)
}) })
it('fails with already taken `name` or `email`', async () => { await t.test('fails with already taken `name` or `email`', async (t) => {
prismaMock.user.findFirst.mockResolvedValue(userExample) sinon.stub(prisma, 'user').value({
findFirst: async () => {
return userExample
}
})
sinon.stub(emailTransporter, 'sendMail').value(() => {})
const response = await application.inject({ const response = await application.inject({
method: 'POST', method: 'POST',
url: '/users/signup', url: '/users/signup',
payload payload
}) })
expect(response.statusCode).toEqual(400) t.equal(response.statusCode, 400)
}) })
}) })

View File

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

View File

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

View File

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

View File

@ -1,77 +1,98 @@
import tap from 'tap'
import sinon from 'sinon'
import httpErrors from 'http-errors' import httpErrors from 'http-errors'
import jwt from 'jsonwebtoken' import jwt from 'jsonwebtoken'
import { userExample } from '../../../models/User.js'
import { prismaMock } from '../../../__test__/setup.js'
import { getUserWithBearerToken } from '../authenticateUser.js' import { getUserWithBearerToken } from '../authenticateUser.js'
import prisma from '../../database/prisma.js'
import { userExample } from '../../../models/User.js'
const { Unauthorized, Forbidden, BadRequest } = httpErrors const { Unauthorized, Forbidden, BadRequest } = httpErrors
describe('tools/plugins/authenticateUser - getUserWithBearerToken', () => { await tap.test(
afterEach(() => { 'tools/plugins/authenticateUser - getUserWithBearerToken',
jest.clearAllMocks() async (t) => {
}) t.afterEach(() => {
sinon.restore()
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
}) })
jwt.verify = jest.fn<any, any[]>((() => {
return { id: userExample.id, currentStrategy: 'local' } await t.test('shoulds succeeds with the right information', async (t) => {
}) as any) sinon.stub(prisma, 'user').value({
await expect( findUnique: async () => {
async () => await getUserWithBearerToken('Bearer token') return userExample
).rejects.toThrow(BadRequest) }
}) })
}) 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 fastify from 'fastify'
import fastifySocketIo from '../socket-io.js' import fastifySocketIo from '../socket-io.js'
describe('tools/plugins/socket-io', () => { await tap.test('tools/plugins/socket-io', async (t) => {
it('should close socket server on fastify close', async () => { await t.test('should close socket server on fastify close', async (t) => {
const PORT = 3030 const PORT = 3030
const application = fastify() const application = fastify()
await application.register(fastifySocketIo) await application.register(fastifySocketIo)
await application.listen(PORT) await application.listen(PORT)
expect(application.io).not.toBeNull() t.not(application.io, null)
await application.close() await application.close()
}) })
}) })

View File

@ -1,137 +1,216 @@
import tap from 'tap'
import sinon from 'sinon'
import { userExample } from '../../../models/User.js' import { userExample } from '../../../models/User.js'
import { userSettingsExample } from '../../../models/UserSettings.js' import { userSettingsExample } from '../../../models/UserSettings.js'
import { prismaMock } from '../../../__test__/setup.js'
import { OAuthStrategy } from '../OAuthStrategy.js' import { OAuthStrategy } from '../OAuthStrategy.js'
import prisma from '../../database/prisma.js'
import { refreshTokenExample } from '../../../models/RefreshToken.js'
const oauthStrategy = new OAuthStrategy('discord') const oauthStrategy = new OAuthStrategy('discord')
describe('/tools/utils/OAuthStrategy - callbackSignin', () => { await tap.test('tools/utils/OAuthStrategy', async (t) => {
it('should signup the user', async () => { await t.test('callbackSignin', async (t) => {
const name = 'Martin' t.afterEach(() => {
const id = '12345' sinon.restore()
prismaMock.oAuth.findFirst.mockResolvedValue(null)
prismaMock.user.count.mockResolvedValue(0)
prismaMock.user.create.mockResolvedValue({
...userExample,
name
}) })
prismaMock.userSetting.create.mockResolvedValue(userSettingsExample)
prismaMock.oAuth.create.mockResolvedValue({ await t.test('should signup the user', async (t) => {
id: 1, const name = 'Martin'
userId: userExample.id, const id = '12345'
provider: 'discord', sinon.stub(prisma, 'user').value({
providerId: id, count: async () => {
updatedAt: new Date(), return 0
createdAt: new Date() },
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: { await t.test('callbackAddStrategy', async (t) => {
provider: 'discord', t.afterEach(() => {
providerId: id 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 } await t.test(
}) 'should not add the strategy if the user is already connected with it',
expect(prismaMock.user.create).toHaveBeenCalledWith({ async (t) => {
data: { name } const name = userExample.name
}) const id = '12345'
expect(prismaMock.userSetting.create).toHaveBeenCalledWith({ sinon.stub(prisma, 'oAuth').value({
data: { findFirst: async () => {
userId: userExample.id 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' import { buildQueryURL } from '../buildQueryURL.js'
test('/tools/utils/buildQueryUrl', () => { await tap.test('tools/utils/buildQueryUrl', async (t) => {
expect( t.equal(
buildQueryURL('http://localhost:8080', { buildQueryURL('http://localhost:8080', {
test: 'query' test: 'query'
}) }),
).toEqual('http://localhost:8080/?test=query') 'http://localhost:8080/?test=query'
expect( )
t.equal(
buildQueryURL('http://localhost:8080/', { buildQueryURL('http://localhost:8080/', {
test: 'query' test: 'query'
}) }),
).toEqual('http://localhost:8080/?test=query') 'http://localhost:8080/?test=query'
expect( )
t.equal(
buildQueryURL('http://localhost:3000', { buildQueryURL('http://localhost:3000', {
test: 'query', test: 'query',
code: 'abc' 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' import { parseStringNullish } from '../parseStringNullish.js'
const defaultString = 'defaultString' const defaultString = 'defaultString'
describe('/tools/utils/parseStringNullish', () => { await tap.test('tools/utils/parseStringNullish', async (t) => {
it('returns `defaultString` if `string === undefined`', () => { await t.test(
expect(parseStringNullish(defaultString, undefined)).toEqual(defaultString) '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`', () => { await t.test('returns `null` if `string.length === 0`', async (t) => {
expect(parseStringNullish(defaultString, null)).toEqual(null) t.equal(parseStringNullish(defaultString, ''), null)
}) })
it('returns `null` if `string.length === 0`', () => { await t.test('returns `string` if `string.length > 0`', async (t) => {
expect(parseStringNullish(defaultString, '')).toEqual(null)
})
it('returns `string` if `string.length > 0`', () => {
const string = 'myString' const string = 'myString'
expect(parseStringNullish(defaultString, string)).toEqual(string) t.equal(parseStringNullish(defaultString, string), string)
}) })
}) })

View File

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