@@ -3,6 +3,7 @@ package lib
3
3
import (
4
4
"net/http"
5
5
"testing"
6
+ "time"
6
7
7
8
"github.com/snyk/parlay/ecosystems/packages"
8
9
@@ -41,20 +42,18 @@ func TestEnrichSBOM(t *testing.T) {
41
42
42
43
bom = EnrichSBOM (bom )
43
44
44
- /*
45
- components = *bom.Components
46
- component := components[0]
47
- licenses := *component.Licenses
45
+ components = * bom .Components
46
+ component := components [0 ]
47
+ licenses := * component .Licenses
48
48
49
- comp := cdx.LicenseChoice(cdx.LicenseChoice{Expression: "BSD-3-Clause"})
49
+ comp := cdx .LicenseChoice (cdx.LicenseChoice {Expression : "BSD-3-Clause" })
50
50
51
- assert.Equal(t, "description", components[0].Description)
52
- assert.Equal(t, comp, licenses[0])
51
+ assert .Equal (t , "description" , components [0 ].Description )
52
+ assert .Equal (t , comp , licenses [0 ])
53
53
54
- httpmock.GetTotalCallCount()
55
- calls := httpmock.GetCallCountInfo()
56
- assert.Equal(t, len(components), calls[`GET =~^https://packages.ecosyste.ms/api/v1/registries`])
57
- */
54
+ httpmock .GetTotalCallCount ()
55
+ calls := httpmock .GetCallCountInfo ()
56
+ assert .Equal (t , len (components ), calls [`GET =~^https://packages.ecosyste.ms/api/v1/registries` ])
58
57
}
59
58
60
59
func TestEnrichSBOMWithoutLicense (t * testing.T ) {
@@ -202,3 +201,66 @@ func TestEnrichRegistryURLWithNonNullRegistryURL(t *testing.T) {
202
201
func pointerToString (s string ) * string {
203
202
return & s
204
203
}
204
+
205
+ func TestEnrichLatestReleasePublishedAt (t * testing.T ) {
206
+ component := cdx.Component {}
207
+ packageData := packages.Package {
208
+ LatestReleasePublishedAt : nil ,
209
+ }
210
+
211
+ result := enrichLatestReleasePublishedAt (component , packageData )
212
+ assert .Equal (t , component , result )
213
+
214
+ latestReleasePublishedAt := time .Date (2023 , time .May , 1 , 0 , 0 , 0 , 0 , time .UTC )
215
+ packageData .LatestReleasePublishedAt = & latestReleasePublishedAt
216
+ expectedTimestamp := latestReleasePublishedAt .UTC ().Format (time .RFC3339 )
217
+ result = enrichLatestReleasePublishedAt (component , packageData )
218
+
219
+ prop := (* result .Properties )[0 ]
220
+ assert .Equal (t , "ecosystems:latest_release_published_at" , prop .Name )
221
+ assert .Equal (t , expectedTimestamp , prop .Value )
222
+ }
223
+
224
+ func TestEnrichLocation (t * testing.T ) {
225
+ assert := assert .New (t )
226
+
227
+ // Test case 1: packageData.RepoMetadata is nil
228
+ component := cdx.Component {Name : "test" }
229
+ packageData := packages.Package {}
230
+ result := enrichLocation (component , packageData )
231
+ assert .Equal (component , result )
232
+
233
+ // Test case 2: packageData.RepoMetadata is not nil, but "owner_record" is missing
234
+ component = cdx.Component {Name : "test" }
235
+ packageData = packages.Package {RepoMetadata : & map [string ]interface {}{
236
+ "not_owner_record" : map [string ]interface {}{},
237
+ }}
238
+ result = enrichLocation (component , packageData )
239
+ assert .Equal (component , result )
240
+
241
+ // Test case 3: "location" field is missing in "owner_record"
242
+ component = cdx.Component {Name : "test" }
243
+ packageData = packages.Package {RepoMetadata : & map [string ]interface {}{
244
+ "owner_record" : map [string ]interface {}{
245
+ "not_location" : "test" ,
246
+ },
247
+ }}
248
+ result = enrichLocation (component , packageData )
249
+ assert .Equal (component , result )
250
+
251
+ // Test case 4: "location" field is present in "owner_record"
252
+ component = cdx.Component {Name : "test" }
253
+ packageData = packages.Package {RepoMetadata : & map [string ]interface {}{
254
+ "owner_record" : map [string ]interface {}{
255
+ "location" : "test_location" ,
256
+ },
257
+ }}
258
+ expectedComponent := cdx.Component {
259
+ Name : "test" ,
260
+ Properties : & []cdx.Property {
261
+ {Name : "ecosystems:owner_location" , Value : "test_location" },
262
+ },
263
+ }
264
+ result = enrichLocation (component , packageData )
265
+ assert .Equal (expectedComponent , result )
266
+ }
0 commit comments