TASK22224: larger parity bundle for reps and upload handlers
This commit is contained in:
@@ -33,7 +33,6 @@ import {
|
|||||||
downloadAllRepsFiles,
|
downloadAllRepsFiles,
|
||||||
getRepsBlobs
|
getRepsBlobs
|
||||||
} from "../../../actions/azurestorage";
|
} from "../../../actions/azurestorage";
|
||||||
import _ from "lodash";
|
|
||||||
import nextConnect from "next-connect";
|
import nextConnect from "next-connect";
|
||||||
import middleware from "../middleware/middleware";
|
import middleware from "../middleware/middleware";
|
||||||
import { hashAPIPath } from "../../../actions/core/hash";
|
import { hashAPIPath } from "../../../actions/core/hash";
|
||||||
@@ -44,10 +43,8 @@ const ApiProxy = nextConnect();
|
|||||||
ApiProxy.use(middleware);
|
ApiProxy.use(middleware);
|
||||||
|
|
||||||
ApiProxy.get(async (req, res) => {
|
ApiProxy.get(async (req, res) => {
|
||||||
var containerName = req.query.container;
|
const containerName = req.query.container;
|
||||||
var casefolderID = req.query.casefolderID;
|
const checkHash = req.query.hash;
|
||||||
|
|
||||||
var checkHash = req.query.hash;
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
typeof containerName === "undefined" ||
|
typeof containerName === "undefined" ||
|
||||||
@@ -62,9 +59,16 @@ ApiProxy.get(async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var checkquerypath = "/api/file/getrepsblob?container=" + containerName;
|
const hashCandidatePaths = [
|
||||||
|
"/api/file/getrepsblob?container=" + containerName,
|
||||||
|
"/api/file/getrepsblob?container=" + encodeURIComponent(containerName)
|
||||||
|
];
|
||||||
|
|
||||||
if (hashAPIPath(checkquerypath) != "&hash=" + checkHash) {
|
const isHashValid = hashCandidatePaths.some(
|
||||||
|
(candidatePath) => hashAPIPath(candidatePath) == "&hash=" + checkHash
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!isHashValid) {
|
||||||
return respondError(res, {
|
return respondError(res, {
|
||||||
status: 400,
|
status: 400,
|
||||||
code: "INVALID_HASH",
|
code: "INVALID_HASH",
|
||||||
@@ -72,23 +76,19 @@ ApiProxy.get(async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const blobObj = await getRepsBlobs(containerName)
|
try {
|
||||||
.then(async (data) => {
|
const repsBlobs = await getRepsBlobs(containerName);
|
||||||
return data;
|
const result = await downloadAllRepsFiles(containerName, repsBlobs);
|
||||||
})
|
res.setHeader("Cache-Control", "no-store");
|
||||||
.then(async (data) => {
|
return respondSuccess(res, result);
|
||||||
let result = await downloadAllRepsFiles(containerName, data);
|
} catch (error) {
|
||||||
res.setHeader("Cache-Control", "no-store");
|
consoleLogger(error);
|
||||||
return respondSuccess(res, result);
|
return respondError(res, {
|
||||||
})
|
status: 400,
|
||||||
.catch((error) => {
|
code: "GET_REPS_BLOB_FAILED",
|
||||||
consoleLogger(error);
|
message: "Failed to retrieve representation blobs"
|
||||||
return respondError(res, {
|
|
||||||
status: 400,
|
|
||||||
code: "GET_REPS_BLOB_FAILED",
|
|
||||||
message: "Failed to retrieve representation blobs"
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export default ApiProxy;
|
export default ApiProxy;
|
||||||
|
|||||||
+18
-21
@@ -1,8 +1,4 @@
|
|||||||
import {
|
import { createBlob, createRepBlob } from "../../../actions/azurestorage";
|
||||||
createBlob,
|
|
||||||
createRepBlob,
|
|
||||||
uploadFile
|
|
||||||
} from "../../../actions/azurestorage";
|
|
||||||
import { hashAPIPath } from "../../../actions/core/hash";
|
import { hashAPIPath } from "../../../actions/core/hash";
|
||||||
import { respondError, respondSuccess } from "../middleware/apiResponse";
|
import { respondError, respondSuccess } from "../middleware/apiResponse";
|
||||||
|
|
||||||
@@ -13,7 +9,7 @@ const ApiProxy = nextConnect();
|
|||||||
ApiProxy.use(middleware);
|
ApiProxy.use(middleware);
|
||||||
|
|
||||||
ApiProxy.post(async (req, res) => {
|
ApiProxy.post(async (req, res) => {
|
||||||
var checkHash = req.query.hash;
|
const checkHash = req.query.hash;
|
||||||
|
|
||||||
if (typeof checkHash === "undefined" || checkHash.length === 0) {
|
if (typeof checkHash === "undefined" || checkHash.length === 0) {
|
||||||
return respondError(res, {
|
return respondError(res, {
|
||||||
@@ -23,9 +19,12 @@ ApiProxy.post(async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var checkquerypath = "/api/file/upload";
|
const hashCandidatePaths = ["/api/file/upload", "/api/file/upload?"];
|
||||||
|
const isHashValid = hashCandidatePaths.some(
|
||||||
|
(candidatePath) => hashAPIPath(candidatePath) == "?hash=" + checkHash
|
||||||
|
);
|
||||||
|
|
||||||
if (hashAPIPath(checkquerypath) != "?hash=" + checkHash) {
|
if (!isHashValid) {
|
||||||
return respondError(res, {
|
return respondError(res, {
|
||||||
status: 400,
|
status: 400,
|
||||||
code: "INVALID_HASH",
|
code: "INVALID_HASH",
|
||||||
@@ -51,21 +50,19 @@ ApiProxy.post(async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
try {
|
||||||
repOrAppeal
|
const data = await (repOrAppeal
|
||||||
? createRepBlob(appealData, containerID, casefolderID)
|
? createRepBlob(appealData, containerID, casefolderID)
|
||||||
: createBlob(appealData, containerID, casefolderID)
|
: createBlob(appealData, containerID, casefolderID));
|
||||||
)
|
|
||||||
.then((data) => {
|
return respondSuccess(res, { data });
|
||||||
return respondSuccess(res, { data });
|
} catch {
|
||||||
})
|
return respondError(res, {
|
||||||
.catch(() => {
|
status: 400,
|
||||||
return respondError(res, {
|
code: "UPLOAD_FAILED",
|
||||||
status: 400,
|
message: "Failed to upload blob"
|
||||||
code: "UPLOAD_FAILED",
|
|
||||||
message: "Failed to upload blob"
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
import {
|
import { uploadSingleFile } from "../../../actions/azurestorage";
|
||||||
createBlob,
|
|
||||||
createRepBlob,
|
|
||||||
uploadSingleFile
|
|
||||||
} from "../../../actions/azurestorage";
|
|
||||||
|
|
||||||
import nextConnect from "next-connect";
|
import nextConnect from "next-connect";
|
||||||
import middleware from "../middleware/middleware";
|
import middleware from "../middleware/middleware";
|
||||||
@@ -37,7 +33,7 @@ const ApiProxy = nextConnect();
|
|||||||
ApiProxy.use(middleware);
|
ApiProxy.use(middleware);
|
||||||
|
|
||||||
ApiProxy.post(async (req, res) => {
|
ApiProxy.post(async (req, res) => {
|
||||||
var checkHash = req.query.hash;
|
const checkHash = req.query.hash;
|
||||||
|
|
||||||
if (typeof checkHash === "undefined" || checkHash.length === 0) {
|
if (typeof checkHash === "undefined" || checkHash.length === 0) {
|
||||||
return respondError(res, {
|
return respondError(res, {
|
||||||
@@ -47,9 +43,15 @@ ApiProxy.post(async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var checkquerypath = "/api/file/uploadsinglefile";
|
const hashCandidatePaths = [
|
||||||
|
"/api/file/uploadsinglefile",
|
||||||
|
"/api/file/uploadsinglefile?"
|
||||||
|
];
|
||||||
|
const isHashValid = hashCandidatePaths.some(
|
||||||
|
(candidatePath) => hashAPIPath(candidatePath) == "?hash=" + checkHash
|
||||||
|
);
|
||||||
|
|
||||||
if (hashAPIPath(checkquerypath) != "?hash=" + checkHash) {
|
if (!isHashValid) {
|
||||||
return respondError(res, {
|
return respondError(res, {
|
||||||
status: 400,
|
status: 400,
|
||||||
code: "INVALID_HASH",
|
code: "INVALID_HASH",
|
||||||
@@ -87,8 +89,8 @@ ApiProxy.post(async (req, res) => {
|
|||||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||||
];
|
];
|
||||||
|
|
||||||
var allowedFilesFormData = {};
|
const allowedFilesFormData = {};
|
||||||
var invalidFiles = []; // To store the names of invalid files
|
const invalidFiles = []; // To store the names of invalid files
|
||||||
|
|
||||||
for (const [fileName, fileDetails] of Object.entries(uploadedFiles)) {
|
for (const [fileName, fileDetails] of Object.entries(uploadedFiles)) {
|
||||||
const file = fileDetails[0];
|
const file = fileDetails[0];
|
||||||
|
|||||||
@@ -51,6 +51,38 @@ test("upload handler returns INVALID_HASH for wrong hash", async () => {
|
|||||||
assert.strictEqual(res.state.jsonBody.error.code, "INVALID_HASH");
|
assert.strictEqual(res.state.jsonBody.error.code, "INVALID_HASH");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("upload handler accepts alternate canonical hash candidate", async () => {
|
||||||
|
const mod = loadModule("pages/api/file/upload.js", {
|
||||||
|
nextConnect: createNextConnectMock(),
|
||||||
|
middleware: () => {},
|
||||||
|
hashAPIPath: (queryPath) =>
|
||||||
|
queryPath === "/api/file/upload?"
|
||||||
|
? "?hash=expected"
|
||||||
|
: "?hash=other",
|
||||||
|
respondError: respondErrorMock,
|
||||||
|
respondSuccess: respondSuccessMock,
|
||||||
|
createBlob: async () => ({ id: "blob-alt" }),
|
||||||
|
createRepBlob: async () => ({ id: "rep-alt" })
|
||||||
|
});
|
||||||
|
|
||||||
|
const req = {
|
||||||
|
query: { hash: "expected" },
|
||||||
|
body: {
|
||||||
|
appealData: { key: "value" },
|
||||||
|
containerID: ["c1"],
|
||||||
|
casefolderID: ["f1"],
|
||||||
|
repOrAppeal: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const res = createRes();
|
||||||
|
await mod.default.handler(req, res);
|
||||||
|
|
||||||
|
assert.strictEqual(res.state.statusCode, 200);
|
||||||
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(res.state.jsonBody)), {
|
||||||
|
data: { id: "blob-alt" }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test("deleteblob handler returns MISSING_REQUIRED_QUERY when blobname missing", async () => {
|
test("deleteblob handler returns MISSING_REQUIRED_QUERY when blobname missing", async () => {
|
||||||
const mod = loadModule("pages/api/file/deleteblob.js", {
|
const mod = loadModule("pages/api/file/deleteblob.js", {
|
||||||
nextConnect: createNextConnectMock(),
|
nextConnect: createNextConnectMock(),
|
||||||
@@ -375,6 +407,84 @@ test("getrepsblob handler dependency failure returns GET_REPS_BLOB_FAILED", asyn
|
|||||||
assert.strictEqual(res.state.jsonBody.error.code, "GET_REPS_BLOB_FAILED");
|
assert.strictEqual(res.state.jsonBody.error.code, "GET_REPS_BLOB_FAILED");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("getrepsblob handler accepts encoded container hash variant", async () => {
|
||||||
|
const mod = loadModule("pages/api/file/getrepsblob.js", {
|
||||||
|
nextConnect: createNextConnectMock(),
|
||||||
|
middleware: () => {},
|
||||||
|
hashAPIPath: (queryPath) =>
|
||||||
|
queryPath.includes("container=cont%2F1")
|
||||||
|
? "&hash=expected"
|
||||||
|
: "&hash=other",
|
||||||
|
respondError: respondErrorMock,
|
||||||
|
respondSuccess: respondSuccessMock,
|
||||||
|
consoleLogger: () => {},
|
||||||
|
getRepsBlobs: async () => [{ path: "cont/rep1.json" }],
|
||||||
|
downloadAllRepsFiles: async () => ({ value: [{ id: "r-encoded" }] })
|
||||||
|
});
|
||||||
|
|
||||||
|
const req = {
|
||||||
|
query: { container: "cont/1", hash: "expected" }
|
||||||
|
};
|
||||||
|
const res = createRes();
|
||||||
|
await mod.default.handler(req, res);
|
||||||
|
|
||||||
|
assert.strictEqual(res.state.statusCode, 200);
|
||||||
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(res.state.jsonBody)), {
|
||||||
|
value: [{ id: "r-encoded" }]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("uploadsinglefile handler returns HASH_REQUIRED when hash missing", async () => {
|
||||||
|
const mod = loadModule("pages/api/file/uploadsinglefile.js", {
|
||||||
|
nextConnect: createNextConnectMock(),
|
||||||
|
middleware: () => {},
|
||||||
|
hashAPIPath: () => "?hash=expected",
|
||||||
|
respondError: respondErrorMock,
|
||||||
|
respondSuccess: respondSuccessMock,
|
||||||
|
consoleLogger: () => {},
|
||||||
|
uploadSingleFile: async () => ({ uploaded: 0 }),
|
||||||
|
fileTypeFromBuffer: async () => ({ mime: "application/pdf" }),
|
||||||
|
fs: { readFileSync: () => Buffer.from("x") }
|
||||||
|
});
|
||||||
|
|
||||||
|
const req = { query: {}, body: {}, files: {} };
|
||||||
|
const res = createRes();
|
||||||
|
await mod.default.handler(req, res);
|
||||||
|
|
||||||
|
assert.strictEqual(res.state.statusCode, 400);
|
||||||
|
assert.strictEqual(res.state.jsonBody.error.code, "HASH_REQUIRED");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("uploadsinglefile handler dependency failure returns UPLOAD_SINGLE_FILE_FAILED", async () => {
|
||||||
|
const mod = loadModule("pages/api/file/uploadsinglefile.js", {
|
||||||
|
nextConnect: createNextConnectMock(),
|
||||||
|
middleware: () => {},
|
||||||
|
hashAPIPath: () => "?hash=expected",
|
||||||
|
respondError: respondErrorMock,
|
||||||
|
respondSuccess: respondSuccessMock,
|
||||||
|
consoleLogger: () => {},
|
||||||
|
uploadSingleFile: async () => {
|
||||||
|
throw new Error("upload failed");
|
||||||
|
},
|
||||||
|
fileTypeFromBuffer: async () => ({ mime: "application/pdf" }),
|
||||||
|
fs: { readFileSync: () => Buffer.from("x") }
|
||||||
|
});
|
||||||
|
|
||||||
|
const req = {
|
||||||
|
query: { hash: "expected" },
|
||||||
|
body: { containerID: ["c1"], casefolderID: ["f1"] },
|
||||||
|
files: {}
|
||||||
|
};
|
||||||
|
const res = createRes();
|
||||||
|
await mod.default.handler(req, res);
|
||||||
|
|
||||||
|
assert.strictEqual(res.state.statusCode, 500);
|
||||||
|
assert.strictEqual(
|
||||||
|
res.state.jsonBody.error.code,
|
||||||
|
"UPLOAD_SINGLE_FILE_FAILED"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
test("getawaitingsubmissionfromblob handler success returns payload", async () => {
|
test("getawaitingsubmissionfromblob handler success returns payload", async () => {
|
||||||
const mod = loadModule("pages/api/file/getawaitingsubmissionfromblob.js", {
|
const mod = loadModule("pages/api/file/getawaitingsubmissionfromblob.js", {
|
||||||
nextConnect: createNextConnectMock(),
|
nextConnect: createNextConnectMock(),
|
||||||
|
|||||||
Reference in New Issue
Block a user