do not allow uploading same photo twice

This commit is contained in:
2020-10-14 21:07:53 +03:00
committed by Stepan Usatiuk
parent c20642dba4
commit 6e7c6ae566
2 changed files with 35 additions and 1 deletions

View File

@@ -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;

View File

@@ -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);
});