DB improv., sessions/users handlers improv.

This commit is contained in:
Florian Sylvain
2023-01-27 15:57:36 +01:00
parent 7bd6edfd70
commit f261dc5f93
8 changed files with 193 additions and 108 deletions
+36 -5
View File
@@ -1,7 +1,9 @@
import { initServer } from '../app.js'
import request from 'supertest'
import { User } from '.prisma/client'
const app = initServer()
let jwtCookie: string | undefined
describe('GET /v1', () => {
it('returns status code 200 and api version message', async () => {
@@ -13,10 +15,10 @@ describe('GET /v1', () => {
})
})
describe('POST /v1/login', () => {
describe('POST /v1/session/login', () => {
it('returns status code 200 and set httpOnly jwt token cookie', async () => {
const res = await request(app)
.post('/v1/login')
.post('/v1/session/login')
.set('Content-Type', 'application/json')
.send(JSON.stringify({
email: "a@a.com",
@@ -24,10 +26,39 @@ describe('POST /v1/login', () => {
}))
const jwtRegEx = /^jwt=.*Path=\/.*HttpOnly.*Secure.*SameSite=Strict.*$/
const jwtToken = res.get('Set-Cookie')
.filter(cookie => cookie.match(jwtRegEx))
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(jwtToken).not.toBe(undefined)
expect(jwtCookie).not.toBe(undefined)
})
})
describe('PUT /v1/user', () => {
it('returns status code 200', async () => {
const res = await request(app)
.put('/v1/user')
.set('Content-Type', 'application/json')
.set('Cookie', jwtCookie as string)
.send(JSON.stringify({
name: 'didier'
}))
expect(res.statusCode).toEqual(200)
})
it('returns status code 400 and an error message', async () => {
const res = await request(app)
.put('/v1/user')
.set('Content-Type', 'application/json')
.set('Cookie', 'jwt=23897yr0287yf.12423fv.23f4325')
.send(JSON.stringify({
name: 'didier'
}))
expect(res.statusCode).toEqual(400)
})
})