Repository: CVEProject/cve-services
Pull request #342 (2021-03-18 20:58:43)

Including missing update to the corresponding cve id document
Adding update to the cve id collection for the corresponding cve id record. This is a fix for issue #335 .


Patch:
diff --git a/src/controller/cve.controller/cve.controller.js b/src/controller/cve.controller/cve.controller.js
index 01147afd..343f1c5c 100644
--- a/src/controller/cve.controller/cve.controller.js
+++ b/src/controller/cve.controller/cve.controller.js
@@ -42,6 +42,9 @@ async function submitCve (req, res, next) {
       return res.status(400).json(error.cveRecordExists())
     }
 
+    // update the cve id in MongoDB
+    await cveIdRepo.updateByCveId(cveId, { state: state })
+
     // create cve record in MongoDB if it doesn't exist
     await cveRepo.updateByCveId(cveId, newCve, { upsert: true })
 
diff --git a/src/repositories/cveIdRepository.js b/src/repositories/cveIdRepository.js
index 17a76302..d4e9061c 100644
--- a/src/repositories/cveIdRepository.js
+++ b/src/repositories/cveIdRepository.js
@@ -7,8 +7,11 @@ class CveIdRepository extends BaseRepository {
   }
 
   async findOneByCveId (id) {
-    const results = this.collection.findOne().byCveId(id)
-    return results
+    return this.collection.findOne().byCveId(id)
+  }
+
+  async updateByCveId (id, cveId, options = {}) {
+    return this.collection.findOneAndUpdate().byCveId(id).updateOne(cveId).setOptions(options)
   }
 }
 


Test patch:
diff --git a/test/unit-tests/cve/cveTest.js b/test/unit-tests/cve/cveTest.js
index 3f91b335..14ac1bfe 100644
--- a/test/unit-tests/cve/cveTest.js
+++ b/test/unit-tests/cve/cveTest.js
@@ -41,6 +41,10 @@ class MyCveIdNegativeTests {
 
     return null
   }
+
+  async updateByCveId () {
+    return null
+  }
 }
 
 class MyCveNegativeTests {
@@ -58,6 +62,19 @@ class MyCveNegativeTests {
 }
 
 class MyCveIdPositiveTests {
+  constructor () {
+    this.publicDoc = cveFixtures.cvePublic4
+    this.rejectDoc = cveFixtures.cveReject4
+  }
+
+  getCveIdPublic () {
+    return this.publicDoc
+  }
+
+  getCveIdReject () {
+    return this.rejectDoc
+  }
+
   async findOneByCveId (id) {
     if (id === cveIdPublic4) {
       return cveFixtures.cvePublic4
@@ -65,10 +82,20 @@ class MyCveIdPositiveTests {
 
     return cveFixtures.cveReject4
   }
+
+  async updateByCveId (id, cveId) {
+    if (id === cveIdPublic4) {
+      this.publicDoc.state = cveId.state
+    } else {
+      this.rejectDoc.state = cveId.state
+    }
+
+    return null
+  }
 }
 
 class MyCvePositiveTests {
-  async findOneByCveId (id) {
+  async findOneByCveId () {
     return null
   }
 
@@ -207,6 +234,11 @@ describe('Testing the /cve/:id endpoint in Cve Controller', () => {
 
   context('Positive Tests', () => {
     it('State PUBLIC: should return the cve record because the cve record was created', (done) => {
+      const cveIdTestRepo = new MyCveIdPositiveTests()
+      const doc = cveIdTestRepo.getCveIdPublic() // get internal state of cveId document
+      expect(doc).to.have.property('cve_id').and.to.equal(cveIdPublic4)
+      expect(doc).to.have.property('state').and.to.equal(CONSTANTS.CVE_STATES.RESERVED)
+
       chai.request(server)
         .post(`/cve-create-record-positive-tests/${cveIdPublic4}`)
         .set(cveFixtures.secretariatHeader)
@@ -221,11 +253,18 @@ describe('Testing the /cve/:id endpoint in Cve Controller', () => {
           expect(res.body).to.have.property('created').and.to.be.a('object')
           expect(res.body.created).to.have.nested.property('CVE_data_meta.ID').and.to.equal(cveIdPublic4)
           expect(res.body.created).to.have.nested.property('CVE_data_meta.STATE').and.to.equal(CONSTANTS.CVE_STATES.PUBLIC)
+          expect(doc).to.have.property('cve_id').and.to.equal(cveIdPublic4)
+          expect(doc).to.have.property('state').and.to.equal(CONSTANTS.CVE_STATES.PUBLIC)
           done()
         })
     })
 
     it('STATE REJECT: should return the cve record because the cve record was created', (done) => {
+      const cveIdTestRepo = new MyCveIdPositiveTests()
+      const doc = cveIdTestRepo.getCveIdReject() // get internal state of cveId document
+      expect(doc).to.have.property('cve_id').and.to.equal(cveIdReject4)
+      expect(doc).to.have.property('state').and.to.equal(CONSTANTS.CVE_STATES.RESERVED)
+
       chai.request(server)
         .post(`/cve-create-record-positive-tests/${cveIdReject4}`)
         .set(cveFixtures.secretariatHeader)
@@ -240,6 +279,8 @@ describe('Testing the /cve/:id endpoint in Cve Controller', () => {
           expect(res.body).to.have.property('created').and.to.be.a('object')
           expect(res.body.created).to.have.nested.property('CVE_data_meta.ID').and.to.equal(cveIdReject4)
           expect(res.body.created).to.have.nested.property('CVE_data_meta.STATE').and.to.equal(CONSTANTS.CVE_STATES.REJECT)
+          expect(doc).to.have.property('cve_id').and.to.equal(cveIdReject4)
+          expect(doc).to.have.property('state').and.to.equal(CONSTANTS.CVE_STATES.REJECT)
           done()
         })
     })
diff --git a/test/unit-tests/cve/mockObjects.cve.js b/test/unit-tests/cve/mockObjects.cve.js
index cad64a6a..8f0434de 100644
--- a/test/unit-tests/cve/mockObjects.cve.js
+++ b/test/unit-tests/cve/mockObjects.cve.js
@@ -49,7 +49,7 @@ const cveAvailable4 = {
 const cvePublic4 = {
   cve_id: 'CVE-2017-5832',
   cve_year: '2017',
-  state: CONSTANTS.CVE_STATES.PUBLIC,
+  state: CONSTANTS.CVE_STATES.RESERVED,
   owning_cna: secretariatOrg.UUID,
   reserved: Date.now(),
   requested_by: {
@@ -61,7 +61,7 @@ const cvePublic4 = {
 const cveReject4 = {
   cve_id: 'CVE-2017-5835',
   cve_year: '2017',
-  state: CONSTANTS.CVE_STATES.REJECT,
+  state: CONSTANTS.CVE_STATES.RESERVED,
   owning_cna: secretariatOrg.UUID,
   reserved: Date.now(),
   requested_by: {