Skip to content

Commit

Permalink
test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
GSinseswa721 committed May 31, 2024
1 parent 5346a98 commit b579607
Show file tree
Hide file tree
Showing 21 changed files with 725 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ env:
GOOGLE_CLIENT_ID: ${{secrets.GOOGLE_CLIENT_ID}}
GOOGLE_CLIENT_SECRET: ${{secrets.GOOGLE_CLIENT_SECRET}}

STRIPE_SECRET_KEY: ${{secrets.STRIPE_SECRET_KEYT}}

jobs:
build-lint-test-coverage:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"reflect-metadata": "^0.2.2",
"socket.io": "^4.7.5",
"source-map-support": "^0.5.21",
"stripe": "^15.8.0",
"superagent": "^9.0.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0",
Expand Down
154 changes: 154 additions & 0 deletions src/__test__/auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import request from 'supertest';
import express, { Request, Response } from 'express';
import {
userVerificationService,
userRegistrationService,
userLoginService,
userEnableTwoFactorAuth,
userDisableTwoFactorAuth,
userValidateOTP,
userResendOtpService,
logoutService,
} from '../services';
import { userPasswordResetService } from '../services/userServices/userPasswordResetService';
import { sendPasswordResetLinkService } from '../services/userServices/sendResetPasswordLinkService';
import { activateUserService } from '../services/updateUserStatus/activateUserService';
import { deactivateUserService } from '../services/updateUserStatus/deactivateUserService';
import { userProfileUpdateServices } from '../services/userServices/userProfileUpdateServices';
import { activateUser, disable2FA, disactivateUser, enable2FA, login, logout, resendOTP, sampleAPI, sendPasswordResetLink, userPasswordReset, userProfileUpdate, userRegistration, userVerification, verifyOTP } from '../controllers';

// Mock the services
jest.mock('../services', () => ({
userVerificationService: jest.fn(),
userRegistrationService: jest.fn(),
userLoginService: jest.fn(),
userEnableTwoFactorAuth: jest.fn(),
userDisableTwoFactorAuth: jest.fn(),
userValidateOTP: jest.fn(),
userResendOtpService: jest.fn(),
logoutService: jest.fn(),
}));

jest.mock('../services/userServices/userPasswordResetService', () => ({
userPasswordResetService: jest.fn(),
}));

jest.mock('../services/userServices/sendResetPasswordLinkService', () => ({
sendPasswordResetLinkService: jest.fn(),
}));

jest.mock('../services/updateUserStatus/activateUserService', () => ({
activateUserService: jest.fn(),
}));

jest.mock('../services/updateUserStatus/deactivateUserService', () => ({
deactivateUserService: jest.fn(),
}));

jest.mock('../services/userServices/userProfileUpdateServices', () => ({
userProfileUpdateServices: jest.fn(),
}));

const app = express();
app.use(express.json());

app.post('/register', userRegistration);
app.post('/verify', userVerification);
app.post('/login', login);
app.post('/enable-2fa', enable2FA);
app.post('/disable-2fa', disable2FA);
app.post('/verify-otp', verifyOTP);
app.post('/resend-otp', resendOTP);
app.get('/sample', sampleAPI);
app.post('/reset-password', userPasswordReset);
app.post('/send-reset-link', sendPasswordResetLink);
app.post('/activate', activateUser);
app.post('/deactivate', disactivateUser);
app.post('/logout', logout);
app.put('/update-profile', userProfileUpdate);

