Skip to content

Commit

Permalink
Extend test coverage and make scan not fail due to billing plan
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked committed Apr 23, 2024
1 parent b4045cc commit 887c4df
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
11 changes: 6 additions & 5 deletions internal/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ func (billingPlanError BillingPlanError) Error() string {
func (dScanner *DebrickedScanner) scanFingerprint(options DebrickedOptions) error {
if options.Fingerprint {
if !dScanner.IsEnterpriseCustomer() {
return BillingPlanError{errorMessage: "Could not validate Enterprise billing plan, which is a requirement for fingerprinting and manifestless matching."}
fmt.Printf(
"%s Could not validate enterprise billing plan, which is a requirement for fingerprinting and manifestless matching.",
color.YellowString("⚠️"),
)

return nil
}
fingerprints, err := dScanner.fingerprint.FingerprintFiles(
options.Path, file.DefaultExclusionsFingerprint(), false, options.MinFingerprintContentLength,
Expand All @@ -196,21 +201,18 @@ type BillingPlan struct {
func (dScanner *DebrickedScanner) IsEnterpriseCustomer() bool {
res, err := (*dScanner.client).Get(enterpriseCheckUri, "application/json")
if err != nil {
fmt.Printf("%s Unable to get billing plan from the server. Defaulting to non-enterprise plan.\n", color.YellowString("⚠️"))

return false
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
fmt.Printf("%s Unable to get billing plan from the server. Defaulting to non-enterprise plan.\n", color.YellowString("⚠️"))

return false
}

billingPlanJSON, err := io.ReadAll(res.Body)
if err != nil {
fmt.Printf("%s Unable to get billing plan correctly formatted from the server. Defaulting to non-enterprise plan.\n", color.YellowString("⚠️"))

return false
}
Expand All @@ -219,7 +221,6 @@ func (dScanner *DebrickedScanner) IsEnterpriseCustomer() bool {

err = json.Unmarshal(billingPlanJSON, &billingPlan)
if err != nil {
fmt.Printf("%s Unable to get billing plan correctly formatted from the server. Defaulting to non-enterprise plan.\n", color.YellowString("⚠️"))

return false
}
Expand Down
72 changes: 66 additions & 6 deletions internal/scan/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,9 +668,54 @@ func TestScanWithFingerprint(t *testing.T) {
assert.Contains(t, cwd, path)
}

func TestScanWithCallgraph(t *testing.T) {
if runtime.GOOS == windowsOS {
t.Skipf("TestScan is skipped due to Windows env")
}
clientMock := testdata.NewDebClientMock()
addMockedFormatsResponse(clientMock, "yarn\\.lock")
addMockedFileUploadResponse(clientMock)
addMockedFinishResponse(clientMock, http.StatusNoContent)
addMockedStatusResponse(clientMock, http.StatusOK, 100)

generatorMock := callgraphTestdata.GeneratorMock{}

scanner := makeScanner(clientMock, nil, &generatorMock)
scanner.fingerprint = fingerprint.NewFingerprinter()

cwd, _ := os.Getwd()
defer resetWd(t, cwd)

path := testdataNpm
repositoryName := path
commitName := "testdata/npm-commit-callgraph"
opts := DebrickedOptions{
Path: path,
Resolve: false,
Fingerprint: false,
CallGraph: true,
Exclusions: nil,
RepositoryName: repositoryName,
CommitName: commitName,
BranchName: "",
CommitAuthor: "",
RepositoryUrl: "",
IntegrationName: "",
}
err := scanner.Scan(opts)
assert.ErrorContains(t, err, "failed to find dependency files")
cwd, _ = os.Getwd()
assert.Contains(t, cwd, path)
}

func TestScanFingerprintWithoutEnterprise(t *testing.T) {
clientMock := testdata.NewDebClientMock()
clientMock.SetServiceUp(false)
addMockedFormatsResponse(clientMock, "yarn\\.lock")
addMockedFileUploadResponse(clientMock)
addMockedBillingPlanResponse(clientMock, "free")
addMockedFinishResponse(clientMock, http.StatusNoContent)
addMockedStatusResponse(clientMock, http.StatusOK, 100)
clientMock.SetServiceUp(true)

scanner := makeScanner(clientMock, nil, nil)
path := testdataNpm
Expand All @@ -693,13 +738,12 @@ func TestScanFingerprintWithoutEnterprise(t *testing.T) {
os.Stdout = w

err := scanner.Scan(opts)

assert.NotNil(t, err)
_ = w.Close()
output, _ := io.ReadAll(r)
os.Stdout = rescueStdout

assert.ErrorContains(t, err, "Could not validate Enterprise billing plan")
assert.Contains(t, string(output), "Unable to get billing plan from the server")
assert.Contains(t, string(output), "Could not validate enterprise billing plan")
}

func TestScanWithFingerprintMalformedBilling(t *testing.T) {
Expand Down Expand Up @@ -736,8 +780,16 @@ func TestScanWithFingerprintMalformedBilling(t *testing.T) {
RepositoryUrl: "",
IntegrationName: "",
}
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
err := scanner.Scan(opts)
assert.ErrorContains(t, err, "Could not validate Enterprise billing plan")
assert.Equal(t, err, nil)
_ = w.Close()
output, _ := io.ReadAll(r)
os.Stdout = rescueStdout

assert.Contains(t, string(output), "Could not validate enterprise billing plan")
cwd, _ = os.Getwd()
assert.Contains(t, cwd, path)
}
Expand Down Expand Up @@ -776,8 +828,16 @@ func TestScanWithFingerprintBillingStatusCodeError(t *testing.T) {
RepositoryUrl: "",
IntegrationName: "",
}
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
err := scanner.Scan(opts)
assert.ErrorContains(t, err, "Could not validate Enterprise billing plan")
assert.Equal(t, err, nil)
_ = w.Close()
output, _ := io.ReadAll(r)
os.Stdout = rescueStdout

assert.Contains(t, string(output), "Could not validate enterprise billing plan")
cwd, _ = os.Getwd()
assert.Contains(t, cwd, path)
}
Expand Down

0 comments on commit 887c4df

Please sign in to comment.