Skip to content

Commit 99e8b3b

Browse files
committed
sessioncontroller testing
1 parent f4156a1 commit 99e8b3b

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

__tests__/server.test.tsx

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55

66
import marketplaceController from '../server/controllers/marketplaceController';
7+
import sessionController from '../server/controllers/sessionController';
78
import app from '../server/server';
89
import mockData from '../mockData';
910
import { profileEnd } from 'console';
10-
import { Projects } from '../server/models/reactypeModels';
11+
import { Projects, Sessions} from '../server/models/reactypeModels';
1112
const request = require('supertest');
1213
const mongoose = require('mongoose');
1314
const 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', () => {

__tests__/userAuth.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ describe('User Authentication tests', () => {
9393

9494
});
9595

96+
97+
9698
// import request from 'supertest';
9799
// import app from '../server/server';
98100
// import mockObj from '../mockData';

mockData.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ const mockObj = {
3939
name: 'super test project',
4040
userId: '64f551e5b28d5292975e08c8',
4141
username: 'test',
42+
forked: false,
43+
published: false,
44+
isLoggedIn: false,
4245
project: {
4346
name: 'test',
4447
isLoggedIn: false,

server/controllers/sessionController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ const sessionController: SessionController = {
1717
// if the request cookies exist then it assigns it to cookieId
1818
cookieId = req.cookies.ssid;
1919
} else {
20+
console.log('reqcookies', 'was null')
2021
// else it creates a new cookieId for the user based on the userId
2122
cookieId = req.body.userId;
2223
}
2324

2425
// find session from request session ID in mongodb
2526
const session = await Sessions.findOne({ cookieId });
26-
2727
if (!session) {
28+
console.log('session', session)
2829
return res.redirect('/');
2930
}
3031
return next();

0 commit comments

Comments
 (0)