44
55
66import marketplaceController from '../server/controllers/marketplaceController' ;
7+ import sessionController from '../server/controllers/sessionController' ;
78import app from '../server/server' ;
89import mockData from '../mockData' ;
910import { profileEnd } from 'console' ;
10- import { Projects } from '../server/models/reactypeModels' ;
11+ import { Projects , Sessions } from '../server/models/reactypeModels' ;
1112const request = require ( 'supertest' ) ;
1213const mongoose = require ( 'mongoose' ) ;
1314const mockNext = jest . fn ( ) ; // Mock nextFunction
@@ -288,8 +289,89 @@ describe('Server endpoint tests', () => {
288289 } ) ;
289290 } ) ;
290291
292+ xdescribe ( 'SessionController tests' , ( ) => {
293+
291294
295+
296+
297+ afterEach ( ( ) => {
298+ jest . resetAllMocks ( ) ;
299+ } )
300+
301+ xdescribe ( 'isLoggedIn' , ( ) => {
302+ // Mock Express request and response objects and next function
303+ const mockReq : any = {
304+ cookies : null , //trying to trigger if cookies was not assigned
305+ body : {
306+ userId : 'sampleUserId' , // Set up a sample userId in the request body
307+ } ,
308+ }
309+
310+ const mockRes : any = {
311+ json : jest . fn ( ) ,
312+ status : jest . fn ( ) ,
313+ redirect : jest . fn ( )
314+ } ;
315+
316+ const next = jest . fn ( ) ;
317+ it ( 'Assign userId from request body to cookieId' , async ( ) => {
318+ // Call isLoggedIn
319+ await sessionController . isLoggedIn ( mockReq , mockRes , next ) ;
320+ expect ( mockRes . redirect ) . toHaveBeenCalledWith ( '/' ) ;
321+ // Ensure that next() was called
322+ } ) ;
323+
324+ it ( 'Trigger a database query error for findOne' , async ( ) => {
325+ jest . spyOn ( mongoose . model ( 'Sessions' ) , 'findOne' ) . mockImplementation ( ( ) => {
326+ throw new Error ( 'Database query error' ) ;
327+ } ) ;
328+ // Call isLoggedIn
329+ await sessionController . isLoggedIn ( mockReq , mockRes , next ) ;
330+
331+ // Ensure that next() was called with the error
332+ expect ( next ) . toHaveBeenCalledWith ( expect . objectContaining ( {
333+ log : expect . stringMatching ( 'Database query error' ) , // The 'i' flag makes it case-insensitive
334+ } ) ) ;
335+ } ) ;
336+ } ) ;
292337
338+ xdescribe ( 'startSession' , ( ) => {
339+ const mockReq : any = {
340+ cookies : projectToSave . userId , //trying to trigger if cookies was not assigned
341+ body : {
342+ userId : 'sampleUserId' , // Set up a sample userId in the request body
343+ } ,
344+ }
345+
346+ const mockRes : any = {
347+ json : jest . fn ( ) ,
348+ status : jest . fn ( ) ,
349+ redirect : jest . fn ( )
350+ } ;
351+
352+ jest . spyOn ( mongoose . model ( 'Sessions' ) , 'findOne' ) . mockImplementation ( ( ) => {
353+ throw new Error ( 'Database query error' ) ;
354+ } ) ;
355+
356+ const next = jest . fn ( ) ;
357+
358+ it ( 'Trigger a database query error for findOne' , async ( ) => {
359+
360+ jest . spyOn ( mongoose . model ( 'Sessions' ) , 'findOne' ) . mockImplementation ( ( ) => {
361+ throw new Error ( 'Database query error' ) ;
362+ } ) ;
363+ // Call startSession
364+ await sessionController . startSession ( mockReq , mockRes , next ) ;
365+
366+ // Ensure that next() was called with the error
367+ expect ( next ) . toHaveBeenCalledWith ( expect . objectContaining ( {
368+ log : expect . stringMatching ( 'Database query error' ) , // The 'i' flag makes it case-insensitive
369+ } ) ) ;
370+ } ) ;
371+
372+ } ) ;
373+
374+ } ) ;
293375
294376} ) ;
295377
@@ -299,6 +381,7 @@ describe('Server endpoint tests', () => {
299381
300382
301383
384+
302385// describe('marketplaceController Middleware', () => {
303386// describe('getProjects tests', () => {
304387// it('should add the projects as an array to res.locals', () => {
0 commit comments