Skip to content

Commit

Permalink
implementing order tracking and order managment issue
Browse files Browse the repository at this point in the history
implementing order tracking and order managment issue

implementing order tracking and order managment issue

implementing order tracking and order managment issue

adding documentation and tests

writing tests
  • Loading branch information
UwicyezaG authored and elijahladdie committed May 26, 2024
1 parent f7e268b commit 1b7a1a6
Show file tree
Hide file tree
Showing 32 changed files with 1,250 additions and 159 deletions.
116 changes: 95 additions & 21 deletions src/__test__/cart.test.test.ts → src/__test__/cart.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

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

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

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'server' 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

'server' 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

'server' 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

'server' is defined but never used
Expand All @@ -9,6 +10,7 @@ import { Product } from '../entities/Product';
import { Category } from '../entities/Category';
import { Cart } from '../entities/Cart';
import { CartItem } from '../entities/CartItem';
import { cleanDatabase } from './test-assets/DatabaseCleanup';

const vendor1Id = uuid();
const buyer1Id = uuid();
Expand Down Expand Up @@ -38,11 +40,11 @@ const sampleVendor1: UserInterface = {
id: vendor1Id,
firstName: 'vendor1',
lastName: 'user',
email: 'vendor1@example.com',
email: 'vendo111@example.com',
password: 'password',
userType: 'Vendor',
gender: 'Male',
phoneNumber: '126380996347',
phoneNumber: '11126380996347',
photoUrl: 'https://example.com/photo.jpg',
role: 'VENDOR',
};
Expand All @@ -51,11 +53,11 @@ const sampleBuyer1: UserInterface = {
id: buyer1Id,
firstName: 'buyer1',
lastName: 'user',
email: 'buyer1@example.com',
email: 'elijahladdiedv@gmail.com',
password: 'password',
userType: 'Buyer',
gender: 'Male',
phoneNumber: '126380996347',
phoneNumber: '12116380996347',
photoUrl: 'https://example.com/photo.jpg',
role: 'BUYER',
};
Expand All @@ -64,11 +66,11 @@ const sampleBuyer2: UserInterface = {
id: buyer2Id,
firstName: 'buyer1',
lastName: 'user',
email: 'buyer12@example.com',
email: 'buyer1112@example.com',
password: 'password',
userType: 'Buyer',
gender: 'Male',
phoneNumber: '126380996348',
phoneNumber: '12116380996348',
photoUrl: 'https://example.com/photo.jpg',
role: 'BUYER',
};
Expand Down Expand Up @@ -169,21 +171,8 @@ beforeAll(async () => {
});

afterAll(async () => {
const connection = getConnection();

const userRepository = connection.getRepository(User);
const categoryRepository = connection.getRepository(Category);
const productRepository = connection.getRepository(Product);
const cartRepository = connection.getRepository(Cart);

await cartRepository.delete({});
await productRepository.delete({});
await categoryRepository.delete({});
await userRepository.delete({});
await cleanDatabase()

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

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing semicolon

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

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing semicolon

// Close the connection to the test database
await connection.close();
server.close();
});

describe('Cart management for guest/buyer', () => {
Expand Down Expand Up @@ -521,4 +510,89 @@ describe('Cart management for guest/buyer', () => {
expect(response.body.data.cart).toBeDefined;
});
});
});
});

describe('Order management tests', () => {
let orderId: string | null;
describe('Create order', () => {
it('should return 400 when user ID is not provided', async () => {
const response = await request(app)
.post('/product/orders')
.send({
address: {
country: 'Test Country',
city: 'Test City',
street: 'Test Street',
},
}).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({
address: {
country: 'Test Country',
city: 'Test City',
street: 'Test Street',
},
})
.set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`);

expect(response.status).toBe(400);
expect(response.body.message).toBeUndefined;
orderId = response.body.data?.orderId; // Assuming orderId is returned in response
});
});

describe('Get orders', () => {
it('should return orders for the buyer', async () => {
const response = await request(app)
.get('/product/client/orders')
.set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`);
expect(response.status).toBe(404);
expect(response.body.message).toBeUndefined;

});

it('should return 404 if the buyer has no orders', async () => {
const response = await request(app)
.get('/product/client/orders')
.set('Authorization', `Bearer ${getAccessToken(buyer2Id, sampleBuyer2.email)}`);
expect(response.status).toBe(404);
expect(response.body.message).toBeUndefined;
});
});

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 () => {
const response = await request(app)
.get('/product/orders/history')
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(404);
});
});

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

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

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed

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

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed
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);
});
});
});
13 changes: 2 additions & 11 deletions src/__test__/coupon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { User, UserInterface } from '../entities/User';
import { Coupon } from '../entities/coupon';
import { Product } from '../entities/Product';
import { v4 as uuid } from 'uuid';
import { cleanDatabase } from './test-assets/DatabaseCleanup';

const vendor1Id = uuid();
const product1Id = uuid();
Expand Down Expand Up @@ -86,18 +87,8 @@ beforeAll(async () => {
});

afterAll(async () => {
const connection = getConnection();
await cleanDatabase()

const couponRepository = connection.getRepository(Coupon);
await couponRepository.delete({});

const productRepository = connection.getRepository(Product);
await productRepository.delete({});

const userRepository = connection.getRepository(User);
await userRepository.delete({});

await connection.close();
server.close();
});

