-
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
1 parent
698de8f
commit 581269b
Showing
2 changed files
with
61 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# 2024.1-SENTINELA-BACKEND-USUARIOS | ||
# 2024.2-SENTINELA-BACKEND-USUARIOS | ||
|
||
## Variáveis de ambiente | ||
|
||
|
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,60 @@ | ||
const mongoose = require("mongoose"); | ||
const request = require("supertest"); | ||
const { app, startServer } = require("../index"); | ||
const initializeRoles = require("../Utils/initDatabase"); | ||
|
||
jest.spyOn(mongoose, "connect").mockResolvedValue(); | ||
jest.mock("../Utils/initDatabase"); | ||
|
||
describe("Express App Tests", () => { | ||
beforeEach(() => { | ||
process.env.SECRET = "test_secret"; | ||
process.env.FRONT_HOST = "localhost"; | ||
process.env.PORT = 3000; | ||
jest.resetModules(); | ||
}); | ||
|
||
it("should return 'Hello, world!' for GET /", async () => { | ||
const response = await request(app).get("/"); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.text).toBe("Hello, world!"); | ||
}); | ||
|
||
it("should allow requests with correct CORS origin", async () => { | ||
const response = await request(app) | ||
.get("/") | ||
.set("Origin", "http://localhost"); | ||
|
||
expect(response.statusCode).toBe(200); | ||
}); | ||
|
||
it("should block requests with incorrect CORS origin", async () => { | ||
process.env.FRONT_HOST = "example.com"; | ||
const response = await request(app) | ||
.get("/") | ||
.set("Origin", "http://notallowed.com"); | ||
|
||
expect(response.statusCode).toBe(500); | ||
}); | ||
|
||
it("should start the server and connect to MongoDB", async () => { | ||
const consoleLogSpy = jest.spyOn(console, "log").mockImplementation(); | ||
const consoleErrorSpy = jest | ||
.spyOn(console, "error") | ||
.mockImplementation(); | ||
|
||
await startServer(); | ||
|
||
expect(mongoose.connect).toHaveBeenCalled(); | ||
expect(initializeRoles).toHaveBeenCalled(); | ||
expect(consoleLogSpy).toHaveBeenCalledWith("Connected to MongoDB"); | ||
|
||
consoleLogSpy.mockRestore(); | ||
consoleErrorSpy.mockRestore(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
mongoose.disconnect(); | ||
}); | ||
}); |