describe('User Controller', () => {
it('should call userRegistrationService on /register', async () => {
(userRegistrationService as jest.Mock).mockImplementationOnce((req: Request, res: Response) => res.status(201).send());

Check warning on line 72 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 72 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u
await request(app).post('/register').send({});
expect(userRegistrationService).toHaveBeenCalled();
});

it('should call userVerificationService on /verify', async () => {
(userVerificationService as jest.Mock).mockImplementationOnce((req: Request, res: Response) => res.status(200).send());

Check warning on line 78 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 78 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u
await request(app).post('/verify').send({});
expect(userVerificationService).toHaveBeenCalled();
});

it('should call userLoginService on /login', async () => {
(userLoginService as jest.Mock).mockImplementationOnce((req: Request, res: Response) => res.status(200).send());

Check warning on line 84 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 84 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u
await request(app).post('/login').send({});
expect(userLoginService).toHaveBeenCalled();
});

it('should call userEnableTwoFactorAuth on /enable-2fa', async () => {
(userEnableTwoFactorAuth as jest.Mock).mockImplementationOnce((req: Request, res: Response) => res.status(200).send());

Check warning on line 90 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 90 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u
await request(app).post('/enable-2fa').send({});
expect(userEnableTwoFactorAuth).toHaveBeenCalled();
});

it('should call userDisableTwoFactorAuth on /disable-2fa', async () => {
(userDisableTwoFactorAuth as jest.Mock).mockImplementationOnce((req: Request, res: Response) => res.status(200).send());

Check warning on line 96 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 96 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u
await request(app).post('/disable-2fa').send({});
expect(userDisableTwoFactorAuth).toHaveBeenCalled();
});

it('should call userValidateOTP on /verify-otp', async () => {
(userValidateOTP as jest.Mock).mockImplementationOnce((req: Request, res: Response) => res.status(200).send());

Check warning on line 102 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 102 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u
await request(app).post('/verify-otp').send({});
expect(userValidateOTP).toHaveBeenCalled();
});

it('should call userResendOtpService on /resend-otp', async () => {
(userResendOtpService as jest.Mock).mockImplementationOnce((req: Request, res: Response) => res.status(200).send());

Check warning on line 108 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 108 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u
await request(app).post('/resend-otp').send({});
expect(userResendOtpService).toHaveBeenCalled();
});

it('should return 200 on /sample', async () => {
const response = await request(app).get('/sample');
expect(response.status).toBe(200);
expect(response.body).toEqual({ message: 'Token is valid' });
});

it('should call userPasswordResetService on /reset-password', async () => {
(userPasswordResetService as jest.Mock).mockImplementationOnce((req: Request, res: Response) => res.status(200).send());

Check warning on line 120 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 120 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u
await request(app).post('/reset-password').send({});
expect(userPasswordResetService).toHaveBeenCalled();
});

it('should call sendPasswordResetLinkService on /send-reset-link', async () => {
(sendPasswordResetLinkService as jest.Mock).mockImplementationOnce((req: Request, res: Response) => res.status(200).send());

Check warning on line 126 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 126 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u
await request(app).post('/send-reset-link').send({});
expect(sendPasswordResetLinkService).toHaveBeenCalled();
});

it('should call activateUserService on /activate', async () => {
(activateUserService as jest.Mock).mockImplementationOnce((req: Request, res: Response) => res.status(200).send());

Check warning on line 132 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 132 in src/__test__/auth.test.ts

View workflow job for this annotation

GitHub Actions / build-lint-test-coverage

'req' is defined but never used. Allowed unused args must match /^_/u
await request(app).post('/activate').send({});
expect(activateUserService).toHaveBeenCalled();
});

it('should call deactivateUserService on /deactivate', async () => {
(deactivateUserService as jest.Mock).mockImplementationOnce((req: Request, res: Response) => res.status(200).send());
await request(app).post('/deactivate').send({});
expect(deactivateUserService).toHaveBeenCalled();
});

it('should call logoutService on /logout', async () => {
(logoutService as jest.Mock).mockImplementationOnce((req: Request, res: Response) => res.status(200).send());
await request(app).post('/logout').send({});
expect(logoutService).toHaveBeenCalled();
});

it('should call userProfileUpdateServices on /update-profile', async () => {
(userProfileUpdateServices as jest.Mock).mockImplementationOnce((req: Request, res: Response) => res.status(200).send());
await request(app).put('/update-profile').send({});
expect(userProfileUpdateServices).toHaveBeenCalled();
});
});
2 changes: 1 addition & 1 deletion src/__test__/cart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,4 +608,4 @@ describe('Order management tests', () => {
expect(response.status).toBe(500);
});
});
});
});
40 changes: 37 additions & 3 deletions src/__test__/getProduct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { User, UserInterface } from '../entities/User';
import { v4 as uuid } from 'uuid';
import { Product } from '../entities/Product';
import { Category } from '../entities/Category';
import { Cart } from '../entities/Cart';
import { cleanDatabase } from './test-assets/DatabaseCleanup';

const vendor1Id = uuid();
const BuyerID = uuid();
const product1Id = uuid();
const Invalidproduct = '11278df2-d026-457a-9471-4749f038df68';
const catId = uuid();
Expand Down Expand Up @@ -37,6 +39,18 @@ const sampleVendor1: UserInterface = {
photoUrl: 'https://example.com/photo.jpg',
role: 'VENDOR',
};
const sampleBuyer1: UserInterface = {
id: BuyerID,
firstName: 'vendor1o',
lastName: 'user',
email: '[email protected]',
password: 'password',
userType: 'Vendor',
gender: 'Male',
phoneNumber: '000380996348',
photoUrl: 'https://example.com/photo.jpg',
role: 'BUYER',
};

const sampleCat = {
id: catId,
Expand All @@ -53,23 +67,23 @@ const sampleProduct1 = {
vendor: sampleVendor1,
categories: [sampleCat],
};

let cardID : string;
beforeAll(async () => {
const connection = await dbConnection();

const categoryRepository = connection?.getRepository(Category);
await categoryRepository?.save({ ...sampleCat });

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

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

afterAll(async () => {
await cleanDatabase();

server.close();
});

Expand Down Expand Up @@ -122,3 +136,23 @@ describe('Get single product', () => {
expect(response.body.message).toBe('Product not found');
}, 10000);
});
describe('Cart Order and payment functionalities', () => {
it('should create a cart for a product', async () => {
const productId = product1Id;
const quantity = 8;

const token = getAccessToken(BuyerID, sampleBuyer1.email);

const response = await request(app)
.post('/cart')
.set('Authorization', `Bearer ${token}`)
.send({ productId, quantity });


expect(response.status).toBe(201);
expect(response.body.data.cart).toBeDefined();
cardID = JSON.stringify(response.body.data.cart.id)
});

}
)
23 changes: 23 additions & 0 deletions src/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import request from 'supertest';
import { app } from '../index';

describe('Express App', () => {
it('should have JSON parsing enabled', async () => {
const response = await request(app)
.post('/test')
.send({ name: 'test' })
.set('Content-Type', 'application/json');

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

it('should respond to a valid route', async () => {
const response = await request(app)
.post('/test')
.send({ name: 'test' })
.set('Content-Type', 'application/json');

expect(response.status).toBe(200);
expect(response.body).toEqual({ message: 'Route works!' });
});
});
34 changes: 34 additions & 0 deletions src/__test__/index.utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { formatMoney, formatDate } from '../utils/index';

describe('Utility Functions', () => {
describe('formatMoney', () => {
it('should format a number as currency with default currency RWF', () => {
expect(formatMoney(1234.56)).toBeDefined();
});

it('should format a number as currency with specified currency', () => {
expect(formatMoney(1234.56, 'USD')).toBe('$1,234.56');
expect(formatMoney(1234.56, 'EUR')).toBe('€1,234.56');
});

it('should format a number with no cents if amount is a whole number', () => {
expect(formatMoney(1234)).toBeDefined();
});
});

describe('formatDate', () => {
it('should format a date string into a more readable format', () => {
const date = new Date('2024-05-28');
expect(formatDate(date)).toBe('May 28, 2024');
});

it('should format another date correctly', () => {
const date = new Date('2020-01-01');
expect(formatDate(date)).toBe('January 1, 2020');
});

it('should handle invalid date strings gracefully', () => {
expect(formatDate(new Date('invalid-date'))).toBe('Invalid Date');
});
});
});
Loading

0 comments on commit b579607

Please sign in to comment.