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" } })); + }); + }); +});