-
-
Notifications
You must be signed in to change notification settings - Fork 28
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 #395 from SquirrelCorporation/test-add-integration…
…-tests [TEST] Add integration tests and mock strategy for Passport
- Loading branch information
Showing
9 changed files
with
448 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
120 changes: 120 additions & 0 deletions
120
server/src/tests/integration-tests/test/controllers/rest/login.test.ts
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,120 @@ | ||
import bcrypt from 'bcrypt'; | ||
import mongoose from 'mongoose'; | ||
import request from 'supertest'; | ||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { SALT_ROUNDS, UsersModel } from '../../../../../data/database/model/User'; | ||
import UserRepo from '../../../../../data/database/repository/UserRepo'; | ||
import app from '../../server'; | ||
|
||
describe('Auth Integration Tests', () => { | ||
vi.mock('../../../data/database/repository/UserRepo', () => ({ | ||
findByEmailAndPassword: vi.fn(), | ||
})); | ||
|
||
beforeAll(async () => { | ||
await mongoose.connect(process.env['MONGO_URI'] as string); | ||
}); | ||
|
||
afterAll(async () => { | ||
await mongoose.disconnect(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await mongoose.connection.db?.dropDatabase(); | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
const user = new UsersModel({ | ||
email: 'test@example.com', | ||
password: 'password', | ||
role: 'admin', | ||
avatar: 'test', | ||
name: 'test', | ||
}); | ||
await user.save(); | ||
}); | ||
|
||
describe('POST /login', () => { | ||
it('should return 200 and set jwt cookie on successful login', async () => { | ||
const response = await request(app) | ||
.post('/users/login') | ||
.send({ username: 'test@example.com', password: 'password' }); | ||
|
||
// Assertions | ||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual( | ||
expect.objectContaining({ | ||
message: 'Login success', | ||
data: { | ||
currentAuthority: 'admin', | ||
}, | ||
}), | ||
); | ||
expect(response.headers['set-cookie']).toBeDefined(); | ||
}); | ||
|
||
it('should return 401 for invalid credentials', async () => { | ||
const response = await request(app) | ||
.post('/users/login') | ||
.send({ username: 'invalid@example.com', password: 'wrongpassword' }); | ||
|
||
// Assertions | ||
expect(response.status).toBe(401); | ||
expect(response.body).toEqual( | ||
expect.objectContaining({ | ||
message: 'Identification is incorrect!', | ||
success: false, | ||
}), | ||
); | ||
}); | ||
|
||
it('should return 400 when email is incorrect', async () => { | ||
const response = await request(app) | ||
.post('/users/login') | ||
.send({ username: 'invalid_example.com', password: 'password' }); | ||
|
||
// Assertions | ||
expect(response.status).toBe(400); | ||
}); | ||
|
||
it('should return 400 when email is missing', async () => { | ||
const response = await request(app).post('/users/login').send({ password: 'password' }); | ||
|
||
// Assertions | ||
expect(response.status).toBe(400); | ||
}); | ||
|
||
it('should return 400 when password is missing', async () => { | ||
const response = await request(app) | ||
.post('/users/login') | ||
.send({ username: 'test@example.com' }); | ||
|
||
// Assertions | ||
expect(response.status).toBe(400); | ||
}); | ||
}); | ||
|
||
describe('POST /logout', () => { | ||
it('should clear jwt cookie on successful logout', async () => { | ||
const response = await request(app).post('/users/logout').set('Cookie', 'jwt=fake-jwt-token'); | ||
|
||
// Assertions | ||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual( | ||
expect.objectContaining({ | ||
message: 'Logout success', | ||
}), | ||
); | ||
expect(response.headers['set-cookie']).toContainEqual(expect.stringContaining('jwt=;')); | ||
}); | ||
|
||
it('should return 401 if no jwt token is found', async () => { | ||
const response = await request(app).post('/users/logout'); | ||
|
||
// Assertions | ||
expect(response.status).toBe(401); | ||
expect(response.body).toEqual({ error: 'Invalid jwt' }); | ||
}); | ||
}); | ||
}); |
71 changes: 71 additions & 0 deletions
71
server/src/tests/integration-tests/test/controllers/rest/settings.test.ts
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,71 @@ | ||
import request from 'supertest'; | ||
import mongoose from 'mongoose'; | ||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import app from '../../server'; | ||
import UserRepo from '../../../../../data/database/repository/UserRepo'; | ||
|
||
describe('User Controllers Integration Tests', () => { | ||
beforeAll(async () => { | ||
await mongoose.connect(process.env['MONGO_URI'] as string); | ||
}); | ||
|
||
afterAll(async () => { | ||
await mongoose.disconnect(); | ||
}); | ||
|
||
beforeEach(() => { | ||
vi.spyOn(UserRepo, 'findByEmail').mockResolvedValue({ | ||
email: 'test@example.com', | ||
name: 'test', | ||
avatar: 'test', | ||
password: 'test', | ||
role: 'test', | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
vi.restoreAllMocks(); // Reset mocks after each test | ||
}); | ||
|
||
describe('resetUserApiKey', () => { | ||
it('should reset the user API key', async () => { | ||
const mockUuid = 'new-unique-uuid'; | ||
const resetApiKeySpy = vi.spyOn(UserRepo, 'resetApiKey').mockResolvedValue(mockUuid); | ||
|
||
const response = await request(app) | ||
.post('/users/settings/resetApiKey') | ||
.set('content-type', 'application/json'); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual({ | ||
success: true, | ||
message: 'Reset Api Key', | ||
data: { | ||
uuid: mockUuid, | ||
}, | ||
}); | ||
|
||
expect(resetApiKeySpy).toHaveBeenCalledWith('test@example.com'); | ||
}); | ||
}); | ||
|
||
describe('setUserLoglevel', () => { | ||
it('should set the user log level', async () => { | ||
const userLogsLevel = { terminal: 5 }; | ||
const updateLogsLevelSpy = vi.spyOn(UserRepo, 'updateLogsLevel').mockResolvedValue(); | ||
|
||
const response = await request(app) | ||
.post('/users/settings/logs') | ||
.send(userLogsLevel) | ||
.set('content-type', 'application/json'); | ||
|
||
expect(response.status).toBe(200); | ||
expect(response.body).toEqual({ | ||
message: 'Set user log level', | ||
success: true, | ||
}); | ||
|
||
expect(updateLogsLevelSpy).toHaveBeenCalledWith('test@example.com', userLogsLevel); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.