feat: usage of ESM modules imports (instead of CommonJS) (#5)
Replace `jest` with `tap`.
This commit is contained in:
parent
91a0e2a76f
commit
19b6f96ecf
@ -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'
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
.env
|
.env
|
||||||
build
|
build
|
||||||
coverage
|
coverage
|
||||||
|
.nyc_output
|
||||||
node_modules
|
node_modules
|
||||||
tmp
|
tmp
|
||||||
temp
|
temp
|
||||||
|
@ -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"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
.github/workflows/analyze.yml
vendored
2
.github/workflows/analyze.yml
vendored
@ -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'
|
||||||
|
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@ -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'
|
||||||
|
4
.github/workflows/lint.yml
vendored
4
.github/workflows/lint.yml
vendored
@ -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'
|
||||||
|
4
.github/workflows/release.yml
vendored
4
.github/workflows/release.yml
vendored
@ -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'
|
||||||
|
11
.github/workflows/test.yml
vendored
11
.github/workflows/test.yml
vendored
@ -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
1
.gitignore
vendored
@ -8,6 +8,7 @@ build
|
|||||||
|
|
||||||
# testing
|
# testing
|
||||||
coverage
|
coverage
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
# envs
|
# envs
|
||||||
.env
|
.env
|
||||||
|
@ -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
2
.swcrc
@ -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
8
.taprc
Normal 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
25
.vscode/settings.json
vendored
@ -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"
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
@ -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'
|
||||||
|
@ -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: [
|
||||||
{
|
{
|
||||||
|
@ -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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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"]
|
|
||||||
}
|
|
10990
package-lock.json
generated
10990
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
47
package.json
47
package.json
@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
) => {
|
) => {
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
process.env.JWT_ACCESS_EXPIRES_IN = '15 minutes'
|
|
||||||
process.env.JWT_ACCESS_SECRET = 'accessTokenSecret'
|
|
||||||
process.env.JWT_REFRESH_SECRET = 'refreshTokenSecret'
|
|
@ -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>
|
|
@ -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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,6 @@ 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)
|
||||||
@ -43,9 +42,3 @@ const main = async (): Promise<void> => {
|
|||||||
})
|
})
|
||||||
await application.register(fastifySwagger, swaggerOptions)
|
await application.register(fastifySwagger, swaggerOptions)
|
||||||
await application.register(services)
|
await application.register(services)
|
||||||
}
|
|
||||||
|
|
||||||
main().catch((error) => {
|
|
||||||
console.error(error)
|
|
||||||
process.exit(1)
|
|
||||||
})
|
|
||||||
|
@ -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)
|
|
||||||
})
|
|
||||||
|
@ -34,7 +34,6 @@ 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 }
|
||||||
@ -50,9 +49,3 @@ const main = async (): Promise<void> => {
|
|||||||
where: { logo }
|
where: { logo }
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
main().catch((error) => {
|
|
||||||
console.error(error)
|
|
||||||
process.exit(1)
|
|
||||||
})
|
|
||||||
|
@ -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)
|
const { accessToken } = await authenticateUserTest()
|
||||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
sinon.stub(prisma, 'channel').value({
|
||||||
prismaMock.channel.count.mockResolvedValue(2)
|
findUnique: async () => {
|
||||||
prismaMock.channel.delete.mockResolvedValue(channelExample)
|
return channelExample
|
||||||
prismaMock.channel.findFirst.mockResolvedValue({
|
},
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...channelExample,
|
...channelExample,
|
||||||
id: defaultChannelId
|
id: defaultChannelId
|
||||||
|
}
|
||||||
|
},
|
||||||
|
count: async () => {
|
||||||
|
return 2
|
||||||
|
},
|
||||||
|
delete: async () => {
|
||||||
|
return channelExample
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return memberExample
|
||||||
|
}
|
||||||
})
|
})
|
||||||
const { accessToken } = await authenticateUserTest()
|
|
||||||
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 = {
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'channel').value({
|
||||||
|
findUnique: async () => {
|
||||||
|
return channelExample
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
isOwner: false
|
isOwner: false
|
||||||
}
|
}
|
||||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
}
|
||||||
prismaMock.member.findFirst.mockResolvedValue(member)
|
})
|
||||||
const { accessToken } = await authenticateUserTest()
|
|
||||||
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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}`
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const responseJson = response.json()
|
sinon.stub(prisma, 'member').value({
|
||||||
expect(response.statusCode).toEqual(404)
|
findFirst: async () => {
|
||||||
expect(responseJson.message).toEqual('Channel not found')
|
return null
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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)
|
const { accessToken } = await authenticateUserTest()
|
||||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
sinon.stub(prisma, 'channel').value({
|
||||||
prismaMock.channel.update.mockResolvedValue(channelExample)
|
findUnique: async () => {
|
||||||
prismaMock.channel.findFirst.mockResolvedValue({
|
return channelExample
|
||||||
|
},
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...channelExample,
|
...channelExample,
|
||||||
id: defaultChannelId
|
id: defaultChannelId
|
||||||
|
}
|
||||||
|
},
|
||||||
|
update: async () => {
|
||||||
|
return {
|
||||||
|
...channelExample,
|
||||||
|
name: newName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return memberExample
|
||||||
|
}
|
||||||
})
|
})
|
||||||
const { accessToken } = await authenticateUserTest()
|
|
||||||
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 = {
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'channel').value({
|
||||||
|
findUnique: async () => {
|
||||||
|
return channelExample
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
isOwner: false
|
isOwner: false
|
||||||
}
|
}
|
||||||
prismaMock.channel.findUnique.mockResolvedValue(channelExample)
|
}
|
||||||
prismaMock.member.findFirst.mockResolvedValue(member)
|
})
|
||||||
const { accessToken } = await authenticateUserTest()
|
|
||||||
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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({
|
})
|
||||||
|
|
||||||
|
await t.test('succeeds', async (t) => {
|
||||||
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'channel').value({
|
||||||
|
findUnique: async () => {
|
||||||
|
return channelExample
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
user: userExample
|
user: userExample
|
||||||
} as any)
|
}
|
||||||
prismaMock.message.findMany.mockResolvedValue([messageExample])
|
}
|
||||||
const { accessToken } = await authenticateUserTest()
|
})
|
||||||
|
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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({
|
})
|
||||||
|
|
||||||
|
await t.test('succeeds', async (t) => {
|
||||||
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'channel').value({
|
||||||
|
findUnique: async () => {
|
||||||
|
return channelExample
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
user: userExample
|
user: userExample
|
||||||
} as any)
|
}
|
||||||
prismaMock.message.create.mockResolvedValue(messageExample)
|
}
|
||||||
const { accessToken } = await authenticateUserTest()
|
})
|
||||||
|
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)
|
const { accessToken } = await authenticateUserTest()
|
||||||
prismaMock.member.findFirst.mockResolvedValue({
|
sinon.stub(prisma, 'channel').value({
|
||||||
|
findUnique: async () => {
|
||||||
|
return channelExample
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
user: userExample
|
user: userExample
|
||||||
} as any)
|
}
|
||||||
const { accessToken } = await authenticateUserTest()
|
}
|
||||||
|
})
|
||||||
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')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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: {
|
||||||
|
@ -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()
|
||||||
|
})
|
||||||
|
|
||||||
|
await t.test('succeeds and delete the guild', async (t) => {
|
||||||
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
isOwner: true,
|
isOwner: true,
|
||||||
guild: guildExample
|
guild: guildExample
|
||||||
} as any)
|
}
|
||||||
prismaMock.guild.delete.mockResolvedValue(guildExample)
|
}
|
||||||
const { accessToken } = await authenticateUserTest()
|
})
|
||||||
|
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({
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
isOwner: false,
|
isOwner: false,
|
||||||
guild: guildExample
|
guild: guildExample
|
||||||
} as any)
|
}
|
||||||
const { accessToken } = await authenticateUserTest()
|
}
|
||||||
|
})
|
||||||
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')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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]', () => {
|
|
||||||
it('succeeds', async () => {
|
|
||||||
const defaultChannelId = 5
|
const defaultChannelId = 5
|
||||||
prismaMock.member.findFirst.mockResolvedValue({
|
|
||||||
|
await tap.test('GET /guilds/[guildId]', async (t) => {
|
||||||
|
t.afterEach(() => {
|
||||||
|
sinon.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
await t.test('succeeds', async (t) => {
|
||||||
|
const { accessToken, user } = await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
guild: guildExample,
|
guild: guildExample,
|
||||||
user: userExample
|
user: userExample
|
||||||
} as any)
|
}
|
||||||
prismaMock.channel.findFirst.mockResolvedValue({
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'channel').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...channelExample,
|
...channelExample,
|
||||||
id: defaultChannelId
|
id: defaultChannelId
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
const { accessToken, user } = await authenticateUserTest()
|
|
||||||
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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]', () => {
|
|
||||||
it('succeeds and edit the guild', async () => {
|
|
||||||
const defaultChannelId = 5
|
const defaultChannelId = 5
|
||||||
const newName = 'New guild name'
|
const newName = 'New guild name'
|
||||||
const newDescription = 'New guild description'
|
const newDescription = 'New guild description'
|
||||||
prismaMock.member.findFirst.mockResolvedValue({
|
|
||||||
|
await tap.test('PUT /guilds/[guildId]', async (t) => {
|
||||||
|
t.afterEach(() => {
|
||||||
|
sinon.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
await t.test('succeeds and edit the guild', async (t) => {
|
||||||
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
isOwner: true,
|
isOwner: true,
|
||||||
guild: guildExample
|
guild: guildExample
|
||||||
} as any)
|
}
|
||||||
prismaMock.guild.update.mockResolvedValue({
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'channel').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
|
...channelExample,
|
||||||
|
id: defaultChannelId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'guild').value({
|
||||||
|
update: async () => {
|
||||||
|
return {
|
||||||
...guildExample,
|
...guildExample,
|
||||||
name: newName,
|
name: newName,
|
||||||
description: newDescription
|
description: newDescription
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
prismaMock.channel.findFirst.mockResolvedValue({
|
|
||||||
...channelExample,
|
|
||||||
id: defaultChannelId
|
|
||||||
})
|
|
||||||
const { accessToken } = await authenticateUserTest()
|
|
||||||
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 { accessToken } = await authenticateUserTest()
|
||||||
const newDescription = 'New guild description'
|
sinon.stub(prisma, 'member').value({
|
||||||
prismaMock.member.findFirst.mockResolvedValue({
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
isOwner: false,
|
isOwner: false,
|
||||||
guild: guildExample
|
guild: guildExample
|
||||||
} as any)
|
}
|
||||||
const { accessToken } = await authenticateUserTest()
|
}
|
||||||
|
})
|
||||||
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')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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', () => {
|
|
||||||
it('succeeds', async () => {
|
|
||||||
const defaultChannelId = 5
|
const defaultChannelId = 5
|
||||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
|
||||||
prismaMock.channel.create.mockResolvedValue(channelExample)
|
await tap.test('POST /guilds/[guildId]/channels', async (t) => {
|
||||||
prismaMock.channel.findFirst.mockResolvedValue({
|
t.afterEach(() => {
|
||||||
|
sinon.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
await t.test('succeeds', async (t) => {
|
||||||
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return memberExample
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'channel').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...channelExample,
|
...channelExample,
|
||||||
id: defaultChannelId
|
id: defaultChannelId
|
||||||
|
}
|
||||||
|
},
|
||||||
|
create: async () => {
|
||||||
|
return channelExample
|
||||||
|
}
|
||||||
})
|
})
|
||||||
const { accessToken } = await authenticateUserTest()
|
|
||||||
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 = {
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
isOwner: false
|
isOwner: false
|
||||||
}
|
}
|
||||||
prismaMock.member.findFirst.mockResolvedValue(member)
|
}
|
||||||
const { accessToken } = await authenticateUserTest()
|
})
|
||||||
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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 },
|
||||||
|
@ -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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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
|
const { accessToken } = await authenticateUserTest()
|
||||||
prismaMock.guild.findUnique.mockResolvedValue(guildExample)
|
sinon.stub(prisma, 'member').value({
|
||||||
prismaMock.member.findFirst.mockResolvedValue(memberExample)
|
findFirst: async () => {
|
||||||
prismaMock.channel.findFirst.mockResolvedValue({
|
return memberExample
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'channel').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...channelExample,
|
...channelExample,
|
||||||
id: defaultChannelId
|
id: defaultChannelId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'guild').value({
|
||||||
|
findUnique: async () => {
|
||||||
|
return guildExample
|
||||||
|
}
|
||||||
})
|
})
|
||||||
const { accessToken } = await authenticateUserTest()
|
|
||||||
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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({
|
|
||||||
|
await t.test('succeeds', async (t) => {
|
||||||
|
const { accessToken, user } = await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'guild').value({
|
||||||
|
create: async () => {
|
||||||
|
return guildExample
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
create: async () => {
|
||||||
|
return memberExample
|
||||||
|
},
|
||||||
|
findUnique: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
...userExample
|
...userExample
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'channel').value({
|
||||||
|
create: async () => {
|
||||||
|
return channelExample
|
||||||
|
}
|
||||||
})
|
})
|
||||||
prismaMock.channel.create.mockResolvedValue(channelExample)
|
|
||||||
const { accessToken, user } = await authenticateUserTest()
|
|
||||||
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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()
|
||||||
|
})
|
||||||
|
|
||||||
|
await t.test('succeeds', async (t) => {
|
||||||
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'message').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...messageExample,
|
...messageExample,
|
||||||
channel: channelExample
|
channel: channelExample
|
||||||
} as any)
|
}
|
||||||
prismaMock.member.findFirst.mockResolvedValue({
|
},
|
||||||
|
delete: async () => {
|
||||||
|
return messageExample
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
user: userExample
|
user: userExample
|
||||||
} as any)
|
}
|
||||||
prismaMock.message.delete.mockResolvedValue(messageExample)
|
}
|
||||||
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}`,
|
||||||
@ -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({
|
const { accessToken } = await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'message').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...messageExample,
|
...messageExample,
|
||||||
channel: channelExample
|
channel: channelExample
|
||||||
} as any)
|
}
|
||||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
}
|
||||||
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: `/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({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...messageExample,
|
...messageExample,
|
||||||
channel: channelExample
|
channel: channelExample
|
||||||
} as any)
|
}
|
||||||
prismaMock.member.findFirst.mockResolvedValue({
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
userId: randomUserIdOwnerOfMessage
|
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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(() => {
|
||||||
|
sinon.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
await t.test('succeeds', async (t) => {
|
||||||
|
const { accessToken } = await authenticateUserTest()
|
||||||
const newValue = 'some message'
|
const newValue = 'some message'
|
||||||
prismaMock.message.findFirst.mockResolvedValue({
|
sinon.stub(prisma, 'message').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...messageExample,
|
...messageExample,
|
||||||
channel: channelExample
|
channel: channelExample
|
||||||
} as any)
|
}
|
||||||
prismaMock.member.findFirst.mockResolvedValue({
|
},
|
||||||
|
update: async () => {
|
||||||
|
return {
|
||||||
|
...messageExample,
|
||||||
|
value: newValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
user: userExample
|
user: userExample
|
||||||
} as any)
|
}
|
||||||
prismaMock.message.update.mockResolvedValue({
|
}
|
||||||
...messageExample,
|
|
||||||
value: newValue
|
|
||||||
})
|
})
|
||||||
const { accessToken } = await authenticateUserTest()
|
|
||||||
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 { accessToken } = await authenticateUserTest()
|
||||||
const newValue = 'some message'
|
const newValue = 'some message'
|
||||||
prismaMock.message.findFirst.mockResolvedValue({
|
sinon.stub(prisma, 'message').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...messageExample,
|
...messageExample,
|
||||||
channel: channelExample
|
channel: channelExample
|
||||||
} as any)
|
}
|
||||||
prismaMock.member.findFirst.mockResolvedValue(null)
|
}
|
||||||
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: `/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(
|
||||||
|
'fails if the member is not the owner of the message',
|
||||||
|
async (t) => {
|
||||||
|
const { accessToken } = await authenticateUserTest()
|
||||||
const newValue = 'some message'
|
const newValue = 'some message'
|
||||||
const randomUserIdOwnerOfMessage = 14
|
const randomUserIdOwnerOfMessage = 14
|
||||||
prismaMock.message.findFirst.mockResolvedValue({
|
sinon.stub(prisma, 'message').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...messageExample,
|
...messageExample,
|
||||||
channel: channelExample
|
channel: channelExample
|
||||||
} as any)
|
}
|
||||||
prismaMock.member.findFirst.mockResolvedValue({
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'member').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...memberExample,
|
...memberExample,
|
||||||
userId: randomUserIdOwnerOfMessage
|
userId: randomUserIdOwnerOfMessage
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
const { accessToken } = await authenticateUserTest()
|
|
||||||
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
|
})
|
||||||
|
t.equal(response.statusCode, 400)
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
expect(response.statusCode).toEqual(400)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
@ -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')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,14 +1,23 @@
|
|||||||
|
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 { prismaMock } from '../../../../__test__/setup.js'
|
|
||||||
|
|
||||||
describe('GET /users/confirm-email', () => {
|
await tap.test('GET /users/confirm-email', async (t) => {
|
||||||
it('should succeeds', async () => {
|
t.afterEach(() => {
|
||||||
prismaMock.user.findFirst.mockResolvedValue(userExample)
|
sinon.restore()
|
||||||
prismaMock.user.update.mockResolvedValue({
|
})
|
||||||
...userExample,
|
|
||||||
isConfirmed: true,
|
await t.test('succeeds', async (t) => {
|
||||||
temporaryToken: null
|
sinon.stub(prisma, 'user').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return userExample
|
||||||
|
},
|
||||||
|
update: async () => {
|
||||||
|
return { ...userExample, isConfirmed: true, temporaryToken: null }
|
||||||
|
}
|
||||||
})
|
})
|
||||||
const response = await application.inject({
|
const response = await application.inject({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
@ -17,15 +26,17 @@ describe('GET /users/confirm-email', () => {
|
|||||||
temporaryToken: userExample.temporaryToken ?? ''
|
temporaryToken: userExample.temporaryToken ?? ''
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
expect(response.statusCode).toEqual(200)
|
t.equal(response.statusCode, 200)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should fails with invalid `temporaryToken`', async () => {
|
await t.test('should fails with invalid `temporaryToken`', async (t) => {
|
||||||
prismaMock.user.findFirst.mockResolvedValue(null)
|
sinon.stub(prisma, 'user').value({
|
||||||
prismaMock.user.update.mockResolvedValue({
|
findFirst: async () => {
|
||||||
...userExample,
|
return null
|
||||||
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 +45,6 @@ describe('GET /users/confirm-email', () => {
|
|||||||
temporaryToken: userExample.temporaryToken ?? ''
|
temporaryToken: userExample.temporaryToken ?? ''
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
expect(response.statusCode).toEqual(403)
|
t.equal(response.statusCode, 403)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,15 +1,29 @@
|
|||||||
|
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 { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
import { authenticateUserTest } from '../../../../__test__/utils/authenticateUserTest.js'
|
||||||
import { prismaMock } from '../../../../__test__/setup.js'
|
|
||||||
import { userExample } from '../../../../models/User.js'
|
|
||||||
|
|
||||||
describe('PUT /users/current', () => {
|
await tap.test('PUT /users/current', async (t) => {
|
||||||
it('succeeds with valid accessToken and valid name', async () => {
|
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({
|
||||||
|
...userStubValue,
|
||||||
|
findFirst: async () => {
|
||||||
|
return null
|
||||||
|
},
|
||||||
|
update: async () => {
|
||||||
|
return {
|
||||||
...user,
|
...user,
|
||||||
name: newName
|
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({
|
||||||
|
...userStubValue,
|
||||||
|
findFirst: async () => {
|
||||||
|
return null
|
||||||
|
},
|
||||||
|
update: async () => {
|
||||||
|
return {
|
||||||
...user,
|
...user,
|
||||||
status: newStatus
|
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({
|
||||||
|
...userStubValue,
|
||||||
|
findFirst: async () => {
|
||||||
|
return null
|
||||||
|
},
|
||||||
|
update: async () => {
|
||||||
|
return {
|
||||||
...user,
|
...user,
|
||||||
website: newWebsite
|
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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 },
|
||||||
|
@ -1,22 +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 { 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(() => {
|
||||||
|
sinon.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
await t.test(
|
||||||
|
'succeeds and edit the theme, language, isPublicEmail and isPublicGuilds',
|
||||||
|
async (t) => {
|
||||||
const newSettings = {
|
const newSettings = {
|
||||||
theme: 'light',
|
theme: 'light',
|
||||||
language: 'fr',
|
language: 'fr',
|
||||||
isPublicEmail: true,
|
isPublicEmail: true,
|
||||||
isPublicGuilds: true
|
isPublicGuilds: true
|
||||||
}
|
}
|
||||||
prismaMock.userSetting.findFirst.mockResolvedValue(userSettingsExample)
|
const { accessToken, userSettingStubValue } = await authenticateUserTest()
|
||||||
prismaMock.userSetting.update.mockResolvedValue({
|
sinon.stub(prisma, 'userSetting').value({
|
||||||
|
...userSettingStubValue,
|
||||||
|
findFirst: async () => {
|
||||||
|
return userSettingsExample
|
||||||
|
},
|
||||||
|
update: async () => {
|
||||||
|
return {
|
||||||
...userSettingsExample,
|
...userSettingsExample,
|
||||||
...newSettings
|
...newSettings
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
const { accessToken } = await authenticateUserTest()
|
|
||||||
const response = await application.inject({
|
const response = await application.inject({
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
url: '/users/current/settings',
|
url: '/users/current/settings',
|
||||||
@ -26,23 +42,31 @@ describe('PUT /users/current/settings', () => {
|
|||||||
payload: newSettings
|
payload: newSettings
|
||||||
})
|
})
|
||||||
const responseJson = response.json()
|
const responseJson = response.json()
|
||||||
expect(response.statusCode).toEqual(200)
|
t.equal(response.statusCode, 200)
|
||||||
expect(responseJson.settings.theme).toEqual(newSettings.theme)
|
t.equal(responseJson.settings.theme, newSettings.theme)
|
||||||
expect(responseJson.settings.language).toEqual(newSettings.language)
|
t.equal(responseJson.settings.language, newSettings.language)
|
||||||
expect(responseJson.settings.isPublicEmail).toEqual(
|
t.equal(responseJson.settings.isPublicEmail, newSettings.isPublicEmail)
|
||||||
newSettings.isPublicEmail
|
t.equal(responseJson.settings.isPublicGuilds, newSettings.isPublicGuilds)
|
||||||
|
}
|
||||||
)
|
)
|
||||||
expect(responseJson.settings.isPublicGuilds).toEqual(
|
|
||||||
newSettings.isPublicGuilds
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('fails with invalid language', async () => {
|
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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({
|
})
|
||||||
|
|
||||||
|
await t.test('succeeds', async (t) => {
|
||||||
|
const { accessToken, refreshToken, refreshTokenStubValue } =
|
||||||
|
await authenticateUserTest()
|
||||||
|
sinon.stub(prisma, 'refreshToken').value({
|
||||||
|
...refreshTokenStubValue,
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...refreshTokenExample,
|
...refreshTokenExample,
|
||||||
id: 1,
|
id: 1,
|
||||||
token: refreshToken
|
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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)
|
})
|
||||||
|
|
||||||
|
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(200)
|
t.equal(response.statusCode, 200)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("fails with email that doesn't exist", async () => {
|
await t.test("fails with email that doesn't exist", async (t) => {
|
||||||
|
sinon.stub(prisma, 'user').value({
|
||||||
|
findUnique: async () => {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
})
|
||||||
const response = await application.inject({
|
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 unconfirmed account', async () => {
|
await t.test('fails with unconfirmed account', async (t) => {
|
||||||
prismaMock.user.findUnique.mockResolvedValue({
|
sinon.stub(prisma, 'user').value({
|
||||||
|
findUnique: async () => {
|
||||||
|
return {
|
||||||
...userExample,
|
...userExample,
|
||||||
isConfirmed: false
|
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 if userSettings doenst' exist", async () => {
|
await t.test("fails if userSettings doesn't exist", async (t) => {
|
||||||
prismaMock.user.findUnique.mockResolvedValue(userExample)
|
sinon.stub(prisma, 'user').value({
|
||||||
prismaMock.userSetting.findFirst.mockResolvedValue(null)
|
findUnique: async () => {
|
||||||
|
return userExample
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'userSetting').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
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 with a request already in progress', async () => {
|
await t.test('fails with a request already in progress', async (t) => {
|
||||||
prismaMock.user.findUnique.mockResolvedValue({
|
sinon.stub(prisma, 'user').value({
|
||||||
|
findUnique: async () => {
|
||||||
|
return {
|
||||||
...userExample,
|
...userExample,
|
||||||
|
temporaryToken: 'random-token',
|
||||||
temporaryExpirationToken: new Date(Date.now() + ms('1 hour'))
|
temporaryExpirationToken: new Date(Date.now() + ms('1 hour'))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(prisma, 'userSetting').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return userSettingsExample
|
||||||
|
}
|
||||||
})
|
})
|
||||||
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,14 +1,29 @@
|
|||||||
|
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()
|
||||||
|
})
|
||||||
|
|
||||||
|
await t.test('succeeds', async (t) => {
|
||||||
|
const temporaryToken = 'random-token'
|
||||||
|
sinon.stub(prisma, 'user').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
...userExample,
|
...userExample,
|
||||||
|
temporaryToken,
|
||||||
temporaryExpirationToken: new Date(Date.now() + ms('1 hour'))
|
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',
|
||||||
@ -18,11 +33,23 @@ describe('PUT /users/reset-password', () => {
|
|||||||
temporaryToken: userExample.temporaryToken
|
temporaryToken: userExample.temporaryToken
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
expect(response.statusCode).toEqual(200)
|
t.equal(response.statusCode, 200)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('fails with expired temporaryToken', async () => {
|
await t.test('fails with expired temporaryToken', 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 +58,6 @@ describe('PUT /users/reset-password', () => {
|
|||||||
temporaryToken: userExample.temporaryToken
|
temporaryToken: userExample.temporaryToken
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
expect(response.statusCode).toEqual(400)
|
t.equal(response.statusCode, 400)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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,
|
||||||
|
@ -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({
|
||||||
|
findUnique: () => {
|
||||||
|
return {
|
||||||
...userExample,
|
...userExample,
|
||||||
password: null
|
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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
|
||||||
|
@ -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',
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import * as Prisma from '@prisma/client'
|
import Prisma from '@prisma/client'
|
||||||
|
|
||||||
const { PrismaClient } = Prisma
|
const { PrismaClient } = Prisma
|
||||||
|
|
||||||
|
@ -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 () => {
|
await t.test('shoulds succeeds with the right information', async (t) => {
|
||||||
prismaMock.user.findUnique.mockResolvedValue(userExample)
|
sinon.stub(prisma, 'user').value({
|
||||||
|
findUnique: async () => {
|
||||||
|
return userExample
|
||||||
|
}
|
||||||
|
})
|
||||||
const currentStrategy = 'local'
|
const currentStrategy = 'local'
|
||||||
jwt.verify = jest.fn<any, any[]>((() => {
|
sinon.stub(jwt, 'verify').value(() => {
|
||||||
return { id: userExample.id, currentStrategy }
|
return { id: userExample.id, currentStrategy }
|
||||||
}) as any)
|
})
|
||||||
const userWithBearerToken = await getUserWithBearerToken('Bearer token')
|
const userWithBearerToken = await getUserWithBearerToken('Bearer token')
|
||||||
expect(userWithBearerToken.current.id).toEqual(userExample.id)
|
t.equal(userWithBearerToken.current.id, userExample.id)
|
||||||
expect(userWithBearerToken.current.name).toEqual(userExample.name)
|
t.equal(userWithBearerToken.current.name, userExample.name)
|
||||||
expect(userWithBearerToken.accessToken).toEqual('token')
|
t.equal(userWithBearerToken.accessToken, 'token')
|
||||||
expect(userWithBearerToken.currentStrategy).toEqual(currentStrategy)
|
t.equal(userWithBearerToken.currentStrategy, currentStrategy)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('shoulds throws `Unauthorized` if `bearerToken` is not a string', async () => {
|
await t.test(
|
||||||
await expect(
|
'shoulds throws `Unauthorized` if `bearerToken` is not a string',
|
||||||
async () => await getUserWithBearerToken(undefined)
|
async (t) => {
|
||||||
).rejects.toThrow(Unauthorized)
|
await t.rejects(getUserWithBearerToken(undefined), Unauthorized)
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
it('shoulds throws `Unauthorized` if `bearerToken` is not to the right format: `"Bearer token"`', async () => {
|
await t.test(
|
||||||
await expect(
|
'shoulds throws `Unauthorized` if `bearerToken` is not to the right format: `"Bearer token"`',
|
||||||
async () => await getUserWithBearerToken('Bearer')
|
async (t) => {
|
||||||
).rejects.toThrow(Unauthorized)
|
await t.rejects(getUserWithBearerToken('Bearer'), Unauthorized)
|
||||||
await expect(async () => await getUserWithBearerToken('')).rejects.toThrow(
|
await t.rejects(getUserWithBearerToken(''), Unauthorized)
|
||||||
|
await t.rejects(
|
||||||
|
getUserWithBearerToken('Bearer token token2'),
|
||||||
Unauthorized
|
Unauthorized
|
||||||
)
|
)
|
||||||
await expect(
|
}
|
||||||
async () => await getUserWithBearerToken('Bearer token token2')
|
)
|
||||||
).rejects.toThrow(Unauthorized)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('shoulds throws `Forbidden` if invalid `bearerToken` by `jwt.verify`', async () => {
|
await t.test(
|
||||||
jwt.verify = jest.fn<any, any[]>((() => {
|
'shoulds throws `Forbidden` if invalid `bearerToken` by `jwt.verify`',
|
||||||
|
async (t) => {
|
||||||
|
sinon.stub(jwt, 'verify').value(() => {
|
||||||
throw new Error('Invalid token')
|
throw new Error('Invalid token')
|
||||||
}) as any)
|
|
||||||
await expect(
|
|
||||||
async () => await getUserWithBearerToken('Bearer token')
|
|
||||||
).rejects.toThrow(Forbidden)
|
|
||||||
})
|
})
|
||||||
|
await t.rejects(getUserWithBearerToken('Bearer token'), Forbidden)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
it("shoulds throws `Forbidden` if the user doesn't exist", async () => {
|
await t.test(
|
||||||
prismaMock.user.findUnique.mockResolvedValue(null)
|
"shoulds throws `Forbidden` if the user doesn't exist",
|
||||||
jwt.verify = jest.fn<any, any[]>((() => {
|
async (t) => {
|
||||||
|
sinon.stub(prisma, 'user').value({
|
||||||
|
findUnique: async () => {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
})
|
||||||
|
sinon.stub(jwt, 'verify').value(() => {
|
||||||
return { id: userExample.id }
|
return { id: userExample.id }
|
||||||
}) as any)
|
|
||||||
await expect(
|
|
||||||
async () => await getUserWithBearerToken('Bearer token')
|
|
||||||
).rejects.toThrow(Forbidden)
|
|
||||||
})
|
})
|
||||||
|
await t.rejects(getUserWithBearerToken('Bearer token'), Forbidden)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
it('shoulds throws `BadRequest` if the user account is not confirmed', async () => {
|
await t.test(
|
||||||
prismaMock.user.findUnique.mockResolvedValue({
|
'shoulds throws `BadRequest` if the user account is not confirmed',
|
||||||
|
async (t) => {
|
||||||
|
sinon.stub(prisma, 'user').value({
|
||||||
|
findUnique: async () => {
|
||||||
|
return {
|
||||||
...userExample,
|
...userExample,
|
||||||
isConfirmed: false
|
isConfirmed: false
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
jwt.verify = jest.fn<any, any[]>((() => {
|
sinon.stub(jwt, 'verify').value(() => {
|
||||||
return { id: userExample.id, currentStrategy: 'local' }
|
return { id: userExample.id, currentStrategy: 'local' }
|
||||||
}) as any)
|
|
||||||
await expect(
|
|
||||||
async () => await getUserWithBearerToken('Bearer token')
|
|
||||||
).rejects.toThrow(BadRequest)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
await t.rejects(getUserWithBearerToken('Bearer token'), BadRequest)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@ -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()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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) => {
|
||||||
|
t.afterEach(() => {
|
||||||
|
sinon.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
await t.test('should signup the user', async (t) => {
|
||||||
const name = 'Martin'
|
const name = 'Martin'
|
||||||
const id = '12345'
|
const id = '12345'
|
||||||
prismaMock.oAuth.findFirst.mockResolvedValue(null)
|
sinon.stub(prisma, 'user').value({
|
||||||
prismaMock.user.count.mockResolvedValue(0)
|
count: async () => {
|
||||||
prismaMock.user.create.mockResolvedValue({
|
return 0
|
||||||
|
},
|
||||||
|
create: async () => {
|
||||||
|
return {
|
||||||
...userExample,
|
...userExample,
|
||||||
name
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
prismaMock.userSetting.create.mockResolvedValue(userSettingsExample)
|
sinon.stub(prisma, 'refreshToken').value({
|
||||||
prismaMock.oAuth.create.mockResolvedValue({
|
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,
|
id: 1,
|
||||||
userId: userExample.id,
|
userId: userExample.id,
|
||||||
provider: 'discord',
|
provider: 'discord',
|
||||||
providerId: id,
|
providerId: id,
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
createdAt: 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 })
|
await oauthStrategy.callbackSignin({ id, name })
|
||||||
expect(prismaMock.oAuth.findFirst).toHaveBeenCalledWith({
|
t.equal(
|
||||||
|
oAuthCreateSpy.calledWith({
|
||||||
|
data: {
|
||||||
|
userId: userExample.id,
|
||||||
|
provider: 'discord',
|
||||||
|
providerId: id
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
true
|
||||||
|
)
|
||||||
|
t.equal(
|
||||||
|
oAuthFindFirstSpy.calledWith({
|
||||||
where: {
|
where: {
|
||||||
provider: 'discord',
|
provider: 'discord',
|
||||||
providerId: id
|
providerId: id
|
||||||
}
|
}
|
||||||
})
|
}),
|
||||||
expect(prismaMock.user.count).toHaveBeenCalledWith({
|
true
|
||||||
where: { name }
|
)
|
||||||
})
|
t.equal(userCountSpy.calledWith({ where: { name } }), true)
|
||||||
expect(prismaMock.user.create).toHaveBeenCalledWith({
|
t.equal(userCreateSpy.calledWith({ data: { name } }), true)
|
||||||
data: { name }
|
t.equal(
|
||||||
})
|
userSettingCreateSpy.calledWith({
|
||||||
expect(prismaMock.userSetting.create).toHaveBeenCalledWith({
|
|
||||||
data: {
|
data: {
|
||||||
userId: userExample.id
|
userId: userExample.id
|
||||||
}
|
}
|
||||||
})
|
}),
|
||||||
expect(prismaMock.oAuth.create).toHaveBeenCalledWith({
|
true
|
||||||
data: {
|
)
|
||||||
userId: userExample.id,
|
|
||||||
provider: 'discord',
|
|
||||||
providerId: id
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('/tools/utils/OAuthStrategy - callbackAddStrategy', () => {
|
await t.test('callbackAddStrategy', async (t) => {
|
||||||
it('should add the strategy to the user', async () => {
|
t.afterEach(() => {
|
||||||
|
sinon.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
await t.test('should add the strategy to the user', async (t) => {
|
||||||
const name = userExample.name
|
const name = userExample.name
|
||||||
const id = '12345'
|
const id = '12345'
|
||||||
prismaMock.oAuth.findFirst.mockResolvedValue(null)
|
sinon.stub(prisma, 'oAuth').value({
|
||||||
prismaMock.oAuth.create.mockResolvedValue({
|
findFirst: async () => {
|
||||||
|
return null
|
||||||
|
},
|
||||||
|
create: async () => {
|
||||||
|
return {
|
||||||
id: 1,
|
id: 1,
|
||||||
userId: userExample.id,
|
userId: userExample.id,
|
||||||
provider: 'discord',
|
provider: 'discord',
|
||||||
providerId: id,
|
providerId: id,
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
createdAt: new Date()
|
createdAt: new Date()
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
const oAuthCreateSpy = sinon.spy(prisma.oAuth, 'create')
|
||||||
|
const oAuthFindFirstSpy = sinon.spy(prisma.oAuth, 'findFirst')
|
||||||
const result = await oauthStrategy.callbackAddStrategy(
|
const result = await oauthStrategy.callbackAddStrategy(
|
||||||
{ id, name },
|
{ id, name },
|
||||||
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
|
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
|
||||||
)
|
)
|
||||||
expect(prismaMock.oAuth.findFirst).toHaveBeenCalledWith({
|
t.equal(result, 'success')
|
||||||
where: {
|
t.equal(
|
||||||
provider: 'discord',
|
oAuthCreateSpy.calledWith({
|
||||||
providerId: id
|
|
||||||
}
|
|
||||||
})
|
|
||||||
expect(prismaMock.oAuth.create).toHaveBeenCalledWith({
|
|
||||||
data: {
|
data: {
|
||||||
userId: userExample.id,
|
userId: userExample.id,
|
||||||
provider: 'discord',
|
provider: 'discord',
|
||||||
providerId: id
|
providerId: id
|
||||||
}
|
}
|
||||||
})
|
}),
|
||||||
expect(result).toEqual('success')
|
true
|
||||||
|
)
|
||||||
|
t.equal(
|
||||||
|
oAuthFindFirstSpy.calledWith({
|
||||||
|
where: {
|
||||||
|
provider: 'discord',
|
||||||
|
providerId: id
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
true
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not add the strategy if the account of the provider is already used', async () => {
|
await t.test(
|
||||||
|
'should not add the strategy if the account of the provider is already used',
|
||||||
|
async (t) => {
|
||||||
const name = userExample.name
|
const name = userExample.name
|
||||||
const id = '12345'
|
const id = '12345'
|
||||||
prismaMock.oAuth.findFirst.mockResolvedValue({
|
sinon.stub(prisma, 'oAuth').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
id: 1,
|
id: 1,
|
||||||
userId: 2,
|
userId: 2,
|
||||||
provider: 'discord',
|
provider: 'discord',
|
||||||
providerId: id,
|
providerId: id,
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
createdAt: new Date()
|
createdAt: new Date()
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
const oAuthFindFirstSpy = sinon.spy(prisma.oAuth, 'findFirst')
|
||||||
const result = await oauthStrategy.callbackAddStrategy(
|
const result = await oauthStrategy.callbackAddStrategy(
|
||||||
{ id, name },
|
{ id, name },
|
||||||
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
|
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
|
||||||
)
|
)
|
||||||
expect(prismaMock.oAuth.findFirst).toHaveBeenCalledWith({
|
t.equal(result, 'This account is already used by someone else')
|
||||||
|
t.equal(
|
||||||
|
oAuthFindFirstSpy.calledWith({
|
||||||
where: {
|
where: {
|
||||||
provider: 'discord',
|
provider: 'discord',
|
||||||
providerId: id
|
providerId: id
|
||||||
}
|
}
|
||||||
})
|
}),
|
||||||
expect(prismaMock.oAuth.create).not.toHaveBeenCalled()
|
true
|
||||||
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 () => {
|
await t.test(
|
||||||
|
'should not add the strategy if the user is already connected with it',
|
||||||
|
async (t) => {
|
||||||
const name = userExample.name
|
const name = userExample.name
|
||||||
const id = '12345'
|
const id = '12345'
|
||||||
prismaMock.oAuth.findFirst.mockResolvedValue({
|
sinon.stub(prisma, 'oAuth').value({
|
||||||
|
findFirst: async () => {
|
||||||
|
return {
|
||||||
id: 1,
|
id: 1,
|
||||||
userId: userExample.id,
|
userId: userExample.id,
|
||||||
provider: 'discord',
|
provider: 'discord',
|
||||||
providerId: id,
|
providerId: id,
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
createdAt: new Date()
|
createdAt: new Date()
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
const oAuthFindFirstSpy = sinon.spy(prisma.oAuth, 'findFirst')
|
||||||
const result = await oauthStrategy.callbackAddStrategy(
|
const result = await oauthStrategy.callbackAddStrategy(
|
||||||
{ id, name },
|
{ id, name },
|
||||||
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
|
{ accessToken: '123', current: userExample, currentStrategy: 'local' }
|
||||||
)
|
)
|
||||||
expect(prismaMock.oAuth.findFirst).toHaveBeenCalledWith({
|
t.equal(result, 'You are already using this account')
|
||||||
|
t.equal(
|
||||||
|
oAuthFindFirstSpy.calledWith({
|
||||||
where: {
|
where: {
|
||||||
provider: 'discord',
|
provider: 'discord',
|
||||||
providerId: id
|
providerId: id
|
||||||
}
|
}
|
||||||
})
|
}),
|
||||||
expect(prismaMock.oAuth.create).not.toHaveBeenCalled()
|
true
|
||||||
expect(result).toEqual('You are already using this account')
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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'
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
@ -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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -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",
|
||||||
|
Reference in New Issue
Block a user