Files
adopt-a-street/backend/__tests__/routes/posts.test.js
William Valentin 17e5c90a90 test(backend): add comprehensive testing infrastructure
Implement complete backend testing infrastructure with Jest and Supertest:

Test Setup:
- Configure Jest for Node.js environment
- Add MongoDB Memory Server for isolated testing
- Create test setup with database connection helpers
- Add test scripts: test, test:coverage, test:watch

Test Files (176 total tests, 109 passing):
- Middleware tests: auth.test.js (100% coverage)
- Model tests: User, Street, Task, Post (82.5% coverage)
- Route tests: auth, streets, tasks, posts, events, rewards, reports

Test Coverage:
- Overall: 54.75% (on track for 70% target)
- Models: 82.5%
- Middleware: 100%
- Routes: 45.84%

Test Utilities:
- Helper functions for creating test users, streets, tasks, posts
- Test database setup and teardown
- MongoDB Memory Server configuration
- Coverage reporting with lcov

Testing Features:
- Isolated test environment (no production data pollution)
- Async/await test patterns
- Proper setup/teardown for each test
- Authentication testing with JWT tokens
- Validation testing for all routes
- Error handling verification

Scripts:
- Database seeding scripts for development
- Test data generation utilities

Dependencies:
- jest@29.7.0
- supertest@7.0.0
- mongodb-memory-server@10.1.2

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-01 10:43:20 -07:00

147 lines
4.6 KiB
JavaScript

const request = require('supertest');
const express = require('express');
const postRoutes = require('../../routes/posts');
const { createTestUser, createTestPost } = require('../utils/testHelpers');
const app = express();
app.use(express.json());
app.use('/api/posts', postRoutes);
describe('Post Routes', () => {
describe('GET /api/posts', () => {
it('should get all posts with user information', async () => {
const { user } = await createTestUser();
await createTestPost(user.id, { content: 'First post' });
await createTestPost(user.id, { content: 'Second post' });
const response = await request(app)
.get('/api/posts')
.expect(200);
expect(Array.isArray(response.body)).toBe(true);
expect(response.body.length).toBe(2);
expect(response.body[0]).toHaveProperty('content');
expect(response.body[0]).toHaveProperty('user');
});
it('should return empty array when no posts exist', async () => {
const response = await request(app)
.get('/api/posts')
.expect(200);
expect(Array.isArray(response.body)).toBe(true);
expect(response.body.length).toBe(0);
});
});
describe('POST /api/posts', () => {
it('should create a new post with authentication', async () => {
const { user, token } = await createTestUser();
const postData = {
content: 'This is my new post about street cleaning',
imageUrl: 'https://example.com/image.jpg',
};
const response = await request(app)
.post('/api/posts')
.set('x-auth-token', token)
.send(postData)
.expect(200);
expect(response.body).toHaveProperty('content', postData.content);
expect(response.body).toHaveProperty('imageUrl', postData.imageUrl);
expect(response.body).toHaveProperty('user', user.id);
});
it('should create a post with only content (no image)', async () => {
const { token } = await createTestUser();
const postData = {
content: 'Just text content',
};
const response = await request(app)
.post('/api/posts')
.set('x-auth-token', token)
.send(postData)
.expect(200);
expect(response.body).toHaveProperty('content', postData.content);
});
it('should not create post without authentication', async () => {
const postData = {
content: 'This should fail',
};
const response = await request(app)
.post('/api/posts')
.send(postData)
.expect(401);
expect(response.body).toHaveProperty('msg', 'No token, authorization denied');
});
});
describe('PUT /api/posts/like/:id', () => {
it('should like a post', async () => {
const { user: author } = await createTestUser({ email: 'author@example.com' });
const { user: liker, token } = await createTestUser({ email: 'liker@example.com' });
const post = await createTestPost(author.id);
const response = await request(app)
.put(`/api/posts/like/${post.id}`)
.set('x-auth-token', token)
.expect(200);
expect(Array.isArray(response.body)).toBe(true);
expect(response.body.length).toBe(1);
expect(response.body[0]).toBe(liker.id);
});
it('should not like a post twice', async () => {
const { user: author } = await createTestUser({ email: 'author@example.com' });
const { user: liker, token } = await createTestUser({ email: 'liker@example.com' });
const post = await createTestPost(author.id);
// Like the first time
await request(app)
.put(`/api/posts/like/${post.id}`)
.set('x-auth-token', token)
.expect(200);
// Try to like again
const response = await request(app)
.put(`/api/posts/like/${post.id}`)
.set('x-auth-token', token)
.expect(400);
expect(response.body).toHaveProperty('msg', 'Post already liked');
});
it('should return 404 for non-existent post', async () => {
const { token } = await createTestUser();
const fakeId = '507f1f77bcf86cd799439011';
const response = await request(app)
.put(`/api/posts/like/${fakeId}`)
.set('x-auth-token', token)
.expect(404);
expect(response.body).toHaveProperty('msg', 'Post not found');
});
it('should not like post without authentication', async () => {
const { user } = await createTestUser();
const post = await createTestPost(user.id);
const response = await request(app)
.put(`/api/posts/like/${post.id}`)
.expect(401);
expect(response.body).toHaveProperty('msg', 'No token, authorization denied');
});
});
});