Skip to content

Commit 8c200a9

Browse files
committed
Add test fixtures for codebase analysis validation
1 parent 76e3102 commit 8c200a9

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

test-models/Order.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Test model to demonstrate schema extraction
2+
const mongoose = require('mongoose');
3+
4+
const OrderSchema = new mongoose.Schema({
5+
orderId: {
6+
type: String,
7+
required: true,
8+
unique: true
9+
},
10+
userId: {
11+
type: mongoose.Schema.Types.ObjectId,
12+
required: true,
13+
ref: 'User'
14+
},
15+
amount: {
16+
type: Number,
17+
required: true,
18+
min: 0
19+
},
20+
status: {
21+
type: String,
22+
enum: ['pending', 'paid', 'failed', 'cancelled'],
23+
default: 'pending'
24+
},
25+
items: [{
26+
productId: String,
27+
quantity: Number,
28+
price: Number
29+
}],
30+
createdAt: {
31+
type: Date,
32+
default: Date.now
33+
},
34+
updatedAt: {
35+
type: Date,
36+
default: Date.now
37+
}
38+
});
39+
40+
module.exports = mongoose.model('Order', OrderSchema);

test-routes/orderRoutes.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Test route to demonstrate payload and auth policy extraction
2+
const express = require('express');
3+
const Joi = require('joi');
4+
const { celebrate } = require('celebrate');
5+
const { jwtAuth, requireRole } = require('../middleware/auth');
6+
7+
const router = express.Router();
8+
9+
// Order creation with JWT auth and validation
10+
router.post('/orders',
11+
jwtAuth(),
12+
celebrate({
13+
body: Joi.object({
14+
userId: Joi.string().required(),
15+
cartId: Joi.string().required(),
16+
amount: Joi.number().min(0).required(),
17+
items: Joi.array().items(
18+
Joi.object({
19+
productId: Joi.string().required(),
20+
quantity: Joi.number().min(1).required(),
21+
price: Joi.number().min(0).required()
22+
})
23+
).required()
24+
})
25+
}),
26+
async (req, res) => {
27+
const { userId, cartId, amount, items } = req.body;
28+
29+
// Create order logic here
30+
const order = await orderService.createOrder({ userId, cartId, amount, items });
31+
32+
res.json({
33+
orderId: order.id,
34+
status: order.status,
35+
amount: order.amount,
36+
createdAt: order.createdAt
37+
});
38+
}
39+
);
40+
41+
// Admin-only route to get all orders
42+
router.get('/admin/orders',
43+
jwtAuth(),
44+
requireRole('admin'),
45+
async (req, res) => {
46+
const orders = await orderService.getAllOrders();
47+
res.json({ orders });
48+
}
49+
);
50+
51+
module.exports = router;

test-services/OrderService.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Test service to demonstrate service interaction extraction
2+
import { PaymentService } from './PaymentService.js';
3+
import { NotificationService } from './NotificationService.js';
4+
5+
export class OrderService {
6+
constructor(paymentService, notificationService) {
7+
this.paymentService = paymentService;
8+
this.notificationService = notificationService;
9+
}
10+
11+
async createOrder(orderData) {
12+
// Validate cart
13+
this.validateCart(orderData.cart);
14+
15+
// Call PaymentService
16+
const payment = await this.paymentService.processPayment(orderData.amount);
17+
18+
// Create order
19+
const order = await this.saveOrder(orderData, payment);
20+
21+
// Send notification
22+
await this.notificationService.sendOrderConfirmation(order);
23+
24+
return order;
25+
}
26+
27+
validateCart(cart) {
28+
if (!cart || cart.items.length === 0) {
29+
throw new Error('Cart is empty');
30+
}
31+
}
32+
33+
async saveOrder(orderData, payment) {
34+
// Save to database
35+
return { id: 'order_123', ...orderData, paymentId: payment.id };
36+
}
37+
}

0 commit comments

Comments
 (0)