Skip to content

Commit

Permalink
replacing send method with json (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeknovy authored Jan 12, 2023
1 parent ab1139b commit 470d88c
Show file tree
Hide file tree
Showing 42 changed files with 75 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createTokenController } from "./create-token-controller"
jest.mock("../../../db/db")
const mockResponse = () => {
const res: Partial<Response> = {}
res.send = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
}
Expand All @@ -22,6 +22,6 @@ describe("createTokenController", () => {
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response, nextFunction)
expect(querySpy).toHaveBeenCalledTimes(1)
expect(response.send).toHaveBeenCalledTimes(1)
expect(response.json).toHaveBeenCalledTimes(1)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const createTokenController = async (req: IGetUserAuthInfoRequest, res: R
const newToken = `at-${uuidv4()}`
try {
await db.query(createNewApiToken(newToken, description, userId))
return res.status(StatusCode.Created).send({ token: newToken })
return res.status(StatusCode.Created).json({ token: newToken })
} catch(error) {
return next(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getTokensController } from "./get-tokens-controller"
jest.mock("../../../db/db")
const mockResponse = () => {
const res: Partial<Response> = {}
res.send = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
}
Expand All @@ -23,7 +23,7 @@ describe("getTokenController", () => {
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response)
expect(querySpy).toHaveBeenCalledTimes(1)
expect(response.send).toHaveBeenCalledTimes(1)
expect(response.json).toHaveBeenCalledTimes(1)
})

it("should call getOnlyMyApiTokens query when operator role", async () => {
Expand All @@ -38,6 +38,6 @@ describe("getTokenController", () => {
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response)
expect(querySpy).toHaveBeenCalledTimes(1)
expect(response.send).toHaveBeenCalledTimes(1)
expect(response.json).toHaveBeenCalledTimes(1)
})
})
4 changes: 2 additions & 2 deletions src/server/controllers/api-tokens/get-tokens-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export const getTokensController = async (req: IGetUserAuthInfoRequest, res: Res
const { role, userId } = req.user
if (role === AllowedRoles.Operator) {
const myApiKeys = await db.manyOrNone(getOnlyMyApiTokens(userId))
return res.status(StatusCode.Ok).send(myApiKeys)
return res.status(StatusCode.Ok).json(myApiKeys)
}

const result = await db.manyOrNone(getApiTokens())
res.status(StatusCode.Ok).send(result)
res.status(StatusCode.Ok).json(result)
}
6 changes: 3 additions & 3 deletions src/server/controllers/auth/login-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jest.mock("../../../db/db")
jest.mock("./helper/passwords")
const mockResponse = () => {
const res: Partial<Response> = {}
res.send = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
}
Expand Down Expand Up @@ -72,8 +72,8 @@ describe("loginController", () => {
await loginController(
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response, nextFunction)
expect(response.send).toHaveBeenCalledTimes(1)
expect(response.send).toBeCalledWith(
expect(response.json).toHaveBeenCalledTimes(1)
expect(response.json).toBeCalledWith(
expect.objectContaining({
token: expect.any(String),
username: "test",
Expand Down
2 changes: 1 addition & 1 deletion src/server/controllers/auth/login-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const loginController = async (req: Request, res: Response, next: NextFun
return next(boom.unauthorized("Invalid credentials"))
}
const token = generateToken(result[0].id)
return res.status(StatusCode.Ok).send({ token, username, role: result[0].role })
return res.status(StatusCode.Ok).json({ token, username, role: result[0].role })
} catch(error) {
return next(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jest.mock("../../../db/db")
jest.mock("./helper/passwords")
const mockResponse = () => {
const res: Partial<Response> = {}
res.send = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
}
Expand All @@ -26,7 +26,7 @@ describe("loginWithTokenController", () => {
await loginWithTokenController(
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response, nextFunction)
expect(response.send).toBeCalledWith(
expect(response.json).toBeCalledWith(
expect.objectContaining({
jwtToken: expect.any(String),
}))
Expand Down
2 changes: 1 addition & 1 deletion src/server/controllers/auth/login-with-token-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const loginWithTokenController = async (req: Request, res: Response, next
const [tokenData] = await db.query(getApiToken(token))
const { created_by } = tokenData
const jwtToken = generateTokenFromToken(created_by)
return res.status(StatusCode.Ok).send({ jwtToken })
return res.status(StatusCode.Ok).json({ jwtToken })
} catch(error) {
logger.error(error)
return next(boom.unauthorized())
Expand Down
6 changes: 3 additions & 3 deletions src/server/controllers/init/initialize-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { db } from "../../../db/db"
jest.mock("../../../db/db")
const mockResponse = () => {
const res: Partial<Response> = {}
res.send = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
}
Expand All @@ -21,7 +21,7 @@ describe("getInitController", () => {
await getInitController(
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response, nextFunction)
expect(response.send).toBeCalledWith({ initialized: true })
expect(response.json).toBeCalledWith({ initialized: true })
})
it("should return false if no user exists", async function () {
(db.manyOrNone as any).mockResolvedValue([])
Expand All @@ -31,6 +31,6 @@ describe("getInitController", () => {
await getInitController(
request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response, nextFunction)
expect(response.send).toBeCalledWith({ initialized: false })
expect(response.json).toBeCalledWith({ initialized: false })
})
})
4 changes: 2 additions & 2 deletions src/server/controllers/init/initialize-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export const getInitController = async (req: Request, res: Response, next: NextF
try {
const users = await db.manyOrNone(getUsers())
if (users && users.length > 0) {
return res.status(StatusCode.Ok).send({ initialized: true })
return res.status(StatusCode.Ok).json({ initialized: true })
}
return res.status(StatusCode.Ok).send({ initialized: false })
return res.status(StatusCode.Ok).json({ initialized: false })

} catch(error) {
return next(error)
Expand Down
4 changes: 2 additions & 2 deletions src/server/controllers/item/create-item-async-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export const createItemAsyncController = async (req: IGetUserAuthInfoRequest, re

const itemId = item.id
logger.info(`New item for scenario: ${scenarioName} created with id: ${itemId}`)
res.status(StatusCode.Created).send({ itemId, shareToken })
res.status(StatusCode.Created).json({ itemId, shareToken })
} catch(e) {
logger.error(`Creating new async item failed ${e}`)
res.status(StatusCode.InternalError).send()
res.status(StatusCode.InternalError).json()
}
}
2 changes: 1 addition & 1 deletion src/server/controllers/item/create-item-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const createItemController = (req: IGetUserAuthInfoRequest, res: Response

const itemId = item.id

res.status(StatusCode.Ok).send({ itemId, shareToken })
res.status(StatusCode.Ok).json({ itemId, shareToken })

const columnSet = new pg.helpers.ColumnSet([
"elapsed", "success", "bytes", "label",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getItemChartSettingsController } from "./get-item-chart-settings-contro
jest.mock("../../../db/db")
const mockResponse = () => {
const res: Partial<Response> = {}
res.send = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
}
Expand All @@ -20,6 +20,6 @@ describe("getItemChartSettingsController", () => {
await getItemChartSettingsController(request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response)
expect(querySpy).toHaveBeenCalledTimes(1)
expect(response.send).toHaveBeenCalledTimes(1)
expect(response.json).toHaveBeenCalledTimes(1)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export const getItemChartSettingsController = async (
const { userId } = req.user

const chartSettings = await db.oneOrNone(getItemChartSettings(itemId, userId))
res.status(StatusCode.Ok).send(chartSettings?.settings || [])
res.status(StatusCode.Ok).json(chartSettings?.settings || [])
}
6 changes: 3 additions & 3 deletions src/server/controllers/item/get-item-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { db } from "../../../db/db"
jest.mock("../../../db/db")
const mockResponse = () => {
const res: Partial<Response> = {}
res.send = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
}
Expand Down Expand Up @@ -43,8 +43,8 @@ describe("getItemController", () => {
expect(findItemQuerySpy).toHaveBeenCalledTimes(1)
expect(findItemStatsQuerySpy).toHaveBeenCalledTimes(1)
expect(getMonitoringDataQuerySpy).toHaveBeenCalledTimes(1)
expect(response.send).toHaveBeenCalledTimes(1)
expect(response.send).toHaveBeenLastCalledWith({
expect(response.json).toHaveBeenCalledTimes(1)
expect(response.json).toHaveBeenLastCalledWith({
analysisEnabled: true,
baseId: null,
environment: "environment",
Expand Down
2 changes: 1 addition & 1 deletion src/server/controllers/item/get-item-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const getItemController = async (req: IGetUserAuthInfoRequest, res: Respo

const maxCpu = findMinMax(monitoring.map(_ => _.avgCpu)).max

res.status(StatusCode.Ok).send({
res.status(StatusCode.Ok).json({
overview, sutOverview, statistics, status,
plot, extraPlotData, note, environment, hostname, reportStatus, thresholds, analysisEnabled,
baseId: base_id, isBase: base_id === itemId, zeroErrorToleranceEnabled, topMetricsSettings,
Expand Down
2 changes: 1 addition & 1 deletion src/server/controllers/item/get-items-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export const getItemsController = async (req: Request, res: Response) => {
_.base = !_.base ? false : true
return _
})
res.status(StatusCode.Ok).send({ name: scenarioName, data: idsBaseUpdate, total: parseInt(total, 10) })
res.status(StatusCode.Ok).json({ name: scenarioName, data: idsBaseUpdate, total: parseInt(total, 10) })
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export const getProcessingItemsController = async (req: Request, res: Response)
const processingItems = await db.any(getProcessingItems(projectName, scenarioName))
const inprogress = processingItems.filter((_) => _.reportStatus === ReportStatus.InProgress)
const failed = processingItems.filter((_) => _.reportStatus === ReportStatus.Error)
res.status(StatusCode.Ok).send({ failed, inprogress })
res.status(StatusCode.Ok).json({ failed, inprogress })
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createItemLinkController } from "./create-item-share-token-controller"
jest.mock("../../../../db/db")
const mockResponse = () => {
const res: Partial<Response> = {}
res.send = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
}
Expand All @@ -24,7 +24,7 @@ describe("createItemLinkController", () => {
}
await createItemLinkController(request as unknown as IGetUserAuthInfoRequest, response as unknown as Response)
expect(querySpy).toHaveBeenCalledWith("project", "scenario", "id", "token", "userId", "note")
expect(response.send).toHaveBeenCalledTimes(1)
expect(response.json).toHaveBeenCalledTimes(1)
expect(generateTokenSpy).toHaveBeenCalledTimes(1)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export const createItemLinkController = async (req: IGetUserAuthInfoRequest, res
const { note } = req.body
const token = generateShareToken()
await db.none(createShareToken(projectName, scenarioName, itemId, token, user.userId, note ))
res.status(StatusCode.Created).send({ token })
res.status(StatusCode.Created).json({ token })
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IGetUserAuthInfoRequest } from "../../../middleware/request.model"
jest.mock("../../../../db/db")
const mockResponse = () => {
const res: Partial<Response> = {}
res.send = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
}
Expand All @@ -19,6 +19,6 @@ describe("getItemLinksController", () => {
}
await getItemLinksController(request as unknown as IGetUserAuthInfoRequest, response as unknown as Response)
expect(querySpy).toHaveBeenCalledTimes(1)
expect(response.send).toHaveBeenCalledTimes(1)
expect(response.json).toHaveBeenCalledTimes(1)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export const getItemLinksController = async (req: IGetUserAuthInfoRequest, res:
const { projectName, scenarioName, itemId } = req.params
if ([AllowedRoles.Readonly, AllowedRoles.Operator].includes(role)) {
const myApiKeys = await db.manyOrNone(selectOnlyMyShareTokens(projectName, scenarioName, itemId, userId))
return res.status(StatusCode.Ok).send(myApiKeys)
return res.send(StatusCode.Ok).json(myApiKeys)
}
const shareTokens = await db.manyOrNone(selectShareTokens(projectName, scenarioName, itemId))
res.status(StatusCode.Ok).send(shareTokens)
res.status(StatusCode.Ok).json(shareTokens)

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export const getLabelErrorsController = async (req: Request, res: Response) => {
: acc[rc] = 1
return acc
}, {})
res.status(StatusCode.Ok).send({ stat })
res.status(StatusCode.Ok).json({ stat })
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jest.mock("../../../db/db")

const mockResponse = () => {
const res: Partial<Response> = {}
res.send = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
}
Expand All @@ -26,7 +26,7 @@ describe("getItemChartSettingsController", () => {
await getLabelTrendController(request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response)
expect(querySpy).toHaveBeenCalledTimes(1)
expect(response.send).toHaveBeenCalledTimes(1)
expect(response.json).toHaveBeenCalledTimes(1)
})
it("should fetch getLabelHistory from db", async () => {
const response = mockResponse()
Expand All @@ -41,7 +41,7 @@ describe("getItemChartSettingsController", () => {
await getLabelTrendController(request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response)
expect(querySpy).toHaveBeenCalledTimes(1)
expect(response.send).toHaveBeenCalledTimes(1)
expect(response.json).toHaveBeenCalledTimes(1)
})
it("should sort query output from oldest to newest", async () => {
const response = mockResponse()
Expand Down Expand Up @@ -108,7 +108,7 @@ describe("getItemChartSettingsController", () => {

await getLabelTrendController(request as unknown as IGetUserAuthInfoRequest,
response as unknown as Response)
expect(response.send).toHaveBeenCalledWith({
expect(response.json).toHaveBeenCalledWith({
chartSeries: {
errorRate: [100, 10],
p90: [1, 10],
Expand Down
2 changes: 1 addition & 1 deletion src/server/controllers/label/get-label-trend-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const getLabelTrendController = async (req: Request, res: Response) => {
}, { timePoints: [], p90: [], p95: [], p99: [], errorRate: [], throughput: [], virtualUsers: [], avgResponseTime: [],
avgConnectionTime: [], avgLatency: [], name: [] })

res.status(StatusCode.Ok).send({
res.status(StatusCode.Ok).json({
chartSeries: {
timePoints,
p90, p95, p99,
Expand Down
2 changes: 1 addition & 1 deletion src/server/controllers/label/get-label-vu-controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const getLabelVirtualUsersController = async (req: Request, res: Response
const result = await db.query(getMaxVuForLabel(
scenarioName, projectName, label,
itemId, environment))
res.status(StatusCode.Ok).send({ result })
res.status(StatusCode.Ok).json({ result })
} catch(error) {
return next(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { db } from "../../../db/db"
jest.mock("../../../db/db")
const mockResponse = () => {
const res: Partial<Response> = {}
res.send = jest.fn().mockReturnValue(res)
res.json = jest.fn().mockReturnValue(res)
res.status = jest.fn().mockReturnValue(res)
return res
}
Expand All @@ -28,6 +28,6 @@ describe("getLatestItemsController", function () {
response as unknown as Response)
expect(latestItemSpy).toHaveBeenNthCalledWith(1, request.user.userId)

expect(response.send).toHaveBeenCalledWith([])
expect(response.json).toHaveBeenCalledWith([])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import { IGetUserAuthInfoRequest } from "../../middleware/request.model"
export const getLatestItemsControllers = async (req: IGetUserAuthInfoRequest, res: Response) => {
const user = req.user
const items = await db.many(latestItems(user.userId))
res.status(StatusCode.Ok).send(items)
res.status(StatusCode.Ok).json(items)
}
Loading

0 comments on commit 470d88c

Please sign in to comment.