91 lines
2.1 KiB
TypeScript
91 lines
2.1 KiB
TypeScript
import request from 'supertest';
|
|
import app from '../server';
|
|
import { initDatabase } from '../database';
|
|
|
|
beforeAll(async () => {
|
|
await initDatabase();
|
|
});
|
|
|
|
describe('POST /orders', () => {
|
|
it('should create a new order', async () => {
|
|
const orderData = {
|
|
customerId: 'CUST1',
|
|
customerName: 'Lui Denkwerk',
|
|
customerEmail: 'lui@test.com',
|
|
items: [
|
|
{
|
|
productId: 'TEST1',
|
|
quantity: 2,
|
|
price: 29.99
|
|
}
|
|
],
|
|
shippingAddress: {
|
|
street: 'teststraße 123',
|
|
city: 'Grevenbroich',
|
|
postalCode: '41515',
|
|
country: 'DE'
|
|
}
|
|
};
|
|
|
|
const response = await request(app)
|
|
.post('/orders')
|
|
.send(orderData)
|
|
.expect(201);
|
|
|
|
expect(response.body.success).toBe(true);
|
|
expect(response.body.data).toHaveProperty('id');
|
|
expect(response.body.data.totalAmount).toBe(59.98);
|
|
});
|
|
|
|
it('should reject invalid email', async () => {
|
|
const invalidData = {
|
|
customerId: 'CUST1',
|
|
customerName: 'Lui Denkwerk',
|
|
customerEmail: 'nomailstring',
|
|
items: [{ productId: 'TEST1', quantity: 1, price: 29.99 }],
|
|
shippingAddress: {
|
|
street: 'teststraße 123',
|
|
city: 'Grevenbroich',
|
|
postalCode: '41515',
|
|
country: 'DE'
|
|
}
|
|
};
|
|
|
|
const response = await request(app)
|
|
.post('/orders')
|
|
.send(invalidData)
|
|
.expect(400);
|
|
|
|
expect(response.body.success).toBe(false);
|
|
});
|
|
|
|
it('should require all fields', async () => {
|
|
const response = await request(app)
|
|
.post('/orders')
|
|
.send({})
|
|
.expect(400);
|
|
|
|
expect(response.body.success).toBe(false);
|
|
});
|
|
|
|
it('should reject negative quantity', async () => {
|
|
const invalidData = {
|
|
customerId: 'CUST1',
|
|
customerName: 'Lui Denkwerk',
|
|
customerEmail: 'lui@test.com',
|
|
items: [{ productId: 'NOTISCH1', quantity: -1, price: 29.99 }],
|
|
shippingAddress: {
|
|
street: 'teststraße 123',
|
|
city: 'Grevenbroich',
|
|
postalCode: '41515',
|
|
country: 'DE'
|
|
}
|
|
};
|
|
|
|
await request(app)
|
|
.post('/orders')
|
|
.send(invalidData)
|
|
.expect(400);
|
|
});
|
|
});
|