Skip to content

Commit b8bb908

Browse files
committed
fixes test
1 parent 3e4e4c9 commit b8bb908

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

internal/core/vulndb/scan/scan_integration_test.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,6 @@ func TestScanning(t *testing.T) {
177177
// should be only a single vulnerability
178178
assert.Nil(t, err)
179179
assert.Len(t, vulns, 1)
180-
// mark the vuln as accepted
181-
vulns[0].State = models.VulnStateAccepted
182-
// save it
183-
err = dependencyVulnRepository.Save(nil, &vulns[0])
184-
assert.Nil(t, err)
185-
186180
// create an accepted event inside the database
187181
acceptedEvent := models.NewAcceptedEvent(vulns[0].ID, vulns[0].GetType(), "abc", "accepting the vulnerability")
188182
err = dependencyVulnRepository.ApplyAndSave(nil, &vulns[0], &acceptedEvent)
@@ -219,12 +213,27 @@ func TestScanning(t *testing.T) {
219213
newVuln = v
220214
}
221215
}
216+
222217
assert.NotEmpty(t, newVuln.Events)
223218
lastTwoEvents := newVuln.Events[len(newVuln.Events)-2:]
224-
assert.Equal(t, models.EventTypeAccepted, lastTwoEvents[0].Type)
225-
assert.Equal(t, "accepting the vulnerability", *lastTwoEvents[0].Justification)
226-
assert.Equal(t, "main", *lastTwoEvents[0].OriginalAssetVersionName)
227-
assert.Equal(t, models.EventTypeDetectedOnAnotherBranch, lastTwoEvents[1].Type)
219+
220+
// we can not really rely on the created_at since the events are created in the same second
221+
// nevertheless - one has to be the accepted event and the other the detected on different branch event
222+
var accEvent models.VulnEvent
223+
var detectedOnAnotherBranchEvent models.VulnEvent
224+
for _, ev := range lastTwoEvents {
225+
if ev.Type == models.EventTypeAccepted {
226+
accEvent = ev
227+
} else {
228+
detectedOnAnotherBranchEvent = ev
229+
}
230+
}
231+
232+
assert.NotEmpty(t, accEvent)
233+
assert.NotEmpty(t, detectedOnAnotherBranchEvent)
234+
assert.Equal(t, models.EventTypeAccepted, accEvent.Type)
235+
assert.Equal(t, "accepting the vulnerability", *accEvent.Justification)
236+
assert.Equal(t, "main", *accEvent.OriginalAssetVersionName)
228237
})
229238
}
230239

internal/database/repositories/vulnerability_repository.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ func (r *VulnerabilityRepository[T]) GetByAssetID(
112112

113113
var vulns = []T{}
114114
// get all vulnerabilities of the asset
115-
if err := r.Repository.GetDB(tx).Where("asset_id = ?", assetID).Preload("Events").Find(&vulns).Error; err != nil {
115+
if err := r.Repository.GetDB(tx).Where("asset_id = ?", assetID).Preload("Events", func(db *gorm.DB) *gorm.DB {
116+
return db.Order("vuln_events.created_at ASC")
117+
}).Find(&vulns).Error; err != nil {
116118
return nil, err
117119
}
118120
return vulns, nil

0 commit comments

Comments
 (0)