@@ -3,7 +3,6 @@ package get
33import (
44 "encoding/json"
55 "fmt"
6- "log"
76 "net/http"
87 "testing"
98
@@ -27,6 +26,19 @@ func (m *mockOnemodelManagerCustomError) GraphqlQuery(_ []byte) ([]byte, error)
2726 return nil , fmt .Errorf ("HTTP %d: Not Found" , http .StatusNotFound )
2827}
2928
29+ type mockOnemodelManagerCustomFallback struct {
30+ calls int
31+ }
32+
33+ func (m * mockOnemodelManagerCustomFallback ) GraphqlQuery (_ []byte ) ([]byte , error ) {
34+ m .calls ++
35+ if m .calls == 1 {
36+ return nil , fmt .Errorf (`{"errors":[{"message":"Cannot query field \"attachments\" on type \"Evidence\"."}]}` )
37+ }
38+ response := `{"data":{"evidence":{"searchEvidence":{"totalCount":1,"edges":[{"cursor":"1","node":{"predicateSlug":"test-slug","downloadPath":"test/path","verified":true,"signingKey":{"alias":"test-alias"},"subject":{"sha256":"test-digest"},"createdBy":"test-user","createdAt":"2024-01-01T00:00:00Z"}}]}}}}`
39+ return []byte (response ), nil
40+ }
41+
3042func validatePredicateEvidence (t * testing.T , result []byte ) {
3143 var output CustomEvidenceOutput
3244 err := json .Unmarshal (result , & output )
@@ -74,22 +86,19 @@ func TestNewGetEvidenceCustom(t *testing.T) {
7486// Test getEvidence method
7587func TestGetCustomEvidence (t * testing.T ) {
7688 tests := []struct {
77- name string
78- onemodelClient onemodel.Manager
79- expectedError bool
80- expectedEvidenceLen int
89+ name string
90+ onemodelClient onemodel.Manager
91+ expectedError bool
8192 }{
8293 {
83- name : "Successful evidence retrieval" ,
84- onemodelClient : & mockOnemodelManagerCustomSuccess {},
85- expectedError : false ,
86- expectedEvidenceLen : 1 ,
94+ name : "Successful evidence retrieval" ,
95+ onemodelClient : & mockOnemodelManagerCustomSuccess {},
96+ expectedError : false ,
8797 },
8898 {
89- name : "Error retrieving evidence" ,
90- onemodelClient : & mockOnemodelManagerCustomError {},
91- expectedError : true ,
92- expectedEvidenceLen : 0 ,
99+ name : "Error retrieving evidence" ,
100+ onemodelClient : & mockOnemodelManagerCustomError {},
101+ expectedError : true ,
93102 },
94103 }
95104
@@ -112,23 +121,20 @@ func TestGetCustomEvidence(t *testing.T) {
112121 assert .Empty (t , evidence )
113122 } else {
114123 assert .NoError (t , err )
115- assert .NotEmpty (t , evidence )
116-
117- // Additional check on the number of edges in the result
118- var data map [string ]any
119- if err := json .Unmarshal (evidence , & data ); err == nil {
120- if evidenceData , ok := data ["data" ].(map [string ]any ); ok {
121- if evidenceNode , ok := evidenceData ["evidence" ].(map [string ]any ); ok {
122- if searchEvidence , ok := evidenceNode ["searchEvidence" ].(map [string ]any ); ok {
123- edgesInterface , ok := searchEvidence ["edges" ].([]any )
124- if ! ok {
125- log .Fatalf ("Type assertion failed: expected []any" )
126- }
127- edges := edgesInterface
128- assert .Equal (t , tt .expectedEvidenceLen , len (edges ))
129- }
130- }
131- }
124+
125+ var output CustomEvidenceOutput
126+ assert .NoError (t , json .Unmarshal (evidence , & output ))
127+ assert .Equal (t , SchemaVersion , output .SchemaVersion )
128+ assert .Equal (t , ArtifactType , output .Type )
129+ assert .Equal (t , "myRepo/my/path" , output .Result .RepoPath )
130+
131+ if assert .Len (t , output .Result .Evidence , 1 ) {
132+ entry := output .Result .Evidence [0 ]
133+ assert .Equal (t , "test-slug" , entry .PredicateSlug )
134+ assert .Equal (t , "test/path" , entry .DownloadPath )
135+ assert .Equal (t , true , entry .Verified )
136+ assert .Equal (t , "test-user" , entry .CreatedBy )
137+ assert .Equal (t , "2024-01-01T00:00:00Z" , entry .CreatedAt )
132138 }
133139 }
134140 })
@@ -311,3 +317,91 @@ func TestTransformGraphQLOutput(t *testing.T) {
311317 })
312318 }
313319}
320+
321+ func TestGetCustomEvidence_FallbackToLegacyQueryWhenAttachmentsUnsupported (t * testing.T ) {
322+ manager := & mockOnemodelManagerCustomFallback {}
323+ g := & getEvidenceCustom {
324+ subjectRepoPath : "myRepo/my/path" ,
325+ getEvidenceBase : getEvidenceBase {
326+ includePredicate : true ,
327+ },
328+ }
329+
330+ evidence , err := g .getEvidence (manager )
331+ assert .NoError (t , err )
332+ assert .Equal (t , 2 , manager .calls , "should have made 2 GraphQL calls (initial + fallback)" )
333+
334+ var output CustomEvidenceOutput
335+ assert .NoError (t , json .Unmarshal (evidence , & output ))
336+ assert .Equal (t , SchemaVersion , output .SchemaVersion )
337+ assert .Equal (t , ArtifactType , output .Type )
338+
339+ if assert .Len (t , output .Result .Evidence , 1 ) {
340+ entry := output .Result .Evidence [0 ]
341+ assert .Equal (t , "test-slug" , entry .PredicateSlug )
342+ assert .Equal (t , "test/path" , entry .DownloadPath )
343+ assert .Equal (t , true , entry .Verified )
344+ assert .Equal (t , "test-user" , entry .CreatedBy )
345+ }
346+ }
347+
348+ func TestTransformGraphQLOutput_WithAttachments (t * testing.T ) {
349+ g := & getEvidenceCustom {
350+ subjectRepoPath : "test-repo/path/file.txt" ,
351+ getEvidenceBase : getEvidenceBase {
352+ includePredicate : false ,
353+ },
354+ }
355+
356+ input := []byte (`{
357+ "data": {
358+ "evidence": {
359+ "searchEvidence": {
360+ "edges": [{
361+ "node": {
362+ "predicateSlug": "slug",
363+ "downloadPath": "evd/path",
364+ "verified": true,
365+ "subject": {"sha256": "sub-sha"},
366+ "createdBy": "me",
367+ "createdAt": "2026-01-01T00:00:00Z",
368+ "attachments": [{
369+ "name": "a.txt",
370+ "sha256": "abc",
371+ "type": "text/plain",
372+ "downloadPath": "repo/.evidence/att/a.txt"
373+ }]
374+ }
375+ }]
376+ }
377+ }
378+ }
379+ }` )
380+
381+ result , err := g .transformGraphQLOutput (input )
382+ assert .NoError (t , err )
383+
384+ var output CustomEvidenceOutput
385+ assert .NoError (t , json .Unmarshal (result , & output ))
386+
387+ assert .Equal (t , SchemaVersion , output .SchemaVersion )
388+ assert .Equal (t , ArtifactType , output .Type )
389+ assert .Equal (t , "test-repo/path/file.txt" , output .Result .RepoPath )
390+
391+ if assert .Len (t , output .Result .Evidence , 1 ) {
392+ entry := output .Result .Evidence [0 ]
393+ assert .Equal (t , "slug" , entry .PredicateSlug )
394+ assert .Equal (t , "evd/path" , entry .DownloadPath )
395+ assert .Equal (t , true , entry .Verified )
396+ assert .Equal (t , "me" , entry .CreatedBy )
397+ assert .Equal (t , "2026-01-01T00:00:00Z" , entry .CreatedAt )
398+
399+ if assert .Len (t , entry .Attachments , 1 ) {
400+ att := entry .Attachments [0 ]
401+ assert .Equal (t , "a.txt" , att .Name )
402+ assert .Equal (t , "abc" , att .Sha256 )
403+ assert .Equal (t , "text/plain" , att .Type )
404+ assert .Equal (t , "repo/.evidence/att/a.txt" , att .DownloadPath )
405+ }
406+ }
407+ }
0 commit comments