|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import request from 'supertest'; |
| 3 | +import app from '../../../app'; |
| 4 | +import { testCategoryId, testProductId } from '../../../test/setup'; |
| 5 | + |
| 6 | +async function adminToken() { |
| 7 | + const res = await request(app).post('/api/auth/login').send({ |
| 8 | + email: 'admin@test.com', |
| 9 | + password: 'password123', |
| 10 | + }); |
| 11 | + return res.body.data.accessToken as string; |
| 12 | +} |
| 13 | + |
| 14 | +async function customerToken() { |
| 15 | + const res = await request(app).post('/api/auth/login').send({ |
| 16 | + email: 'customer@test.com', |
| 17 | + password: 'password123', |
| 18 | + }); |
| 19 | + return res.body.data.accessToken as string; |
| 20 | +} |
| 21 | + |
| 22 | +describe('Admin API', () => { |
| 23 | + describe('Authorization', () => { |
| 24 | + it('blocks unauthenticated requests', async () => { |
| 25 | + const res = await request(app).get('/api/admin/stats'); |
| 26 | + expect(res.status).toBe(401); |
| 27 | + }); |
| 28 | + |
| 29 | + it('blocks customer role', async () => { |
| 30 | + const token = await customerToken(); |
| 31 | + const res = await request(app).get('/api/admin/stats').set('Authorization', `Bearer ${token}`); |
| 32 | + expect(res.status).toBe(403); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('GET /api/admin/stats', () => { |
| 37 | + it('returns dashboard stats', async () => { |
| 38 | + const token = await adminToken(); |
| 39 | + const res = await request(app).get('/api/admin/stats').set('Authorization', `Bearer ${token}`); |
| 40 | + expect(res.status).toBe(200); |
| 41 | + expect(res.body.data).toHaveProperty('totalOrders'); |
| 42 | + expect(res.body.data).toHaveProperty('totalRevenueOre'); |
| 43 | + expect(res.body.data).toHaveProperty('totalProducts'); |
| 44 | + expect(res.body.data).toHaveProperty('totalCustomers'); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('Product management', () => { |
| 49 | + it('creates a product', async () => { |
| 50 | + const token = await adminToken(); |
| 51 | + const res = await request(app) |
| 52 | + .post('/api/admin/products') |
| 53 | + .set('Authorization', `Bearer ${token}`) |
| 54 | + .send({ |
| 55 | + name: 'Admin Product', |
| 56 | + slug: 'admin-product', |
| 57 | + description: 'Created by admin', |
| 58 | + priceOre: 19900, |
| 59 | + stock: 25, |
| 60 | + categoryId: testCategoryId, |
| 61 | + }); |
| 62 | + expect(res.status).toBe(201); |
| 63 | + expect(res.body.data.product.name).toBe('Admin Product'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('updates a product', async () => { |
| 67 | + const token = await adminToken(); |
| 68 | + const res = await request(app) |
| 69 | + .patch(`/api/admin/products/${testProductId}`) |
| 70 | + .set('Authorization', `Bearer ${token}`) |
| 71 | + .send({ stock: 99 }); |
| 72 | + expect(res.status).toBe(200); |
| 73 | + expect(res.body.data.product.stock).toBe(99); |
| 74 | + }); |
| 75 | + |
| 76 | + it('deletes a product', async () => { |
| 77 | + const token = await adminToken(); |
| 78 | + // Create a product to delete |
| 79 | + const created = await request(app) |
| 80 | + .post('/api/admin/products') |
| 81 | + .set('Authorization', `Bearer ${token}`) |
| 82 | + .send({ |
| 83 | + name: 'To Delete', |
| 84 | + slug: 'to-delete', |
| 85 | + description: 'Will be deleted', |
| 86 | + priceOre: 100, |
| 87 | + stock: 1, |
| 88 | + categoryId: testCategoryId, |
| 89 | + }); |
| 90 | + const id = created.body.data.product.id; |
| 91 | + const res = await request(app) |
| 92 | + .delete(`/api/admin/products/${id}`) |
| 93 | + .set('Authorization', `Bearer ${token}`); |
| 94 | + expect(res.status).toBe(200); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('Order management', () => { |
| 99 | + it('lists all orders', async () => { |
| 100 | + const token = await adminToken(); |
| 101 | + const res = await request(app).get('/api/admin/orders').set('Authorization', `Bearer ${token}`); |
| 102 | + expect(res.status).toBe(200); |
| 103 | + expect(res.body.data.orders).toBeInstanceOf(Array); |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
0 commit comments