Jest (broken)

This commit is contained in:
2020-08-19 15:31:02 +02:00
parent 4c18ed646b
commit 4715fc1814
85 changed files with 5889 additions and 11876 deletions

71
services/azureServices.js Normal file
View File

@@ -0,0 +1,71 @@
import azureBlob from "@azure/storage-blob";
import getStream from "into-stream";
// SETUP
const containerURL = `https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net/img/`;
const container = "img";
const OneMB = 1024 * 1024;
const uploadOptions = { bufferSize: 4 * OneMB, maxBuffers: 2 };
const sharedKeyCredential = new azureBlob.StorageSharedKeyCredential(
process.env.AZURE_STORAGE_ACCOUNT_NAME,
process.env.AZURE_STORAGE_ACCOUNT_KEY
);
const pipeline = azureBlob.newPipeline(sharedKeyCredential);
const blobServiceClient = new azureBlob.BlobServiceClient(
`https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net`,
pipeline
);
// CODE
export function renameBlob(blobURL) {
const blobName = blobURL.replace(containerURL, "");
const containerClient = blobServiceClient.getContainerClient(container);
const tempBlob = containerClient.getBlobClient(blobName);
const newBlob = containerClient.getBlobClient(removePrefix(blobName));
newBlob.syncCopyFromURL(tempBlob.url);
return newBlob.url;
}
export async function uploadBlob(request, resp) {
const blobName = makeTempBlobName(request.file.originalname);
const stream = getStream(request.file.buffer);
const containerClient = blobServiceClient.getContainerClient(container);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const response = {
imgURL: `https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net/img/${blobName}`,
};
try {
await blockBlobClient
.uploadStream(
stream,
uploadOptions.bufferSize,
uploadOptions.maxBuffers,
{ blobHTTPHeaders: { blobContentType: "image/jpeg" } }
)
.then(() => {
setDeleteTempBlobTimer(blobName, containerClient, 1);
resp.send(response);
});
} catch (err) {
console.log(err);
}
}
export function removePrefix(string) {
const newString = string.replace("TEMP_", "");
return newString;
}
export function makeTempBlobName(originalName) {
const identifier = Math.random().toString().replace(/0\./, "");
return `TEMP_${identifier}-${originalName}`;
}
export function setDeleteTempBlobTimer(blobName, containerClient, minutes) {
let blob = containerClient.getBlobClient(blobName);
setTimeout(() => {
blob.delete();
}, 1000 * 60 * minutes);
}

View File

@@ -0,0 +1,8 @@
import { removePrefix } from "./azureServices.js";
import { JsonWebTokenError } from "jsonwebtoken";
import azureBlob from "@azure/storage-blob";
jest.mock("@azure/storage-blob");
test("should remove TEMP_ prefix", () => {
expect(removePrefix("TEMP_abcdef")).toBe("abcdef");
});

View File

@@ -1,28 +0,0 @@
import azureBlob from "@azure/storage-blob";
const containerURL = `https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net/img/`;
const container = "img";
const sharedKeyCredential = new azureBlob.StorageSharedKeyCredential(
process.env.AZURE_STORAGE_ACCOUNT_NAME,
process.env.AZURE_STORAGE_ACCOUNT_KEY
);
const pipeline = azureBlob.newPipeline(sharedKeyCredential);
const blobServiceClient = new azureBlob.BlobServiceClient(
`https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net`,
pipeline
);
export default function renameBlob(blobURL) {
const blobName = blobURL.replace(containerURL, "");
const containerClient = blobServiceClient.getContainerClient(container);
const tempBlob = containerClient.getBlobClient(blobName);
const newBlob = containerClient.getBlobClient(removePrefix(blobName));
newBlob.syncCopyFromURL(tempBlob.url);
return newBlob.url;
}
function removePrefix(string) {
const newString = string.replace("TEMP_", "");
return newString;
}

View File

@@ -3,7 +3,7 @@ import Dish from "../models/dish.js";
import User from "../models/users.js";
import mongoose from "mongoose";
import sanitizer from "string-sanitizer";
import renameBlob from "./renameBlob.js";
import { renameBlob } from "./azureServices.js";
import jwt from "jsonwebtoken";
import bcrypt from "bcrypt";
import * as config from "../config/index.js";