From 9b7c82a5fa3babb64cce7b4aa091b3c552f30394 Mon Sep 17 00:00:00 2001 From: Jacob DeWar Date: Thu, 19 Jan 2023 16:53:01 -0500 Subject: [PATCH] Fix malformed URI error for search fixes MAT-1151 flag=none test plan - Login to canvas and grab token for RCS session - Make API call to /api/announcements - Use something for search_term that would cause URI error, like 80% > Verify search results are as expected, no error returned Change-Id: Ia97d30dc67bb400bc6ace4a6ac7054c3e28b05c0 Reviewed-on: https://gerrit.instructure.com/c/canvas-rce-api/+/309209 Tested-by: Service Cloud Jenkins Reviewed-by: Deyvison Penha QA-Review: Deyvison Penha Product-Review: Allison Howell --- CHANGELOG.md | 17 ++++++++++++++++- app/utils/search.js | 14 ++++++++++---- package.json | 2 +- test/service/utils/search.test.js | 9 +++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33414ef..38652ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # Changelog + All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), @@ -6,17 +7,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.24] + +Fix malformed URI error for search + ## [1.23] + Response for files list includes file category + ## [1.21] + Rename Buttons & Icon text to Icon Maker + ## [1.20] + ### Added + - Responses from the documents API now include the file's media_entry_id. This ID corresponds to a Canvas MediaObject. - The files API now accepts query params that communicate to Canvas the "replaced by" chain context + ## [1.19] + ### Added + - A changelog to make changes more clear ### Changed -- The canvas-rce-api Docker image now uses Node 16 and NPM 8 \ No newline at end of file + +- The canvas-rce-api Docker image now uses Node 16 and NPM 8 diff --git a/app/utils/search.js b/app/utils/search.js index d98e5c4..f7b5570 100644 --- a/app/utils/search.js +++ b/app/utils/search.js @@ -5,10 +5,16 @@ function getSearch(query) { return ""; } - const isSearchTermEncoded = searchTerm !== decodeURIComponent(searchTerm); - return isSearchTermEncoded - ? `&search_term=${searchTerm}` - : `&search_term=${encodeURIComponent(searchTerm)}`; + let encodedTerm; + try { + isSearchTermEncoded = searchTerm !== decodeURIComponent(searchTerm); + encodedTerm = isSearchTermEncoded + ? searchTerm + : encodeURIComponent(searchTerm); + } catch { + encodedTerm = encodeURIComponent(searchTerm); + } + return `&search_term=${encodedTerm}`; } module.exports = { getSearch }; diff --git a/package.json b/package.json index 86a3854..4b92732 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "canvas-rce-api", - "version": "1.23.0", + "version": "1.24.0", "description": "An API for proxying requests by the RCE to Canvas and other services.", "engines": { "node": "^16", diff --git a/test/service/utils/search.test.js b/test/service/utils/search.test.js index 71a3c72..b579313 100644 --- a/test/service/utils/search.test.js +++ b/test/service/utils/search.test.js @@ -44,4 +44,13 @@ describe("search(query)", () => { `Expected ${getSearchResult} to end with ${encodedSearchText}` ); }); + + it("does not return an error for malformed URIs", () => { + const searchTerm = "80%"; + const encodedSearchText = encodeURIComponent(searchTerm); + query.search_term = searchTerm; + + const getSearchResult = search.getSearch(query); + assert(getSearchResult.endsWith(encodedSearchText)); + }); });