Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:atlp-rwanda/knights-ecomm-be int…
Browse files Browse the repository at this point in the history
…o develop
  • Loading branch information
Calebgisa72 committed May 25, 2024
2 parents 4412e1b + f7e268b commit c5e2d29
Show file tree
Hide file tree
Showing 18 changed files with 1,075 additions and 323 deletions.
2 changes: 1 addition & 1 deletion src/__test__/cart.test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,4 @@ describe('Cart management for guest/buyer', () => {
expect(response.body.data.cart).toBeDefined;
});
});
});
});
233 changes: 233 additions & 0 deletions src/__test__/coupon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import request from 'supertest';
import jwt from 'jsonwebtoken';
import { app, server } from '../index';
import { getConnection } from 'typeorm';
import { dbConnection } from '../startups/dbConnection';
import { User, UserInterface } from '../entities/User';
import { Coupon } from '../entities/coupon';
import { Product } from '../entities/Product';
import { v4 as uuid } from 'uuid';

const vendor1Id = uuid();
const product1Id = uuid();
const couponCode = 'DISCOUNT20';
const couponCode1 = 'DISCOUNT10';
const invalidCouponCode = 'INVALIDCODE';

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

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

Check warning on line 19 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,
email: email,
},
jwtSecretKey
);
};

const sampleVendor1: UserInterface = {
id: vendor1Id,
firstName: 'Vendor',
lastName: 'User',
email: '[email protected]',
password: 'password',
userType: 'Vendor',
gender: 'Male',
phoneNumber: '1234567890',
photoUrl: 'https://example.com/photo.jpg',
role: 'VENDOR',
};

const sampleProduct1 = {
id: product1Id,
name: 'Test Product',
description: 'Amazing product',
images: ['photo1.jpg', 'photo2.jpg', 'photo3.jpg'],
newPrice: 200,
quantity: 10,
vendor: sampleVendor1,
};

const sampleCoupon = {
code: couponCode,
discountRate: 20,
expirationDate: new Date('2025-01-01'),
maxUsageLimit: 100,
discountType: 'PERCENTAGE',
product: sampleProduct1,
vendor: sampleVendor1,
};

const sampleCoupon1 = {
code: couponCode1,
discountRate: 20,
expirationDate: new Date('2025-01-01'),
maxUsageLimit: 100,
discountType: 'PERCENTAGE',
product: sampleProduct1,
vendor: sampleVendor1,
};

beforeAll(async () => {
const connection = await dbConnection();

const userRepository = connection?.getRepository(User);
await userRepository?.save(sampleVendor1);

const productRepository = connection?.getRepository(Product);
await productRepository?.save(sampleProduct1);

const couponRepository = connection?.getRepository(Coupon);
await couponRepository?.save(sampleCoupon);

const couponRepository1 = connection?.getRepository(Coupon);
await couponRepository1?.save(sampleCoupon1);
});

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

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

describe('Coupon Management System', () => {
describe('Create Coupon', () => {
it('should create a new coupon', async () => {
const response = await request(app)
.post(`/coupons/vendor/${vendor1Id}/`)
.send({
code: 'NEWCOUPON10',
discountRate: 10,
expirationDate: '2025-12-31',
maxUsageLimit: 50,
discountType: 'PERCENTAGE',
product: product1Id,
})
.set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`);

expect(response.status).toBe(201);
expect(response.body.status).toBe('success');
}, 10000);

it('should return 400 for invalid coupon data', async () => {
const response = await request(app)
.post(`/coupons/vendor/${vendor1Id}/`)
.send({
code: '',
discountRate: 'invalid',
expirationDate: 'invalid-date',
maxUsageLimit: 'invalid',
discountType: 'INVALID',
product: 'invalid-product-id',
})
.set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`);

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

describe('Get All Coupons', () => {
it('should retrieve all coupons for a vendor', async () => {
const response = await request(app)
.get(`/coupons/vendor/${vendor1Id}/access-coupons`)
.set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`);

expect(response.status).toBe(200);
expect(response.body.status).toBe('success');
expect(response.body.data).toBeInstanceOf(Object);
}, 10000);

it('should return 404 if no coupons found', async () => {
const newVendorId = uuid();
const response = await request(app)
.get(`/coupons/vendor/${newVendorId}/access-coupons`)
.set('Authorization', `Bearer ${getAccessToken(newVendorId, '[email protected]')}`);

expect(response.status).toBe(401);
}, 10000);
});

describe('Read Coupon', () => {
it('should read a single coupon by code', async () => {
const response = await request(app)
.get(`/coupons/vendor/${vendor1Id}/checkout/${couponCode}`)
.set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`);

expect(response.status).toBe(200);
}, 10000);

it('should return 404 for invalid coupon code', async () => {
const response = await request(app)
.get(`/coupons/vendor/${vendor1Id}/checkout/${invalidCouponCode}`)
.set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`);

expect(response.status).toBe(404);
expect(response.body.status).toBe('error');
expect(response.body.message).toBe('Invalid coupon');
}, 10000);
});

describe('Update Coupon', () => {
it('should update an existing coupon', async () => {
const response = await request(app)
.put(`/coupons/vendor/${vendor1Id}/update-coupon/${couponCode1}`)
.send({
code: 'KAGAHEBUZO04',
})
.set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`);

expect(response.status).toBe(200);
expect(response.body.status).toBe('success');
}, 10000);

it('should return 404 for updating a non-existent coupon', async () => {
const response = await request(app)
.put(`/coupons/vendor/${vendor1Id}/update-coupon/${invalidCouponCode}`)
.send({
discountRate: 25,
})
.set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`);

expect(response.status).toBe(404);
expect(response.body.message).toBe('Coupon not found');
}, 10000);
});

describe('Delete Coupon', () => {
it('should delete an existing coupon', async () => {
const response = await request(app)
.delete(`/coupons/vendor/${vendor1Id}/checkout/delete`)
.send({
code: couponCode,
})
.set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`);

expect(response.status).toBe(200);
expect(response.body.status).toBe('success');
}, 10000);

it('should return 404 for deleting a non-existent coupon', async () => {
const response = await request(app)
.delete(`/coupons/vendor/${vendor1Id}/checkout/delete`)
.send({
code: invalidCouponCode,
})
.set('Authorization', `Bearer ${getAccessToken(vendor1Id, sampleVendor1.email)}`);

expect(response.status).toBe(404);
expect(response.body.status).toBe('error');
expect(response.body.message).toBe('Invalid coupon');
}, 10000);
});
});
Loading

0 comments on commit c5e2d29

Please sign in to comment.