Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion agency/server/controllers/blogController.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,16 @@ const updateBlog = async (req, res) => {
const blog = await Blog.findById(req.params.id);

if (blog) {
Object.assign(blog, req.body);
const { title, author, date, category, image, excerpt, content, readTime, tags, slug, status } = req.body;

// Only update fields provided in request
const updateFields = { title, author, date, category, image, excerpt, content, readTime, tags, slug, status };
Object.keys(updateFields).forEach(key => {
if (updateFields[key] !== undefined) {
blog[key] = updateFields[key];
}
});

const updatedBlog = await blog.save();
res.json(updatedBlog);
} else {
Expand Down
7 changes: 5 additions & 2 deletions agency/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
"dev": "nodemon index.js",
"test": "jest"
},
"dependencies": {
"bcryptjs": "^3.0.3",
Expand All @@ -23,6 +24,8 @@
"nodemailer": "^8.0.2"
},
"devDependencies": {
"nodemon": "^3.1.0"
"jest": "^30.4.0",
"nodemon": "^3.1.0",
"supertest": "^7.2.2"
Comment on lines +27 to +29

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Update the lockfiles for the new test dependencies

Adding jest/supertest here without regenerating the npm lockfiles leaves the clean-install metadata stale: agency/server/package-lock.json still lists only nodemon for this package, and the root lock entry for agency/server also lacks these new devDependencies. In CI or any workflow that uses npm ci (the npm help describes it as a clean install from the lockfile), this can fail or install from dependency data that does not match package.json, so the newly added npm test script may not be reproducible until the lockfiles are updated with these dependencies.

Useful? React with πŸ‘Β / πŸ‘Ž.

}
}
239 changes: 239 additions & 0 deletions agency/server/tests/blogController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
const { getBlogs, getBlogById, createBlog, updateBlog, deleteBlog } = require('../controllers/blogController');
const Blog = require('../models/Blog');

jest.mock('../models/Blog');

describe('Blog Controller', () => {
let req, res;

beforeEach(() => {
req = {
params: {},
body: {},
user: undefined
};
res = {
json: jest.fn(),
status: jest.fn().mockReturnThis()
};
jest.clearAllMocks();
});

describe('getBlogs', () => {
it('should fetch published blogs for public users', async () => {
const mockBlogs = [{ title: 'Blog 1' }, { title: 'Blog 2' }];
const sortMock = jest.fn().mockResolvedValue(mockBlogs);
Blog.find.mockReturnValue({ sort: sortMock });

await getBlogs(req, res);

expect(Blog.find).toHaveBeenCalledWith({ status: 'published' });
expect(sortMock).toHaveBeenCalledWith({ createdAt: -1 });
expect(res.json).toHaveBeenCalledWith(mockBlogs);
});

it('should fetch all blogs for admin users', async () => {
req.user = { isAdmin: true };
const mockBlogs = [{ title: 'Blog 1' }, { title: 'Draft' }];
const sortMock = jest.fn().mockResolvedValue(mockBlogs);
Blog.find.mockReturnValue({ sort: sortMock });

await getBlogs(req, res);

expect(Blog.find).toHaveBeenCalledWith({});
expect(sortMock).toHaveBeenCalledWith({ createdAt: -1 });
expect(res.json).toHaveBeenCalledWith(mockBlogs);
});

it('should handle errors', async () => {
const errorMessage = 'Database error';
Blog.find.mockImplementation(() => {
throw new Error(errorMessage);
});

await getBlogs(req, res);

expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({ message: errorMessage });
});
});

describe('getBlogById', () => {
it('should find blog by slug first', async () => {
req.params.id = 'my-blog-post';
const mockBlog = { title: 'My Blog Post', status: 'published' };
Blog.findOne.mockResolvedValue(mockBlog);

await getBlogById(req, res);

expect(Blog.findOne).toHaveBeenCalledWith({ slug: 'my-blog-post' });
expect(Blog.findById).not.toHaveBeenCalled();
expect(res.json).toHaveBeenCalledWith(mockBlog);
});

it('should find blog by ID if slug not found and ID is valid', async () => {
req.params.id = '507f1f77bcf86cd799439011';
const mockBlog = { title: 'My Blog Post', status: 'published' };
Blog.findOne.mockResolvedValue(null);
Blog.findById.mockResolvedValue(mockBlog);

await getBlogById(req, res);

expect(Blog.findOne).toHaveBeenCalledWith({ slug: '507f1f77bcf86cd799439011' });
expect(Blog.findById).toHaveBeenCalledWith('507f1f77bcf86cd799439011');
expect(res.json).toHaveBeenCalledWith(mockBlog);
});

it('should not search by ID if slug not found and ID is invalid format', async () => {
req.params.id = 'invalid-id-format';
Blog.findOne.mockResolvedValue(null);

await getBlogById(req, res);

expect(Blog.findOne).toHaveBeenCalledWith({ slug: 'invalid-id-format' });
expect(Blog.findById).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(404);
expect(res.json).toHaveBeenCalledWith({ message: 'Blog post not found' });
});

it('should return 404 if not found', async () => {
req.params.id = 'non-existent-slug';
Blog.findOne.mockResolvedValue(null);

await getBlogById(req, res);

expect(res.status).toHaveBeenCalledWith(404);
expect(res.json).toHaveBeenCalledWith({ message: 'Blog post not found' });
});

it('should handle errors', async () => {
req.params.id = 'my-blog-post';
const errorMessage = 'Database error';
Blog.findOne.mockRejectedValue(new Error(errorMessage));

await getBlogById(req, res);

expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({ message: errorMessage });
});
});

describe('createBlog', () => {
it('should create a new blog post', async () => {
req.body = {
title: 'New Blog',
author: 'Test Author',
category: 'Tech',
image: 'image.jpg',
excerpt: 'Excerpt',
content: 'Content',
readTime: '5 min',
tags: ['test'],
slug: 'new-blog',
status: 'draft'
};

const mockSavedBlog = { _id: '123', ...req.body };
const saveMock = jest.fn().mockResolvedValue(mockSavedBlog);
Blog.mockImplementation(() => ({
save: saveMock
}));

await createBlog(req, res);

expect(Blog).toHaveBeenCalledWith(req.body);
expect(saveMock).toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(201);
expect(res.json).toHaveBeenCalledWith(mockSavedBlog);
});

it('should handle validation errors', async () => {
req.body = { title: 'Missing Fields' };
const errorMessage = 'Validation Error';
Blog.mockImplementation(() => ({
save: jest.fn().mockRejectedValue(new Error(errorMessage))
}));

await createBlog(req, res);

expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ message: errorMessage });
});
});

