From 661d2ef0c701ad0a8404506e3ad63e7ebda9402c Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Wed, 14 Oct 2020 20:02:05 +0300 Subject: [PATCH] do not upload photos twice --- frontend/package.json | 2 +- src/routes/photos.ts | 5 +++ src/tests/integration/photos.test.ts | 59 ++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/frontend/package.json b/frontend/package.json index ae494bb..203a83b 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -3,7 +3,7 @@ "scripts": { "start": "parcel serve src/index.html", "build": "parcel build src/index.html", - "lint": "eslint ./src/** --ext .js,.jsx,.ts,.tsx && tsc --noEmit", + "lint": "eslint ./src/** --ext .js,.jsx,.ts,.tsx", "lint-fix": "eslint ./src/** --ext .js,.jsx,.ts,.tsx --fix", "test": "jest" }, diff --git a/src/routes/photos.ts b/src/routes/photos.ts index 29fd96d..805a149 100644 --- a/src/routes/photos.ts +++ b/src/routes/photos.ts @@ -69,6 +69,11 @@ photosRouter.post("/photos/upload/:id", async (ctx) => { return; } + if (await photo.isUploaded()) { + ctx.throw(400, "Already uploaded"); + return; + } + if (ctx.request.files) { const files = ctx.request.files; if (Object.keys(files).length > 1) { diff --git a/src/tests/integration/photos.test.ts b/src/tests/integration/photos.test.ts index 4cf55e3..99f7081 100644 --- a/src/tests/integration/photos.test.ts +++ b/src/tests/integration/photos.test.ts @@ -140,6 +140,65 @@ describe("photos", function () { ); }); + it("should not upload a photo twice", async function () { + const response = await request(callback) + .post("/photos/new") + .set({ + Authorization: `Bearer ${seed.user1.toJWT()}`, + "Content-Type": "application/json", + }) + .send({ + hash: dogHash, + size: dogSize, + format: dogFormat, + } as IPhotosNewPostBody) + .expect(200); + + expect(response.body.error).to.be.false; + + const photo = response.body.data as IPhotoJSON; + + expect(photo.hash).to.be.equal(dogHash); + const dbPhoto = await Photo.findOneOrFail({ + id: photo.id, + user: seed.user1.id as any, + }); + expect(dbPhoto.hash).to.be.equal(dogHash); + + expect(await dbPhoto.isUploaded()).to.be.equal(false); + + await request(callback) + .post(`/photos/upload/${photo.id}`) + .set({ + Authorization: `Bearer ${seed.user1.toJWT()}`, + "Content-Type": "application/json", + }) + .attach("photo", dogPath) + .expect(200); + + expect(await dbPhoto.isUploaded()).to.be.equal(true); + + await request(callback) + .post(`/photos/upload/${photo.id}`) + .set({ + Authorization: `Bearer ${seed.user1.toJWT()}`, + "Content-Type": "application/json", + }) + .attach("photo", dogPath) + .expect(400); + + const showResp = await request(callback) + .get(`/photos/showByID/${photo.id}`) + .set({ + Authorization: `Bearer ${seed.user1.toJWT()}`, + }) + .expect(200); + + expect(parseInt(showResp.header["content-length"])).to.equal( + dogFileSize, + ); + }); + it("should not upload a wrong photo", async function () { const response = await request(callback) .post("/photos/new")