From d40f142f72b6fbf05b5a9943e7a53d6441a64372 Mon Sep 17 00:00:00 2001 From: Weston Dransfield Date: Tue, 25 Jan 2022 16:26:05 -0700 Subject: [PATCH] Forward "category" on file uploads refs MAT-631 Test Plan - Check out this commit, g/283585 (canvas), and g/283594 (canvas) - In Canvas, create a button & icon - In Canvas rails console, inspect the Attachment record created for the button & icon - Verify the category of the attachment is "buttons_and_icons" Change-Id: Icb851ebcbb5a5fdace0b39d2d82b938d090fb459 Reviewed-on: https://gerrit.instructure.com/c/canvas-rce-api/+/283596 Tested-by: Service Cloud Jenkins Reviewed-by: Joe Hernandez QA-Review: Joe Hernandez Product-Review: Weston Dransfield --- app/api/upload.js | 3 +- test/service/api/upload.test.js | 56 ++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/app/api/upload.js b/app/api/upload.js index 6890b54..d252eb1 100644 --- a/app/api/upload.js +++ b/app/api/upload.js @@ -21,7 +21,8 @@ function transformBody(body) { contentType: body.file.type || body.file.contentType || undefined, parent_folder_id: body.file.parentFolderId, on_duplicate: body.onDuplicate || "rename", - success_include: ["preview_url"] + success_include: ["preview_url"], + category: body.category || undefined, }; return canvasUploadPreflightBody; diff --git a/test/service/api/upload.test.js b/test/service/api/upload.test.js index bbe3b5d..acc43d5 100644 --- a/test/service/api/upload.test.js +++ b/test/service/api/upload.test.js @@ -5,7 +5,7 @@ const sinon = require("sinon"); const { canvasPath, canvasResponseHandler, - transformBody + transformBody, } = require("../../../app/api/upload"); describe("Upload API", () => { @@ -14,8 +14,8 @@ describe("Upload API", () => { const path = canvasPath({ body: { contextType: "course", - contextId: 47 - } + contextId: 47, + }, }); assert(path === `/api/v1/courses/47/files`); }); @@ -24,8 +24,8 @@ describe("Upload API", () => { const path = canvasPath({ body: { contextType: "group", - contextId: 47 - } + contextId: 47, + }, }); assert(path === `/api/v1/groups/47/files`); }); @@ -34,8 +34,8 @@ describe("Upload API", () => { const path = canvasPath({ body: { contextType: "user", - contextId: 47 - } + contextId: 47, + }, }); assert(path === `/api/v1/users/47/files`); }); @@ -47,15 +47,15 @@ describe("Upload API", () => { beforeEach(() => { request = { protocol: "http", - get: () => "canvashost" + get: () => "canvashost", }; response = { status: sinon.spy(), - send: sinon.spy() + send: sinon.spy(), }; canvasResponse = { status: 200, - body: [] + body: [], }; }); @@ -77,19 +77,19 @@ describe("Upload API", () => { name: "filename", size: 42, type: "jpeg", - parentFolderId: 1 + parentFolderId: 1, }, - ...overrides + ...overrides, }; } it("reshapes the body to the format canvas wants", () => { - const fixed = transformBody(getBody({onDuplicate: 'overwrite'})); + const fixed = transformBody(getBody({ onDuplicate: "overwrite" })); assert.equal(fixed.name, "filename"); assert.equal(fixed.size, 42); assert.equal(fixed.contentType, "jpeg"); assert.equal(fixed.parent_folder_id, 1); - assert.equal(fixed.on_duplicate, 'overwrite') + assert.equal(fixed.on_duplicate, "overwrite"); }); it("renames files on duplicate instead of overwriting them", () => { @@ -111,13 +111,31 @@ describe("Upload API", () => { }); describe('when "onDuplicate" is undefined', () => { - const overrides = {onDuplicate: undefined} + const overrides = { onDuplicate: undefined }; - const subject = () => transformBody(getBody(overrides)) + const subject = () => transformBody(getBody(overrides)); it('defaults duplication strategy to "rename"', () => { - assert.equal(subject().on_duplicate, 'rename') - }) - }) + assert.equal(subject().on_duplicate, "rename"); + }); + }); + + describe('when "category" is specified', () => { + const overrides = { category: "buttons_and_icons" }; + + const subject = () => transformBody(getBody(overrides)); + + it("sets the category in the body", () => { + assert.equal(subject().category, "buttons_and_icons"); + }); + }); + + describe('when "category" is not specified', () => { + const subject = () => transformBody(getBody()); + + it("sets the category in the body", () => { + assert.equal(subject().category, undefined); + }); + }); }); });