Skip to content

Commit

Permalink
rebase from update profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawaic6 authored and ambroisegithub committed May 28, 2024
1 parent 48b2d73 commit e53d0ba
Show file tree
Hide file tree
Showing 26 changed files with 965 additions and 29 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 @@ -70,7 +71,8 @@
],
"testPathIgnorePatterns": [
"/node_modules/",
"/src/emails/"
"/src/emails/",
"/src/middlewares/"
]
},
"devDependencies": {
Expand Down Expand Up @@ -102,4 +104,4 @@
"ts-node-dev": "^2.0.0",
"typescript": "^5.4.5"
}
}
}
139 changes: 138 additions & 1 deletion src/__test__/cartController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ 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,7 +20,6 @@ describe('Cart controller tests', () => {
let productId: number;
let itemId: number;
let categoryId: number;

beforeAll(async () => {
buyerToken = await getBuyerToken();
vendorToken = await getVendorToken();
Expand Down Expand Up @@ -200,3 +204,136 @@ describe('Cart controller tests', () => {
expect(response.body.count).toBeGreaterThanOrEqual(0);
});
});

describe('Checkout Tests', () => {
let buyerToken: string;
let productId: number;
let orderId: number;
beforeAll(async () => {
buyerToken = await getBuyerToken();
});
it('should return a 400 status code if validation errors occur', async () => {
const response = await request(app)
.post('/api/v1/checkout')
.set('Authorization', `Bearer ${buyerToken}`)
.send({
deliveryInfo: '',
paymentInfo: '',
couponCode: 'DISCOUNT10',
});

expect(response.status).toBe(400);
expect(response.body.errors).toBeDefined();
});

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 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 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');
});
});
2 changes: 1 addition & 1 deletion src/__test__/coupon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,4 @@ describe('Coupon Controller Tests', () => {
expect(response.statusCode).toEqual(404);
expect(response.body.error).toEqual('Coupon not found');
});
});
});
4 changes: 2 additions & 2 deletions src/__test__/testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import app from '../app';

export async function beforeAllHook() {
await DbConnection.instance.initializeDb();
await dbConnection.synchronize(true) // This will drop all tables
await dbConnection.synchronize(true); // This will drop all tables
}
export async function getAdminToken() {
const userRepository = await DbConnection.connection.getRepository(UserModel);
Expand Down Expand Up @@ -129,4 +129,4 @@ export const getBuyerToken = async () => {

export async function afterAllHook() {
await DbConnection.instance.disconnectDb();
}
}
82 changes: 81 additions & 1 deletion src/__test__/userController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,84 @@ describe('Get All Users Tests', () => {
expect(response.status).toBe(200);
expect(response.body.message).toEqual('All users deleted successfully');
});
});
});
describe('update user Profile', () => {
interface IUser {
id: number;
firstName: string;
lastName: string;
email: string;
password?: string;
userType?: Role;
googleId?: string;
facebookId?: string;
picture?: string;
provider?: string;
isVerified: boolean;
twoFactorCode?: number;
}

interface Role {
id: number;
name: string;
permissions: string[];
}


let user: IUser | undefined | null;
const userData = {
firstName: 'jan',
lastName: 'bosco',
email: 'bosco@gmail.com',
password: 'boscoPassword123',
};

beforeEach(async () => {

await request(app).post('/api/v1/register').send(userData);
user = await userRepository.findOne({ where: { email: userData.email } });
});

it('should update the user profile successfully', async () => {
if (user) {
const newUserData = {
firstName: 'NewFirstName',
lastName: 'NewLastName',
email: 'newemail@example.com',
password: 'bosco@gmail.com',
};

const response = await request(app)
.put(`/api/v1/updateProfile/${user?.id}`)
.send(newUserData);
expect(response.statusCode).toBe(201);
expect(response.body.message).toBe('User updated successfully');
}
});

it('should return 404 when user not found', async () => {
const Id = 999;
const response = await request(app)
.put(`/api/v1/updateProfile/${Id}`)
.send(userData);
expect(response.statusCode).toBe(404);
expect(response.body.error).toBe('User not found');
});

it('should return 400 when email already exists', async () => {
if (user) {
const newUserData = {
firstName: 'NewFirstName',
lastName: 'NewLastName',
email: 'newemail@example.com',
password: 'bosco@gmail.com',
};

const response = await request(app)
.put(`/api/v1/updateProfile/${user.id}`)
.send(newUserData);
expect(response.statusCode).toBe(400);
expect(response.body.error).toBe('Email is already taken');
}
});
});
1 change: 0 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import swaggerUi from 'swagger-ui-express';
import swaggerSpec from './docs/swaggerconfig';
import 'reflect-metadata';
import router from './routes/index';

import fs from 'fs';
import path from 'path';
import authRoutes from './routes/auth-routes';
Expand Down
2 changes: 1 addition & 1 deletion src/config/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ const config: {
production,
};

export default config[env];
export default config[env];
Loading

0 comments on commit e53d0ba

Please sign in to comment.