forked from garageScript/databases
-
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.
…d add unit test
- Loading branch information
1 parent
deac68a
commit c125820
Showing
6 changed files
with
153 additions
and
94 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,18 @@ | ||
const db = require("../../sequelize/db"); | ||
const routes = {}; | ||
|
||
routes.postgres = async (req, res) => { | ||
if (!req.session.username) return res.redirect("/"); | ||
const { Accounts } = db.getModels(); | ||
const userAccount = await Accounts.findOne({ | ||
where: { | ||
username: req.session.username, | ||
}, | ||
}); | ||
res.render("postgres", { | ||
username: req.session.username, | ||
dbPassword: userAccount.dbPassword, | ||
}); | ||
}; | ||
|
||
module.exports = routes; |
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,36 @@ | ||
jest.mock("../../sequelize/db"); | ||
|
||
const db = require("../../sequelize/db"); | ||
const { postgres } = require("./renderRoutes"); | ||
|
||
const mockFindOne = jest.fn(); | ||
db.getModels = () => { | ||
return { | ||
Accounts: { | ||
findOne: mockFindOne, | ||
}, | ||
}; | ||
}; | ||
const mockResponse = { | ||
render: jest.fn(), | ||
redirect: jest.fn(), | ||
}; | ||
const mockRequest = { | ||
session: {}, | ||
}; | ||
|
||
describe("Testing render router", () => { | ||
test("postgres function should call res.redirect if session is undefined", async () => { | ||
await postgres(mockRequest, mockResponse); | ||
expect(mockResponse.redirect).toHaveBeenCalled(); | ||
}); | ||
test("postgres function should call res.render if session user is found", async () => { | ||
mockRequest.session.username = "testuser"; | ||
const userAccount = { | ||
dbPassword: "testdbpw", | ||
}; | ||
mockFindOne.mockReturnValue(userAccount); | ||
await postgres(mockRequest, mockResponse); | ||
expect(mockResponse.render).toHaveBeenCalled(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,87 +1,101 @@ | ||
jest.mock('./routes/userRoutes') | ||
jest.mock('express') | ||
jest.mock('mailgun-js') | ||
jest.mock('../sequelize/db') | ||
jest.mock("./routes/userRoutes"); | ||
jest.mock("./routes/renderRoutes"); | ||
jest.mock("express"); | ||
jest.mock("mailgun-js"); | ||
jest.mock("../sequelize/db"); | ||
|
||
const express = require('express') | ||
const dbModule = require('../sequelize/db') | ||
const userRoutes = require('./routes/userRoutes') | ||
const express = require("express"); | ||
const dbModule = require("../sequelize/db"); | ||
const userRoutes = require("./routes/userRoutes"); | ||
const renderRoutes = require("./routes/renderRoutes"); | ||
|
||
userRoutes.createUser = jest.fn() | ||
userRoutes.loginUser = jest.fn() | ||
userRoutes.logoutUser = jest.fn() | ||
userRoutes.deleteUser = jest.fn() // userRoutes should be mocked before requiring server | ||
userRoutes.resetUserPassword = jest.fn() | ||
userRoutes.updateDBPassword=jest.fn() | ||
userRoutes.createUser = jest.fn(); | ||
userRoutes.loginUser = jest.fn(); | ||
userRoutes.logoutUser = jest.fn(); | ||
userRoutes.deleteUser = jest.fn(); | ||
userRoutes.resetUserPassword = jest.fn(); | ||
userRoutes.updateDBPassword = jest.fn(); | ||
renderRoutes.postgres = jest.fn(); | ||
// router functions should be mocked before requiring server | ||
const { startServer, stopServer, getApp } = require("./server"); | ||
|
||
const {startServer, stopServer, getApp} = require('./server') | ||
|
||
dbModule.start = jest.fn() | ||
dbModule.close = jest.fn() | ||
dbModule.start = jest.fn(); | ||
dbModule.close = jest.fn(); | ||
|
||
const app = { | ||
set: ()=>{}, | ||
set: () => {}, | ||
use: () => {}, | ||
get: () => {}, | ||
patch:jest.fn(), | ||
get: jest.fn(), | ||
patch: jest.fn(), | ||
post: jest.fn(), | ||
delete: jest.fn(), | ||
listen: jest.fn().mockImplementation((port, callback) => callback()), | ||
name: 'Carl Sagan' | ||
} | ||
express.mockReturnValue(app) | ||
name: "Carl Sagan", | ||
}; | ||
express.mockReturnValue(app); | ||
|
||
describe('Testing the server', () => { | ||
describe("Testing the server", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
test('getApp should return null when startServer has not been called', () => { | ||
const result = getApp() | ||
expect(result).toBe(null) | ||
}) | ||
test('getApp should return an express server after startServer has been called', async () => { | ||
await startServer() | ||
const result = getApp() | ||
expect(result.name).toEqual('Carl Sagan') | ||
}) | ||
test('startServer should return an object', async () => { | ||
const result = await startServer(1000) | ||
expect(result.listen.mock.calls[0][0]).toBe(1000) | ||
}) | ||
test('stopServer should call server.close', async () => { | ||
const server = {close: jest.fn().mockImplementation((a) => a())} | ||
app.listen.mockImplementation((a,b) => { | ||
jest.clearAllMocks(); | ||
}); | ||
test("getApp should return null when startServer has not been called", () => { | ||
const result = getApp(); | ||
expect(result).toBe(null); | ||
}); | ||
test("getApp should return an express server after startServer has been called", async () => { | ||
await startServer(); | ||
const result = getApp(); | ||
expect(result.name).toEqual("Carl Sagan"); | ||
}); | ||
test("startServer should return an object", async () => { | ||
const result = await startServer(1000); | ||
expect(result.listen.mock.calls[0][0]).toBe(1000); | ||
}); | ||
test("stopServer should call server.close", async () => { | ||
const server = { close: jest.fn().mockImplementation((a) => a()) }; | ||
app.listen.mockImplementation((a, b) => { | ||
// Need to setTimeout so the promise resolves | ||
// is called after the function returns | ||
setTimeout(b, 1) | ||
return server | ||
}) | ||
await startServer() | ||
await stopServer() | ||
expect(dbModule.close).toHaveBeenCalled() | ||
expect(server.close).toHaveBeenCalled() | ||
}) | ||
}) | ||
setTimeout(b, 1); | ||
return server; | ||
}); | ||
await startServer(); | ||
await stopServer(); | ||
expect(dbModule.close).toHaveBeenCalled(); | ||
expect(server.close).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe("Testing user routes", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
test("should call user router functions", async () => { | ||
await startServer(); | ||
await app.patch.mock.calls[0][1](); | ||
expect(userRoutes.updateDBPassword).toHaveBeenCalled(); | ||
await app.post.mock.calls[0][1](); | ||
expect(userRoutes.resetPasswordEmail).toHaveBeenCalled(); | ||
await app.post.mock.calls[1][1](); | ||
expect(userRoutes.createUser).toHaveBeenCalled(); | ||
await app.delete.mock.calls[0][1](); | ||
expect(userRoutes.deleteUser).toHaveBeenCalled(); | ||
await app.post.mock.calls[2][1](); | ||
expect(userRoutes.loginUser).toHaveBeenCalled(); | ||
await app.delete.mock.calls[1][1](); | ||
expect(userRoutes.logoutUser).toHaveBeenCalled(); | ||
await app.post.mock.calls[3][1](); | ||
expect(userRoutes.userResetPassword).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('Testing routes', () => { | ||
describe("Testing render routes", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
it('should call router functions', async () => { | ||
await startServer() | ||
await app.patch.mock.calls[0][1]() | ||
expect(userRoutes.updateDBPassword).toHaveBeenCalled() | ||
await app.post.mock.calls[0][1]() | ||
expect(userRoutes.resetPasswordEmail).toHaveBeenCalled() | ||
await app.post.mock.calls[1][1]() | ||
expect(userRoutes.createUser).toHaveBeenCalled() | ||
await app.delete.mock.calls[0][1]() | ||
expect(userRoutes.deleteUser).toHaveBeenCalled() | ||
await app.post.mock.calls[2][1]() | ||
expect(userRoutes.loginUser).toHaveBeenCalled() | ||
await app.delete.mock.calls[1][1]() | ||
expect(userRoutes.logoutUser).toHaveBeenCalled() | ||
await app.post.mock.calls[3][1]() | ||
expect(userRoutes.userResetPassword).toHaveBeenCalled() | ||
}) | ||
}) | ||
jest.clearAllMocks(); | ||
}); | ||
test("should call render router functions", async () => { | ||
await startServer(); | ||
await app.get.mock.calls[7][1](); | ||
expect(renderRoutes.postgres).toHaveBeenCalled(); | ||
}); | ||
}); |
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