Skip to content

Commit

Permalink
Merge pull request #117 from AsliSema/fixing-tests
Browse files Browse the repository at this point in the history
fixed testing everything together
  • Loading branch information
nourkrimesh authored Aug 10, 2023
2 parents 0fe9517 d879866 commit 0ed9022
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 68,7 @@ app.all('*', (req: Request, res: Response, next: NextFunction) => {
// a global error middleware that catch errors and present them in a structured way.
app.use(errorHandlerMiddleware);

app.listen(config.server.port, () => {
export const server = app.listen(config.server.port, () => {
console.log(`Server is running at http://localhost:${config.server.port}`);
});

Expand Down
7 changes: 5 additions & 2 deletions src/controllers/dish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 81,12 @@ const getAllDishes = asyncHandler(async (req: Request, res: Response) => {
let filteredDishes = dishes.filter((dish) => {
const cook = Object.assign(dish.cook);
return (
cook.address.city === req?.user?.address.city &&
cook.address.district === req?.user?.address.district
cook.address.city === req?.user?.address.city
);
// return (
// cook.address.city === req?.user?.address.city &&
// cook.address.district === req?.user?.address.district
// );
});
count = filteredDishes.length;
filteredDishes = filteredDishes
Expand Down
10 changes: 5 additions & 5 deletions src/routes/__tests__/admin.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 1,8 @@
import request from 'supertest';
import app from '../../app';
import { server } from '../../app';
import { StatusCodes } from 'http-status-codes';
import User from '../../models/user';
import generateToken from '../../utils/generateToken';
import bcrypt from 'bcrypt';
import Order from '../../models/order';
import Dish from '../../models/dish';

Expand Down Expand Up @@ -103,19 102,20 @@ describe('test specific admin routes', () => {
await User.findOneAndDelete({ email: adminUser.email });
await Dish.findOneAndDelete({ _id: dish._id });
await Order.findOneAndDelete({ _id: order._id });
server.close();
});

describe('DELETE /api/admin/users/userID', () => {
it('Should delete user profile', async () => {
const response = await request(app)
const response = await request(server)
.delete(`/api/admin/users/${customerUser._id}`)
.set('Authorization', `Bearer ${adminToken}`);

expect(response.statusCode).toBe(StatusCodes.OK);
expect(response.body).toHaveProperty('message', 'User deleted');
});
it('Should return 400 in case of unauthorized user', async () => {
const response = await request(app).delete(
const response = await request(server).delete(
`/api/admin/users/${customerUser._id}`
);

Expand All @@ -129,7 129,7 @@ describe('test specific admin routes', () => {

describe('PUT /api/admin/order/:orderID', () => {
it('updates order status as admin', async () => {
const response = await request(app)
const response = await request(server)
.put(`/api/admin/order/${order._id}`)
.set('Authorization', `Bearer ${adminToken}`)
.send({
Expand Down
25 changes: 13 additions & 12 deletions src/routes/__tests__/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 1,5 @@
import request from 'supertest';
import app from '../../app';
import { server } from '../../app';
import { StatusCodes } from 'http-status-codes';
import User from '../../models/user';
import generateToken from '../../utils/generateToken';
Expand Down Expand Up @@ -55,11 55,12 @@ describe('Auth routes', () => {
afterAll(async () => {
// Cleanup: Delete the test user from the database
await User.findOneAndDelete({ email: testUser.email });
server.close();
});

describe('POST /api/users/signup', () => {
it('Should register a new user', async () => {
const response = await request(app)
const response = await request(server)
.post('/api/users/signup')
.send(testUser);

Expand All @@ -69,7 70,7 @@ describe('Auth routes', () => {
});

it('Should fail to register with an already existing email', async () => {
const response = await request(app)
const response = await request(server)
.post('/api/users/signup')
.send(testUser);

Expand All @@ -83,7 84,7 @@ describe('Auth routes', () => {

describe('POST /api/users/signin', () => {
it('Should sign in an existing user with correct credentials', async () => {
const response = await request(app)
const response = await request(server)
.post('/api/users/signin')
.send({ email: testUser.email, password: testUser.password });

Expand All @@ -93,7 94,7 @@ describe('Auth routes', () => {
});

it('Should fail to sign in with incorrect password', async () => {
const response = await request(app)
const response = await request(server)
.post('/api/users/signin')
.send({ email: testUser.email, password: 'incorrectpassword' });

Expand All @@ -107,7 108,7 @@ describe('Auth routes', () => {

describe('GET /api/users/profile', () => {
it('Should return user profile', async () => {
const response = await request(app)
const response = await request(server)
.get('/api/users/profile')
.set('Authorization', `Bearer ${authToken}`);

Expand All @@ -118,7 119,7 @@ describe('Auth routes', () => {

it('Should return error if there is no logged in user', async () => {
const emptyToken = '';
const response = await request(app)
const response = await request(server)
.get('/api/users/profile')
.set('Authorization', `Bearer ${emptyToken}`);

Expand All @@ -141,7 142,7 @@ describe('Auth routes', () => {
};

// Make the update request
const updateResponse = await request(app)
const updateResponse = await request(server)
.put(`/api/users/${user._id}`)
.set('Authorization', `Bearer ${authToken}`)
.send(updatedUserInfo);
Expand Down Expand Up @@ -169,7 170,7 @@ describe('Auth routes', () => {
phone: '5559876543',
};

const updateResponse = await request(app)
const updateResponse = await request(server)
.put('/api/users/:userID')
.send(updatedUserInfo);

Expand All @@ -187,7 188,7 @@ describe('Auth routes', () => {
// Invalid data, missing required fields or wrong formats
};

const response = await request(app)
const response = await request(server)
.put('/api/users/update')
.set('Authorization', `Bearer ${authToken}`)
.send(updatedUserInfo);
Expand All @@ -198,7 199,7 @@ describe('Auth routes', () => {

describe('DELETE /api/users', () => {
it('Should delete own user profile', async () => {
const response = await request(app)
const response = await request(server)
.delete('/api/users')
.set('Authorization', `Bearer ${authToken}`);

Expand All @@ -210,7 211,7 @@ describe('Auth routes', () => {

it('Should return error if there is no logged in user', async () => {
const emptyToken = '';
const response = await request(app)
const response = await request(server)
.delete('/api/users')
.set('Authorization', `Bearer ${emptyToken}`);

Expand Down
16 changes: 8 additions & 8 deletions src/routes/__tests__/cart.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 1,5 @@
import request from 'supertest';
import app from '../../app';
import { config } from '../../config/config';
import { server } from '../../app';
import { StatusCodes } from 'http-status-codes';
import User from '../../models/user';
import generateToken from '../../utils/generateToken';
Expand Down Expand Up @@ -66,6 65,7 @@ describe('Cart routes', () => {
await User.findOneAndDelete({ email: customerUser.email });
await User.findOneAndDelete({ email: cookUser.email });
await Dish.findOneAndDelete({ _id: testDish._id });
server.close();
});

describe('POST /api/cart', () => {
Expand All @@ -75,7 75,7 @@ describe('Cart routes', () => {
quantity: 2,
};

const response = await request(app)
const response = await request(server)
.post('/api/cart')
.set('Authorization', `Bearer ${customerToken}`)
.send(newItem);
Expand All @@ -91,7 91,7 @@ describe('Cart routes', () => {

describe('GET /api/cart when there is a cart', () => {
it('Should get cart for customer', async () => {
const response = await request(app)
const response = await request(server)
.get('/api/cart')
.set('Authorization', `Bearer ${customerToken}`);

Expand All @@ -104,7 104,7 @@ describe('Cart routes', () => {
it('Should update dish quantity in cart', async () => {
const quantity = 3;

const response = await request(app)
const response = await request(server)
.put(`/api/cart/${testDish._id.toString()}`)
.set('Authorization', `Bearer ${customerToken}`)
.send({ quantity });
Expand All @@ -123,7 123,7 @@ describe('Cart routes', () => {

describe('DELETE /api/cart/{dishID}', () => {
it('Should delete a dish from cart', async () => {
const response = await request(app)
const response = await request(server)
.delete(`/api/cart/${testDish._id.toString()}`)
.set('Authorization', `Bearer ${customerToken}`)
.send();
Expand All @@ -139,7 139,7 @@ describe('Cart routes', () => {

describe('DELETE /api/cart', () => {
it("Should delete customer's cart", async () => {
const response = await request(app)
const response = await request(server)
.delete(`/api/cart`)
.set('Authorization', `Bearer ${customerToken}`)
.send();
Expand All @@ -150,7 150,7 @@ describe('Cart routes', () => {

describe('GET api/cart/', () => {
it("Should return 404 (NOT_FOUND) if the customer don't have a cart", async () => {
const response = await request(app)
const response = await request(server)
.get('/api/cart')
.set('Authorization', `Bearer ${customerToken}`);

Expand Down
27 changes: 14 additions & 13 deletions src/routes/__tests__/dish.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 1,5 @@
import request from 'supertest';
import app from '../../app';
import { server } from '../../app';
import Dish from '../../models/dish';
import User from '../../models/user';
import bcrypt from 'bcrypt';
Expand Down Expand Up @@ -79,17 79,18 @@ describe('Dish routes', () => {
await Dish.findOneAndDelete({ name: dish.name });
await Dish.findOneAndDelete({ name: dish_test_2.name });
await User.findOneAndDelete({ email: testUser2.email });
server.close();
});

/*Get All Dishes - 2 Test*/
describe('GET /api/dish', () => {
it('should get all dishes', async () => {
const response = await request(app).get('/api/dish');
const response = await request(server).get('/api/dish');
expect(response.statusCode).toBe(StatusCodes.OK);
expect(Array.isArray(response.body.data)).toBeTruthy();
});
it('should get all dishes that belong to that cook.', async () => {
const response = await request(app)
const response = await request(server)
.get('/api/dish')
.set('Authorization', `Bearer ${authToken}`);
expect(response.statusCode).toBe(StatusCodes.OK);
Expand All @@ -99,22 100,22 @@ describe('Dish routes', () => {
/*Get Dish by ID - 2 Tests*/
describe('GET /api/dish/:id', () => {
it('should get a dish by id', async () => {
const response = await request(app).get(`/api/dish/${dish.id}`);
const response = await request(server).get(`/api/dish/${dish.id}`);

expect(response.statusCode).toBe(StatusCodes.OK);
expect(response.body.data.id).toBe(dish.id);
});

it('should return 400 if dish not found', async () => {
const response = await request(app).get('/api/dish/123');
const response = await request(server).get('/api/dish/123');
expect(response.statusCode).toBe(StatusCodes.BAD_REQUEST);
});
});

/*Create new dish - 2 Test*/
describe('POST /api/dish', () => {
it('should create a new dish', async () => {
const response = await request(app)
const response = await request(server)
.post('/api/dish')
.set('Authorization', `Bearer ${authToken}`)
.send(dish_test_2);
Expand All @@ -123,7 124,7 @@ describe('Dish routes', () => {
expect(response.body.data.name).toBe(dish_test_2.name);
});
it('should return 401 if customers try to create new dish.', async () => {
const response = await request(app).post('/api/dish').send(dish_test_2);
const response = await request(server).post('/api/dish').send(dish_test_2);

expect(response.statusCode).toBe(StatusCodes.UNAUTHORIZED);
});
Expand All @@ -135,7 136,7 @@ describe('Dish routes', () => {
name: 'Updated Dish Name_1',
};

const response = await request(app)
const response = await request(server)
.put(`/api/dish/${dish.id}`)
.set('Authorization', `Bearer ${authToken}`)
.send(updates);
Expand All @@ -146,7 147,7 @@ describe('Dish routes', () => {
name: 'Updated Name',
};

const response = await request(app)
const response = await request(server)
.put('/api/dish/123')
.set('Authorization', `Bearer ${authToken}`)
.send(updates);
Expand All @@ -158,7 159,7 @@ describe('Dish routes', () => {
name: 'Updated Name',
};

const response = await request(app)
const response = await request(server)
.put(`/api/dish/${dish.id}`)
.send(updates);

Expand All @@ -168,7 169,7 @@ describe('Dish routes', () => {
/*Delete dish by ID - 3 Tests*/
describe('DELETE /api/dish/:id', () => {
it('should delete a dish', async () => {
const response = await request(app)
const response = await request(server)
.delete(`/api/dish/${dish.id}`)
.set('Authorization', `Bearer ${authToken}`);

Expand All @@ -179,15 180,15 @@ describe('Dish routes', () => {
it('should return 400 if dish does not exist', async () => {
const invalidId = '123';

const response = await request(app)
const response = await request(server)
.delete(`/api/dish/${invalidId}`)
.set('Authorization', `Bearer ${authToken}`);

expect(response.statusCode).toBe(StatusCodes.BAD_REQUEST);
});

it('should return 401 UNAUTHORIZED if invalid token', async () => {
const response = await request(app).delete(`/api/dish/${dish.id}`);
const response = await request(server).delete(`/api/dish/${dish.id}`);

expect(response.statusCode).toBe(StatusCodes.UNAUTHORIZED);
});
Expand Down
3 changes: 2 additions & 1 deletion src/routes/__tests__/order.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 1,5 @@
import request from 'supertest';
import app from '../../app';
import app, { server } from '../../app';
import Dish from '../../models/dish';
import User from '../../models/user';
import { StatusCodes } from 'http-status-codes';
Expand Down Expand Up @@ -81,6 81,7 @@ describe('Order routes', () => {
await User.findOneAndDelete({ email: cookUser.email });
await Cart.findOneAndDelete({ _id: cart._id });
await Order.findOneAndDelete({ _id: order._id });
server.close();
});

describe('POST /api/order/:cartId', () => {
Expand Down

0 comments on commit 0ed9022

Please sign in to comment.