Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix increase coverages #115

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 131 additions & 2 deletions src/__test__/cart.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import request from 'supertest';
import jwt from 'jsonwebtoken';
import { app, server } from '../index';
import { getConnection } from 'typeorm';

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

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

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

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

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

'getConnection' is defined but never used
import { dbConnection } from '../startups/dbConnection';
import { v4 as uuid } from 'uuid';
import { User, UserInterface } from '../entities/User';
Expand All @@ -26,7 +26,7 @@

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

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

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

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing return type on function

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

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing return type on function
return jwt.sign(
{
id: id,
Expand Down Expand Up @@ -435,7 +435,7 @@

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`)
Expand Down Expand Up @@ -484,7 +484,7 @@
.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

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

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing semicolon

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

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing semicolon
});
it('should create new feedback to the ordered product', async () => {
const response = await request(app)
Expand All @@ -492,7 +492,7 @@
.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

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

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing semicolon

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

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Missing semicolon
});
it('should updated existing feedback successfully', async () => {
const response = await request(app)
Expand All @@ -508,6 +508,13 @@
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(200);
});
it('should remove recorderd 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);
});
it('should remove recorder feedback as admin ', async () => {
const response = await request(app)
.delete(`/feedback/admin/delete/${feedback2Id}`)
Expand All @@ -516,6 +523,128 @@
expect(response.status).toBe(401);
});
});

Check warning on line 526 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 526 in src/__test__/cart.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

Trailing spaces not allowed
describe('Feedback API', () => {

describe('Add feedback to the product with order', () => {
it('should create new feedback for 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 another feedback for 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 fail to create feedback with missing orderId', async () => {
const response = await request(app)
.post(`/feedback/${productId}/new`)
.send({ comment: 'Missing orderId' })
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(404);
});

it('should fail to create feedback with missing comment', async () => {
const response = await request(app)
.post(`/feedback/${productId}/new`)
.send({ orderId })
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(500);
});

it('should fail to create feedback with invalid productId', async () => {
const response = await request(app)
.post(`/feedback/invalidProductId/new`)
.send({ orderId, comment: 'Invalid productId' })
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(500);
});
});

describe('Update feedback', () => {
it('should update 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 fail to update feedback with invalid feedbackId', async () => {
const response = await request(app)
.put(`/feedback/update/invalidFeedbackId`)
.send({ orderId, comment: 'Invalid feedbackId' })
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(500);
});

it('should fail to update feedback without authorization', async () => {
const response = await request(app)
.put(`/feedback/update/${feedbackId}`)
.send({ orderId, comment: 'Unauthorized update' });
expect(response.status).toBe(401);
});
});

describe('Delete feedback', () => {
it('should remove recorded feedback', async () => {
const response = await request(app)
.delete(`/feedback/delete/${feedbackId}`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(200);
});

it('should not allow a different user (admin) to remove feedback', async () => {
const response = await request(app)
.delete(`/feedback/admin/delete/${feedback2Id}`)
.set('Authorization', `Bearer ${getAccessToken(buyer3Id, sampleBuyer3.email)}`);
expect(response.status).toBe(401);
});

it('should fail to delete feedback with invalid feedbackId', async () => {
const response = await request(app)
.delete(`/feedback/delete/invalidFeedbackId`)
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(500);
});

it('should fail to delete feedback without authorization', async () => {
const response = await request(app)
.delete(`/feedback/delete/${feedback2Id}`);
expect(response.status).toBe(401);
});
});

describe('Edge Cases', () => {
it('should not allow creating feedback for a product not in the order', async () => {
const invalidOrderId = 999; // Assuming an invalid orderId
const response = await request(app)
.post(`/feedback/${productId}/new`)
.send({ orderId: invalidOrderId, comment: 'Invalid orderId' })
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(500);
});

it('should fail to update feedback with a comment that is too long', async () => {
const longComment = 'a'.repeat(1001); // Assuming max length is 1000
const response = await request(app)
.put(`/feedback/update/${feedback2Id}`)
.send({ orderId, comment: longComment })
.set('Authorization', `Bearer ${getAccessToken(buyer1Id, sampleBuyer1.email)}`);
expect(response.status).toBe(200);
});
});
});

});

describe('Deleting product from cart', () => {
Expand Down Expand Up @@ -652,4 +781,4 @@
expect(response.body.data.cart).toBeDefined;
});
});
});
});
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import router from './routes';
import { addDocumentation } from './startups/docs';
import 'reflect-metadata';
import cookieParser from 'cookie-parser';
import session from 'express-session';
import session from "express-session";
import passport from 'passport';

import { CustomError, errorHandler } from './middlewares/errorHandler';
Expand Down
Loading