describe('updateBlog', () => {
it('should update a blog post', async () => {
req.params.id = '123';
req.body = { title: 'Updated Title', content: 'Updated content' };

const saveMock = jest.fn().mockResolvedValue({ _id: '123', ...req.body });
const mockBlog = { _id: '123', title: 'Old Title', save: saveMock };
Blog.findById.mockResolvedValue(mockBlog);

await updateBlog(req, res);

expect(Blog.findById).toHaveBeenCalledWith('123');
expect(mockBlog.title).toBe('Updated Title');
expect(mockBlog.content).toBe('Updated content');
expect(saveMock).toHaveBeenCalled();
expect(res.json).toHaveBeenCalledWith({ _id: '123', ...req.body });
});

it('should return 404 if blog to update not found', async () => {
req.params.id = 'non-existent-id';
Blog.findById.mockResolvedValue(null);

await updateBlog(req, res);

expect(res.status).toHaveBeenCalledWith(404);
expect(res.json).toHaveBeenCalledWith({ message: 'Blog post not found' });
});

it('should handle update errors', async () => {
req.params.id = '123';
const errorMessage = 'Update Error';
Blog.findById.mockRejectedValue(new Error(errorMessage));

await updateBlog(req, res);

expect(res.status).toHaveBeenCalledWith(400);
expect(res.json).toHaveBeenCalledWith({ message: errorMessage });
});
});

describe('deleteBlog', () => {
it('should delete a blog post', async () => {
req.params.id = '123';
const deleteOneMock = jest.fn().mockResolvedValue();
const mockBlog = { _id: '123', title: 'Blog to Delete', deleteOne: deleteOneMock };
Blog.findById.mockResolvedValue(mockBlog);

await deleteBlog(req, res);

expect(Blog.findById).toHaveBeenCalledWith('123');
expect(deleteOneMock).toHaveBeenCalled();
expect(res.json).toHaveBeenCalledWith({ message: 'Blog post removed' });
});

it('should return 404 if blog to delete not found', async () => {
req.params.id = 'non-existent-id';
Blog.findById.mockResolvedValue(null);

await deleteBlog(req, res);

expect(res.status).toHaveBeenCalledWith(404);
expect(res.json).toHaveBeenCalledWith({ message: 'Blog post not found' });
});

it('should handle deletion errors', async () => {
req.params.id = '123';
const errorMessage = 'Delete Error';
Blog.findById.mockRejectedValue(new Error(errorMessage));

await deleteBlog(req, res);

expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({ message: errorMessage });
});
});
});
6 changes: 3 additions & 3 deletions nexus/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"audit:verify-chain": "ts-node -r tsconfig-paths/register scripts/verify-audit-chain.ts"
},
"dependencies": {
"@nestjs/bullmq": "11.0.4",
"@nexus/shared": "*",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/auto-instrumentations-node": "^0.70.1",
Expand All @@ -43,10 +44,10 @@
"@sentry/profiling-node": "^10.42.0",
"@types/multer": "^2.0.0",
"@types/streamifier": "^0.1.2",
"ajv-formats": "2.1.1",
"axios": "^1.13.5",
"bcrypt": "^6.0.0",
"bullmq": "5.70.1",
"@nestjs/bullmq": "11.0.4",
"cache-manager": "^7.2.8",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
Expand All @@ -70,8 +71,7 @@
"streamifier": "^0.1.1",
"swagger-ui-express": "^5.0.1",
"winston": "^3.19.0",
"zod": "^4.3.6",
"ajv": "^8.18.0"
"zod": "^4.3.6"
},
"devDependencies": {
"@nestjs/cli": "^11.0.16",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { useState, useEffect } from "react";
import { useState, useEffect, useCallback } from "react";
import {
Cpu,
Plus,
Expand Down
1 change: 1 addition & 0 deletions nexus/frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ api.interceptors.response.use(
const contentType = response.headers["content-type"];
if (
contentType &&
typeof contentType === "string" &&
contentType.includes("text/html") &&
typeof response.data === "string"
) {
Expand Down
Loading