-
Notifications
You must be signed in to change notification settings - Fork 81
/
javascript_test.go
115 lines (93 loc) · 2.84 KB
/
javascript_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package insider_test
import (
"context"
"net/http"
"testing"
"time"
"github.com/insidersec/insider"
"github.com/insidersec/insider/report"
"github.com/insidersec/insider/testutil"
"github.com/stretchr/testify/assert"
)
type fakeNpm struct {
result insider.AuditResult
}
func (npm fakeNpm) AuditLibraries(insider.PackageJSON) (insider.AuditResult, error) {
return npm.result, nil
}
func TestJavaScriptAnalyze(t *testing.T) {
// Expected base package.json based on testdata/javascript/package.json
expectedPkgJSON := insider.PackageJSON{
Name: "teste",
Version: "0.1.0",
Dependencies: map[string]string{
"express": "^4.17.1",
},
}
mockAuditResult := insider.AuditResult{
Advisories: map[string]insider.Advisory{
"express": {},
},
}
npm := fakeNpm{
result: mockAuditResult,
}
testcases := []struct {
name string
dir string
err bool
pkgJSON insider.PackageJSON
auditResult insider.AuditResult
libraries int
}{
{
name: "Test with package.json",
dir: "testdata/javascript",
err: false,
pkgJSON: expectedPkgJSON,
auditResult: mockAuditResult,
libraries: 1,
},
{
name: "Test without package.json",
dir: "testdata/javascript/foo",
err: false,
pkgJSON: insider.PackageJSON{},
auditResult: insider.AuditResult{},
libraries: 0,
},
}
for _, tt := range testcases {
t.Run(tt.name, func(t *testing.T) {
analyzer := insider.NewJavaScriptAnalyzer(npm, testutil.NewTestLogger(t))
rep, err := analyzer.Analyze(context.Background(), tt.dir)
if tt.err {
assert.NotNil(t, err, "Expected not nil error to analyze javascript")
} else {
assert.Nil(t, err, "Expected nil error to analyze javascript: %v", err)
}
r, ok := rep.(report.Report)
assert.True(t, ok, "Expected type report.Report on return of javascript.Analyze")
assert.Equal(t, tt.libraries, len(r.Libraries), "Expected %d library on report", tt.libraries)
})
}
}
func TestNPMAuditLibraries(t *testing.T) {
fakeResponse := `{"actions":[],"advisories":{"express": {}},"muted":[],"metadata":{"vulnerabilities":{"info":0,"low":0,"moderate":0,"high":0,"critical":0},"dependencies":21,"devDependencies":0,"optionalDependencies":0,"totalDependencies":21}}`
testServer := testutil.NewHttpTestServer([]byte(fakeResponse), http.StatusOK)
defer func() { testServer.Close() }()
npm := insider.NewNPMAdvisory(testServer.URL, "agent", 20*time.Second)
fakePKGJson := insider.PackageJSON{
Name: "teste",
Version: "0.1.0",
Dependencies: map[string]string{
"express": "^4.17.1",
},
}
expectedAdvisories := map[string]insider.Advisory{
"express": {},
}
r, err := npm.AuditLibraries(fakePKGJson)
assert.Nil(t, err, "Expected nil error to audit libraries %v", err)
assert.Equal(t, r.Advisories, expectedAdvisories)
}