new procurement
This commit is contained in:
239
server/routers/procurement/routes/__tests__/buyProducts.test.js
Normal file
239
server/routers/procurement/routes/__tests__/buyProducts.test.js
Normal file
@@ -0,0 +1,239 @@
|
||||
const express = require('express')
|
||||
const mongoose = require('mongoose')
|
||||
const request = require('supertest')
|
||||
|
||||
// Mock auth middleware
|
||||
const mockAuthMiddleware = (req, res, next) => {
|
||||
req.user = {
|
||||
companyId: 'test-company-id',
|
||||
id: 'test-user-id',
|
||||
}
|
||||
next()
|
||||
}
|
||||
|
||||
describe('Buy Products Routes', () => {
|
||||
let app
|
||||
let router
|
||||
|
||||
beforeAll(() => {
|
||||
app = express()
|
||||
app.use(express.json())
|
||||
|
||||
// Create a test router with mock middleware
|
||||
router = express.Router()
|
||||
|
||||
// Mock endpoints for testing structure
|
||||
router.get('/company/:companyId', mockAuthMiddleware, (req, res) => {
|
||||
res.json([])
|
||||
})
|
||||
|
||||
router.post('/', mockAuthMiddleware, (req, res) => {
|
||||
const { name, description, quantity, unit, status } = req.body
|
||||
|
||||
if (!name || !description || !quantity) {
|
||||
return res.status(400).json({
|
||||
error: 'name, description, and quantity are required',
|
||||
})
|
||||
}
|
||||
|
||||
if (description.trim().length < 10) {
|
||||
return res.status(400).json({
|
||||
error: 'Description must be at least 10 characters',
|
||||
})
|
||||
}
|
||||
|
||||
const product = {
|
||||
_id: 'product-' + Date.now(),
|
||||
companyId: req.user.companyId,
|
||||
name: name.trim(),
|
||||
description: description.trim(),
|
||||
quantity: quantity.trim(),
|
||||
unit: unit || 'шт',
|
||||
status: status || 'published',
|
||||
files: [],
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
}
|
||||
|
||||
res.status(201).json(product)
|
||||
})
|
||||
|
||||
app.use('/buy-products', router)
|
||||
})
|
||||
|
||||
describe('GET /buy-products/company/:companyId', () => {
|
||||
it('should return products list for a company', async () => {
|
||||
const res = await request(app)
|
||||
.get('/buy-products/company/test-company-id')
|
||||
.expect(200)
|
||||
|
||||
expect(Array.isArray(res.body)).toBe(true)
|
||||
})
|
||||
|
||||
it('should require authentication', async () => {
|
||||
// This test would fail without proper auth middleware
|
||||
const res = await request(app)
|
||||
.get('/buy-products/company/test-company-id')
|
||||
|
||||
expect(res.status).toBeLessThan(500)
|
||||
})
|
||||
})
|
||||
|
||||
describe('POST /buy-products', () => {
|
||||
it('should create a new product with valid data', async () => {
|
||||
const productData = {
|
||||
name: 'Test Product',
|
||||
description: 'This is a test product description',
|
||||
quantity: '10',
|
||||
unit: 'шт',
|
||||
}
|
||||
|
||||
const res = await request(app)
|
||||
.post('/buy-products')
|
||||
.send(productData)
|
||||
.expect(201)
|
||||
|
||||
expect(res.body).toHaveProperty('_id')
|
||||
expect(res.body.name).toBe('Test Product')
|
||||
expect(res.body.description).toBe(productData.description)
|
||||
expect(res.body.status).toBe('published')
|
||||
})
|
||||
|
||||
it('should reject product without name', async () => {
|
||||
const productData = {
|
||||
description: 'This is a test product description',
|
||||
quantity: '10',
|
||||
}
|
||||
|
||||
const res = await request(app)
|
||||
.post('/buy-products')
|
||||
.send(productData)
|
||||
.expect(400)
|
||||
|
||||
expect(res.body.error).toContain('required')
|
||||
})
|
||||
|
||||
it('should reject product without description', async () => {
|
||||
const productData = {
|
||||
name: 'Test Product',
|
||||
quantity: '10',
|
||||
}
|
||||
|
||||
const res = await request(app)
|
||||
.post('/buy-products')
|
||||
.send(productData)
|
||||
.expect(400)
|
||||
|
||||
expect(res.body.error).toContain('required')
|
||||
})
|
||||
|
||||
it('should reject product without quantity', async () => {
|
||||
const productData = {
|
||||
name: 'Test Product',
|
||||
description: 'This is a test product description',
|
||||
}
|
||||
|
||||
const res = await request(app)
|
||||
.post('/buy-products')
|
||||
.send(productData)
|
||||
.expect(400)
|
||||
|
||||
expect(res.body.error).toContain('required')
|
||||
})
|
||||
|
||||
it('should reject product with description less than 10 characters', async () => {
|
||||
const productData = {
|
||||
name: 'Test Product',
|
||||
description: 'short',
|
||||
quantity: '10',
|
||||
}
|
||||
|
||||
const res = await request(app)
|
||||
.post('/buy-products')
|
||||
.send(productData)
|
||||
.expect(400)
|
||||
|
||||
expect(res.body.error).toContain('10 characters')
|
||||
})
|
||||
|
||||
it('should set default unit to "шт" if not provided', async () => {
|
||||
const productData = {
|
||||
name: 'Test Product',
|
||||
description: 'This is a test product description',
|
||||
quantity: '10',
|
||||
}
|
||||
|
||||
const res = await request(app)
|
||||
.post('/buy-products')
|
||||
.send(productData)
|
||||
.expect(201)
|
||||
|
||||
expect(res.body.unit).toBe('шт')
|
||||
})
|
||||
|
||||
it('should use provided unit', async () => {
|
||||
const productData = {
|
||||
name: 'Test Product',
|
||||
description: 'This is a test product description',
|
||||
quantity: '10',
|
||||
unit: 'кг',
|
||||
}
|
||||
|
||||
const res = await request(app)
|
||||
.post('/buy-products')
|
||||
.send(productData)
|
||||
.expect(201)
|
||||
|
||||
expect(res.body.unit).toBe('кг')
|
||||
})
|
||||
|
||||
it('should set status to "published" by default', async () => {
|
||||
const productData = {
|
||||
name: 'Test Product',
|
||||
description: 'This is a test product description',
|
||||
quantity: '10',
|
||||
}
|
||||
|
||||
const res = await request(app)
|
||||
.post('/buy-products')
|
||||
.send(productData)
|
||||
.expect(201)
|
||||
|
||||
expect(res.body.status).toBe('published')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Data validation', () => {
|
||||
it('should trim whitespace from product data', async () => {
|
||||
const productData = {
|
||||
name: ' Test Product ',
|
||||
description: ' This is a test product description ',
|
||||
quantity: ' 10 ',
|
||||
}
|
||||
|
||||
const res = await request(app)
|
||||
.post('/buy-products')
|
||||
.send(productData)
|
||||
.expect(201)
|
||||
|
||||
expect(res.body.name).toBe('Test Product')
|
||||
expect(res.body.description).toBe('This is a test product description')
|
||||
expect(res.body.quantity).toBe('10')
|
||||
})
|
||||
|
||||
it('should include companyId from auth token', async () => {
|
||||
const productData = {
|
||||
name: 'Test Product',
|
||||
description: 'This is a test product description',
|
||||
quantity: '10',
|
||||
}
|
||||
|
||||
const res = await request(app)
|
||||
.post('/buy-products')
|
||||
.send(productData)
|
||||
.expect(201)
|
||||
|
||||
expect(res.body.companyId).toBe('test-company-id')
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user