Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#92 Vendor and Admin order management #98

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ env:
CLOUDINARY_API_SECRET: ${{secrets.CLOUDINARY_API_SECRET}}
GOOGLE_CLIENT_ID: ${{secrets.GOOGLE_CLIENT_ID}}
GOOGLE_CLIENT_SECRET: ${{secrets.GOOGLE_CLIENT_SECRET}}


jobs:
build-lint-test-coverage:
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"passport-google-oauth20": "^2.0.0",
"pg": "^8.11.5",
"reflect-metadata": "^0.2.2",
"socket.io": "^4.7.5",
"source-map-support": "^0.5.21",
"superagent": "^9.0.1",
"swagger-jsdoc": "^6.2.8",
Expand Down Expand Up @@ -78,6 +79,7 @@
"@types/nodemailer": "^6.4.15",
"@types/passport-google-oauth20": "^2.0.16",
"@types/reflect-metadata": "^0.1.0",
"@types/socket.io": "^3.0.2",
"@types/supertest": "^6.0.2",
"@types/uuid": "^9.0.8",
"@types/winston": "^2.4.4",
Expand Down
17 changes: 6 additions & 11 deletions src/__test__/cart.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

import request from 'supertest';
import jwt from 'jsonwebtoken';
import { app, server } from '../index';
import { getConnection } from 'typeorm';

Check warning on line 4 in src/__test__/cart.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 4 in src/__test__/cart.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used

Check warning on line 4 in src/__test__/cart.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 4 in src/__test__/cart.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used
import { dbConnection } from '../startups/dbConnection';
import { v4 as uuid } from 'uuid';
import { User, UserInterface } from '../entities/User';
Expand All @@ -26,7 +25,7 @@

const jwtSecretKey = process.env.JWT_SECRET || '';

const getAccessToken = (id: string, email: string) => {

Check warning on line 28 in src/__test__/cart.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing return type on function

Check warning on line 28 in src/__test__/cart.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing return type on function
return jwt.sign(
{
id: id,
Expand Down Expand Up @@ -171,8 +170,9 @@
});

afterAll(async () => {
await cleanDatabase()
await cleanDatabase();

server.close();
});

describe('Cart management for guest/buyer', () => {
Expand Down Expand Up @@ -524,12 +524,12 @@
city: 'Test City',
street: 'Test Street',
},
}).set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
})
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(400);
});

