-
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.
Merge pull request #114 from atlp-rwanda/ft-increase-order-coverages
increase order coverages
- Loading branch information
Showing
32 changed files
with
345 additions
and
26 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { isTokenValide } from '../middlewares/isValid'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { getRepository } from 'typeorm'; | ||
import { User } from '../entities/User'; | ||
|
||
jest.mock('typeorm', () => ({ | ||
...jest.requireActual('typeorm'), | ||
getRepository: jest.fn().mockImplementation((entity: any) => { | ||
if (entity === User) { | ||
return { | ||
findOne: jest.fn(), | ||
}; | ||
} | ||
return jest.requireActual('typeorm').getRepository(entity); | ||
}), | ||
})); | ||
|
||
const mockRequest = (userPayload: any): Request => { | ||
return { | ||
cookies: { token: 'mockToken' }, | ||
user: userPayload, | ||
} as unknown as Request; | ||
}; | ||
|
||
const mockResponse = () => { | ||
const res: any = {}; | ||
res.status = jest.fn().mockReturnValue(res); | ||
res.json = jest.fn().mockReturnValue(res); | ||
return res; | ||
}; | ||
|
||
const mockNext = jest.fn(); | ||
|
||
describe('isTokenValide middleware', () => { | ||
it('should return 401 if no user payload', async () => { | ||
const req = mockRequest(null); | ||
const res = mockResponse(); | ||
|
||
await isTokenValide(req as Request, res as Response, mockNext as NextFunction); | ||
|
||
expect(res.status).toHaveBeenCalledWith(401); | ||
expect(res.json).toHaveBeenCalledWith({ Message: 'Sorry, You are not authorized' }); | ||
}); | ||
}); |
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,83 @@ | ||
import request from 'supertest'; | ||
import { app, server } from '../index'; | ||
import { createConnection, getRepository } from 'typeorm'; | ||
import { User, UserInterface } from '../entities/User'; | ||
import { cleanDatabase } from './test-assets/DatabaseCleanup'; | ||
import jwt from 'jsonwebtoken'; | ||
import { v4 as uuid } from 'uuid'; | ||
import { dbConnection } from '../startups/dbConnection'; | ||
|
||
|
||
const adminId = uuid(); | ||
const adminId1 = uuid(); | ||
|
||
const jwtSecretKey = process.env.JWT_SECRET || ''; | ||
|
||
const getAccessToken = (id: string, email: string) => { | ||
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 sampleAdmin1: UserInterface = { | ||
id: adminId1, | ||
firstName: 'admin', | ||
lastName: 'user', | ||
email: '[email protected]', | ||
password: process.env.TEST_USER_PASS, | ||
userType: 'Admin', | ||
gender: 'Male', | ||
phoneNumber: '126380997', | ||
photoUrl: 'https://example.com/photo.jpg', | ||
verified: false, | ||
role: 'ADMIN', | ||
}; | ||
|
||
beforeAll(async () => { | ||
const connection = await dbConnection(); | ||
|
||
const userRepository = connection?.getRepository(User); | ||
await userRepository?.save([sampleAdmin, sampleAdmin1]); | ||
}); | ||
|
||
afterAll(async () => { | ||
await cleanDatabase(); | ||
|
||
server.close(); | ||
}); | ||
|
||
describe('POST /user/login', () => { | ||
it('should not login a user with unverified email', async () => { | ||
|
||
const loginUser = { | ||
email: '[email protected]', | ||
password: process.env.TEST_USER_LOGIN_PASS, | ||
}; | ||
|
||
const loginResponse = await request(app).post('/user/login').send(loginUser); | ||
|
||
expect(loginResponse.status).toBe(400); | ||
expect(loginResponse.body).toBeDefined(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// index.test.ts | ||
|
||
import request from 'supertest'; | ||
import { app, server } from '../index'; | ||
import { dbConnection } from '../startups/dbConnection'; | ||
import { cleanDatabase } from './test-assets/DatabaseCleanup'; | ||
|
||
beforeAll(async () => { | ||
await dbConnection(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await cleanDatabase(); | ||
server.close(); | ||
}); | ||
|
||
describe('USER ROUTE', () => { | ||
it('should respond with 404, user not found', async () => { | ||
const response = await request(app) | ||
.get('/login/success') | ||
.set('Content-Type', 'application/json'); | ||
|
||
expect(response.status).toBe(404); | ||
}); | ||
|
||
it('Should respond 401, Login failed', async () => { | ||
const response = await request(app) | ||
.post('/login/failed') | ||
.set('Content-Type', 'application/json'); | ||
|
||
expect(response.status).toBe(404); | ||
}); | ||
}); |
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,102 @@ | ||
import { getConnection, getRepository, Repository } from 'typeorm'; | ||
import { User, UserInterface } from '../entities/User'; | ||
import { cleanDatabase } from './test-assets/DatabaseCleanup'; | ||
import {app, server } from '../index'; | ||
import { v4 as uuid } from 'uuid'; | ||
import { dbConnection } from '../startups/dbConnection'; | ||
import request from 'supertest'; | ||
import jwt from 'jsonwebtoken'; | ||
|
||
const adminId = uuid(); | ||
|
||
const jwtSecretKey = process.env.JWT_SECRET || ''; | ||
|
||
const getAccessToken = (id: string, email: string) => { | ||
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', | ||
}; | ||
|
||
|
||
|
||
beforeAll(async () => { | ||
const connection = await dbConnection(); | ||
if (!connection) { | ||
console.error('Failed to connect to the database'); | ||
return; | ||
} | ||
|
||
const userRepository = connection.getRepository(User); | ||
await userRepository.save(sampleAdmin); | ||
}); | ||
|
||
afterAll(async () => { | ||
await cleanDatabase(); | ||
server.close(); | ||
}); | ||
|
||
describe('User profile update service', () => { | ||
it('should validate a invalid user and return 400', async () => { | ||
const res = await request(app) | ||
.put('/user/update') | ||
.set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`) | ||
.send(); | ||
|
||
expect(res.statusCode).toBe(400); | ||
}); | ||
|
||
it('should validate a valid user', async () => { | ||
const res = await request(app) | ||
.put('/user/update') | ||
.set('Authorization', `Bearer ${getAccessToken(adminId, sampleAdmin.email)}`) | ||
.send({ | ||
firstName: 'admin', | ||
lastName: 'user', | ||
email: process.env.TEST_USER_EMAIL, | ||
gender: 'Male', | ||
phoneNumber: '126380997', | ||
photoUrl: 'https://example.com/photo.jpg', | ||
id: sampleAdmin.id, | ||
}); | ||
|
||
expect(res.statusCode).toBe(201); | ||
}); | ||
|
||
it('should return 403 if user not authorized', async () => { | ||
const fakeID = uuid(); | ||
|
||
const res = await request(app) | ||
.put('/user/update') | ||
.send({ | ||
firstName: 'admin', | ||
lastName: 'user', | ||
email: process.env.TEST_USER_EMAIL, | ||
gender: 'Male', | ||
phoneNumber: '126380997', | ||
photoUrl: 'https://example.com/photo.jpg', | ||
id: fakeID, | ||
}); | ||
|
||
expect(res.statusCode).toBe(403); | ||
}); | ||
}); |
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
Oops, something went wrong.