|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import request from 'supertest'; |
| 3 | +import app from '../../../app'; |
| 4 | +import { testProductSlug, testCategoryId, prisma } from '../../../test/setup'; |
| 5 | + |
| 6 | +describe('Products API', () => { |
| 7 | + describe('GET /api/products', () => { |
| 8 | + it('returns a list of products', async () => { |
| 9 | + const res = await request(app).get('/api/products'); |
| 10 | + expect(res.status).toBe(200); |
| 11 | + expect(res.body.data.products).toBeInstanceOf(Array); |
| 12 | + expect(res.body.data.products.length).toBeGreaterThanOrEqual(1); |
| 13 | + }); |
| 14 | + |
| 15 | + it('supports limit param', async () => { |
| 16 | + const res = await request(app).get('/api/products?limit=1'); |
| 17 | + expect(res.status).toBe(200); |
| 18 | + expect(res.body.data.products.length).toBeLessThanOrEqual(1); |
| 19 | + }); |
| 20 | + |
| 21 | + it('supports category filter', async () => { |
| 22 | + const res = await request(app).get('/api/products?category=test-category'); |
| 23 | + expect(res.status).toBe(200); |
| 24 | + expect(res.body.data.products.every((p: { category: { slug: string } }) => p.category.slug === 'test-category')).toBe(true); |
| 25 | + }); |
| 26 | + |
| 27 | + it('supports search filter', async () => { |
| 28 | + const res = await request(app).get('/api/products?search=Test'); |
| 29 | + expect(res.status).toBe(200); |
| 30 | + expect(res.body.data.products.length).toBeGreaterThanOrEqual(1); |
| 31 | + }); |
| 32 | + |
| 33 | + it('supports exclude param', async () => { |
| 34 | + const allRes = await request(app).get('/api/products'); |
| 35 | + const productId = allRes.body.data.products[0].id; |
| 36 | + const res = await request(app).get(`/api/products?exclude=${productId}`); |
| 37 | + expect(res.body.data.products.every((p: { id: string }) => p.id !== productId)).toBe(true); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('GET /api/products/:slug', () => { |
| 42 | + it('returns product by slug', async () => { |
| 43 | + const res = await request(app).get(`/api/products/${testProductSlug}`); |
| 44 | + expect(res.status).toBe(200); |
| 45 | + expect(res.body.data.product.slug).toBe(testProductSlug); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns 404 for unknown slug', async () => { |
| 49 | + const res = await request(app).get('/api/products/does-not-exist'); |
| 50 | + expect(res.status).toBe(404); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('Admin product CRUD', () => { |
| 55 | + async function adminToken() { |
| 56 | + const res = await request(app).post('/api/auth/login').send({ |
| 57 | + email: 'admin@test.com', |
| 58 | + password: 'password123', |
| 59 | + }); |
| 60 | + return res.body.data.accessToken as string; |
| 61 | + } |
| 62 | + |
| 63 | + it('admin can create a product', async () => { |
| 64 | + const token = await adminToken(); |
| 65 | + const res = await request(app) |
| 66 | + .post('/api/admin/products') |
| 67 | + .set('Authorization', `Bearer ${token}`) |
| 68 | + .send({ |
| 69 | + name: 'New Product', |
| 70 | + slug: 'new-product', |
| 71 | + description: 'Desc', |
| 72 | + priceOre: 9900, |
| 73 | + stock: 10, |
| 74 | + categoryId: testCategoryId, |
| 75 | + }); |
| 76 | + expect(res.status).toBe(201); |
| 77 | + expect(res.body.data.product.slug).toBe('new-product'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('customer cannot create a product', async () => { |
| 81 | + const loginRes = await request(app).post('/api/auth/login').send({ |
| 82 | + email: 'customer@test.com', |
| 83 | + password: 'password123', |
| 84 | + }); |
| 85 | + const token = loginRes.body.data.accessToken; |
| 86 | + const res = await request(app) |
| 87 | + .post('/api/admin/products') |
| 88 | + .set('Authorization', `Bearer ${token}`) |
| 89 | + .send({ name: 'x', slug: 'x', priceOre: 100, stock: 1, categoryId: testCategoryId }); |
| 90 | + expect(res.status).toBe(403); |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments