Skip to content

Commit

Permalink
test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
GSinseswa721 authored and Ndevu12 committed May 30, 2024
1 parent 5346a98 commit 1edec63
Show file tree
Hide file tree
Showing 8 changed files with 560 additions and 1 deletion.
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();
});
});
34 changes: 34 additions & 0 deletions src/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import request from 'supertest';
import { app, server } from '../../src/index';

describe('Express App', () => {
afterAll((done) => {
server.close();
done();
});

it('should respond with 404 for unknown routes', async () => {
const response = await request(app).get('/unknown-route');
expect(response.status).toBe(404);
expect(response.body.message).toBe(`Can't find /unknown-route on the server!`);
});

it('should have CORS enabled', async () => {
const response = await request(app).get('/');
expect(response.headers['access-control-allow-origin']).toBe('*');
});

it('should have JSON parsing enabled', async () => {
const response = await request(app)
.post('/some-endpoint')
.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).get('/valid-route');
expect(response.status).toBe(200);
expect(response.body).toEqual({ message: 'Success' });
});
});
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)).toBe('RWF 1,234.56');
});

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)).toBe('RWF 1,234');
});
});

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');
});
});
});
80 changes: 80 additions & 0 deletions src/__test__/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { dbConnection } from '../startups/dbConnection';
import logger from '../utils/logger';
import winston from 'winston';
import { cleanDatabase } from './test-assets/DatabaseCleanup';
import { server } from '../../src/index';

console.log = jest.fn();
console.error = jest.fn();
console.warn = jest.fn();
console.info = jest.fn();
console.debug = jest.fn();

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

});

afterAll(async () => {
await cleanDatabase();
server.close();
});
describe('Logger', () => {
it('should create a logger with the correct configuration', () => {
expect(winston.createLogger).toHaveBeenCalledWith(expect.objectContaining({
level: 'info',
levels: expect.any(Object),
format: expect.anything(),
transports: expect.any(Array),
}));
});

it('should log messages with the correct level and format', () => {
const testMessage = 'Test log message';
const testLevel = 'info';

logger.log(testLevel, testMessage);

expect(logger.log).toHaveBeenCalledWith(testLevel, testMessage);
});

it('should correctly handle info level logs', () => {
const testMessage = 'Test info message';

logger.info(testMessage);

expect(logger.info).toHaveBeenCalledWith(testMessage);
});

it('should correctly handle warn level logs', () => {
const testMessage = 'Test warn message';

logger.warn(testMessage);

expect(logger.warn).toHaveBeenCalledWith(testMessage);
});

it('should correctly handle error level logs', () => {
const testMessage = 'Test error message';

logger.error(testMessage);

expect(logger.error).toHaveBeenCalledWith(testMessage);
});

it('should correctly handle debug level logs', () => {
const testMessage = 'Test debug message';

logger.debug(testMessage);

expect(logger.debug).toHaveBeenCalledWith(testMessage);
});

it('should correctly handle http level logs', () => {
const testMessage = 'Test http message';

logger.http(testMessage);

expect(logger.http).toHaveBeenCalledWith(testMessage);
});
});
Loading

0 comments on commit 1edec63

Please sign in to comment.