forked from mozilla/blurts-server
-
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.
refactor home to controller and add tests
- Loading branch information
1 parent
94b6a7b
commit 0d3700d
Showing
3 changed files
with
86 additions
and
15 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,27 @@ | ||
"use strict"; | ||
|
||
const express = require("express"); | ||
|
||
|
||
function home(req, res) { | ||
let breach = null; | ||
if (req.query.breach) { | ||
const reqBreachName = req.query.breach.toLowerCase(); | ||
breach = req.app.locals.breaches.filter(breach => breach.Name.toLowerCase() === reqBreachName)[0]; | ||
} | ||
res.render("monitor", { | ||
title: "Firefox Monitor", | ||
breach: breach, | ||
}); | ||
} | ||
|
||
|
||
function notFound(req, res) { | ||
res.status(404) | ||
res.render("404"); | ||
} | ||
|
||
module.exports = { | ||
home, | ||
notFound, | ||
} |
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,54 @@ | ||
"use strict"; | ||
|
||
const httpMocks = require("node-mocks-http"); | ||
|
||
const home = require("../controllers/home"); | ||
|
||
|
||
function addBreachesToMockRequest(mockRequest) { | ||
const mockBreaches = [ | ||
{Name: 'Test'}, | ||
{Name: 'DontShow'}, | ||
]; | ||
mockRequest.app = { locals: { breaches: mockBreaches } }; | ||
return mockRequest; | ||
} | ||
|
||
test("home GET without breach renders monitor without breach", () => { | ||
let mockRequest = { query: { breach: null } }; | ||
mockRequest = addBreachesToMockRequest(mockRequest); | ||
const mockResponse = { render: jest.fn() }; | ||
|
||
home.home(mockRequest, mockResponse); | ||
|
||
const mockRenderCallArgs = mockResponse.render.mock.calls[0]; | ||
expect(mockRenderCallArgs[0]).toBe("monitor"); | ||
expect(mockRenderCallArgs[1].breach).toBe(null); | ||
}); | ||
|
||
|
||
test("home GET with breach renders monitor with breach", () => { | ||
const testBreach = {Name: 'Test'}; | ||
let mockRequest = { query: { breach: testBreach.Name } }; | ||
mockRequest = addBreachesToMockRequest(mockRequest); | ||
const mockResponse = { render: jest.fn() }; | ||
|
||
home.home(mockRequest, mockResponse); | ||
|
||
const mockRenderCallArgs = mockResponse.render.mock.calls[0]; | ||
expect(mockRenderCallArgs[0]).toBe("monitor"); | ||
expect(mockRenderCallArgs[1].breach).toEqual(testBreach); | ||
}); | ||
|
||
|
||
test("notFound set status 404 and renders 404", () => { | ||
const mockRequest = {}; | ||
const mockResponse = { status: jest.fn(), render: jest.fn() }; | ||
|
||
home.notFound(mockRequest, mockResponse); | ||
|
||
const mockStatusCallArgs = mockResponse.status.mock.calls[0]; | ||
const mockRenderCallArgs = mockResponse.render.mock.calls[0]; | ||
expect(mockStatusCallArgs[0]).toBe(404); | ||
expect(mockRenderCallArgs[0]).toBe("404"); | ||
}); |