mirror of
https://github.com/Floriansylvain/SnippetsManager.git
synced 2026-08-19 11:43:15 +02:00
Improved tets and added PUT route for user acc
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
/*
|
||||||
|
Warnings:
|
||||||
|
|
||||||
|
- A unique constraint covering the columns `[email]` on the table `User` will be added. If there are existing duplicate values, this will fail.
|
||||||
|
|
||||||
|
*/
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX `User_email_key` ON `User`(`email`);
|
||||||
@@ -9,7 +9,7 @@ datasource db {
|
|||||||
|
|
||||||
model User {
|
model User {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
email String
|
email String @unique
|
||||||
password String
|
password String
|
||||||
name String
|
name String
|
||||||
picture_path String
|
picture_path String
|
||||||
|
|||||||
+35
-13
@@ -1,10 +1,22 @@
|
|||||||
import { initServer } from '../app.js'
|
import { initServer } from '../app.js'
|
||||||
import request from 'supertest'
|
import request from 'supertest'
|
||||||
import { User } from '.prisma/client'
|
import { PrismaClient } from '@prisma/client'
|
||||||
|
|
||||||
|
const prisma = new PrismaClient()
|
||||||
const app = initServer()
|
const app = initServer()
|
||||||
let jwtCookie: string | undefined
|
let jwtCookie: string | undefined
|
||||||
|
|
||||||
|
const testUserCredentials = {
|
||||||
|
email: "a@a.com",
|
||||||
|
password: "aaaaaaaaaa"
|
||||||
|
}
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await prisma.user.delete({
|
||||||
|
where: { email: "a@a.com" }
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('GET /v1', () => {
|
describe('GET /v1', () => {
|
||||||
it('returns status code 200 and api version message', async () => {
|
it('returns status code 200 and api version message', async () => {
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
@@ -15,50 +27,60 @@ describe('GET /v1', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('POST /v1/session/register', () => {
|
||||||
|
it('returns status code 200 and a success message', async () => {
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/v1/session/register')
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.send(JSON.stringify(testUserCredentials))
|
||||||
|
|
||||||
|
expect(res.statusCode).toEqual(200)
|
||||||
|
expect(res.body).toHaveProperty('message')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('POST /v1/session/login', () => {
|
describe('POST /v1/session/login', () => {
|
||||||
it('returns status code 200 and set httpOnly jwt token cookie', async () => {
|
it('returns status code 200 and set httpOnly jwt token cookie', async () => {
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
.post('/v1/session/login')
|
.post('/v1/session/login')
|
||||||
.set('Content-Type', 'application/json')
|
.set('Content-Type', 'application/json')
|
||||||
.send(JSON.stringify({
|
.send(JSON.stringify(testUserCredentials))
|
||||||
email: "a@a.com",
|
|
||||||
password: "aaaaaaaaaa"
|
|
||||||
}))
|
|
||||||
|
|
||||||
const jwtRegEx = /^jwt=.*Path=\/.*HttpOnly.*Secure.*SameSite=Strict.*$/
|
const jwtRegEx = /^jwt=.*Path=\/.*HttpOnly.*Secure.*SameSite=Strict.*$/
|
||||||
jwtCookie = res.get('Set-Cookie')
|
jwtCookie = res.get('Set-Cookie')
|
||||||
?.filter(cookie => cookie.match(jwtRegEx))[0]
|
?.filter(cookie => cookie.match(jwtRegEx))[0]
|
||||||
|
|
||||||
// jwtToken = jwtCookie.match('(^|;)\\s*jwt\\s*=\\s*([^;]+)')?.pop() || ''
|
|
||||||
|
|
||||||
expect(res.statusCode).toEqual(200)
|
expect(res.statusCode).toEqual(200)
|
||||||
expect(jwtCookie).not.toBe(undefined)
|
expect(jwtCookie).not.toBe(undefined)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
describe('PUT /v1/user', () => {
|
describe('PUT /v1/user', () => {
|
||||||
it('returns status code 200', async () => {
|
it('returns status code 200 and success message', async () => {
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
.put('/v1/user')
|
.put('/v1/user')
|
||||||
.set('Content-Type', 'application/json')
|
.set('Content-Type', 'application/json')
|
||||||
.set('Cookie', jwtCookie as string)
|
.set('Cookie', jwtCookie as string)
|
||||||
.send(JSON.stringify({
|
.send(JSON.stringify({
|
||||||
name: 'didier'
|
id: 1234,
|
||||||
|
name: 'didier',
|
||||||
|
pouet: 'alo'
|
||||||
}))
|
}))
|
||||||
|
|
||||||
expect(res.statusCode).toEqual(200)
|
expect(res.statusCode).toEqual(200)
|
||||||
|
expect(res.body).toHaveProperty('message')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns status code 400 and an error message', async () => {
|
it('returns status code 400 and error message', async () => {
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
.put('/v1/user')
|
.put('/v1/user')
|
||||||
.set('Content-Type', 'application/json')
|
.set('Content-Type', 'application/json')
|
||||||
.set('Cookie', 'jwt=23897yr0287yf.12423fv.23f4325')
|
.set('Cookie', jwtCookie as string)
|
||||||
.send(JSON.stringify({
|
.send(JSON.stringify({
|
||||||
name: 'didier'
|
picture_path: 424242
|
||||||
}))
|
}))
|
||||||
|
|
||||||
expect(res.statusCode).toEqual(400)
|
expect(res.statusCode).toEqual(400)
|
||||||
|
expect(res.body).toHaveProperty('message')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
+18
-16
@@ -1,10 +1,16 @@
|
|||||||
import { PrismaClient, User } from '@prisma/client'
|
import { PrismaClient } from '@prisma/client'
|
||||||
import express, { RequestHandler } from 'express'
|
import express, { RequestHandler } from 'express'
|
||||||
import jwt from 'jsonwebtoken'
|
import jwt from 'jsonwebtoken'
|
||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
const userRouter = express.Router()
|
const userRouter = express.Router()
|
||||||
const prisma = new PrismaClient()
|
const prisma = new PrismaClient()
|
||||||
|
|
||||||
|
const editableUserData = z.object({
|
||||||
|
name: z.string().optional(),
|
||||||
|
picture_path: z.string().optional()
|
||||||
|
})
|
||||||
|
|
||||||
function parseJwtUserId(jwtoken: string): number | undefined {
|
function parseJwtUserId(jwtoken: string): number | undefined {
|
||||||
const payload = jwt.decode(jwtoken)
|
const payload = jwt.decode(jwtoken)
|
||||||
if (payload == undefined) {
|
if (payload == undefined) {
|
||||||
@@ -15,28 +21,24 @@ function parseJwtUserId(jwtoken: string): number | undefined {
|
|||||||
return payload.userId
|
return payload.userId
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateUser(userId: number): Promise<User> {
|
|
||||||
return await prisma.user.update({
|
|
||||||
where: { id: userId },
|
|
||||||
data: {
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const userRouterPut: RequestHandler = async (req, res) => {
|
const userRouterPut: RequestHandler = async (req, res) => {
|
||||||
const userId = parseJwtUserId(req.cookies.jwt)
|
const userId = parseJwtUserId(req.cookies.jwt)
|
||||||
if (userId === undefined) {
|
if (userId === undefined) {
|
||||||
res.status(400).json({
|
res.status(400).json({ message: 'Incorrect JWT payload.' })
|
||||||
message: 'Incorrect JWT payload.'
|
|
||||||
})
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await updateUser(userId)
|
try {
|
||||||
|
await prisma.user.update({
|
||||||
|
where: { id: userId },
|
||||||
|
data: editableUserData.parse(req.body)
|
||||||
|
})
|
||||||
|
} catch (error: any) {
|
||||||
|
res.status(400).json({ message: (error.issues ?? error) })
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
res.json({
|
res.json({ message: 'User successfully updated.' })
|
||||||
message: 'User successfully updated.'
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
userRouter.put("/", userRouterPut)
|
userRouter.put("/", userRouterPut)
|
||||||
|
|||||||
Reference in New Issue
Block a user