diff --git a/backend/tests/unit/routes/forum.test.js b/backend/tests/unit/routes/forum.test.js index d556c58..8732119 100644 --- a/backend/tests/unit/routes/forum.test.js +++ b/backend/tests/unit/routes/forum.test.js @@ -46,18 +46,18 @@ jest.mock('sequelize', () => ({ })); jest.mock('../../../middleware/auth', () => ({ - authenticateToken: (req, res, next) => { + authenticateToken: jest.fn((req, res, next) => { req.user = { id: 'user-123', role: 'user', isVerified: true }; next(); - }, - requireAdmin: (req, res, next) => { + }), + requireAdmin: jest.fn((req, res, next) => { if (req.user && req.user.role === 'admin') { next(); } else { res.status(403).json({ error: 'Admin access required' }); } - }, - optionalAuth: (req, res, next) => next(), + }), + optionalAuth: jest.fn((req, res, next) => next()), })); jest.mock('../../../utils/logger', () => ({ @@ -97,6 +97,7 @@ jest.mock('../../../config/imageLimits', () => ({ })); const { ForumPost, ForumComment, PostTag, User } = require('../../../models'); +const { authenticateToken } = require('../../../middleware/auth'); const forumRoutes = require('../../../routes/forum'); const app = express(); @@ -1072,32 +1073,22 @@ describe('Forum Routes', () => { }); }); -// Admin routes tests - skipped due to complex mock requirements -describe.skip('Forum Admin Routes', () => { - let adminApp; - +describe('Forum Admin Routes', () => { beforeEach(() => { jest.clearAllMocks(); - // Create app with admin user - adminApp = express(); - adminApp.use(express.json()); + // Override auth mock to use admin user + authenticateToken.mockImplementation((req, res, next) => { + req.user = { id: 'admin-123', role: 'admin', isVerified: true }; + next(); + }); + }); - // Override auth middleware to set admin user - jest.resetModules(); - jest.doMock('../../../middleware/auth', () => ({ - authenticateToken: (req, res, next) => { - req.user = { id: 'admin-123', role: 'admin', isVerified: true }; - next(); - }, - requireAdmin: (req, res, next) => next(), - optionalAuth: (req, res, next) => next(), - })); - - const forumRoutesAdmin = require('../../../routes/forum'); - adminApp.use('/forum', forumRoutesAdmin); - adminApp.use((err, req, res, next) => { - res.status(500).json({ error: err.message }); + afterEach(() => { + // Reset back to regular user for other test suites + authenticateToken.mockImplementation((req, res, next) => { + req.user = { id: 'user-123', role: 'user', isVerified: true }; + next(); }); }); @@ -1114,7 +1105,7 @@ describe.skip('Forum Admin Routes', () => { ForumPost.findByPk.mockResolvedValue(mockPost); User.findByPk.mockResolvedValue({ id: 'admin-123', firstName: 'Admin' }); - const response = await request(adminApp) + const response = await request(app) .delete('/forum/admin/posts/post-1') .send({ reason: 'Violates community guidelines' }); @@ -1127,7 +1118,7 @@ describe.skip('Forum Admin Routes', () => { }); it('should return 400 when reason not provided', async () => { - const response = await request(adminApp) + const response = await request(app) .delete('/forum/admin/posts/post-1') .send({}); @@ -1138,7 +1129,7 @@ describe.skip('Forum Admin Routes', () => { it('should return 404 for non-existent post', async () => { ForumPost.findByPk.mockResolvedValue(null); - const response = await request(adminApp) + const response = await request(app) .delete('/forum/admin/posts/non-existent') .send({ reason: 'Test reason' }); @@ -1153,7 +1144,7 @@ describe.skip('Forum Admin Routes', () => { ForumPost.findByPk.mockResolvedValue(mockPost); - const response = await request(adminApp) + const response = await request(app) .delete('/forum/admin/posts/post-1') .send({ reason: 'Test reason' }); @@ -1173,7 +1164,7 @@ describe.skip('Forum Admin Routes', () => { ForumPost.findByPk.mockResolvedValue(mockPost); - const response = await request(adminApp) + const response = await request(app) .patch('/forum/admin/posts/post-1/restore'); expect(response.status).toBe(200); @@ -1188,7 +1179,7 @@ describe.skip('Forum Admin Routes', () => { it('should return 404 for non-existent post', async () => { ForumPost.findByPk.mockResolvedValue(null); - const response = await request(adminApp) + const response = await request(app) .patch('/forum/admin/posts/non-existent/restore'); expect(response.status).toBe(404); @@ -1202,7 +1193,7 @@ describe.skip('Forum Admin Routes', () => { ForumPost.findByPk.mockResolvedValue(mockPost); - const response = await request(adminApp) + const response = await request(app) .patch('/forum/admin/posts/post-1/restore'); expect(response.status).toBe(400); @@ -1232,7 +1223,7 @@ describe.skip('Forum Admin Routes', () => { ForumPost.findByPk.mockResolvedValue(mockPost); User.findByPk.mockResolvedValue({ id: 'admin-123', firstName: 'Admin' }); - const response = await request(adminApp) + const response = await request(app) .delete('/forum/admin/comments/comment-1') .send({ reason: 'Inappropriate content' }); @@ -1246,7 +1237,7 @@ describe.skip('Forum Admin Routes', () => { }); it('should return 400 when reason not provided', async () => { - const response = await request(adminApp) + const response = await request(app) .delete('/forum/admin/comments/comment-1') .send({}); @@ -1257,7 +1248,7 @@ describe.skip('Forum Admin Routes', () => { it('should return 404 for non-existent comment', async () => { ForumComment.findByPk.mockResolvedValue(null); - const response = await request(adminApp) + const response = await request(app) .delete('/forum/admin/comments/non-existent') .send({ reason: 'Test reason' }); @@ -1272,7 +1263,7 @@ describe.skip('Forum Admin Routes', () => { ForumComment.findByPk.mockResolvedValue(mockComment); - const response = await request(adminApp) + const response = await request(app) .delete('/forum/admin/comments/comment-1') .send({ reason: 'Test reason' }); @@ -1299,7 +1290,7 @@ describe.skip('Forum Admin Routes', () => { ForumComment.findByPk.mockResolvedValue(mockComment); ForumPost.findByPk.mockResolvedValue(mockPost); - const response = await request(adminApp) + const response = await request(app) .patch('/forum/admin/comments/comment-1/restore'); expect(response.status).toBe(200); @@ -1315,7 +1306,7 @@ describe.skip('Forum Admin Routes', () => { it('should return 404 for non-existent comment', async () => { ForumComment.findByPk.mockResolvedValue(null); - const response = await request(adminApp) + const response = await request(app) .patch('/forum/admin/comments/non-existent/restore'); expect(response.status).toBe(404); @@ -1329,7 +1320,7 @@ describe.skip('Forum Admin Routes', () => { ForumComment.findByPk.mockResolvedValue(mockComment); - const response = await request(adminApp) + const response = await request(app) .patch('/forum/admin/comments/comment-1/restore'); expect(response.status).toBe(400); @@ -1351,7 +1342,7 @@ describe.skip('Forum Admin Routes', () => { ForumComment.findAll.mockResolvedValue([]); User.findByPk.mockResolvedValue({ id: 'admin-123', firstName: 'Admin' }); - const response = await request(adminApp) + const response = await request(app) .patch('/forum/admin/posts/post-1/close'); expect(response.status).toBe(200); @@ -1364,7 +1355,7 @@ describe.skip('Forum Admin Routes', () => { it('should return 404 for non-existent post', async () => { ForumPost.findByPk.mockResolvedValue(null); - const response = await request(adminApp) + const response = await request(app) .patch('/forum/admin/posts/non-existent/close'); expect(response.status).toBe(404); @@ -1378,7 +1369,7 @@ describe.skip('Forum Admin Routes', () => { ForumPost.findByPk.mockResolvedValue(mockPost); - const response = await request(adminApp) + const response = await request(app) .patch('/forum/admin/posts/post-1/close'); expect(response.status).toBe(400); @@ -1397,7 +1388,7 @@ describe.skip('Forum Admin Routes', () => { ForumPost.findByPk.mockResolvedValue(mockPost); - const response = await request(adminApp) + const response = await request(app) .patch('/forum/admin/posts/post-1/reopen'); expect(response.status).toBe(200); @@ -1411,7 +1402,7 @@ describe.skip('Forum Admin Routes', () => { it('should return 404 for non-existent post', async () => { ForumPost.findByPk.mockResolvedValue(null); - const response = await request(adminApp) + const response = await request(app) .patch('/forum/admin/posts/non-existent/reopen'); expect(response.status).toBe(404); @@ -1425,7 +1416,7 @@ describe.skip('Forum Admin Routes', () => { ForumPost.findByPk.mockResolvedValue(mockPost); - const response = await request(adminApp) + const response = await request(app) .patch('/forum/admin/posts/post-1/reopen'); expect(response.status).toBe(400);