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 {
|
||||
id Int @id @default(autoincrement())
|
||||
email String
|
||||
email String @unique
|
||||
password String
|
||||
name String
|
||||
picture_path String
|
||||
|
||||
+35
-13
@@ -1,10 +1,22 @@
|
||||
import { initServer } from '../app.js'
|
||||
import request from 'supertest'
|
||||
import { User } from '.prisma/client'
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
const app = initServer()
|
||||
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', () => {
|
||||
it('returns status code 200 and api version message', async () => {
|
||||
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', () => {
|
||||
it('returns status code 200 and set httpOnly jwt token cookie', async () => {
|
||||
const res = await request(app)
|
||||
.post('/v1/session/login')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send(JSON.stringify({
|
||||
email: "a@a.com",
|
||||
password: "aaaaaaaaaa"
|
||||
}))
|
||||
.send(JSON.stringify(testUserCredentials))
|
||||
|
||||
const jwtRegEx = /^jwt=.*Path=\/.*HttpOnly.*Secure.*SameSite=Strict.*$/
|
||||
jwtCookie = res.get('Set-Cookie')
|
||||
?.filter(cookie => cookie.match(jwtRegEx))[0]
|
||||
|
||||
// jwtToken = jwtCookie.match('(^|;)\\s*jwt\\s*=\\s*([^;]+)')?.pop() || ''
|
||||
|
||||
expect(res.statusCode).toEqual(200)
|
||||
expect(jwtCookie).not.toBe(undefined)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('PUT /v1/user', () => {
|
||||
it('returns status code 200', async () => {
|
||||
it('returns status code 200 and success message', async () => {
|
||||
const res = await request(app)
|
||||
.put('/v1/user')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Cookie', jwtCookie as string)
|
||||
.send(JSON.stringify({
|
||||
name: 'didier'
|
||||
id: 1234,
|
||||
name: 'didier',
|
||||
pouet: 'alo'
|
||||
}))
|
||||
|
||||
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)
|
||||
.put('/v1/user')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Cookie', 'jwt=23897yr0287yf.12423fv.23f4325')
|
||||
.set('Cookie', jwtCookie as string)
|
||||
.send(JSON.stringify({
|
||||
name: 'didier'
|
||||
picture_path: 424242
|
||||
}))
|
||||
|
||||
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 jwt from 'jsonwebtoken'
|
||||
import { z } from 'zod'
|
||||
|
||||
const userRouter = express.Router()
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
const editableUserData = z.object({
|
||||
name: z.string().optional(),
|
||||
picture_path: z.string().optional()
|
||||
})
|
||||
|
||||
function parseJwtUserId(jwtoken: string): number | undefined {
|
||||
const payload = jwt.decode(jwtoken)
|
||||
if (payload == undefined) {
|
||||
@@ -15,28 +21,24 @@ function parseJwtUserId(jwtoken: string): number | undefined {
|
||||
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 userId = parseJwtUserId(req.cookies.jwt)
|
||||
if (userId === undefined) {
|
||||
res.status(400).json({
|
||||
message: 'Incorrect JWT payload.'
|
||||
})
|
||||
res.status(400).json({ message: 'Incorrect JWT payload.' })
|
||||
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({
|
||||
message: 'User successfully updated.'
|
||||
})
|
||||
res.json({ message: 'User successfully updated.' })
|
||||
}
|
||||
|
||||
userRouter.put("/", userRouterPut)
|
||||
|
||||
Reference in New Issue
Block a user