Skip to content

Commit

Permalink
improved test to include check of config profile with repo
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman committed Dec 10, 2024
1 parent 50463d5 commit 86b7d03
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
55 changes: 55 additions & 0 deletions testdata/configprofile/configProfileWithRepoExample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"profile_name": "default-profile-with-repo",
"repository": {
"id": "my-repo-id-123",
"name": "my-repo",
"url": "https://github.com/myUser/my-repo.git",
"provider": "github"
},
"frogbot_config": {
"email_author": "[email protected]",
"aggregate_fixes": true,
"avoid_previous_pr_comments_deletion": true,
"branch_name_template": "frogbot-${IMPACTED_PACKAGE}-${BRANCH_NAME_HASH}",
"pr_title_template": "[🐸 Frogbot] Upgrade {IMPACTED_PACKAGE} to {FIX_VERSION}",
"pr_comment_title": "Frogbot notes:",
"commit_message_template": "Upgrade {IMPACTED_PACKAGE} to {FIX_VERSION}",
"show_secrets_as_pr_comment": false
},
"modules": [
{
"module_name": "default-module",
"path_from_root": ".",
"releases_repo": "nuget-remote",
"analyzer_manager_version": "1.8.1",
"additional_paths_for_module": ["lib1", "utils/lib2"],
"exclude_paths": ["**/.git/**", "**/*test*/**", "**/*venv*/**", "**/*node_modules*/**", "**/target/**"],
"scan_config": {
"scan_timeout": 600,
"exclude_pattern": "*.md",
"enable_sca_scan": true,
"enable_contextual_analysis_scan": true,
"sast_scanner_config": {
"enable_sast_scan": true
},
"secrets_scanner_config": {
"enable_secrets_scan": true
},
"iac_scanner_config": {
"enable_iac_scan": true
},
"applications_scanner_config": {
"enable_applications_scan": true
},
"services_scanner_config": {
"enable_services_scan": true
}
},
"protected_branches": ["main", "master"],
"include_exclude_mode": 0,
"include_exclude_pattern": "*test*",
"report_analytics": true
}
],
"is_default": true
}
39 changes: 24 additions & 15 deletions utils/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
configParamsTestFile = filepath.Join("..", "testdata", "config", "frogbot-config-test-params.yml")
configEmptyScanParamsTestFile = filepath.Join("..", "testdata", "config", "frogbot-config-empty-scan.yml")
configProfileFile = filepath.Join("..", "testdata", "configprofile", "configProfileExample.json")
configProfileWithRepoFile = filepath.Join("..", "testdata", "configprofile", "configProfileWithRepoExample.json")
)

func TestExtractParamsFromEnvError(t *testing.T) {
Expand Down Expand Up @@ -700,6 +701,7 @@ func TestGetConfigProfileIfExistsAndValid(t *testing.T) {
profileName string
xrayVersion string
failureExpected bool
profileWithRepo bool
}{
{
name: "Deprecated Server - Valid ConfigProfile",
Expand Down Expand Up @@ -740,6 +742,7 @@ func TestGetConfigProfileIfExistsAndValid(t *testing.T) {
profileName: "",
xrayVersion: services.ConfigProfileByUrlMinXrayVersion,

Check failure on line 743 in utils/params_test.go

View workflow job for this annotation

GitHub Actions / Go-Sec

undefined: services.ConfigProfileByUrlMinXrayVersion
failureExpected: false,
profileWithRepo: true,
},
{
name: "Profile by Name - Non existing profile name",
Expand All @@ -766,24 +769,30 @@ func TestGetConfigProfileIfExistsAndValid(t *testing.T) {
defer mockServer.Close()

configProfile, err := getConfigProfileIfExistsAndValid(testcase.xrayVersion, services.ConfigProfileMinXscVersion, serverDetails)
if testcase.useProfile {
if testcase.failureExpected {
assert.Error(t, err)
} else {
require.NotNil(t, configProfile)
assert.NoError(t, err)
var configProfileContentForComparison []byte
configProfileContentForComparison, err = os.ReadFile(configProfileFile)
assert.NoError(t, err)
var configProfileFromFile services.ConfigProfile
err = json.Unmarshal(configProfileContentForComparison, &configProfileFromFile)
assert.NoError(t, err)
assert.Equal(t, configProfileFromFile, *configProfile)
}
} else {

if !testcase.useProfile {
assert.Nil(t, configProfile)
assert.Nil(t, err)
return
}
if testcase.failureExpected {
assert.Error(t, err)
return
}

require.NotNil(t, configProfile)
assert.NoError(t, err)
var configProfileContentForComparison []byte
if testcase.profileWithRepo {
configProfileContentForComparison, err = os.ReadFile(configProfileWithRepoFile)
} else {
configProfileContentForComparison, err = os.ReadFile(configProfileFile)
}
assert.NoError(t, err)
var configProfileFromFile services.ConfigProfile
err = json.Unmarshal(configProfileContentForComparison, &configProfileFromFile)
assert.NoError(t, err)
assert.Equal(t, configProfileFromFile, *configProfile)
})
}
}
2 changes: 1 addition & 1 deletion utils/testsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func CreateXscMockServerForConfigProfile(t *testing.T, xrayVersion string) (mock
// Endpoint to profile by URL
case strings.Contains(r.RequestURI, "/xsc/profile_repos") && isXrayAfterXscMigration:
assert.Equal(t, http.MethodPost, r.Method)
content, err := os.ReadFile("../testdata/configprofile/configProfileExample.json")
content, err := os.ReadFile("../testdata/configprofile/configProfileWithRepoExample.json")
assert.NoError(t, err)
w.WriteHeader(http.StatusOK)
_, err = w.Write(content)
Expand Down

0 comments on commit 86b7d03

Please sign in to comment.