it('should create a new order', async () => {

const response = await request(app)
.post('/product/orders')
.send({
Expand All @@ -547,7 +547,6 @@
});

it('should insert a new order', async () => {

const response = await request(app)
.post('/product/orders')
.send({
Expand All @@ -570,9 +569,8 @@
const response = await request(app)
.get('/product/client/orders')
.set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`);
expect(response.status).toBe(404);
expect(response.status).toBe(404);
expect(response.body.message).toBeUndefined;

});

it('should return 404 if the buyer has no orders', async () => {
Expand All @@ -586,13 +584,11 @@

describe('Get transaction history', () => {
it('should return transaction history for the buyer', async () => {

const response = await request(app)
.get('/product/orders/history')
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(404);
expect(response.body.message).toBe('No transaction history found');

});

it('should return 400 when user ID is not provided', async () => {
Expand All @@ -605,12 +601,11 @@

describe('Update order', () => {
it('should update order status successfully', async () => {

const response = await request(app)
.put(`/product/client/orders/${orderId}`)
.send({ orderStatus: 'delivered' })
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(500);
expect(response.status).toBe(500);
});
});
});
80 changes: 41 additions & 39 deletions src/__test__/coupon.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import request from 'supertest';
import jwt from 'jsonwebtoken';
import { app, server } from '../index';
import { getConnection } from 'typeorm';

Check warning on line 4 in src/__test__/coupon.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 4 in src/__test__/coupon.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used

Check warning on line 4 in src/__test__/coupon.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 4 in src/__test__/coupon.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used
import { dbConnection } from '../startups/dbConnection';
import { User, UserInterface } from '../entities/User';
import { Coupon } from '../entities/coupon';
Expand All @@ -21,7 +21,7 @@
const couponCode = 'DISCOUNT20';
const couponCode1 = 'DISCOUNT10';
const couponCode2 = 'DISCOUNT99';
const couponCode3 = 'DISCOUNT22'
const couponCode3 = 'DISCOUNT22';
const expiredCouponCode = 'EXPIRED';
const finishedCouponCode = 'FINISHED';
const moneyCouponCode = 'MONEY';
Expand All @@ -29,7 +29,7 @@

const jwtSecretKey = process.env.JWT_SECRET || '';

const getAccessToken = (id: string, email: string) => {

Check warning on line 32 in src/__test__/coupon.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing return type on function

Check warning on line 32 in src/__test__/coupon.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing return type on function
return jwt.sign(
{
id: id,
Expand Down Expand Up @@ -199,11 +199,10 @@

const cartItemRepository = connection?.getRepository(CartItem);
await cartItemRepository?.save({ ...sampleCartItem1 });

});

afterAll(async () => {
await cleanDatabase()
await cleanDatabase();

server.close();
});
Expand Down Expand Up @@ -340,84 +339,87 @@
});

describe('Buyer Coupon Application', () => {
describe('Checking Coupon Conditions', () =>{
describe('Checking Coupon Conditions', () => {
it('should return 400 when no coupon submitted', async () => {
const response = await request(app)
.post(`/coupons/apply`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);

expect(response.status).toBe(400);
expect(response.body.message).toBe('Coupon Code is required');
})
expect(response.status).toBe(400);
expect(response.body.message).toBe('Coupon Code is required');
});
it('should return 404 if coupon code is not found in the database', async () => {
const response = await request(app)
.post(`/coupons/apply`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`)
.send({
couponCode: "InvalidCode",
couponCode: 'InvalidCode',
});

expect(response.status).toBe(404);
expect(response.body.message).toBe('Invalid Coupon Code');
})
expect(response.status).toBe(404);
expect(response.body.message).toBe('Invalid Coupon Code');
});
it('should not allow use of expired tokens', async () => {
const response = await request(app)
.post(`/coupons/apply`)
.post(`/coupons/apply`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`)
.send({
couponCode: expiredCoupon.code,
});

expect(response.status).toBe(400);
expect(response.body.message).toBe('Coupon is expired');
})
expect(response.status).toBe(400);
expect(response.body.message).toBe('Coupon is expired');
});
it('should not allow use of coupon that reach maximum users', async () => {
const response = await request(app)
.post(`/coupons/apply`)
.post(`/coupons/apply`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`)
.send({
couponCode: finishedCoupon.code,
});

expect(response.status).toBe(400);
expect(response.body.message).toBe('Coupon Discount Ended');
})
expect(response.status).toBe(400);
expect(response.body.message).toBe('Coupon Discount Ended');
});
it('Should not work when the product is not in cart', async () => {
const response = await request(app)
.post(`/coupons/apply`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`)
.send({
.post(`/coupons/apply`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`)
.send({
couponCode: sampleCoupon3.code,
});

expect(response.status).toBe(404);
expect(response.body.message).toBe("No product in Cart with that coupon code");
})
})
expect(response.body.message).toBe('No product in Cart with that coupon code');
});
});

describe("Giving discount according the the product coupon", () => {
describe('Giving discount according the the product coupon', () => {
it('Should give discont when discount-type is percentage', async () => {
const response = await request(app)
.post(`/coupons/apply`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`)
.send({
.post(`/coupons/apply`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`)
.send({
couponCode: sampleCoupon2.code,
});

expect(response.status).toBe(200);
expect(response.body.message).toBe(`Coupon Code successfully activated discount on product: ${sampleProduct1.name}`);
})
expect(response.body.message).toBe(
`Coupon Code successfully activated discount on product: ${sampleProduct1.name}`
);
});
it('Should give discont when discount-type is money', async () => {
const response = await request(app)
.post(`/coupons/apply`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`)
.send({
.post(`/coupons/apply`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`)
.send({
couponCode: moneyCoupon.code,
});

expect(response.status).toBe(200);
expect(response.body.message).toBe(`Coupon Code successfully activated discount on product: ${sampleProduct1.name}`);
})
})

})
expect(response.body.message).toBe(
`Coupon Code successfully activated discount on product: ${sampleProduct1.name}`
);
});
});
});
78 changes: 39 additions & 39 deletions src/__test__/errorHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import { Request, Response } from 'express';
import { CustomError, errorHandler } from '../middlewares/errorHandler'
import { CustomError, errorHandler } from '../middlewares/errorHandler';

