-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
222 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
import request from 'supertest'; | ||
import { app, server } from '../index'; | ||
import { dbConnection } from '../startups/dbConnection'; | ||
import { getRepository } from 'typeorm'; | ||
import { Feedback } from '../entities/Feedback'; | ||
import { v4 as uuid } from 'uuid'; | ||
import { User, UserInterface } from '../entities/User'; | ||
import { Product } from '../entities/Product'; | ||
import { Order } from '../entities/Order'; | ||
import { Category } from '../entities/Category'; | ||
import jwt from 'jsonwebtoken'; | ||
import { cleanDatabase } from './test-assets/DatabaseCleanup'; | ||
|
||
const vendorId = uuid(); | ||
const productId = uuid(); | ||
const buyerId = uuid(); | ||
const orderId = uuid(); | ||
const adminId = uuid(); | ||
const feedbackId1 = uuid(); | ||
const feedbackId2 = uuid(); | ||
const invalidFeedbackId = '11278df2-d026-457a-9471-4749f038df68'; | ||
|
||
const jwtSecretKey = process.env.JWT_SECRET || ''; | ||
|
||
const getAccessToken = (id: string, email: string) => { | ||
Check warning on line 25 in src/__test__/adminDeleteFeedBack.test.ts GitHub Actions / build-lint-test-coverage
Check warning on line 25 in src/__test__/adminDeleteFeedBack.test.ts GitHub Actions / build-lint-test-coverage
Check warning on line 25 in src/__test__/adminDeleteFeedBack.test.ts GitHub Actions / build-lint-test-coverage
|
||
return jwt.sign( | ||
{ | ||
id: id, | ||
email: email, | ||
}, | ||
jwtSecretKey | ||
); | ||
}; | ||
|
||
if (!process.env.TEST_USER_EMAIL || !process.env.TEST_BUYER_EMAIL || !process.env.TEST_VENDOR1_EMAIL || !process.env.TEST_VENDOR_EMAIL || !process.env.TEST_USER_PASS) throw new Error('TEST_USER_PASS or TEST_USER_EMAIL not set in .env'); | ||
|
||
const sampleAdmin: UserInterface = { | ||
id: adminId, | ||
firstName: 'admin', | ||
lastName: 'user', | ||
email:process.env.TEST_USER_EMAIL, | ||
password: process.env.TEST_USER_PASS, | ||
userType: 'Admin', | ||
gender: 'Male', | ||
phoneNumber: '126380997', | ||
photoUrl: 'https://example.com/photo.jpg', | ||
verified: true, | ||
role: 'ADMIN', | ||
}; | ||
|
||
const sampleVendor: UserInterface = { | ||
id: vendorId, | ||
firstName: 'vendor', | ||
lastName: 'user', | ||
email:process.env.TEST_VENDOR_EMAIL, | ||
password: process.env.TEST_USER_PASS, | ||
userType: 'Vendor', | ||
gender: 'Male', | ||
phoneNumber: '126380996347', | ||
photoUrl: 'https://example.com/photo.jpg', | ||
verified: true, | ||
role: 'VENDOR', | ||
}; | ||
|
||
const sampleBuyer: UserInterface = { | ||
id: buyerId, | ||
firstName: 'buyer', | ||
lastName: 'user', | ||
email: process.env.TEST_BUYER_EMAIL, | ||
password: process.env.TEST_USER_PASS, | ||
userType: 'Buyer', | ||
gender: 'Male', | ||
phoneNumber: '6380996347', | ||
photoUrl: 'https://example.com/photo.jpg', | ||
verified: true, | ||
role: 'BUYER', | ||
}; | ||
|
||
const sampleCat = { | ||
id: uuid(), | ||
name: 'accessories', | ||
}; | ||
|
||
const sampleProduct = { | ||
id: productId, | ||
name: 'test product', | ||
description: 'amazing product', | ||
images: ['photo1.jpg', 'photo2.jpg', 'photo3.jpg'], | ||
newPrice: 200, | ||
quantity: 10, | ||
vendor: sampleVendor, | ||
categories: [sampleCat], | ||
}; | ||
|
||
const sampleOrder = { | ||
id: orderId, | ||
totalPrice: 400, | ||
quantity: 2, | ||
orderDate: new Date(), | ||
buyer: sampleBuyer, | ||
orderStatus: 'received', | ||
address: 'Rwanda, Kigali, KK20st', | ||
}; | ||
const sampleFeedback1 = { | ||
id: feedbackId1, | ||
user: sampleBuyer as User, | ||
product: sampleProduct as Product, | ||
rating: 4, | ||
order: sampleOrder as Order, | ||
comment: 'Great product!', | ||
}; | ||
|
||
const sampleFeedback2 = { | ||
id: feedbackId2, | ||
user: sampleBuyer as User, | ||
product: sampleProduct as Product, | ||
order: sampleOrder as Order, | ||
rating: 2, | ||
comment: 'Not satisfied.', | ||
}; | ||
|
||
beforeAll(async () => { | ||
const connection = await dbConnection(); | ||
|
||
const categoryRepository = connection?.getRepository(Category); | ||
await categoryRepository?.save(sampleCat); | ||
|
||
const userRepository = connection?.getRepository(User); | ||
await userRepository?.save([sampleAdmin, sampleVendor, sampleBuyer]); | ||
|
||
const productRepository = connection?.getRepository(Product); | ||
await productRepository?.save(sampleProduct); | ||
|
||
const orderRepository = connection?.getRepository(Order); | ||
await orderRepository?.save(sampleOrder); | ||
|
||
const feedbackRepository = connection?.getRepository(Feedback); | ||
await feedbackRepository?.save({ ...sampleFeedback1 }); | ||
await feedbackRepository?.save({ ...sampleFeedback2 }); | ||
}); | ||
|
||
afterAll(async () => { | ||
await cleanDatabase(); | ||
server.close(); | ||
}); | ||
|
||
describe('/DELETE admin Delete Feedback Service', () => { | ||
it('should successfully delete a feedback', async () => { | ||
const response = await request(app) | ||
.delete(`/feedback/admin/delete/${feedbackId1}`) | ||
.set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`) | ||
.send(); | ||
expect(response.status).toBe(200); | ||
|
||
const feedbackRepository = getRepository(Feedback); | ||
const feedback = await feedbackRepository.findOne({ where: { id: feedbackId1 } }); | ||
expect(feedback).toBeNull(); | ||
}); | ||
|
||
it('should return 404 if feedback not found', async () => { | ||
const response = await request(app) | ||
.delete(`/feedback/admin/delete/${invalidFeedbackId}`) | ||
.set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`) | ||
.send(); | ||
expect(response.status).toBe(404); | ||
expect(response.body.message).toBe('Feedback not found'); | ||
}); | ||
|
||
it('should handle server errors', async () => { | ||
const response = await request(app) | ||
.delete(`/feedback/admin/delete/123`) | ||
.set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`) | ||
.send(); | ||
expect(response.status).toBe(500); | ||
}); | ||
}); | ||
|
||
describe('/DELETE Access controll on Admin deleting feedback route.', () => { | ||
it('should return 403 if user is not an admin', async () => { | ||
const response = await request(app) | ||
.delete(`/feedback/admin/delete/${feedbackId2}`) | ||
.set('Authorization', `Bearer ${getAccessToken(vendorId, sampleVendor.email)}`) | ||
.send(); | ||
expect(response.status).toBe(403); | ||
expect(response.body.message).toBe('Unauthorized action'); | ||
}); | ||
|
||
it('should return 404 if user is not found', async () => { | ||
const response = await request(app) | ||
.delete(`/feedback/admin/delete/${feedbackId2}`) | ||
.set('Authorization', `Bearer ${getAccessToken(invalidFeedbackId, sampleBuyer.email)}`) | ||
.send(); | ||
expect(response.status).toBe(403); | ||
}); | ||
|
||
it('should return 403 if user is not authenticated', async () => { | ||
const response = await request(app) | ||
.delete(`/feedback/admin/delete/${feedbackId2}`) | ||
.send(); | ||
expect(response.status).toBe(401); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters