From 705b6cbc1c53c43adfc3e355288a0d3c8a54a9a0 Mon Sep 17 00:00:00 2001 From: Kevin Midboe Date: Sat, 20 Aug 2022 17:18:36 +0200 Subject: [PATCH] Prevent assert error when checking request status, returns success 200 --- src/request/request.js | 11 +++++------ src/webserver/controllers/request/getRequest.js | 14 +++++++++++++- ...serIWantAForbiddenErrorIfTheTokenIsMalformed.js | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/request/request.js b/src/request/request.js index 3f3d91b..e5a034e 100644 --- a/src/request/request.js +++ b/src/request/request.js @@ -33,7 +33,8 @@ class RequestRepository { // downloaded: "(select status from requests where id is request.id and type is request.type limit 1)", // deluge: '(select status from deluge_torrent where id is request.id and type is request.type limit 1)', // fetchAllFilterStatus: 'select * from request where ' - // readWithoutUserData: "select id, title, year, type, status, date from requests where id is ? and type is ?", + readWithoutUserData: + "select id, title, year, type, status, date from requests where id is ? and type is ?", read: "select id, title, year, type, status, requested_by, ip, date, user_agent from requests where id is ? and type is ?" }; } @@ -83,7 +84,8 @@ class RequestRepository { return this.database .get(this.queries.readWithoutUserData, [id, type]) .then(row => { - assert(row, "Could not find request item with that id and type"); + if (!row) return null; + return { id: row.id, title: row.title, @@ -122,10 +124,7 @@ class RequestRepository { return this.database .all(fetchQuery, fetchParams) .then(async rows => { - const sqliteResponse = await this.database.get( - fetchTotalResults, - filter || null - ); + const sqliteResponse = await this.database.get(fetchTotalResults); const { totalRequests } = sqliteResponse; const totalPages = Math.ceil(totalRequests / 26); diff --git a/src/webserver/controllers/request/getRequest.js b/src/webserver/controllers/request/getRequest.js index b4a491f..d73f091 100644 --- a/src/webserver/controllers/request/getRequest.js +++ b/src/webserver/controllers/request/getRequest.js @@ -14,7 +14,19 @@ function fetchAllRequests(req, res) { request .getRequestByIdAndType(id, type) - .then(result => res.send(result)) + .then(result => { + if (!result) { + return res.send({ + success: false, + message: `Item ${type} with id ${id} has not been requested` + }); + } + + return res.send({ + success: true, + result + }); + }) .catch(error => { return res.status(error?.statusCode || 500).send({ success: false, diff --git a/tests/system/asAUserIWantAForbiddenErrorIfTheTokenIsMalformed.js b/tests/system/asAUserIWantAForbiddenErrorIfTheTokenIsMalformed.js index 72d0074..8dc4c9c 100644 --- a/tests/system/asAUserIWantAForbiddenErrorIfTheTokenIsMalformed.js +++ b/tests/system/asAUserIWantAForbiddenErrorIfTheTokenIsMalformed.js @@ -4,7 +4,7 @@ const chaiHttp = require("chai-http"); const server = require("../../src/webserver/server"); const resetDatabase = require("../helpers/resetDatabase"); -// const assert = require("assert"); + chai.use(chaiHttp); describe("As a user I want a forbidden error if the token is malformed", () => {