Skip to content

Commit 9decfbe

Browse files
Sam Sahbaei Razaviclaude
andcommitted
test: reviews API — list, post, one-per-user guard
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 9d108ad commit 9decfbe

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { describe, it, expect } from 'vitest';
2+
import request from 'supertest';
3+
import app from '../../../app';
4+
import { testProductSlug } from '../../../test/setup';
5+
6+
async function loginCustomer() {
7+
const res = await request(app).post('/api/auth/login').send({
8+
email: 'customer@test.com',
9+
password: 'password123',
10+
});
11+
return res.body.data.accessToken as string;
12+
}
13+
14+
describe('Reviews API', () => {
15+
describe('GET /api/products/:slug/reviews', () => {
16+
it('returns empty reviews for new product', async () => {
17+
const res = await request(app).get(`/api/products/${testProductSlug}/reviews`);
18+
expect(res.status).toBe(200);
19+
expect(res.body.data.reviews).toEqual([]);
20+
expect(res.body.data.count).toBe(0);
21+
expect(res.body.data.avgRating).toBeNull();
22+
});
23+
});
24+
25+
describe('POST /api/products/:slug/reviews', () => {
26+
it('creates a review when authenticated', async () => {
27+
const token = await loginCustomer();
28+
const res = await request(app)
29+
.post(`/api/products/${testProductSlug}/reviews`)
30+
.set('Authorization', `Bearer ${token}`)
31+
.send({ rating: 5, body: 'Excellent coffee!' });
32+
expect(res.status).toBe(201);
33+
expect(res.body.data.review.rating).toBe(5);
34+
});
35+
36+
it('rejects unauthenticated review', async () => {
37+
const res = await request(app)
38+
.post(`/api/products/${testProductSlug}/reviews`)
39+
.send({ rating: 4 });
40+
expect(res.status).toBe(401);
41+
});
42+
43+
it('rejects duplicate review from same user', async () => {
44+
const token = await loginCustomer();
45+
await request(app)
46+
.post(`/api/products/${testProductSlug}/reviews`)
47+
.set('Authorization', `Bearer ${token}`)
48+
.send({ rating: 4 });
49+
const res = await request(app)
50+
.post(`/api/products/${testProductSlug}/reviews`)
51+
.set('Authorization', `Bearer ${token}`)
52+
.send({ rating: 3, body: 'Changed my mind' });
53+
expect(res.status).toBe(409);
54+
});
55+
56+
it('rejects rating outside 1-5', async () => {
57+
const token = await loginCustomer();
58+
const res = await request(app)
59+
.post(`/api/products/${testProductSlug}/reviews`)
60+
.set('Authorization', `Bearer ${token}`)
61+
.send({ rating: 6 });
62+
expect(res.status).toBe(400);
63+
});
64+
65+
it('avg rating updates after review', async () => {
66+
const token = await loginCustomer();
67+
await request(app)
68+
.post(`/api/products/${testProductSlug}/reviews`)
69+
.set('Authorization', `Bearer ${token}`)
70+
.send({ rating: 4 });
71+
const res = await request(app).get(`/api/products/${testProductSlug}/reviews`);
72+
expect(res.body.data.avgRating).toBe(4);
73+
expect(res.body.data.count).toBe(1);
74+
});
75+
});
76+
});

0 commit comments

Comments
 (0)