generated from RCDD-202303-TUR-BEW/backend-capstone-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from AsliSema/test-order
done
- Loading branch information
Showing
1 changed file
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,192 @@ | ||
import request from 'supertest'; | ||
import app from '../../app'; | ||
import Dish from '../../models/dish'; | ||
import User from '../../models/user'; | ||
import bcrypt from 'bcrypt'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import generateToken from '../../utils/generateToken'; | ||
import Cart from '../../models/cart'; | ||
import Order from '../../models/order'; | ||
|
||
describe('Order routes', () => { | ||
const testUser = { | ||
firstname: 'Test', | ||
lastname: 'User', | ||
email: '[email protected]', | ||
password: 'testpassword', | ||
phone: '5554567890', | ||
address: { | ||
city: '16', | ||
district: 'Nilüfer', | ||
neighborhood: 'Ertuğrul Mah', | ||
addressInfo: 'No: 16, Dr: 7', | ||
}, | ||
}; | ||
|
||
let authToken; | ||
let user; | ||
let dish; | ||
let cookUser; | ||
let cart; | ||
let order; | ||
let cookToken; | ||
|
||
beforeAll(async () => { | ||
// Create a test user with a hashed password | ||
const hashedPassword = await bcrypt.hash(testUser.password, 10); | ||
user = await User.create({ | ||
firstname: testUser.firstname, | ||
lastname: testUser.lastname, | ||
email: testUser.email, | ||
password: hashedPassword, | ||
phone: testUser.phone, | ||
address: testUser.address, | ||
}); | ||
|
||
cookUser = await User.create({ | ||
firstname: 'Test Cook', | ||
lastname: 'User', | ||
email: '[email protected]', | ||
password: 'testpassword', | ||
phone: '5554567890', | ||
address: { | ||
city: '16', | ||
district: 'Nilüfer', | ||
neighborhood: 'Ertuğrul Mah', | ||
addressInfo: 'No: 16, Dr: 7', | ||
}, | ||
}); | ||
cookToken = generateToken(cookUser._id); | ||
|
||
cookUser.role = 'cook'; | ||
await cookUser.save(); | ||
|
||
dish = await Dish.create({ | ||
name: 'Test Dish 13', | ||
cook: cookUser._id, | ||
description: 'dish57dish18dish14dish4dish4', | ||
images: ['dish image'], | ||
quantity: 5, | ||
price: 17, | ||
category: 'dinner', | ||
}); | ||
|
||
cart = await Cart.create({ | ||
user: user._id, | ||
cookID: cookUser._id, | ||
cartItems: [ | ||
{ | ||
product: dish._id, | ||
quantity: 2, | ||
}, | ||
], | ||
totalCartPrice: 34, | ||
}); | ||
|
||
order = await Order.create({ | ||
user: user._id, | ||
cookId: cookUser._id, | ||
orderItems: [ | ||
{ | ||
product: dish._id, | ||
quantity: 2, | ||
}, | ||
], | ||
deliveryFee: 5, | ||
deliveryAddress: 'Test Address', | ||
totalOrderPrice: 34, | ||
}); | ||
|
||
authToken = generateToken(user._id); | ||
}); | ||
|
||
afterAll(async () => { | ||
// Cleanup: Delete the test user from the database | ||
await User.findOneAndDelete({ email: testUser.email }); | ||
await Dish.findOneAndDelete({ _id: dish._id }); | ||
await User.findOneAndDelete({ email: cookUser.email }); | ||
await Cart.findOneAndDelete({ _id: cart._id }); | ||
await Order.findOneAndDelete({ _id: order._id }); | ||
}); | ||
|
||
describe('POST /api/order/:cartId', () => { | ||
it('should creates a new order and return 201', async () => { | ||
const response = await request(app) | ||
.post(`/api/order/${cart._id}`) | ||
.set('Authorization', `Bearer ${authToken}`); | ||
expect(response.statusCode).toBe(StatusCodes.CREATED); | ||
expect(response.body.data.user).toEqual(cart.user.toString()); | ||
}); | ||
it('Should return 401 in case the user is not authorized', async () => { | ||
const response = await request(app).post(`/api/order/${cart._id}`); | ||
expect(response.statusCode).toBe(StatusCodes.UNAUTHORIZED); | ||
//expect(response.body.data.user).toEqual(cart.user.toString()); | ||
}); | ||
}); | ||
|
||
describe('PUT /api/order/:orderID', () => { | ||
it('Should update the order status and return 200', async () => { | ||
const updatedStatus = { | ||
isPaid: true, | ||
isDelivered: true, | ||
}; | ||
|
||
const response = await request(app) | ||
.put(`/api/order/${order._id}`) | ||
.set('Authorization', `Bearer ${cookToken}`) | ||
.send(updatedStatus); | ||
|
||
expect(response.statusCode).toBe(StatusCodes.OK); | ||
expect(response.body).toHaveProperty('order'); | ||
expect(response.body.order._id).toEqual(order._id.toString()); | ||
|
||
expect(response.body.order).toHaveProperty('isDelivered', true); | ||
expect(response.body.order).toHaveProperty('isPaid', true); | ||
}); | ||
|
||
it('Should return 401 in case the user is not authorized', async () => { | ||
const updatedStatus = { | ||
isPaid: true, | ||
isDelivered: true, | ||
}; | ||
|
||
const response = await request(app) | ||
.put(`/api/order/${order._id}`) | ||
.send(updatedStatus); | ||
|
||
expect(response.statusCode).toBe(StatusCodes.UNAUTHORIZED); | ||
}); | ||
}); | ||
|
||
describe('GET /api/order/:orderID', () => { | ||
it('Should get the order by ID and return 200', async () => { | ||
const response = await request(app) | ||
.get(`/api/order/${order._id}`) | ||
.set('Authorization', `Bearer ${authToken}`); | ||
|
||
expect(response.statusCode).toBe(StatusCodes.OK); | ||
}); | ||
it('Should return 401 in case the user is not authorized', async () => { | ||
const response = await request(app).get(`/api/order/${order._id}`); | ||
|
||
expect(response.statusCode).toBe(StatusCodes.UNAUTHORIZED); | ||
}); | ||
}); | ||
|
||
describe('GET /api/order', () => { | ||
it('Should get All orders that belong to cook and return 200', async () => { | ||
const response = await request(app) | ||
.get('/api/order') | ||
.set('Authorization', `Bearer ${authToken}`); | ||
|
||
expect(response.statusCode).toBe(StatusCodes.OK); | ||
expect(response.body).toHaveProperty('orders'); | ||
expect(Array.isArray(response.body.orders)).toBe(true); | ||
}); | ||
|
||
it('Should return 401 in case the user is not authorized', async () => { | ||
const response = await request(app).get('/api/order'); | ||
expect(response.statusCode).toBe(StatusCodes.UNAUTHORIZED); | ||
}); | ||
}); | ||
}); |