diff --git a/src/__tests__/admin.test.js b/src/__tests__/admin.test.js deleted file mode 100644 index e9d68a7..0000000 --- a/src/__tests__/admin.test.js +++ /dev/null @@ -1,315 +0,0 @@ -const supertest = require('supertest'); -const app = require('../app'); // Import your Express app -const db = require('../db/connection'); -const { User } = require('../models/user'); -const { sendChefWelcomeEmail } = require('../utils/email'); -// const uploadImage = require('../services/gcs'); - -const req = supertest(app); -jest.mock('../utils/email'); - -let adminToken; -let customerToken; -let customerId; -let chefId; -const fakeUserId = '123432334334445563122154'; - -const customerUser = { - firstName: 'customer', - lastName: 'customer', - password: 'Customer%123', - username: 'customerUser', - email: 'customer@example.com', - role: 'customer', -}; - -const adminUser = { - firstName: 'admin', - lastName: 'admin', - password: 'Admin%123', - username: 'AdminUser', - email: 'admin@example.com', - role: 'admin', -}; - -const newChef = { - firstName: 'New', - lastName: 'Chef', - username: 'chefUser', - email: 'newchef@example.com', - password: 'Newchef$123', - role: 'chef', -}; -beforeAll(async () => { - await db.connectToMongo(); - let res = await req.post('/api/auth/signup').send(adminUser); - [adminToken] = res.headers['set-cookie'][0].split(';'); - res = await req.post('/api/auth/signup').send(customerUser); - customerId = res.body.data._id; - [customerToken] = res.headers['set-cookie'][0].split(';'); -}); - -afterAll(async () => { - // await db.clearDatabase(); - await User.deleteMany({}); - await db.closeDatabase(); -}); - -describe('Admin Endpoints', () => { - describe('GET /api/admin/user', () => { - it('should return all users', async () => { - const res = await req.get('/api/admin/user').set('Cookie', adminToken); - - // Check if the response status code is correct - expect(res.statusCode).toBe(200); - - // Check if the response contains the users - expect(res.body.data.length).toBeGreaterThan(1); - }); - - it('should return an error message when admin is not authenticated', async () => { - const res = await req.get('/api/admin/user'); - - expect(res.statusCode).toBe(401); - expect(res.body.message).toBe('Unauthenticated'); - }); - - it('should return an error message when the user is not an admin', async () => { - // Pass customer token instead of admin token - const res = await req.get('/api/admin/user').set('Cookie', customerToken); - - // Check if the response status code is correct - expect(res.statusCode).toBe(403); - expect(res.body.message).toBe('This action is unauthorized'); - }); - }); - - describe('GET /api/admin/user/filter', () => { - it('should return all filtered users', async () => { - const res = await req - .get('/api/admin/user/filter?firstName=customer') - .set('Cookie', adminToken); - - // Check if the response status code is correct - expect(res.statusCode).toBe(200); - - // Check if the response contains the filtered users - expect(res.body.data.length).toBe(1); - }); - }); - - describe('GET /api/admin/user/:id', () => { - it('should return a specific user by ID', async () => { - const res = await req - .get(`/api/admin/user/${customerId}`) - .set('Cookie', adminToken); - - // Check if the response status code is correct - expect(res.statusCode).toBe(200); - - // Check if the response contains the users and excludes the admin user - expect(res.body.data.firstName).toBe('customer'); - expect(res.body.data.lastName).toBe('customer'); - expect(res.body.data.email).toBe('customer@example.com'); - expect(res.body.data.role).toBe('customer'); - }); - - it('should return 404 if the user is not found', async () => { - const res = await req - .get(`/api/admin/user/${fakeUserId}`) - .set('Cookie', adminToken); - - // Check if the response status code is correct - expect(res.statusCode).toBe(404); - - expect(res.body.message).toBe('User not found'); - }); - }); - - describe('POST /api/admin/user', () => { - it('should return a new customer', async () => { - const newCustomer = { - firstName: 'New', - lastName: 'Customer', - username: 'newCustomer', - email: 'newcustomer@example.com', - password: 'Newcustomer$123', - role: 'customer', - }; - - const res = await req - .post('/api/admin/user') - .set('Cookie', adminToken) - .send(newCustomer); - - // Check if the response status code is correct - expect(res.statusCode).toBe(201); - - // Check if the response contains the created customer details - expect(res.body.data.firstName).toBe('New'); - expect(res.body.data.lastName).toBe('Customer'); - expect(res.body.data.username).toBe('newCustomer'); - expect(res.body.data.email).toBe('newcustomer@example.com'); - expect(res.body.data.role).toBe('customer'); - }); - - it('should return a new chef', async () => { - const res = await req - .post('/api/admin/user') - .set('Cookie', adminToken) - .send(newChef); - - // Check if the response status code is correct - expect(res.statusCode).toBe(201); - - // Check if the response contains the created chef details - expect(res.body.data.firstName).toBe('New'); - expect(res.body.data.lastName).toBe('Chef'); - expect(res.body.data.username).toBe('chefUser'); - expect(res.body.data.email).toBe('newchef@example.com'); - expect(res.body.data.role).toBe('chef'); - - chefId = res.body.data._id; - }); - - it('should return 409 if the email is already taken', async () => { - const duplicatedUser = { - firstName: 'New', - lastName: 'Customer', - username: 'newCustomer', - email: 'newcustomer@example.com', - password: 'Newcustomer$123', - role: 'customer', - }; - - const res = await req - .post('/api/admin/user') - .set('Cookie', adminToken) - .send(duplicatedUser); - - // Check if the response status code is correct - expect(res.statusCode).toBe(409); - - expect(res.body.message).toBe('The email already exists'); - }); - - it('should return validation errors for invalid data', async () => { - const invalidUser = { - username: 'newCustomer', - role: 'customer', - }; - - const res = await req - .post('/api/admin/user') - .set('Cookie', adminToken) - .send(invalidUser); - - // Check if the response status code is correct - expect(res.statusCode).toBe(422); - - expect(res.body.errors).toBeTruthy(); - expect(res.body.errors.length).toBeGreaterThan(0); - }); - }); - - describe('PUT /api/admin/user/:id', () => { - it('should return the updated user by ID', async () => { - const res = await req - .put(`/api/admin/user/${customerId}`) - .set('Cookie', adminToken) - .send({ firstName: 'test' }); - - // Check if the response status code is correct - expect(res.statusCode).toBe(200); - - // Check if the response contains the updated user details - expect(res.body.data.firstName).toBe('test'); - expect(res.body.data.lastName).toBe('customer'); - expect(res.body.data.email).toBe('customer@example.com'); - expect(res.body.data.role).toBe('customer'); - }); - - it('should return 404 if the user is not found', async () => { - const res = await req - .put(`/api/admin/user/${fakeUserId}`) - .set('Cookie', adminToken) - .send({ firstName: 'test' }); - - // Check if the response status code is correct - expect(res.statusCode).toBe(404); - - expect(res.body.message).toBe('User not found'); - }); - - it('should return validation errors for invalid data', async () => { - const res = await req - .put(`/api/admin/user/${customerId}`) - .set('Cookie', adminToken) - .send({ firstName: 342 }); - - // Check if the response status code is correct - expect(res.statusCode).toBe(422); - - expect(res.body.errors.length).toBeGreaterThan(0); - }); - }); - - describe('PUT /api/admin/user/approve-chef/:id', () => { - it('should approve the user and mark him as a chef.', async () => { - const res = await req - .put(`/api/admin/user/approve-chef/${chefId}`) - .set('Cookie', adminToken); - - // Check if the response status code is correct - expect(res.statusCode).toBe(200); - - expect(res.body.message).toBe('Chef approval successful.'); - expect(sendChefWelcomeEmail).toHaveBeenCalledTimes(1); - expect(sendChefWelcomeEmail).toHaveBeenCalledWith(newChef.email); - }); - - it('should return an error message with user is not found.', async () => { - const res = await req - .put(`/api/admin/user/approve-chef/${fakeUserId}`) - .set('Cookie', adminToken); - - // Check if the response status code is correct - expect(res.statusCode).toBe(404); - - expect(res.body.message).toBe('User not found.'); - }); - - it('should return an error message when user is already approved.', async () => { - const res = await req - .put(`/api/admin/user/approve-chef/${chefId}`) - .set('Cookie', adminToken); - - // Check if the response status code is correct - expect(res.statusCode).toBe(400); - - expect(res.body.message).toBe('User is already approved as a chef.'); - }); - }); - - describe('DELETE /api/admin/user/:id', () => { - it('should return nothing', async () => { - const res = await req - .delete(`/api/admin/user/${customerId}`) - .set('Cookie', adminToken); - - // Check if the response status code is correct - expect(res.statusCode).toBe(204); - }); - - it('should return 404 if the user is not found', async () => { - const res = await req - .delete(`/api/admin/user/${fakeUserId}`) - .set('Cookie', adminToken); - - // Check if the response status code is correct - expect(res.statusCode).toBe(404); - - expect(res.body.message).toBe('User not found'); - }); - }); -}); diff --git a/src/__tests__/dish.test.js b/src/__tests__/dish.test.js deleted file mode 100644 index 07c7591..0000000 --- a/src/__tests__/dish.test.js +++ /dev/null @@ -1,257 +0,0 @@ -// dishes.test.js -const supertest = require('supertest'); -const Dish = require('../models/dish'); -const app = require('../app'); -const db = require('../db/connection'); -const { Chef } = require('../models/user'); - -jest.mock('../utils/email'); - -const req = supertest(app); - -let allDishes; -let fishId; -let chickenId; -let ownerChefToken; -let otherChefToken; - -const ownerChef = { - firstName: 'chef', - lastName: 'chef', - password: 'cheFf%123', - username: 'chefOne', - isApproved: true, - email: 'chef@example.com', - role: 'chef', -}; - -const otherChef = { - firstName: 'chef', - lastName: 'chef', - isApproved: true, - password: 'cheFf%123', - username: 'chefTwo', - email: 'otherChef@example.com', - role: 'chef', -}; - -const spaghetti = { - name: 'Spaghetti Bolognese', - price: 12.5, - ingredients: ['Tomato sauce', 'Minced beef', 'Pasta'], - reviews: [ - { - rate: 5, - comment: 'Absolutely delicious, perfect spaghetti bolognese!', - }, - { - rate: 4, - comment: 'Great Italian classic, will definitely order again.', - }, - ], -}; - -const fish = { - name: 'Fish and Chips', - price: 5, - ingredients: [ - 'Tomato sauce', - 'Cod fish', - 'Potatoes', - 'Vegetable oil', - 'Yogurt marinade', - ], - reviews: [ - { - rate: 4, - comment: 'Perfect portion sizes and crispy batter.', - }, - ], -}; - -const chicken = { - name: 'Chicken Tikka Masala', - price: 10, - ingredients: [ - 'Chicken thighs', - 'Yogurt marinade', - 'Tomato sauce', - 'Spices', - 'Potatoes', - ], - reviews: [ - { - rate: 4, - description: 'Flavours were spot on, really tasty dish.', - }, - { - rate: 3, - description: 'A bit low on spice for my liking.', - }, - ], -}; - -beforeAll(async () => { - await db.connectToMongo(); - const _ownerChef = await Chef.create(ownerChef); - await Chef.create(otherChef); - let res = await req.post('/api/auth/signin').send(ownerChef); - [ownerChefToken] = res.headers['set-cookie'][0].split(';'); - fish.chefId = _ownerChef._id; - chicken.chefId = _ownerChef._id; - spaghetti.chefId = _ownerChef._id; - res = await req.post('/api/auth/signin').send(otherChef); - [otherChefToken] = res.headers['set-cookie'][0].split(';'); - - // Create sample dishes for tests - allDishes = await Dish.insertMany([fish, chicken]); - fishId = allDishes[0]._id; - chickenId = allDishes[1]._id; -}); - -afterAll(async () => { - await db.clearDatabase(); - // await Dish.deleteMany({}); - // await Chef.deleteMany({}); - await db.closeDatabase(); -}); - -describe('GET /api/dishes', () => { - it('should return all dishes', async () => { - const res = await req.get('/api/dishes'); - expect(res.body.data).toHaveLength(allDishes.length); - }); -}); - -describe('GET /api/dishes/filter', () => { - it('should filter dishes by single ingredient and return one item', async () => { - const res = await req - .get('/api/dishes/filter') - .query({ ingredients: 'Chicken thighs' }); - expect(res.body.data).toHaveLength(1); - }); - it('should filter dishes by single ingredient and return multiple items', async () => { - const res = await req - .get('/api/dishes/filter') - .query({ ingredients: 'Potatoes' }); - expect(res.body.data).toHaveLength(2); - }); - it('should filter dishes by multiple ingredients and return one item', async () => { - const res = await req - .get('/api/dishes/filter') - .query({ ingredients: 'Cod fish,Vegetable oil' }); - - expect(res.body.data).toHaveLength(1); - }); - it('should filter dishes by multiple ingredients return multiple items', async () => { - const res = await req - .get('/api/dishes/filter') - .query({ ingredients: 'Tomato sauce,Yogurt marinade' }); - - expect(res.body.data).toHaveLength(2); - }); - it('should filter dishes by minimum price', async () => { - const res = await req.get('/api/dishes/filter').query({ minPrice: 9 }); - - expect(res.body.data).toHaveLength(1); - }); - it('should filter dishes by maximum price', async () => { - const res = await req.get('/api/dishes/filter').query({ maxPrice: 20 }); - - expect(res.body.data).toHaveLength(2); - }); - it('should filter dishes by maximum and minimum price', async () => { - const res = await req - .get('/api/dishes/filter') - .query({ maxPrice: 20, minPrice: 6 }); - - expect(res.body.data).toHaveLength(1); - }); - it('should filter dishes by rate', async () => { - const res = await req.get('/api/dishes/filter').query({ maxRate: 3.5 }); - - expect(res.body.data).toHaveLength(1); - }); - it('should filter dishes by name', async () => { - const res = await req.get('/api/dishes/filter').query({ name: 'chi' }); - - expect(res.body.data).toHaveLength(2); - }); -}); - -describe('POST /api/dishes', () => { - it('should filter create a new dish', async () => { - const res = await req - .post('/api/dishes') - .send(spaghetti) - .set('Cookie', ownerChefToken); - expect(res.body.data.name).toBe('Spaghetti Bolognese'); - }); - it('should return an error message when dish is passed with missing details', async () => { - const res = await req - .post('/api/dishes') - .send({ name: 'hello' }) - .set('Cookie', ownerChefToken); - expect(res.status).toBe(500); - }); -}); - -describe('PUT /api/dishes/:id', () => { - it('should return an updated dish by ID', async () => { - const res = await req - .put(`/api/dishes/${fishId.toString()}`) - .send({ - name: 'Not fish', - }) - .set('Cookie', ownerChefToken); - expect(res.body.data.name).toBe('Not fish'); - }); - it("should not update others' dishes and return an error message", async () => { - const res = await req - .put(`/api/dishes/${fishId.toString()}`) - .send({ - name: 'Not fish', - }) - .set('Cookie', otherChefToken); - expect(res.status).toBe(404); - }); - it('should not update non-existing dishes', async () => { - const res = await req - .put(`/api/dishes/000000000000000000000000`) - .send({ - name: 'Not fish', - }) - .set('Cookie', ownerChefToken); - expect(res.status).toBe(404); - }); -}); - -describe('DELETE /api/dishes/:id', () => { - it('should delete a dish by ID', async () => { - const res = await req - .delete(`/api/dishes/${fishId.toString()}`) - .send({ - name: 'Not fish', - }) - .set('Cookie', ownerChefToken); - expect(res.status).toBe(204); - }); - it("should not delete others' dishes", async () => { - const res = await req - .delete(`/api/dishes/${chickenId.toString()}`) - .send({ - name: 'Not fish', - }) - .set('Cookie', otherChefToken); - expect(res.status).toBe(404); - }); - it('should not delete non-existing dishes', async () => { - const res = await req - .delete(`/api/dishes/000000000000000000000000`) - .send({ - name: 'Not fish', - }) - .set('Cookie', ownerChefToken); - expect(res.status).toBe(404); - }); -}); diff --git a/src/__tests__/order.test.js b/src/__tests__/order.test.js deleted file mode 100644 index f641793..0000000 --- a/src/__tests__/order.test.js +++ /dev/null @@ -1,256 +0,0 @@ -const supertest = require('supertest'); -const app = require('../app'); -const { User, Chef } = require('../models/user'); -const Dish = require('../models/dish'); -const Order = require('../models/order'); -const cartModel = require('../models/cart'); - -const req = supertest(app); -const db = require('../db/connection'); - -jest.mock('../utils/email'); - -let customerToken; -let customerId; -let orderId; -let chefToken; -let allDishes; -let fishId; -// let chickenId; -// let orderExp; -// let cart; - -const customerUser = { - firstName: 'customerTests', - lastName: 'customerTests', - password: 'Customer%123Tests', - username: 'customerUserTests', - email: 'ahmadalashtar@gmail.com', - role: 'customer', -}; -const chefUser = { - firstName: 'cheftest', - lastName: 'cheftest', - password: 'Chef%123test', - username: 'chefUsertest', - isApproved: true, - email: 'cheftest@example.com', - role: 'chef', -}; - -const fish = { - name: 'Fish and Chips', - price: 5, - ingredients: [ - 'Tomato sauce', - 'Cod fish', - 'Potatoes', - 'Vegetable oil', - 'Yogurt marinade', - ], - reviews: [ - { - rate: 4, - description: 'Perfect portion sizes and crispy batter.', - }, - ], -}; - -const chicken = { - name: 'Chicken Tikka Masala', - price: 10, - ingredients: [ - 'Chicken thighs', - 'Yogurt marinade', - 'Tomato sauce', - 'Spices', - 'Potatoes', - ], - reviews: [ - { - rate: 5, - description: 'Flavours were spot on, really tasty dish.', - }, - { - rate: 3, - description: 'A bit low on spice for my liking.', - }, - ], -}; -const cartItemData = { - quantity: 2, -}; - -beforeAll(async () => { - await db.connectToMongo(); - let res = await req.post('/api/auth/signup').send(customerUser); - customerId = res.body.data._id; - [customerToken] = res.headers['set-cookie'][0].split(';'); - const _chefUser = await Chef.create(chefUser); - res = await req.post('/api/auth/signin').send(chefUser); - [chefToken] = res.headers['set-cookie'][0].split(';'); - const chefID = _chefUser._id; - fish.chefId = _chefUser._id; - chicken.chefId = _chefUser._id; - const orderExample = { - customerId, - chefID, - totalPrice: 50.0, - status: 'pending', - orderItems: [ - { - fishId, - quantity: 2, - price: 25.0, - }, - ], - quantity: 2, - }; - const cartItems = [ - { - dishId: fishId, - quantity: 2, - }, - ]; - const cartData = { - customerId, - cartItems, - }; - allDishes = await Dish.insertMany([fish, chicken]); - await Order.create(orderExample); - await cartModel.create(cartData); - fishId = allDishes[0]._id; - // chickenId = allDishes[1]._id; -}); - -afterAll(async () => { - await User.deleteMany({}); - await Dish.deleteMany({}); - await cartModel.deleteMany({}); - await Chef.deleteMany({}); - await db.closeDatabase(); -}); - -// Test suite for order routes -describe('Order Routes', () => { - // Test for getting all orders for customer - describe('GET /api/order/customer', () => { - // Test for success response - it('should return a success response with an array of orders', async () => { - // Create an order for the customer - const orderResponse = await req - .post('/api/orders') - .set('Cookie', customerToken) - .send({ customerId, cartItems: [cartItemData] }); - orderId = orderResponse.body.orders[0]._id; - - // Make a get request to the route - const response = await req - .get('/api/orders/customer') - .set('Cookie', customerToken); - - // Expect a status code of 200 and an array of orders in the response body - expect(response.status).toBe(200); - expect(Array.isArray(response.body.data)).toBe(true); - }); - - // Test for failure response due to invalid token - it('should return a failure response with an error message', async () => { - // Make a get request to the route with an invalid token - const response = await req - .get('/api/orders/customer') - .set('Cookie', 'Bearer invalidtoken'); - - // Expect a status code of 401 and an error message in the response body - expect(response.status).toBe(401); - expect(response.body.message).toEqual('Unauthenticated'); - }); - }); - - // Test for getting all orders for chef - describe('GET /api/v1/order/chef', () => { - // Test for success response - it('should return a success response with an array of orders', async () => { - const response = await req - .get('/api/orders/chef') - .set('Cookie', chefToken); - - // Expect a status code of 200 and an array of orders in the response body - expect(response.status).toBe(200); - expect(Array.isArray(response.body.data)).toBe(true); - expect(response.body.data[0]._id).toEqual(orderId); - }); - - // Test for failure response due to invalid token - it('should return a failure response with an error message', async () => { - // Make a get request to the route with an invalid token - const response = await req - .get('/api/orders/chef') - .set('Cookie', 'Bearer invalidtoken'); - - // Expect a status code of 401 and an error message in the response body - expect(response.status).toBe(401); - expect(response.body.message).toEqual('Unauthenticated'); - }); - }); - - describe('POST /api/orders', () => { - // Test for success response - it('should return a success response with an array of orders', async () => { - // Make a post request to the route with the customer id and cart items - const response = await req - .post('/api/orders') - .set('Cookie', customerToken) - .send({ customerId, cartItems: [cartItemData] }); - - // Expect a status code of 201 and an array of orders in the response body - expect(response.status).toBe(201); - expect(Array.isArray(response.body.orders)).toBe(true); - expect(response.body.orders[0].customerId).toEqual(customerId); - expect(response.body.orders[0].totalPrice).toEqual(10); - expect(response.body.orders[0].status).toEqual('pending'); - expect(Array.isArray(response.body.orders[0].orderItems)).toBe(true); - expect(response.body.orders[0].orderItems[0].price).toEqual(5); - expect(response.body.orders[0].orderItems[0].quantity).toEqual(2); - }); - - // Test for failure response due to invalid input - it('should return a failure response with an error message', async () => { - // Make a post request to the route with an invalid customer id - const response = await req - .post('/api/orders') - .send({ customerId: 'invalidid', cartItems: [cartItemData] }); - - expect(response.status).toBe(401); - }); - }); - - // Test for updating an order - describe('PUT /api/v1/order/:id', () => { - // Test for success response - it('should return a success response with the updated order', async () => { - const response = await req - .put(`/api/orders/${orderId}`) - .set('Cookie', chefToken) - .send({ status: 'in_progress' }); - - // Expect a status code of 200 and the updated order in the response body - expect(response.status).toBe(200); - expect(response.body.data._id).toEqual(orderId); - expect(response.body.data.status).toEqual('in_progress'); - }); - }); - - // Test for deleting an order - describe('DELETE /api/orders/:id', () => { - // Test for success response - it('should return a success response with a message', async () => { - // Make a delete request to the route with the order id - const response = await req - .delete(`/api/orders/${orderId}`) - .set('Cookie', customerToken); - - expect(response.status).toBe(204); - }); - }); -}); diff --git a/src/__tests__/review.test.js b/src/__tests__/review.test.js deleted file mode 100644 index 0b6960b..0000000 --- a/src/__tests__/review.test.js +++ /dev/null @@ -1,112 +0,0 @@ -const supertest = require('supertest'); -const app = require('../app'); -const db = require('../db/connection'); -const Dish = require('../models/dish'); -// const { User } = require('../models/user'); -const req = supertest(app); - -jest.mock('../utils/email'); - -let customerToken; -let fishId; - -const customerUser = { - firstName: 'customerTest', - lastName: 'customerTests', - password: 'Customer%123Tests', - username: 'customerUserTests', - email: 'customerTests@example.com', - role: 'customer', -}; -const fish = { - name: 'Fish and Chips test', - price: 5, - ingredients: [ - 'Tomato sauce', - 'Cod fish', - 'Potatoes', - 'Vegetable oil', - 'Yogurt marinade', - ], -}; - -beforeAll(async () => { - await db.connectToMongo(); - const res = await req.post('/api/auth/signup').send(customerUser); - [customerToken] = res.headers['set-cookie'][0].split(';'); - const dish = await Dish.create(fish); - fishId = dish._id; -}); - -afterAll(async () => { - // await Dish.deleteMany({}); - // await User.deleteMany({}); - await db.clearDatabase(); - await db.closeDatabase(); -}); - -describe('POST /api/reviews/:dishId', () => { - it('should return 422 if no rate provided', async () => { - const res = await req - .post(`/api/reviews/${fishId.toString()}`) - .set('Cookie', customerToken) - .send({ - comment: 'Great dish', - }); - expect(res.statusCode).toBe(422); - }); - - it('should return 201 if review created', async () => { - const res = await req - .post(`/api/reviews/${fishId.toString()}`) - .set('Cookie', customerToken) - .send({ - rate: 5, - comment: 'Great dish', - }); - - expect(res.statusCode).toBe(201); - }); - - it('should return 400 if customer already reviewed the dish', async () => { - const res = await req - .post(`/api/reviews/${fishId.toString()}`) - .set('Cookie', customerToken) - .send({ - rate: 5, - comment: 'Great dish', - }); - expect(res.statusCode).toBe(400); - }); -}); -describe('PUT /api/reviews/:dishId', () => { - it('should return 401 if no token provided', async () => { - const res = await req.put(`/api/reviews/${fishId.toString()}`).send({ - rate: 5, - comment: 'Great dish', - }); - expect(res.statusCode).toBe(401); - }); - it('should return 200 if review updated', async () => { - const res = await req - .put(`/api/reviews/${fishId.toString()}`) - .set('Cookie', customerToken) - .send({ - rating: 5, - comment: 'Great dish', - }); - expect(res.statusCode).toBe(200); - }); -}); -describe('DELETE /api/reviews/:dishId', () => { - it('should return 401 if no token provided', async () => { - const res = await req.delete(`/api/reviews/${fishId}`); - expect(res.statusCode).toBe(401); - }); - it('should return 204 if review deleted', async () => { - const res = await req - .delete(`/api/reviews/${fishId}`) - .set('Cookie', customerToken); - expect(res.statusCode).toBe(204); - }); -}); diff --git a/src/__tests__/user.test.js b/src/__tests__/user.test.js deleted file mode 100644 index d81abb4..0000000 --- a/src/__tests__/user.test.js +++ /dev/null @@ -1,267 +0,0 @@ -// eslint-disable-next-line import/order -const supertest = require('supertest'); -const app = require('../app'); // Import your Express app -const db = require('../db/connection'); -const { User } = require('../models/user'); - -const req = supertest(app); - -const { sendApprovalEmail } = require('../utils/email'); - -jest.mock('../utils/email'); - -let addressId; -let newUserToken; -const newUser = { - firstName: 'John', - lastName: 'Doe', - email: 'john.doe@example.com', - username: 'JohnDoe', - password: 'Correct$123', -}; -beforeAll(async () => { - await db.connectToMongo(); - const res = await req.post('/api/auth/signup').send(newUser); - newUser.id = res.body.data._id; - [newUserToken] = res.headers['set-cookie'][0].split(';'); -}); - -afterAll(async () => { - await User.deleteMany({}); - await db.closeDatabase(); - jest.clearAllMocks(); -}); - -describe('User Endpoints', () => { - describe('GET /api/user/me', () => { - it('should return authenticated user profile', async () => { - const res = await req.get('/api/user/me').set('Cookie', newUserToken); - - expect(res.statusCode).toBe(200); - - expect(res.body.firstName).toBe(newUser.firstName); - expect(res.body.lastName).toBe(newUser.lastName); - expect(res.body.username).toBe(newUser.username); - expect(res.body.email).toBe(newUser.email); - }); - - it('should return unauthorized error message when user is unauthenticated', async () => { - const res = await req.get('/api/user/me'); - - expect(res.statusCode).toBe(401); - - expect(res.body.message).toBe('Unauthenticated'); - }); - }); - - describe('PUT /api/user/profile/role', () => { - it('should update authenticated user role', async () => { - // mock admin who will receive an email - const admin = await User.create({ - firstName: 'admin', - lastName: 'admin', - password: 'Admin%123', - username: 'AdminUser', - email: 'admin@example.com', - role: 'admin', - }); - const res = await req - .put('/api/user/profile/role') - .set('Cookie', newUserToken) - .send({ role: 'chef' }); - - // mock sending email because it is a 3rd party service - // sendApprovalEmail.mockResolvedValueOnce(); - - expect(res.statusCode).toBe(200); - expect(sendApprovalEmail).toHaveBeenCalledTimes(1); - expect(sendApprovalEmail).toHaveBeenCalledWith(admin.email, newUser.id); - expect(res.body.message).toBe('Your role has been updated successfully'); - }); - - it('should return an error message when role is invalid', async () => { - // mock admin who will receive an email - const res = await req - .put('/api/user/profile/role') - .set('Cookie', newUserToken) - .send({ role: 'invalid' }); - - expect(res.statusCode).toBe(422); - }); - - it('should return an error message when user already has a role', async () => { - // mock admin who will receive an email - const res = await req - .put('/api/user/profile/role') - .set('Cookie', newUserToken) - .send({ role: 'chef' }); - - expect(res.statusCode).toBe(422); - expect(res.body.message).toBe("You can't change your role"); - }); - }); - - describe('PUT /api/user/profile', () => { - it('should update user profile and return updated profile', async () => { - const updatedUserProfile = { - addresses: [ - { - city: 'New York', - country: 'USA', - street: '123 Main St', - block: 'A', - postalCode: '10001', - apartment: 'Apt 101', - isDefault: true, - }, - ], - firstName: 'Mich', - experienceYears: 3, - }; - - const res = await req - .put('/api/user/profile') - .set('Cookie', newUserToken) - .send(updatedUserProfile); - - expect(res.statusCode).toBe(200); - expect(res.body.addresses.length).toBeGreaterThan(0); - expect(res.body.addresses[0].city).toBe( - updatedUserProfile.addresses[0].city - ); - expect(res.body.addresses[0].country).toBe( - updatedUserProfile.addresses[0].country - ); - expect(res.body.firstName).toBe(updatedUserProfile.firstName); - expect(res.body.experienceYears).toBe(updatedUserProfile.experienceYears); - - addressId = res.body.addresses[0]._id; - }); - - it('should return an error message when user tries to set many addresses as default one', async () => { - const updatedUserProfile = { - addresses: [ - { - city: 'Istanbul', - country: 'Turkey', - street: '123 Main St', - block: 'A', - postalCode: '10001', - apartment: 'Apt 101', - isDefault: true, - }, - ], - }; - - const res = await req - .put('/api/user/profile') - .set('Cookie', newUserToken) - .send(updatedUserProfile); - - expect(res.statusCode).toBe(422); - }); - - it('should return an error message when user pass invalid address', async () => { - const updatedUserProfile = { - addresses: [ - { - city: 'Istanbul', - isDefault: true, - }, - ], - }; - - const res = await req - .put('/api/user/profile') - .set('Cookie', newUserToken) - .send(updatedUserProfile); - - expect(res.statusCode).toBe(422); - }); - }); - describe('PUT /api/user/profile/address/:id', () => { - it('should update user address by id', async () => { - const updatedUserAddress = { - address: { - city: 'Istanbul', - street: '321 Main St', - block: 'C', - }, - }; - - const res = await req - .put(`/api/user/profile/address/${addressId}`) - .set('Cookie', newUserToken) - .send(updatedUserAddress); - - expect(res.statusCode).toBe(200); - expect(res.body.message).toBe('Address updated successfully'); - }); - - it('should return an error message when address is no exists', async () => { - const updatedUserAddress = { - address: { - city: 'Istanbul', - street: '321 Main St', - block: 'C', - }, - }; - - const res = await req - .put(`/api/user/profile/address/64c51069c583d1f6cd651111`) - .set('Cookie', newUserToken) - .send(updatedUserAddress); - - expect(res.statusCode).toBe(404); - expect(res.body.message).toBe('Address is not found'); - }); - - it('should return an error message when invalid address is passed', async () => { - const updatedUserAddress = { - address: { - city: 'Istanbul32', - street: '321 Main St', - }, - }; - - const res = await req - .put(`/api/user/profile/address/${addressId}`) - .set('Cookie', newUserToken) - .send(updatedUserAddress); - - expect(res.statusCode).toBe(422); - }); - }); - - describe('DELETE /api/user/profile/address/:id', () => { - it('should delete user address by id', async () => { - const res = await req - .delete(`/api/user/profile/address/${addressId}`) - .set('Cookie', newUserToken); - - expect(res.statusCode).toBe(204); - }); - - it('should return an error message when address is not exists', async () => { - const res = await req - .delete(`/api/user/profile/address/${addressId}`) - .set('Cookie', newUserToken); - - expect(res.statusCode).toBe(404); - expect(res.body.message).toBe('Address is not found'); - }); - }); - - describe('DELETE /api/user/profile', () => { - it('should should deactivate user profile and clear his token', async () => { - const res = await req - .delete(`/api/user/profile`) - .set('Cookie', newUserToken); - - expect(res.statusCode).toBe(204); - const tokenCookie = res.headers['set-cookie'][0]; - expect(tokenCookie).toBeDefined(); - expect(tokenCookie.includes('token=;')).toBe(true); - }); - }); -}); diff --git a/src/views/order.ejs b/src/views/order.ejs new file mode 100644 index 0000000..15ec520 --- /dev/null +++ b/src/views/order.ejs @@ -0,0 +1,120 @@ + + +
+Your cart is empty.
+ <% } else { %> +Customer Name | +Chef Name | +Total Price | +Status | +
---|---|---|---|
<%= item.customerId.name %> | +<%= item.chefId.name %> | +<%= item.totalPrice %> | +<%= item.Status %> | + +