Skip to content

Commit

Permalink
created coupons (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
niyobern authored and ambroisegithub committed May 27, 2024
1 parent ea6d0fd commit a1892bf
Show file tree
Hide file tree
Showing 30 changed files with 1,538 additions and 52 deletions.
9 changes: 0 additions & 9 deletions .github/PULL_REQUEST_TEMPLATE.MD

This file was deleted.

13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"passport-facebook": "^3.0.0",
"passport-google-oauth": "^2.0.0",
"pg": "^8.11.5",
"stripe": "^15.8.0",
"supertest": "^7.0.0",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0",
Expand All @@ -67,7 +68,8 @@
"coveragePathIgnorePatterns": [
"/node_modules/",
"/src/emails/",
"/src/middlewares/"
"/src/middlewares/",
"/src/emails/"
],
"testPathIgnorePatterns": [
"/node_modules/",
Expand Down Expand Up @@ -103,4 +105,4 @@
"ts-node-dev": "^2.0.0",
"typescript": "^5.4.5"
}
}
}
123 changes: 120 additions & 3 deletions src/__test__/cartController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
getBuyerToken,
getVendorToken,
} from './testSetup';
import {Cart} from '../database/models/cartEntity'
import dbConnection from '../database';
const cartRepository =dbConnection.getRepository(Cart)
beforeAll(beforeAllHook);
afterAll(afterAllHook);

Expand All @@ -15,13 +18,14 @@ describe('Cart controller tests', () => {
let productId: number;
let itemId: number;
let categoryId: number;
let orderId: number;

beforeAll(async () => {
buyerToken = await getBuyerToken();
vendorToken = await getVendorToken();
});

it('should return cart items and total amount for user', async () => {
it('should return cart items and total amount for the user', async () => {
// create a category
const categoryData = {
name: 'Category4',
Expand Down Expand Up @@ -159,7 +163,8 @@ describe('Cart controller tests', () => {
expect(response.statusCode).toEqual(409);
expect(response.body.msg).toEqual('Invalid Quantity');
});
it('should return 409 if quantity exceeds available quantity while ugating a cart', async () => {

it('should return 409 if quantity exceeds available quantity while updating a cart', async () => {
const exceedQuantity = 50000;
const response = await request(app)
.patch(`/api/v1/cart/${itemId}`)
Expand Down Expand Up @@ -190,6 +195,7 @@ describe('Cart controller tests', () => {
expect(response.body.msg).toEqual('Cart Item deleted successfully');
expect(response.body.count).toBeGreaterThan(0);
});

it('should remove all items from the cart successfully', async () => {
const response = await request(app)
.delete('/api/v1/cart')
Expand All @@ -199,4 +205,115 @@ describe('Cart controller tests', () => {
expect(response.body.msg).toEqual('Cart Items deleted successfully');
expect(response.body.count).toBeGreaterThanOrEqual(0);
});
});

// New tests for checkout, cancel checkout, and get all orders

it('should place an order successfully', async () => {
const cartResponse = await request(app)
.post('/api/v1/cart')
.set('Authorization', `Bearer ${buyerToken}`)
.send({
productId: productId,
quantity: 2,
});

expect(cartResponse.statusCode).toEqual(201);
expect(cartResponse.body.msg).toEqual('Item added to cart successfully');
expect(cartResponse.body.cartItem).toBeDefined();

const checkoutResponse = await request(app)
.post('/api/v1/checkout')
.set('Authorization', `Bearer ${buyerToken}`)
.send({
deliveryInfo: '123 Delivery St.',
paymentInfo: 'VISA 1234',
couponCode: 'DISCOUNT10',
});

expect(checkoutResponse.statusCode).toEqual(201);
expect(checkoutResponse.body.msg).toEqual('Order placed successfully');
expect(checkoutResponse.body.order).toBeDefined();
expect(checkoutResponse.body.trackingNumber).toBeDefined();
orderId = checkoutResponse.body.order.id;
});

it('should cancel an order successfully', async () => {
const response = await request(app)
.delete(`/api/v1/checkout/cancel-order/${orderId}`)
.set('Authorization', `Bearer ${buyerToken}`);

expect(response.statusCode).toEqual(200);
expect(response.body.msg).toEqual('Order canceled successfully');
});

it('should return 404 if order is not found while canceling', async () => {
const nonExistentOrderId = 9999;
const response = await request(app)
.delete(`/api/v1/checkout/cancel-order/${nonExistentOrderId}`)
.set('Authorization', `Bearer ${buyerToken}`);

expect(response.statusCode).toEqual(404);
expect(response.body.msg).toEqual('Order not found');
});


it('should return 401 if user is not found while checking out', async () => {
// Simulate a request with a non-existent user ID
const invalidUserToken = 'Bearer invalid-user-token';

const response = await request(app)
.post('/api/v1/checkout')
.set('Authorization', invalidUserToken)
.send({
deliveryInfo: '123 Delivery St.',
paymentInfo: 'VISA 1234',
couponCode: 'DISCOUNT10',
});

expect(response.statusCode).toEqual(401);
expect(response.body.msg).toBeUndefined();
});




it('should return all orders', async () => {
const response = await request(app)
.get('/api/v1/checkout/getall-order')
.set('Authorization', `Bearer ${buyerToken}`);

expect(response.statusCode).toEqual(200);
expect(response.body.orders).toBeDefined();
});




it('should return 400 if cart is empty while checking out', async () => {
// Clear the cart before attempting to checkout
await cartRepository.delete({});

const response = await request(app)
.post('/api/v1/checkout')
.set('Authorization', `Bearer ${buyerToken}`)
.send({
deliveryInfo: '123 Delivery St.',
paymentInfo: 'VISA 1234',
couponCode: 'DISCOUNT10',
});

expect(response.statusCode).toEqual(400);
expect(response.body.msg).toEqual('Cart is empty');
});


it('should delete all orders', async () => {
const response = await request(app)
.delete('/api/v1/checkout/removeall-order')
.set('Authorization', `Bearer ${buyerToken}`);

expect(response.statusCode).toEqual(200);
expect(response.body.msg).toEqual('All orders deleted successfully');
});

})
Loading

0 comments on commit a1892bf

Please sign in to comment.