-
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 #2 from RazinSyakib43/Ch8BE-unit-testing
[C8 BE*] Auth and App Unit Testing
- Loading branch information
Showing
7 changed files
with
2,440 additions
and
270 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import request from 'supertest'; | ||
import app from "../index"; | ||
|
||
describe('GET /', () => { | ||
it('should render the index page with a title and name', async () => { | ||
const response = await request(app).get('/').query({ name: 'John' }); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.text).toContain('BCR Car Management Dashboard - Hello World!'); | ||
expect(response.text).toContain('John'); | ||
}); | ||
|
||
it('should render the index page with default name', async () => { | ||
const response = await request(app).get('/'); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.text).toContain('BCR Car Management Dashboard - Hello World!'); | ||
expect(response.text).toContain('Guest'); | ||
}); | ||
}); |
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,113 @@ | ||
import request from 'supertest'; | ||
import express from 'express'; | ||
import multer from 'multer'; | ||
import bodyParser from 'body-parser'; | ||
import { registerAdmin, registerMember, loginSuperadmin, loginAdmin, loginMember } from '../controllers/authController'; | ||
import { AuthService } from '../services/authService'; | ||
import { UserService } from '../services/userService'; | ||
|
||
jest.mock('../services/authService'); | ||
jest.mock('../services/userService'); | ||
|
||
const authService = new AuthService(); | ||
const userService = new UserService(); | ||
|
||
const app = express(); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
|
||
const upload = multer(); | ||
app.post('/api/v1/dashboard/auth/register/admin', upload.single('avatar'), registerAdmin); | ||
app.post('/api/v1/dashboard/auth/register/member', upload.single('avatar'), registerMember); | ||
app.post('/api/v1/dashboard/auth/login/superadmin', loginSuperadmin); | ||
app.post('/api/v1/dashboard/auth/login/admin', loginAdmin); | ||
app.post('/api/v1/dashboard/auth/login/member', loginMember); | ||
|
||
interface User { | ||
id: number; | ||
name: string; | ||
email: string; | ||
password: string; | ||
avatar: string | null; | ||
role: string | null; | ||
createdAt: Date | null; | ||
createdBy: string | null; | ||
updatedAt: Date | null; | ||
updatedBy: string | null; | ||
status: string | null; | ||
deletedAt: Date | null; | ||
deletedBy: string | null; | ||
} | ||
|
||
interface ApiResponse { | ||
code: number; | ||
status: string; | ||
message: string; | ||
data: { | ||
token: string; | ||
}; | ||
} | ||
|
||
const mockUserService = userService as jest.Mocked<UserService>; | ||
const mockAuthService = authService as jest.Mocked<AuthService>; | ||
|
||
describe('AuthController', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('POST /register/member', () => { | ||
it('should create a new member user', async () => { | ||
mockUserService.getActiveUserByEmail.mockResolvedValue(null); | ||
mockAuthService.registerMember.mockResolvedValue({ name: 'Himmel', email: '[email protected]', password: "iamhimmel" } as User); | ||
|
||
const response = await request(app) | ||
.post('/api/v1/dashboard/auth/register/member') | ||
.field('name', 'Himmel') | ||
.field('email', '[email protected]') | ||
.field('password', 'himmel') | ||
.attach('avatar', Buffer.from('test'), 'test.jpg'); | ||
|
||
expect(response.status).toBe(201); | ||
expect(response.body.status).toBe('success'); | ||
}); | ||
|
||
it('should return 400 if email is already taken', async () => { | ||
mockUserService.getActiveUserByEmail.mockResolvedValue({ name: 'Frieren', email: '[email protected]', password: "iamfrieren" } as User); | ||
|
||
const response = await request(app) | ||
.post('/api/v1/dashboard/auth/register/member') | ||
.field('name', 'Frieren') | ||
.field('email', '[email protected]') | ||
.field('password', 'iamfrieren') | ||
.attach('avatar', Buffer.from('test'), 'test.jpg'); | ||
|
||
expect(response.status).toBe(400); | ||
expect(response.body.message).toBe('This email is already taken'); | ||
}); | ||
}); | ||
|
||
describe('POST /login/superadmin', () => { | ||
it('should login a superadmin user', async () => { | ||
mockAuthService.loginSuperadmin.mockResolvedValue({ data: { token: 'superadmin-token' } } as ApiResponse); | ||
|
||
const response = await request(app) | ||
.post('/api/v1/dashboard/auth/login/superadmin') | ||
.send({ email: '[email protected]', password: 'iamayano' }); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body.data.token).toBe('superadmin-token'); | ||
}); | ||
|
||
it('should return 500 on login failure', async () => { | ||
mockAuthService.loginSuperadmin.mockRejectedValue(new Error('Login failed')); | ||
|
||
const response = await request(app) | ||
.post('/api/v1/dashboard/auth/login/superadmin') | ||
.send({ email: '[email protected]', password: 'password' }); | ||
|
||
expect(response.status).toBe(500); | ||
expect(response.body.message).toBe('Login failed'); | ||
}); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,7 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testPathIgnorePatterns: ['/node_modules/', '/dist/'], | ||
collectCoverage: true, | ||
coverageDirectory: 'coverage', | ||
}; |
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.