Expand Down
13 changes: 3 additions & 10 deletions src/__test__/getProduct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { User, UserInterface } from '../entities/User';
import { v4 as uuid } from 'uuid';
import { Product } from '../entities/Product';
import { Category } from '../entities/Category';
import { cleanDatabase } from './test-assets/DatabaseCleanup';

const vendor1Id = uuid();
const product1Id = uuid();
Expand Down Expand Up @@ -67,16 +68,8 @@ beforeAll(async () => {
});

afterAll(async () => {
const connection = getConnection();
const userRepository = connection.getRepository(User);
const categoryRepository = connection.getRepository(Category);

const productRepository = await connection.getRepository(Product).delete({});
if (productRepository) {
await userRepository.delete({});
await categoryRepository.delete({});
}
await connection.close();
await cleanDatabase()

server.close();
});

Expand Down
10 changes: 2 additions & 8 deletions src/__test__/isAllowed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getConnection } from 'typeorm';
import { User } from '../entities/User';
import { responseError } from '../utils/response.utils';
import { v4 as uuid } from 'uuid';
import { cleanDatabase } from './test-assets/DatabaseCleanup';

jest.mock('../utils/response.utils');

Expand Down Expand Up @@ -47,15 +48,8 @@ beforeAll(async () => {
});

afterAll(async () => {
const connection = getConnection();
const userRepository = connection.getRepository(User);
await cleanDatabase()


// Delete all records from the User
await userRepository.delete({});

// Close the connection to the test database
await connection.close();
});

describe('Middleware - checkUserStatus', () => {
Expand Down
12 changes: 3 additions & 9 deletions src/__test__/logout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ import request from 'supertest';
import { app, server } from '../index';
import { createConnection, getConnection, getConnectionOptions, getRepository } from 'typeorm';
import { User } from '../entities/User';
import { cleanDatabase } from './test-assets/DatabaseCleanup';

beforeAll(async () => {
// Connect to the test database
const connectionOptions = await getConnectionOptions();
await createConnection({ ...connectionOptions, name: 'testConnection' });
await createConnection();
});

afterAll(async () => {
const connection = getConnection('testConnection');
const userRepository = connection.getRepository(User);
await cleanDatabase()

// Delete all records from the User
await userRepository.delete({});

// Close the connection to the test database
await connection.close();

server.close();
});
Expand Down
15 changes: 3 additions & 12 deletions src/__test__/oauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@ import request from 'supertest';
import { app, server } from '../index';
import { createConnection, getConnection, getConnectionOptions, getRepository } from 'typeorm';
import { User } from '../entities/User';
import { cleanDatabase } from './test-assets/DatabaseCleanup';

beforeAll(async () => {
// Connect to the test database
const connectionOptions = await getConnectionOptions();

await createConnection({ ...connectionOptions, name: 'testConnection' });
await createConnection();
});

afterAll(async () => {
const connection = getConnection('testConnection');
const userRepository = connection.getRepository(User);

// Delete all records from the User
await userRepository.delete({});

// Close the connection to the test database
await connection.close();

await cleanDatabase()
server.close();
});
describe('authentication routes test',() => {
Expand Down
12 changes: 2 additions & 10 deletions src/__test__/productStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { User } from '../entities/User';
import { v4 as uuid } from 'uuid';
import { Product } from '../entities/Product';
import { Category } from '../entities/Category';
import { cleanDatabase } from './test-assets/DatabaseCleanup';

const vendor1Id = uuid();
const vendor2Id = uuid();
Expand Down Expand Up @@ -143,17 +144,8 @@ beforeAll(async () => {
});

afterAll(async () => {
const connection = getConnection();
const userRepository = connection.getRepository(User);
const categoryRepository = connection.getRepository(Category);
await cleanDatabase()

const productRepository = await connection.getRepository(Product).delete({});
if (productRepository) {
await userRepository.delete({});
await categoryRepository.delete({});
}

await connection.close();
server.close();
});

Expand Down
10 changes: 2 additions & 8 deletions src/__test__/roleCheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { responseError } from '../utils/response.utils';
import { dbConnection } from '../startups/dbConnection';
import { v4 as uuid } from 'uuid';
import { getConnection } from 'typeorm';
import { cleanDatabase } from './test-assets/DatabaseCleanup';

let reqMock: Partial<Request>;
let resMock: Partial<Response>;
Expand Down Expand Up @@ -34,14 +35,7 @@ beforeAll(async () => {
});

afterAll(async () => {
const connection = getConnection();
const userRepository = connection.getRepository(User);

// Delete all records from the User
await userRepository.delete({});

// Close the connection to the test database
await connection.close();
await cleanDatabase()
});

describe('hasRole MiddleWare Test', () => {
Expand Down
14 changes: 3 additions & 11 deletions src/__test__/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@ import { app, server } from '../index';
import { createConnection, getConnection, getConnectionOptions, getRepository } from 'typeorm';
import { User } from '../entities/User';
import { response } from 'express';
import { cleanDatabase } from './test-assets/DatabaseCleanup';

beforeAll(async () => {
// Connect to the test database
const connectionOptions = await getConnectionOptions();

await createConnection({ ...connectionOptions, name: 'testConnection' });
await createConnection();
});

jest.setTimeout(20000);
afterAll(async () => {
const connection = getConnection('testConnection');
const userRepository = connection.getRepository(User);

// Delete all records from the User
await userRepository.delete({});
await cleanDatabase()

// Close the connection to the test database
await connection.close();

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

0 comments on commit 1b7a1a6

Please sign in to comment.