Skip to content

Commit

Permalink
Feat-Notification-Management
Browse files Browse the repository at this point in the history
  • Loading branch information
Calebgisa72 committed Jun 4, 2024
1 parent 89ac0c5 commit cc0250e
Show file tree
Hide file tree
Showing 42 changed files with 1,406 additions and 145 deletions.
248 changes: 146 additions & 102 deletions src/__test__/cart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { cleanDatabase } from './test-assets/DatabaseCleanup';
const vendor1Id = uuid();
const buyer1Id = uuid();
const buyer2Id = uuid();
const buyer3Id = uuid();
const product1Id = uuid();
const product2Id = uuid();
const catId = uuid();
Expand Down Expand Up @@ -52,7 +53,7 @@ const sampleBuyer1: UserInterface = {
id: buyer1Id,
firstName: 'buyer1',
lastName: 'user',
email: 'elijahladdiedv@gmail.com',
email: 'manger@gmail.com',
password: 'password',
userType: 'Buyer',
gender: 'Male',
Expand All @@ -65,14 +66,26 @@ const sampleBuyer2: UserInterface = {
id: buyer2Id,
firstName: 'buyer1',
lastName: 'user',
email: 'buyer1112@example.com',
email: 'elijahladdiedv@example.com',
password: 'password',
userType: 'Buyer',
gender: 'Male',
phoneNumber: '12116380996348',
photoUrl: 'https://example.com/photo.jpg',
role: 'BUYER',
};
const sampleBuyer3: UserInterface = {
id: buyer3Id,
firstName: 'buyer1',
lastName: 'user',
email: '[email protected]',
password: 'password',
userType: 'Admin',
gender: 'Male',
phoneNumber: '121163800',
photoUrl: 'https://example.com/photo.jpg',
role: 'ADMIN',
};

const sampleCat = {
id: catId,
Expand Down Expand Up @@ -175,7 +188,7 @@ afterAll(async () => {
server.close();
});

describe('Cart management for guest/buyer', () => {
describe('Cart| Order management for guest/buyer', () => {
describe('Creating new product', () => {
it('should create new product', async () => {
const response = await request(app)
Expand All @@ -193,7 +206,7 @@ describe('Cart management for guest/buyer', () => {

expect(response.status).toBe(201);
expect(response.body.data.product).toBeDefined;
}, 60000);
});

it('return an error if the number of product images exceeds 6', async () => {
const response = await request(app)
Expand Down Expand Up @@ -376,6 +389,135 @@ describe('Cart management for guest/buyer', () => {
});
});

describe('Order management tests', () => {
let orderId: any;
let productId: any;
let feedbackId: any;
let feedback2Id: any;
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(201);
});

it('should return orders for the buyer', async () => {
const response = await request(app)
.get('/product/client/orders')
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(200);
orderId = response.body.data.orders[0]?.id;
productId = response.body.data.orders[0]?.orderItems[0]?.product?.id;
});


it('should get single order', async () => {
const response = await request(app)
.get(`/product/client/orders/${orderId}`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);

expect(response.status).toBe(200);
expect(response.body.data.order).toBeDefined();
});

it('should not return data for single order, if order doesn\'t exist', async () => {
const response = await request(app)
.get(`/product/client/orders/${uuid()}`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);

expect(response.status).toBe(404);
});

it('should not return data for single order, for an incorrect id syntax', async () => {
const response = await request(app)
.get(`/product/client/orders/incorrectId`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);

expect(response.status).toBe(400);
});

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;
});

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(200);
expect(response.body.message).toBe('Transaction history retrieved successfully');
});

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(200);
});
});

describe('Update order', () => {
it('should update order status successfully', async () => {
const response = await request(app)
.put(`/product/client/orders/${orderId}`)
.send({ orderStatus: 'completed' })
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(200);
});
});
describe('Add feedback to the product with order', () => {
it('should create new feedback to the ordered product', async () => {
const response = await request(app)
.post(`/feedback/${productId}/new`)
.send({ orderId, comment: 'Well this product looks so fantastic' })
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(201);
feedbackId = response.body.data.id
});
it('should create new feedback to the ordered product', async () => {
const response = await request(app)
.post(`/feedback/${productId}/new`)
.send({ orderId, comment: 'Murigalike this product looks so fantastic' })
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(201);
feedback2Id = response.body.data.id
});
it('should updated existing feedback successfully', async () => {
const response = await request(app)
.put(`/feedback/update/${feedbackId}`,)
.send({ orderId, comment: 'Well this product looks so lovely' })
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(200);
});
it('should remove recorded feedback', async () => {
const response = await request(app)
.delete(`/feedback/delete/${feedbackId}`)
.send({ orderId, comment: 'Well this product looks so lovely' })
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(200);
});
it('should remove recorder feedback as admin ', async () => {
const response = await request(app)
.delete(`/feedback/admin/delete/${feedback2Id}`)
.send({ orderId, comment: 'Well this product looks so lovely' })
.set('Authorization', `Bearer ${getAccessToken(buyer3Id, sampleBuyer3.email)}`);
expect(response.status).toBe(401);
});
});
});

describe('Deleting product from cart', () => {
it('should return 404 if product does not exist in cart', async () => {
const response = await request(app)
Expand Down Expand Up @@ -511,101 +653,3 @@ describe('Cart management for guest/buyer', () => {
});
});
});

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
});

it('should insert 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 () => {
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);
});
});
});
Loading

0 comments on commit cc0250e

Please sign in to comment.