describe('CustomError', () => {
it('should create a CustomError object with statusCode and status properties', () => {
const message = 'Test error message';
const statusCode = 404;
const customError = new CustomError(message, statusCode);
expect(customError.message).toBe(message);
expect(customError.statusCode).toBe(statusCode);
expect(customError.status).toBe('fail');
});
it('should create a CustomError object with statusCode and status properties', () => {
const message = 'Test error message';
const statusCode = 404;
const customError = new CustomError(message, statusCode);
expect(customError.message).toBe(message);
expect(customError.statusCode).toBe(statusCode);
expect(customError.status).toBe('fail');
});
});

describe('errorHandler', () => {
it('should send correct response with status code and message', () => {
const err = new CustomError('Test error message', 404);
const req = {} as Request;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
const next = jest.fn();
errorHandler(err, req, res, next);
expect(res.status).toHaveBeenCalledWith(404);
expect(res.json).toHaveBeenCalledWith({
status: 404,
message: 'Test error message',
});
describe('errorHandler', () => {
it('should send correct response with status code and message', () => {
const err = new CustomError('Test error message', 404);
const req = {} as Request;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
const next = jest.fn();
errorHandler(err, req, res, next);
expect(res.status).toHaveBeenCalledWith(404);
expect(res.json).toHaveBeenCalledWith({
status: 404,
message: 'Test error message',
});
it('should handle errors with status code 500', () => {
const err = new CustomError('something went wrong', 500);
const req = {} as Request;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
const next = jest.fn();
errorHandler(err, req, res, next);
});
it('should handle errors with status code 500', () => {
const err = new CustomError('something went wrong', 500);
const req = {} as Request;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
const next = jest.fn();
errorHandler(err, req, res, next);

expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({
status: 500,
message: 'something went wrong',
});
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({
status: 500,
message: 'something went wrong',
});
});
});
});
2 changes: 1 addition & 1 deletion src/__test__/getProduct.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import request from 'supertest';
import jwt from 'jsonwebtoken';
import { app, server } from '../index';
import { getConnection } from 'typeorm';

Check warning on line 4 in src/__test__/getProduct.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 4 in src/__test__/getProduct.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used

Check warning on line 4 in src/__test__/getProduct.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 4 in src/__test__/getProduct.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used
import { dbConnection } from '../startups/dbConnection';
import { User, UserInterface } from '../entities/User';
import { v4 as uuid } from 'uuid';
Expand All @@ -16,7 +16,7 @@

const jwtSecretKey = process.env.JWT_SECRET || '';

const getAccessToken = (id: string, email: string) => {

Check warning on line 19 in src/__test__/getProduct.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing return type on function

Check warning on line 19 in src/__test__/getProduct.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing return type on function
return jwt.sign(
{
id: id,
Expand Down Expand Up @@ -68,7 +68,7 @@
});

afterAll(async () => {
await cleanDatabase()
await cleanDatabase();

server.close();
});
Expand Down
29 changes: 14 additions & 15 deletions src/__test__/isAllowed.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextFunction, Request, Response } from 'express';
import { checkUserStatus } from '../middlewares/isAllowed';
import { dbConnection } from '../startups/dbConnection';
import { getConnection } from 'typeorm';

Check warning on line 4 in src/__test__/isAllowed.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 4 in src/__test__/isAllowed.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'getConnection' is defined but never used. Allowed unused vars must match /^_/u
import { User } from '../entities/User';
import { responseError } from '../utils/response.utils';
import { v4 as uuid } from 'uuid';
Expand Down Expand Up @@ -48,24 +48,23 @@
});

afterAll(async () => {
await cleanDatabase()

await cleanDatabase();
});

describe('Middleware - checkUserStatus', () => {
beforeEach(() => {
reqMock = {};
resMock = {
status: jest.fn().mockReturnThis(),
json: jest.fn()
};
nextMock = jest.fn();
});

it('should return 401 if user is not authenticated', async () => {
await checkUserStatus(reqMock as Request, resMock as Response, nextMock);
expect(responseError).toHaveBeenCalledWith(resMock, 401, 'Authentication required');
});
beforeEach(() => {
reqMock = {};
resMock = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};
nextMock = jest.fn();
});

it('should return 401 if user is not authenticated', async () => {
await checkUserStatus(reqMock as Request, resMock as Response, nextMock);
expect(responseError).toHaveBeenCalledWith(resMock, 401, 'Authentication required');
});

it('should return 401 if user is not found', async () => {
reqMock = { user: { id: uuid() } };
Expand Down
3 changes: 1 addition & 2 deletions src/__test__/logout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ beforeAll(async () => {
});

afterAll(async () => {
await cleanDatabase()

await cleanDatabase();

server.close();
});
Expand Down
Loading