Skip to content

Commit

Permalink
WIP: fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elsapet committed Aug 23, 2023
1 parent 55e4fe6 commit e751195
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 75 deletions.
12 changes: 6 additions & 6 deletions pkg/report/output/privacy/.snapshots/TestGetOutput
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(*privacy.Report)({
Subjects: ([]privacy.Subject) (len=2) {
(privacy.Subject) {
(*types.Report)({
Subjects: ([]types.Subject) (len=2) {
(types.Subject) {
DataSubject: (string) (len=4) "User",
DataType: (string) (len=13) "Email Address",
DetectionCount: (int) 1,
Expand All @@ -10,7 +10,7 @@
LowRiskFindingCount: (int) 0,
RulesPassedCount: (int) 0
},
(privacy.Subject) {
(types.Subject) {
DataSubject: (string) (len=7) "Unknown",
DataType: (string) (len=7) "Country",
DetectionCount: (int) 1,
Expand All @@ -21,8 +21,8 @@
RulesPassedCount: (int) 1
}
},
ThirdParty: ([]privacy.ThirdParty) (len=1) {
(privacy.ThirdParty) {
ThirdParty: ([]types.ThirdParty) (len=1) {
(types.ThirdParty) {
ThirdParty: (string) (len=6) "Sentry",
DataSubject: (string) (len=4) "User",
DataTypes: ([]string) (len=1) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/report/output/privacy/privacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ type ThirdPartyRuleCounter struct {

const PLACEHOLDER_VALUE = "Unknown"

func BuildCsvString(dataflow *outputtypes.DataFlow, config settings.Config) (*strings.Builder, error) {
func BuildCsvString(reportOutput *outputtypes.ReportOutput, config settings.Config) (*strings.Builder, error) {
csvStr := &strings.Builder{}
csvStr.WriteString("\nSubject,Data Types,Detection Count,Critical Risk Finding,High Risk Finding,Medium Risk Finding,Low Risk Finding,Rules Passed\n")
err := GetOutput(dataflow, config)
err := GetOutput(reportOutput, config)
if err != nil {
return csvStr, err
}

for _, subject := range output.Data.Subjects {
for _, subject := range reportOutput.PrivacyReport.Subjects {
subjectArr := []string{
subject.DataSubject,
subject.DataType,
Expand All @@ -90,7 +90,7 @@ func BuildCsvString(dataflow *outputtypes.DataFlow, config settings.Config) (*st
csvStr.WriteString("\n")
csvStr.WriteString("Third Party,Subject,Data Types,Critical Risk Finding,High Risk Finding,Medium Risk Finding,Low Risk Finding,Rules Passed\n")

for _, thirdParty := range output.Data.ThirdParty {
for _, thirdParty := range reportOutput.PrivacyReport.ThirdParty {
thirdPartyArr := []string{
thirdParty.ThirdParty,
thirdParty.DataSubject,
Expand Down
17 changes: 9 additions & 8 deletions pkg/report/output/privacy/privacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/bradleyjkemp/cupaloy"
"github.com/stretchr/testify/assert"

"github.com/bearer/bearer/pkg/commands/process/settings"
"github.com/bearer/bearer/pkg/flag"
Expand All @@ -24,8 +23,10 @@ func TestBuildCsvString(t *testing.T) {
t.Fatalf("failed to generate config:%s", err)
}

dataflow := dummyDataflow()
stringBuilder, _ := privacy.BuildCsvString(dataflow, config)
output := &outputtypes.ReportOutput{
Dataflow: dummyDataflow(),
}
stringBuilder, _ := privacy.BuildCsvString(output, config)
cupaloy.SnapshotT(t, stringBuilder.String())
}

Expand All @@ -39,14 +40,14 @@ func TestGetOutput(t *testing.T) {
t.Fatalf("failed to generate config:%s", err)
}

dataflow := dummyDataflow()
output, err := privacy.GetOutput(dataflow, config)
if err != nil {
output := &outputtypes.ReportOutput{
Dataflow: dummyDataflow(),
}
if err = privacy.GetOutput(output, config); err != nil {
t.Fatalf("failed to generate privacy output err:%s", err)
}

assert.Equal(t, dataflow, output.Dataflow)
cupaloy.SnapshotT(t, output.Data)
cupaloy.SnapshotT(t, output.PrivacyReport)
}

func generateConfig(reportOptions flag.ReportOptions) (settings.Config, error) {
Expand Down
42 changes: 21 additions & 21 deletions pkg/report/output/security/.snapshots/TestGetOutput
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(map[string][]security.Result) (len=2) {
(string) (len=8) "critical": ([]security.Result) (len=1) {
(security.Result) {
Rule: (*security.Rule)({
(map[string][]types.Finding) (len=2) {
(string) (len=8) "critical": ([]types.Finding) (len=1) {
(types.Finding) {
Rule: (*types.Rule)({
CWEIDs: ([]string) (len=2) {
(string) (len=3) "209",
(string) (len=3) "532"
Expand All @@ -14,7 +14,7 @@
LineNumber: (int) 1,
FullFilename: (string) "",
Filename: (string) (len=20) "pkg/datatype_leak.rb",
DataType: (*security.DataType)({
DataType: (*types.DataType)({
CategoryUUID: (string) (len=36) "35b94efa-9b67-49b2-abb9-29b6a759a030",
Name: (string) (len=14) "Biometric Data"
}),
Expand All @@ -23,21 +23,21 @@
(string) (len=13) "Personal Data",
(string) (len=25) "Personal Data (Sensitive)"
},
Source: (security.Source) {
Location: (*security.Location)({
Source: (types.Source) {
Location: (*types.Location)({
Start: (int) 1,
End: (int) 0,
Column: (security.Column) {
Column: (types.Column) {
Start: (int) 0,
End: (int) 0
}
})
},
Sink: (security.Sink) {
Location: (*security.Location)({
Sink: (types.Sink) {
Location: (*types.Location)({
Start: (int) 1,
End: (int) 2,
Column: (security.Column) {
Column: (types.Column) {
Start: (int) 10,
End: (int) 28
}
Expand All @@ -54,9 +54,9 @@
}
}
},
(string) (len=4) "high": ([]security.Result) (len=1) {
(security.Result) {
Rule: (*security.Rule)({
(string) (len=4) "high": ([]types.Finding) (len=1) {
(types.Finding) {
Rule: (*types.Rule)({
CWEIDs: ([]string) (len=1) {
(string) (len=3) "295"
},
Expand All @@ -68,26 +68,26 @@
LineNumber: (int) 2,
FullFilename: (string) "",
Filename: (string) (len=21) "config/application.rb",
DataType: (*security.DataType)(<nil>),
DataType: (*types.DataType)(<nil>),
CategoryGroups: ([]string) (len=2) {
(string) (len=3) "PII",
(string) (len=13) "Personal Data"
},
Source: (security.Source) {
Location: (*security.Location)({
Source: (types.Source) {
Location: (*types.Location)({
Start: (int) 2,
End: (int) 0,
Column: (security.Column) {
Column: (types.Column) {
Start: (int) 0,
End: (int) 0
}
})
},
Sink: (security.Sink) {
Location: (*security.Location)({
Sink: (types.Sink) {
Location: (*types.Location)({
Start: (int) 2,
End: (int) 2,
Column: (security.Column) {
Column: (types.Column) {
Start: (int) 10,
End: (int) 28
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/report/output/security/.snapshots/TestTestGetOutputWithSeverity
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(map[string][]security.Result) (len=1) {
(string) (len=8) "critical": ([]security.Result) (len=1) {
(security.Result) {
Rule: (*security.Rule)({
(map[string][]types.Finding) (len=1) {
(string) (len=8) "critical": ([]types.Finding) (len=1) {
(types.Finding) {
Rule: (*types.Rule)({
CWEIDs: ([]string) (len=2) {
(string) (len=3) "209",
(string) (len=3) "532"
Expand All @@ -14,7 +14,7 @@
LineNumber: (int) 1,
FullFilename: (string) "",
Filename: (string) (len=20) "pkg/datatype_leak.rb",
DataType: (*security.DataType)({
DataType: (*types.DataType)({
CategoryUUID: (string) (len=36) "35b94efa-9b67-49b2-abb9-29b6a759a030",
Name: (string) (len=14) "Biometric Data"
}),
Expand All @@ -23,21 +23,21 @@
(string) (len=13) "Personal Data",
(string) (len=25) "Personal Data (Sensitive)"
},
Source: (security.Source) {
Location: (*security.Location)({
Source: (types.Source) {
Location: (*types.Location)({
Start: (int) 1,
End: (int) 0,
Column: (security.Column) {
Column: (types.Column) {
Start: (int) 0,
End: (int) 0
}
})
},
Sink: (security.Sink) {
Location: (*security.Location)({
Sink: (types.Sink) {
Location: (*types.Location)({
Start: (int) 1,
End: (int) 2,
Column: (security.Column) {
Column: (types.Column) {
Start: (int) 10,
End: (int) 28
}
Expand Down
40 changes: 18 additions & 22 deletions pkg/report/output/security/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
dataflowtypes "github.com/bearer/bearer/pkg/report/output/dataflow/types"
"github.com/bearer/bearer/pkg/report/output/security"
"github.com/bearer/bearer/pkg/report/output/types"
outputtypes "github.com/bearer/bearer/pkg/report/output/types"
)

func TestBuildReportString(t *testing.T) {
Expand Down Expand Up @@ -52,9 +53,8 @@ func TestBuildReportString(t *testing.T) {
t.Fatalf("failed to generate config:%s", err)
}

dataflowOutput := dummyDataflowOutput()
output, err := security.GetOutput(dataflowOutput, config, nil)
if err != nil {
output := dummyDataflowOutput()
if err := security.GetOutput(output, config, nil); err != nil {
t.Fatalf("failed to generate security output err:%s", err)
}

Expand All @@ -68,7 +68,7 @@ func TestBuildReportString(t *testing.T) {
MaxPathLength: 0,
}

stringBuilder := security.BuildReportString(config, output, &dummyGoclocResult)
stringBuilder := security.BuildReportString(output, config, &dummyGoclocResult)
cupaloy.SnapshotT(t, stringBuilder.String())
}

Expand All @@ -91,9 +91,8 @@ func TestNoRulesBuildReportString(t *testing.T) {
t.Fatalf("failed to generate config:%s", err)
}

dataflowOutput := dummyDataflowOutput()
output, err := security.GetOutput(dataflowOutput, config, nil)
if err != nil {
output := dummyDataflowOutput()
if err := security.GetOutput(output, config, nil); err != nil {
t.Fatalf("failed to generate security output err:%s", err)
}

Expand All @@ -107,7 +106,7 @@ func TestNoRulesBuildReportString(t *testing.T) {
MaxPathLength: 0,
}

stringBuilder := security.BuildReportString(config, output, &dummyGoclocResult)
stringBuilder := security.BuildReportString(output, config, &dummyGoclocResult)
cupaloy.SnapshotT(t, stringBuilder.String())
}

Expand All @@ -127,15 +126,14 @@ func TestGetOutput(t *testing.T) {
t.Fatalf("failed to generate config:%s", err)
}

dataflowOutput := dummyDataflowOutput()
output, err := security.GetOutput(dataflowOutput, config, nil)
if err != nil {
output := dummyDataflowOutput()
if err = security.GetOutput(output, config, nil); err != nil {
t.Fatalf("failed to generate security output err:%s", err)
}

assert.Equal(t, dataflowOutput.Dataflow, dataflowOutput.Dataflow)
assert.Equal(t, dataflowOutput.Files, dataflowOutput.Files)
cupaloy.SnapshotT(t, output.Data)
assert.Equal(t, output.Dataflow, output.Dataflow)
assert.Equal(t, output.Files, output.Files)
cupaloy.SnapshotT(t, output.Findings)
}

func TestTestGetOutputWithSeverity(t *testing.T) {
Expand All @@ -154,13 +152,12 @@ func TestTestGetOutputWithSeverity(t *testing.T) {
t.Fatalf("failed to generate config:%s", err)
}

dataflowOutput := dummyDataflowOutput()
output, err := security.GetOutput(dataflowOutput, config, nil)
if err != nil {
t.Fatalf("failed to generate security output err: %s", err)
output := dummyDataflowOutput()
if err = security.GetOutput(output, config, nil); err != nil {
t.Fatalf("failed to generate security output err:%s", err)
}

cupaloy.SnapshotT(t, output.Data)
cupaloy.SnapshotT(t, output.Findings)
}

func TestCalculateSeverity(t *testing.T) {
Expand Down Expand Up @@ -189,7 +186,7 @@ func generateConfig(reportOptions flag.ReportOptions) (settings.Config, error) {
return settings.FromOptions(opts, []string{"ruby"})
}

func dummyDataflowOutput() *types.Output[*types.DataFlow] {
func dummyDataflowOutput() *outputtypes.ReportOutput {
subject := "User"
lowRisk := dataflowtypes.RiskDetector{
DetectorID: "ruby_lang_ssl_verification",
Expand Down Expand Up @@ -276,8 +273,7 @@ func dummyDataflowOutput() *types.Output[*types.DataFlow] {
Components: []dataflowtypes.Component{},
}

return &types.Output[*types.DataFlow]{
Data: dataflow,
return &outputtypes.ReportOutput{
Dataflow: dataflow,
Files: []string{"config/application.rb", "pkg/datatype_leak.rb", "app/model/user.rb"},
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/report/output/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ type ReportOutput struct {
Dataflow *DataFlow
Findings map[string][]securitytypes.Finding
Meta saastypes.Meta

PrivacyReport *privacytypes.Report
Stats *statstypes.Stats
PrivacyReport *privacytypes.Report
Stats *statstypes.Stats
}

type DataFlow struct {
Expand Down

0 comments on commit e751195

Please sign in to comment.