diff --git a/src/published-data/published-data.v4.controller.ts b/src/published-data/published-data.v4.controller.ts index a8ea40d9d..b5df4f5e3 100644 --- a/src/published-data/published-data.v4.controller.ts +++ b/src/published-data/published-data.v4.controller.ts @@ -101,6 +101,7 @@ export class PublishedDataV4Controller { async create( @Body() createPublishedDataDto: CreatePublishedDataV4Dto, ): Promise { + await this.validatorService.validate(createPublishedDataDto); return this.publishedDataService.create(createPublishedDataDto); } @@ -390,6 +391,7 @@ export class PublishedDataV4Controller { } } + await this.validatorService.validate(updatePublishedDataDto); return this.publishedDataService.update( { doi: id }, updatePublishedDataDto, @@ -715,6 +717,7 @@ export class PublishedDataV4Controller { ); } + await this.validatorService.validate(data); await this.publishedDataService.update({ doi: id }, data); return returnValue; diff --git a/test/PublishedDataV4.js b/test/PublishedDataV4.js index 12c27d3f6..dbec73f4f 100644 --- a/test/PublishedDataV4.js +++ b/test/PublishedDataV4.js @@ -467,4 +467,65 @@ describe("1600: PublishedDataV4: Test of access to published data v4 endpoints", .expect(TestData.NotFoundStatusCode) .expect("Content-Type", /json/); }); + + describe("Ajv extensions are executed on create/save", () => { + const strippedPublishedData = { ...publishedData, metadata: {} }; + const expectedPublicationYear = new Date().getFullYear(); + let id; + + it("should set 'metadata.publicationYear' on create", () => { + return request(appUrl) + .post("/api/v4/PublishedData") + .send(strippedPublishedData) + .set("Accept", "application/json") + .set({ Authorization: `Bearer ${accessTokenAdminIngestor}` }) + .expect(TestData.EntryCreatedStatusCode) + .expect("Content-Type", /json/) + .then((res) => { + res.body.should.have.property("metadata"); + res.body.metadata.should.have + .property("publicationYear") + .and.equal(expectedPublicationYear); + id = encodeURIComponent(res.body.doi); + }); + }); + + it("should set 'metadata.publicationYear' on patch", () => { + return request(appUrl) + .patch(`/api/v4/PublishedData/${id}`) + .send(strippedPublishedData) + .set("Accept", "application/json") + .set({ Authorization: `Bearer ${accessTokenAdminIngestor}` }) + .expect(TestData.EntryValidStatusCode) + .expect("Content-Type", /json/) + .then((res) => { + res.body.should.have.property("metadata"); + res.body.metadata.should.have + .property("publicationYear") + .and.equal(expectedPublicationYear); + }); + }); + + it("should set 'metadata.publicationYear' on resync", async () => { + await request(appUrl) + .post(`/api/v4/PublishedData/${id}/resync`) + .send(strippedPublishedData) + .set("Accept", "application/json") + .set({ Authorization: `Bearer ${accessTokenAdminIngestor}` }) + .expect(TestData.EntryCreatedStatusCode); + + return request(appUrl) + .get(`/api/v4/PublishedData/${id}`) + .set("Accept", "application/json") + .set({ Authorization: `Bearer ${accessTokenAdminIngestor}` }) + .expect(TestData.EntryValidStatusCode) + .expect("Content-Type", /json/) + .then((res) => { + res.body.should.have.property("metadata"); + res.body.metadata.should.have + .property("publicationYear") + .and.equal(expectedPublicationYear); + }); + }); + }); });