-
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
34 changed files
with
1,324 additions
and
27 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
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(); | ||
}); | ||
}); |
Oops, something went wrong.