From 6e7c6ae566337a4fa42eb4c08097e0aff87843ab Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Wed, 14 Oct 2020 21:07:53 +0300 Subject: [PATCH] do not allow uploading same photo twice --- src/entity/Photo.ts | 1 + src/tests/integration/photos.test.ts | 35 +++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/entity/Photo.ts b/src/entity/Photo.ts index ccca1c4..23fc5df 100644 --- a/src/entity/Photo.ts +++ b/src/entity/Photo.ts @@ -43,6 +43,7 @@ export interface IPhotoReqJSON extends IPhotoJSON { } @Entity() +@Index(["hash", "size", "user"], { unique: true }) export class Photo extends BaseEntity { @PrimaryGeneratedColumn() public id: number; diff --git a/src/tests/integration/photos.test.ts b/src/tests/integration/photos.test.ts index 5d8e366..e38aa7f 100644 --- a/src/tests/integration/photos.test.ts +++ b/src/tests/integration/photos.test.ts @@ -212,6 +212,36 @@ describe("photos", function () { ); }); + it("should not create 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 response2 = 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(400); + }); + it("should not upload a photo twice", async function () { const response = await request(callback) .post("/photos/new") @@ -457,7 +487,10 @@ describe("photos", function () { const photos = response.body.data as IPhotoReqJSON[]; - const userPhotos = [seed.dogPhoto.toReqJSON(), seed.catPhoto.toReqJSON()]; + const userPhotos = [ + seed.dogPhoto.toReqJSON(), + seed.catPhoto.toReqJSON(), + ]; expect(photos).to.deep.equal(userPhotos); });