const request = require('supertest'); const express = require('express'); // Mock CouchDB service before importing routes jest.mock('../../services/couchdbService', () => ({ initialize: jest.fn().mockResolvedValue(true), create: jest.fn(), getById: jest.fn(), find: jest.fn(), createDocument: jest.fn(), updateDocument: jest.fn(), deleteDocument: jest.fn(), findByType: jest.fn(), findUserById: jest.fn(), update: jest.fn(), })); const postRoutes = require('../../routes/posts'); const { createTestUser, createTestPost } = require('../utils/testHelpers'); const couchdbService = require('../../services/couchdbService'); const app = express(); app.use(express.json()); app.use('/api/posts', postRoutes); describe('Post Routes', () => { beforeEach(() => { jest.clearAllMocks(); }); 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 () => { // Mock Post.findAll and Post.countDocuments couchdbService.find .mockResolvedValueOnce([]) // For findAll .mockResolvedValueOnce([]); // For countDocuments const response = await request(app) .get('/api/posts') .expect(200); expect(Array.isArray(response.body.data)).toBe(true); expect(response.body.data.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'); }); }); });