Files
flyer-crawler.projectium.com/src/routes/admin.content.routes.test.ts

423 lines
16 KiB
TypeScript

// src/routes/admin.content.routes.test.ts
import { describe, it, expect, vi, beforeEach } from 'vitest';
import supertest from 'supertest';
import type { Request, Response, NextFunction } from 'express';
import {
createMockUserProfile,
createMockSuggestedCorrection,
createMockBrand,
createMockRecipe,
createMockRecipeComment,
createMockUnmatchedFlyerItem,
} from '../tests/utils/mockFactories';
import type { SuggestedCorrection, Brand, UserProfile, UnmatchedFlyerItem } from '../types';
import { NotFoundError } from '../services/db/errors.db'; // This can stay, it's a type/class not a module with side effects.
import { createTestApp } from '../tests/utils/createTestApp';
// Mock the file upload middleware to allow testing the controller's internal check
vi.mock('../middleware/fileUpload.middleware', () => ({
requireFileUpload: () => (req: Request, res: Response, next: NextFunction) => next(),
}));
vi.mock('../lib/queue', () => ({
serverAdapter: {
getRouter: () => (req: Request, res: Response, next: NextFunction) => next(), // Return a dummy express handler
},
// Mock other exports if needed
emailQueue: {},
cleanupQueue: {},
}));
const { mockedDb } = vi.hoisted(() => {
return {
mockedDb: {
adminRepo: {
getSuggestedCorrections: vi.fn(),
approveCorrection: vi.fn(),
rejectCorrection: vi.fn(),
updateSuggestedCorrection: vi.fn(),
getUnmatchedFlyerItems: vi.fn(),
updateRecipeStatus: vi.fn(),
updateRecipeCommentStatus: vi.fn(),
updateBrandLogo: vi.fn(),
},
flyerRepo: {
getAllBrands: vi.fn(),
deleteFlyer: vi.fn(),
},
recipeRepo: {
deleteRecipe: vi.fn(),
},
userRepo: {
findUserProfileById: vi.fn(),
deleteUserById: vi.fn(),
},
},
};
});
vi.mock('../services/db/index.db', () => ({
adminRepo: mockedDb.adminRepo,
flyerRepo: mockedDb.flyerRepo,
recipeRepo: mockedDb.recipeRepo,
userRepo: mockedDb.userRepo,
personalizationRepo: {},
notificationRepo: {},
}));
// Mock other dependencies
vi.mock('../services/db/recipe.db');
vi.mock('../services/db/user.db');
vi.mock('node:fs/promises', () => ({
// Named exports
writeFile: vi.fn().mockResolvedValue(undefined),
unlink: vi.fn().mockResolvedValue(undefined),
// FIX: Add default export to handle `import fs from ...` syntax.
default: {
writeFile: vi.fn().mockResolvedValue(undefined),
unlink: vi.fn().mockResolvedValue(undefined),
},
}));
vi.mock('../services/backgroundJobService');
vi.mock('../services/geocodingService.server');
vi.mock('../services/queueService.server');
vi.mock('@bull-board/api'); // Keep this mock for the API part
vi.mock('@bull-board/api/bullMQAdapter'); // Keep this mock for the adapter
// Fix: Mock ExpressAdapter as a class to allow `new ExpressAdapter()` to work.
vi.mock('@bull-board/express', () => ({
ExpressAdapter: class {
setBasePath = vi.fn();
getRouter = vi
.fn()
.mockReturnValue((req: Request, res: Response, next: NextFunction) => next());
},
}));
// Mock the logger
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Mock the passport middleware
vi.mock('./passport.routes', () => ({
default: {
authenticate: vi.fn(() => (req: Request, res: Response, next: NextFunction) => {
if (!req.user) return res.status(401).json({ message: 'Unauthorized' });
next();
}),
},
isAdmin: (req: Request, res: Response, next: NextFunction) => {
const user = req.user as UserProfile | undefined;
if (user && user.role === 'admin') next();
else res.status(403).json({ message: 'Forbidden: Administrator access required.' });
},
}));
// Import the router AFTER all mocks are defined.
import adminRouter from './admin.routes';
describe('Admin Content Management Routes (/api/admin)', () => {
const adminUser = createMockUserProfile({
role: 'admin',
user: { user_id: 'admin-user-id', email: 'admin@test.com' },
});
// Create a single app instance with an admin user for all tests in this suite.
const app = createTestApp({
router: adminRouter,
basePath: '/api/admin',
authenticatedUser: adminUser,
});
beforeEach(() => {
vi.clearAllMocks();
});
describe('Corrections Routes', () => {
it('GET /corrections should return corrections data', async () => {
const mockCorrections: SuggestedCorrection[] = [
createMockSuggestedCorrection({ suggested_correction_id: 1 }),
];
vi.mocked(mockedDb.adminRepo.getSuggestedCorrections).mockResolvedValue(mockCorrections);
const response = await supertest(app).get('/api/admin/corrections');
expect(response.status).toBe(200);
expect(response.body).toEqual(mockCorrections);
});
it('should return 500 if the database call fails', async () => {
vi.mocked(mockedDb.adminRepo.getSuggestedCorrections).mockRejectedValue(
new Error('DB Error'),
);
const response = await supertest(app).get('/api/admin/corrections');
expect(response.status).toBe(500);
expect(response.body.message).toBe('DB Error');
});
it('POST /corrections/:id/approve should approve a correction', async () => {
const correctionId = 123;
vi.mocked(mockedDb.adminRepo.approveCorrection).mockResolvedValue(undefined);
const response = await supertest(app).post(`/api/admin/corrections/${correctionId}/approve`);
expect(response.status).toBe(200);
expect(response.body).toEqual({ message: 'Correction approved successfully.' });
expect(vi.mocked(mockedDb.adminRepo.approveCorrection)).toHaveBeenCalledWith(
correctionId,
expect.anything(),
);
});
it('POST /corrections/:id/approve should return 500 on DB error', async () => {
const correctionId = 123;
vi.mocked(mockedDb.adminRepo.approveCorrection).mockRejectedValue(new Error('DB Error'));
const response = await supertest(app).post(`/api/admin/corrections/${correctionId}/approve`);
expect(response.status).toBe(500);
});
it('POST /corrections/:id/reject should reject a correction', async () => {
const correctionId = 789;
vi.mocked(mockedDb.adminRepo.rejectCorrection).mockResolvedValue(undefined);
const response = await supertest(app).post(`/api/admin/corrections/${correctionId}/reject`);
expect(response.status).toBe(200);
expect(response.body).toEqual({ message: 'Correction rejected successfully.' });
});
it('POST /corrections/:id/reject should return 500 on DB error', async () => {
const correctionId = 789;
vi.mocked(mockedDb.adminRepo.rejectCorrection).mockRejectedValue(new Error('DB Error'));
const response = await supertest(app).post(`/api/admin/corrections/${correctionId}/reject`);
expect(response.status).toBe(500);
});
it('PUT /corrections/:id should update a correction', async () => {
const correctionId = 101;
const requestBody = { suggested_value: 'A new corrected value' };
const mockUpdatedCorrection = createMockSuggestedCorrection({
suggested_correction_id: correctionId,
...requestBody,
});
vi.mocked(mockedDb.adminRepo.updateSuggestedCorrection).mockResolvedValue(
mockUpdatedCorrection,
);
const response = await supertest(app)
.put(`/api/admin/corrections/${correctionId}`)
.send(requestBody);
expect(response.status).toBe(200);
expect(response.body).toEqual(mockUpdatedCorrection);
});
it('PUT /corrections/:id should return 400 for invalid data', async () => {
const response = await supertest(app)
.put('/api/admin/corrections/101')
.send({ suggested_value: '' }); // Send empty value
expect(response.status).toBe(400);
});
it('PUT /corrections/:id should return 404 if correction not found', async () => {
vi.mocked(mockedDb.adminRepo.updateSuggestedCorrection).mockRejectedValue(
new NotFoundError('Correction with ID 999 not found'),
);
const response = await supertest(app)
.put('/api/admin/corrections/999')
.send({ suggested_value: 'new value' });
expect(response.status).toBe(404);
expect(response.body.message).toBe('Correction with ID 999 not found');
});
});
describe('Brand Routes', () => {
it('GET /brands should return a list of all brands', async () => {
const mockBrands: Brand[] = [createMockBrand({ brand_id: 1, name: 'Brand A' })];
vi.mocked(mockedDb.flyerRepo.getAllBrands).mockResolvedValue(mockBrands);
const response = await supertest(app).get('/api/admin/brands');
expect(response.status).toBe(200);
expect(response.body).toEqual(mockBrands);
});
it('POST /brands/:id/logo should upload a logo and update the brand', async () => {
const brandId = 55;
vi.mocked(mockedDb.adminRepo.updateBrandLogo).mockResolvedValue(undefined);
const response = await supertest(app)
.post(`/api/admin/brands/${brandId}/logo`)
.attach('logoImage', Buffer.from('dummy-logo-content'), 'test-logo.png');
expect(response.status).toBe(200);
expect(response.body.message).toBe('Brand logo updated successfully.');
expect(vi.mocked(mockedDb.adminRepo.updateBrandLogo)).toHaveBeenCalledWith(
brandId,
expect.stringContaining('/assets/'),
expect.anything(),
);
});
it('POST /brands/:id/logo should return 500 on DB error', async () => {
const brandId = 55;
vi.mocked(mockedDb.adminRepo.updateBrandLogo).mockRejectedValue(new Error('DB Error'));
const response = await supertest(app)
.post(`/api/admin/brands/${brandId}/logo`)
.attach('logoImage', Buffer.from('dummy-logo-content'), 'test-logo.png');
expect(response.status).toBe(500);
});
it('POST /brands/:id/logo should return 400 if no file is uploaded', async () => {
const response = await supertest(app).post('/api/admin/brands/55/logo');
expect(response.status).toBe(400);
expect(response.body.message).toMatch(
/Logo image file is required|The request data is invalid|Logo image file is missing./,
);
});
it('POST /brands/:id/logo should return 400 for an invalid brand ID', async () => {
const response = await supertest(app)
.post('/api/admin/brands/abc/logo')
.attach('logoImage', Buffer.from('dummy-logo-content'), 'test-logo.png');
expect(response.status).toBe(400);
});
});
describe('Recipe and Comment Routes', () => {
it('DELETE /recipes/:recipeId should delete a recipe', async () => {
const recipeId = 300;
vi.mocked(mockedDb.recipeRepo.deleteRecipe).mockResolvedValue(undefined);
const response = await supertest(app).delete(`/api/admin/recipes/${recipeId}`);
expect(response.status).toBe(204);
expect(vi.mocked(mockedDb.recipeRepo.deleteRecipe)).toHaveBeenCalledWith(
recipeId,
expect.anything(),
true,
expect.anything(),
);
});
it('DELETE /recipes/:recipeId should return 400 for invalid ID', async () => {
const response = await supertest(app).delete('/api/admin/recipes/abc');
expect(response.status).toBe(400);
});
it('DELETE /recipes/:recipeId should return 500 on DB error', async () => {
const recipeId = 300;
vi.mocked(mockedDb.recipeRepo.deleteRecipe).mockRejectedValue(new Error('DB Error'));
const response = await supertest(app).delete(`/api/admin/recipes/${recipeId}`);
expect(response.status).toBe(500);
});
it('PUT /recipes/:id/status should update a recipe status', async () => {
const recipeId = 201;
const requestBody = { status: 'public' as const };
const mockUpdatedRecipe = createMockRecipe({ recipe_id: recipeId, status: 'public' });
vi.mocked(mockedDb.adminRepo.updateRecipeStatus).mockResolvedValue(mockUpdatedRecipe);
const response = await supertest(app)
.put(`/api/admin/recipes/${recipeId}/status`)
.send(requestBody);
expect(response.status).toBe(200);
expect(response.body).toEqual(mockUpdatedRecipe);
});
it('PUT /recipes/:id/status should return 400 for an invalid status value', async () => {
const recipeId = 201;
const requestBody = { status: 'invalid_status' };
const response = await supertest(app)
.put(`/api/admin/recipes/${recipeId}/status`)
.send(requestBody);
expect(response.status).toBe(400);
});
it('PUT /recipes/:id/status should return 500 on DB error', async () => {
const recipeId = 201;
const requestBody = { status: 'public' as const };
vi.mocked(mockedDb.adminRepo.updateRecipeStatus).mockRejectedValue(new Error('DB Error'));
const response = await supertest(app)
.put(`/api/admin/recipes/${recipeId}/status`)
.send(requestBody);
expect(response.status).toBe(500);
});
it('PUT /comments/:id/status should update a comment status', async () => {
const commentId = 301;
const requestBody = { status: 'hidden' as const };
const mockUpdatedComment = createMockRecipeComment({
recipe_comment_id: commentId,
status: 'hidden',
}); // This was a duplicate, fixed.
vi.mocked(mockedDb.adminRepo.updateRecipeCommentStatus).mockResolvedValue(mockUpdatedComment);
const response = await supertest(app)
.put(`/api/admin/comments/${commentId}/status`)
.send(requestBody);
expect(response.status).toBe(200);
expect(response.body).toEqual(mockUpdatedComment);
});
it('PUT /comments/:id/status should return 400 for an invalid status value', async () => {
const commentId = 301;
const requestBody = { status: 'invalid_status' };
const response = await supertest(app)
.put(`/api/admin/comments/${commentId}/status`)
.send(requestBody);
expect(response.status).toBe(400);
});
it('PUT /comments/:id/status should return 500 on DB error', async () => {
const commentId = 301;
const requestBody = { status: 'hidden' as const };
vi.mocked(mockedDb.adminRepo.updateRecipeCommentStatus).mockRejectedValue(
new Error('DB Error'),
);
const response = await supertest(app)
.put(`/api/admin/comments/${commentId}/status`)
.send(requestBody);
expect(response.status).toBe(500);
});
});
describe('Unmatched Items Route', () => {
it('GET /unmatched-items should return a list of unmatched items', async () => {
// Correctly create a mock for UnmatchedFlyerItem.
const mockUnmatchedItems: UnmatchedFlyerItem[] = [
createMockUnmatchedFlyerItem({
unmatched_flyer_item_id: 1,
flyer_item_name: 'Mystery Item',
}),
];
vi.mocked(mockedDb.adminRepo.getUnmatchedFlyerItems).mockResolvedValue(mockUnmatchedItems);
const response = await supertest(app).get('/api/admin/unmatched-items');
expect(response.status).toBe(200);
expect(response.body).toEqual(mockUnmatchedItems);
});
it('GET /unmatched-items should return 500 on DB error', async () => {
vi.mocked(mockedDb.adminRepo.getUnmatchedFlyerItems).mockRejectedValue(new Error('DB Error'));
const response = await supertest(app).get('/api/admin/unmatched-items');
expect(response.status).toBe(500);
});
});
describe('Flyer Routes', () => {
it('DELETE /flyers/:flyerId should delete a flyer', async () => {
const flyerId = 42;
vi.mocked(mockedDb.flyerRepo.deleteFlyer).mockResolvedValue(undefined);
const response = await supertest(app).delete(`/api/admin/flyers/${flyerId}`);
expect(response.status).toBe(204);
expect(vi.mocked(mockedDb.flyerRepo.deleteFlyer)).toHaveBeenCalledWith(
flyerId,
expect.anything(),
);
});
it('DELETE /flyers/:flyerId should return 404 if flyer not found', async () => {
const flyerId = 999;
vi.mocked(mockedDb.flyerRepo.deleteFlyer).mockRejectedValue(
new NotFoundError('Flyer with ID 999 not found.'),
);
const response = await supertest(app).delete(`/api/admin/flyers/${flyerId}`);
expect(response.status).toBe(404);
expect(response.body.message).toBe('Flyer with ID 999 not found.');
});
it('DELETE /flyers/:flyerId should return 400 for an invalid flyerId', async () => {
const response = await supertest(app).delete('/api/admin/flyers/abc');
expect(response.status).toBe(400);
expect(response.body.errors[0].message).toMatch(/Expected number, received nan/i);
});
});
});