|
| 1 | +package snykclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/snyk/cli-extension-dep-graph/internal/depgraph" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestScanResultFact_UnmarshalJSON_DepGraph(t *testing.T) { |
| 13 | + jsonData := `{ |
| 14 | + "type": "depGraph", |
| 15 | + "data": { |
| 16 | + "schemaVersion": "1.2.0", |
| 17 | + "pkgManager": { |
| 18 | + "name": "npm", |
| 19 | + "version": "8.0.0", |
| 20 | + "repositories": [ |
| 21 | + { |
| 22 | + "alias": "npmjs" |
| 23 | + } |
| 24 | + ] |
| 25 | + }, |
| 26 | + "pkgs": [ |
| 27 | + { |
| 28 | + |
| 29 | + "info": { |
| 30 | + "name": "root", |
| 31 | + "version": "1.0.0", |
| 32 | + "purl": "pkg:npm/[email protected]" |
| 33 | + } |
| 34 | + }, |
| 35 | + { |
| 36 | + |
| 37 | + "info": { |
| 38 | + "name": "dep", |
| 39 | + "version": "2.0.0" |
| 40 | + } |
| 41 | + } |
| 42 | + ], |
| 43 | + "graph": { |
| 44 | + "rootNodeId": "root-node", |
| 45 | + "nodes": [ |
| 46 | + { |
| 47 | + "nodeId": "root-node", |
| 48 | + |
| 49 | + "deps": [ |
| 50 | + { |
| 51 | + "nodeId": "dep-node" |
| 52 | + } |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "nodeId": "dep-node", |
| 57 | + |
| 58 | + "deps": [] |
| 59 | + } |
| 60 | + ] |
| 61 | + } |
| 62 | + } |
| 63 | + }` |
| 64 | + |
| 65 | + var fact ScanResultFact |
| 66 | + err := json.Unmarshal([]byte(jsonData), &fact) |
| 67 | + |
| 68 | + require.NoError(t, err) |
| 69 | + assert.Equal(t, "depGraph", fact.Type) |
| 70 | + |
| 71 | + depGraph, ok := fact.Data.(*depgraph.DepGraph) |
| 72 | + require.True(t, ok, "expected fact.Data to be *depgraph.DepGraph") |
| 73 | + require.NotNil(t, depGraph) |
| 74 | + |
| 75 | + assert.Equal(t, "1.2.0", depGraph.SchemaVersion) |
| 76 | + assert.Equal(t, "npm", depGraph.PkgManager.Name) |
| 77 | + assert.Equal(t, "8.0.0", depGraph.PkgManager.Version) |
| 78 | + assert.Len(t, depGraph.PkgManager.Repositories, 1) |
| 79 | + assert.Equal(t, "npmjs", depGraph.PkgManager.Repositories[0].Alias) |
| 80 | + |
| 81 | + require.Len(t, depGraph.Pkgs, 2) |
| 82 | + assert. Equal( t, "[email protected]", depGraph. Pkgs[ 0]. ID) |
| 83 | + assert.Equal(t, "root", depGraph.Pkgs[0].Info.Name) |
| 84 | + assert.Equal(t, "1.0.0", depGraph.Pkgs[0].Info.Version) |
| 85 | + assert. Equal( t, "pkg:npm/[email protected]", depGraph. Pkgs[ 0]. Info. PackageURL) |
| 86 | + assert. Equal( t, "[email protected]", depGraph. Pkgs[ 1]. ID) |
| 87 | + |
| 88 | + assert.Equal(t, "root-node", depGraph.Graph.RootNodeID) |
| 89 | + require.Len(t, depGraph.Graph.Nodes, 2) |
| 90 | + assert.Equal(t, "root-node", depGraph.Graph.Nodes[0].NodeID) |
| 91 | + assert. Equal( t, "[email protected]", depGraph. Graph. Nodes[ 0]. PkgID) |
| 92 | + require.Len(t, depGraph.Graph.Nodes[0].Deps, 1) |
| 93 | + assert.Equal(t, "dep-node", depGraph.Graph.Nodes[0].Deps[0].NodeID) |
| 94 | + assert.Equal(t, "dep-node", depGraph.Graph.Nodes[1].NodeID) |
| 95 | + assert.Empty(t, depGraph.Graph.Nodes[1].Deps) |
| 96 | +} |
| 97 | + |
| 98 | +func TestScanResultFact_UnmarshalJSON_OtherType(t *testing.T) { |
| 99 | + jsonData := `{ |
| 100 | + "type": "vulnerability", |
| 101 | + "data": { |
| 102 | + "severity": "high", |
| 103 | + "title": "Test vulnerability" |
| 104 | + } |
| 105 | + }` |
| 106 | + |
| 107 | + var fact ScanResultFact |
| 108 | + err := json.Unmarshal([]byte(jsonData), &fact) |
| 109 | + |
| 110 | + require.NoError(t, err) |
| 111 | + assert.Equal(t, "vulnerability", fact.Type) |
| 112 | + |
| 113 | + dataMap, ok := fact.Data.(map[string]any) |
| 114 | + require.True(t, ok, "expected fact.Data to be map[string]any") |
| 115 | + assert.Equal(t, "high", dataMap["severity"]) |
| 116 | + assert.Equal(t, "Test vulnerability", dataMap["title"]) |
| 117 | +} |
| 118 | + |
| 119 | +func TestScanResultFact_UnmarshalJSON_MalformedDepGraph(t *testing.T) { |
| 120 | + jsonData := `{ |
| 121 | + "type": "depGraph", |
| 122 | + "data": { |
| 123 | + "schemaVersion": "1.2.0", |
| 124 | + "pkgs": "this should be an array not a string" |
| 125 | + } |
| 126 | + }` |
| 127 | + |
| 128 | + var fact ScanResultFact |
| 129 | + err := json.Unmarshal([]byte(jsonData), &fact) |
| 130 | + |
| 131 | + require.Error(t, err) |
| 132 | + assert.Contains(t, err.Error(), "failed to unmarshal depGraph data") |
| 133 | +} |
| 134 | + |
| 135 | +func TestScanResultFact_UnmarshalJSON_MissingDataField(t *testing.T) { |
| 136 | + tests := []struct { |
| 137 | + name string |
| 138 | + jsonData string |
| 139 | + expectedErrMsg string |
| 140 | + }{ |
| 141 | + { |
| 142 | + name: "missing data field for depGraph", |
| 143 | + jsonData: `{ |
| 144 | + "type": "depGraph" |
| 145 | + }`, |
| 146 | + expectedErrMsg: "failed to unmarshal depGraph data", |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "missing data field for other type", |
| 150 | + jsonData: `{ |
| 151 | + "type": "vulnerability" |
| 152 | + }`, |
| 153 | + expectedErrMsg: "failed to unmarshal fact data", |
| 154 | + }, |
| 155 | + } |
| 156 | + |
| 157 | + for _, tt := range tests { |
| 158 | + t.Run(tt.name, func(t *testing.T) { |
| 159 | + var fact ScanResultFact |
| 160 | + err := json.Unmarshal([]byte(tt.jsonData), &fact) |
| 161 | + |
| 162 | + require.Error(t, err) |
| 163 | + assert.Contains(t, err.Error(), tt.expectedErrMsg) |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
0 commit comments