From f104fbc543412c198489592d3f8a634b2b8de760 Mon Sep 17 00:00:00 2001 From: Roland Beres Date: Mon, 12 Sep 2022 12:00:29 +0200 Subject: [PATCH] Add endpoint for rce config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs: QUIZ-9944 flag=none Test plan: - Run canvas and canvas-rce-api locally - Send a get request to http://rce.docker/api/rceConfig with a correct auth token, contextType and contextId parameters - Note that the endpoint returns the exact same body that canvas returns from /api/v1/services/rce_config Change-Id: Ib8fd747f7b46b87b523ab58a4a2cbece97174603 Reviewed-on: https://gerrit.instructure.com/c/canvas-rce-api/+/300728 Tested-by: Service Cloud Jenkins Reviewed-by: Rajmund Csehil Reviewed-by: Viktor Szpisják QA-Review: Rajmund Csehil Product-Review: Rajmund Csehil --- app/api/index.js | 7 +++++ app/api/rceConfig.js | 23 ++++++++++++++++ test/service/api/rceConfig.test.js | 44 ++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 app/api/rceConfig.js create mode 100644 test/service/api/rceConfig.test.js diff --git a/app/api/index.js b/app/api/index.js index 9df8f7f..e571273 100644 --- a/app/api/index.js +++ b/app/api/index.js @@ -11,6 +11,7 @@ const assignments = require("./assignments"); const discussions = require("./discussions"); const modules = require("./modules"); const quizzes = require("./quizzes"); +const rceConfig = require("./rceConfig"); const wikiPages = require("./wikiPages"); const kaltura = require("./kaltura"); const media_objects = require("./mediaObjects"); @@ -61,6 +62,12 @@ function init(auth, unsplashController) { auth, wrapCanvas(quizzes) ); + app.get( + "/api/rceConfig", + statsdKey("api", "rceConfig"), + auth, + wrapCanvas(rceConfig) + ); app.get( "/api/wikiPages", statsdKey("api", "wikiPages"), diff --git a/app/api/rceConfig.js b/app/api/rceConfig.js new file mode 100644 index 0000000..af23a3e --- /dev/null +++ b/app/api/rceConfig.js @@ -0,0 +1,23 @@ +"use strict"; + +function canvasPath(request) { + if (!request.query.contextId) + throw new Error("missing contextId parameter from query param"); + + const queryParamName = (contextType => { + switch (contextType) { + case "course": + return "course_id"; + case "user": + return "user_id"; + case "group": + return "group_id"; + default: + throw new Error(`invalid contextType (${request.query.contextType})`); + } + })(request.query.contextType); + + return `/api/v1/services/rce_config?${queryParamName}=${request.query.contextId}`; +} + +module.exports = { canvasPath }; diff --git a/test/service/api/rceConfig.test.js b/test/service/api/rceConfig.test.js new file mode 100644 index 0000000..1bb2c73 --- /dev/null +++ b/test/service/api/rceConfig.test.js @@ -0,0 +1,44 @@ +"use strict"; + +const assert = require("assert"); + +const { canvasPath } = require("../../../app/api/rceConfig"); + +describe("RCE config API", () => { + describe("canvasPath", () => { + it("builds the correct path including with course_id parameter", () => { + const path = canvasPath({ + query: { contextType: "course", contextId: "1" } + }); + assert.ok(path.match("/api/v1/services/rce_config\\?course_id=1")); + }); + + it("builds the correct path including with user_id parameter", () => { + const path = canvasPath({ + query: { contextType: "user", contextId: "1" } + }); + assert.ok(path.match("/api/v1/services/rce_config\\?user_id=1")); + }); + + it("builds the correct path including with group_id parameter", () => { + const path = canvasPath({ + query: { contextType: "group", contextId: "1" } + }); + assert.ok(path.match("/api/v1/services/rce_config\\?group_id=1")); + }); + + it("throws error in case of missing context type", () => { + assert.throws(() => canvasPath({ query: { contextId: "1" } })); + }); + + it("throws error in case of invalid context type", () => { + assert.throws(() => + canvasPath({ query: { contextType: "asd", contextId: "1" } }) + ); + }); + + it("throws error in case of missing context id", () => { + assert.throws(() => canvasPath({ query: { contextType: "asd" } })); + }); + }); +});