Repository: OWASP/threat-dragon (js)

Issue:
Threat IDs are updating after edit
**Describe the bug**
Threat IDs of threats are being updated to the last threat ID used after the threat is modified.

**Expected behaviour**
Thread ID shouldn't change.

**Environment**
 - Version: 2.0
 - Platform: AppImage
 - OS: RHEL 8.6
 - Browser: n/a

**To Reproduce**

1. Create 2 threats in a TM
2. Update the first threat that was created
3. See the threat ID has been changed to match the second threat



Pull request:
fix bug for threat id number
**Summary**
This fixes the bug where the threat id number is incorrectly overwritten when editing the threat

**Description for the changelog**
fix bug for threat id number

**Other info**
closes #628 

Patch:
diff --git a/td.vue/src/components/Graph.vue b/td.vue/src/components/Graph.vue
index 9a6daf8d..c77ef795 100644
--- a/td.vue/src/components/Graph.vue
+++ b/td.vue/src/components/Graph.vue
@@ -79,7 +79,7 @@ export default {
             console.debug('diagram ID: ' + this.diagram.id);
         },
         threatSelected(threatId) {
-            this.$refs.threatEditDialog.showModal(threatId);
+            this.$refs.threatEditDialog.editThreat(threatId);
         },
         saved() {
             const updated = Object.assign({}, this.diagram);
diff --git a/td.vue/src/components/ThreatEditDialog.vue b/td.vue/src/components/ThreatEditDialog.vue
index 4756b947..9f0f9494 100644
--- a/td.vue/src/components/ThreatEditDialog.vue
+++ b/td.vue/src/components/ThreatEditDialog.vue
@@ -213,7 +213,7 @@ export default {
         };
     },
     methods: {
-        showModal(threatId) {
+        editThreat(threatId) {
             this.threat = this.cellRef.data.threats.find(x => x.id === threatId);
             if (!this.threat) {
                 // this should never happen with a valid threatId
@@ -223,13 +223,16 @@ export default {
             }
             this.newThreat = this.threat.new;
 
-            if (this.threat.new) {
-                // provide a threat number that is unique project wide
+            if (this.threat.new === true) {
+                // provide a new threat number that is unique project wide
                 if (this.threatTop) {
                     this.number = this.threatTop + 1;
                 } else {
                     this.number = 1;
                 }
+                this.$store.dispatch(THREATMODEL_UPDATE, { threatTop: this.number });
+            } else {
+                this.number = this.threat.number;
             }
         },
         updateThreat() {
@@ -250,7 +253,6 @@ export default {
                 this.$store.dispatch(CELL_DATA_UPDATED, this.cellRef.data);
                 dataChanged.updateStyleAttrs(this.cellRef);
             }
-            this.$store.dispatch(THREATMODEL_UPDATE, { threatTop: this.number });
             this.hideModal();
         },
         deleteThreat() {
diff --git a/td.vue/src/store/modules/threatmodel.js b/td.vue/src/store/modules/threatmodel.js
index 04d6d020..5cb6896e 100644
--- a/td.vue/src/store/modules/threatmodel.js
+++ b/td.vue/src/store/modules/threatmodel.js
@@ -179,6 +179,7 @@ const mutations = {
         if (update.fileName) {
             Vue.set(state, 'fileName', update.fileName);
         }
+        console.debug('Threatmodel update: ' + JSON.stringify(update));
     }
 };
 


Test patch:
diff --git a/td.vue/tests/unit/components/graph.spec.js b/td.vue/tests/unit/components/graph.spec.js
index 8db27034..86a087a8 100644
--- a/td.vue/tests/unit/components/graph.spec.js
+++ b/td.vue/tests/unit/components/graph.spec.js
@@ -32,7 +32,7 @@ describe('components/GraphButtons.vue', () => {
         threatEditStub = {
             render: jest.fn(),
             methods: {
-                showModal: jest.fn()
+                editThreat: jest.fn()
             }
         };
 
@@ -112,7 +112,7 @@ describe('components/GraphButtons.vue', () => {
 
     it('shows the threat edit modal dialog', () => {
         wrapper.vm.threatSelected('asdf');
-        expect(threatEditStub.methods.showModal).toHaveBeenCalledWith('asdf');
+        expect(threatEditStub.methods.editThreat).toHaveBeenCalledWith('asdf');
     });
 
     it('saves the threat model diagram', () => {
diff --git a/td.vue/tests/unit/components/threatEditDialog.spec.js b/td.vue/tests/unit/components/threatEditDialog.spec.js
index fddb7a3b..a08d482f 100644
--- a/td.vue/tests/unit/components/threatEditDialog.spec.js
+++ b/td.vue/tests/unit/components/threatEditDialog.spec.js
@@ -50,7 +50,7 @@ describe('components/ThreatEditDialog.vue', () => {
             modal = wrapper.findComponent(BModal);
             wrapper.vm.$refs.editModal.show = jest.fn();
             wrapper.vm.$refs.editModal.hide = jest.fn();
-            wrapper.vm.showModal(getThreatData().id);
+            wrapper.vm.editThreat(getThreatData().id);
         });
 
         it('has a bootstrap modal', () => {
@@ -150,7 +150,7 @@ describe('components/ThreatEditDialog.vue', () => {
                 dataChanged.updateStyleAttrs = jest.fn();
                 mockStore.dispatch = jest.fn();
                 wrapper.vm.$refs.editModal.show = jest.fn();
-                await wrapper.vm.showModal(threatId);
+                await wrapper.vm.editThreat(threatId);
                 await wrapper.vm.confirmDelete();
             }); 
 
@@ -179,7 +179,7 @@ describe('components/ThreatEditDialog.vue', () => {
             wrapper.vm.$refs.editModal.hide = jest.fn();
             mockStore.dispatch = jest.fn();
             dataChanged.updateStyleAttrs = jest.fn();
-            wrapper.vm.showModal(threatId);
+            wrapper.vm.editThreat(threatId);
             wrapper.vm.updateThreat();
         });