Skip to content

Commit

Permalink
e2e: Create test for custom rules path flag (#793)
Browse files Browse the repository at this point in the history
Signed-off-by: oliveira.felipe <oliveira.felipe@zup.com.br>
  • Loading branch information
oliveirafelipezup authored Dec 7, 2021
1 parent a96b0ae commit 3cde903
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 8 deletions.
72 changes: 64 additions & 8 deletions e2e/commands/start/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
package start_test

import (
"encoding/json"
"fmt"
"os"
"path/filepath"

"github.com/ZupIT/horusec-devkit/pkg/enums/severities"
"github.com/ZupIT/horusec-devkit/pkg/enums/vulnerability"
"github.com/ZupIT/horusec-devkit/pkg/utils/logger/enums"
"github.com/google/uuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"

"github.com/ZupIT/horusec-devkit/pkg/enums/severities"
"github.com/ZupIT/horusec-devkit/pkg/enums/vulnerability"
"github.com/ZupIT/horusec-devkit/pkg/utils/logger/enums"
"github.com/ZupIT/horusec/internal/enums/outputtype"
"github.com/ZupIT/horusec/internal/utils/testutil"
)
Expand Down Expand Up @@ -146,11 +148,8 @@ var _ = Describe("running binary Horusec with start parameter", func() {
var certificateFileWithPath string

BeforeEach(func() {
file, err := os.CreateTemp(os.TempDir(), "*.crt")
if err != nil {
Fail(fmt.Sprintf("error: %v", err))
}
certificateFileWithPath = file.Name()

certificateFileWithPath = testutil.GinkgoCreateTmpFile("*.crt")

flags = map[string]string{
testutil.StartFlagProjectPath: projectPath,
Expand Down Expand Up @@ -463,4 +462,61 @@ var _ = Describe("running binary Horusec with start parameter", func() {
Expect(session.Out.Contents()).To(ContainSubstring("YOUR ANALYSIS HAD FINISHED WITHOUT ANY VULNERABILITY!"))
})
})

When("--custom-path-rules is passed", func() {
customRulesJson := testutil.GinkgoCreateTmpFile("*.json")
writeJsonFile(customRulesJson)

BeforeEach(func() {

flags = map[string]string{
testutil.StartFlagProjectPath: testutil.JavaExample1,
testutil.StartFlagCustomRulesPath: customRulesJson,
}
})

It("Checks if the custom rules path property was set", func() {
Expect(session.Out.Contents()).To(ContainSubstring(`\"custom_rules_path\": \"%s\"`, testutil.NormalizePathToAssertInJSON(customRulesJson)))
Eventually(session.Wait(testutil.AverageTimeoutAnalyzeForExamplesFolder).Out).Should(gbytes.Say(`Language: Java`))
Eventually(session.Wait(testutil.AverageTimeoutAnalyzeForExamplesFolder).Out).Should(gbytes.Say(`Severity: LOW`))
Eventually(session.Wait(testutil.AverageTimeoutAnalyzeForExamplesFolder).Out).Should(gbytes.Say(`Confidence: LOW`))
Eventually(session.Wait(testutil.AverageTimeoutAnalyzeForExamplesFolder).Out).Should(gbytes.Say(`RuleID: HS-JAVA-99999999999`))
Eventually(session.Wait(testutil.AverageTimeoutAnalyzeForExamplesFolder).Out).Should(gbytes.Say(`Details: Teste QA`))
Eventually(session.Wait(testutil.AverageTimeoutAnalyzeForExamplesFolder).Out).Should(gbytes.Say(`Teste de description QA`))
Eventually(session.Wait(testutil.AverageTimeoutAnalyzeForExamplesFolder).Out).Should(gbytes.Say(`Type: Vulnerability`))

})
})
})

func writeJsonFile(path string) {
file, err := os.OpenFile(path, os.O_RDWR, os.ModePerm)
if err != nil {
Fail(fmt.Sprintf("The following error occurred when opening the file: %v", err))
}

defer file.Close()

customRules := []map[string]interface{}{

{
"id": "HS-JAVA-99999999999",
"name": "Teste QA",
"description": "Teste de description QA",
"language": "Java",
"severity": "LOW",
"confidence": "LOW",
"type": "Regular",
"expressions": []string{".*"},
},
}

b, err := json.Marshal(customRules)
if err != nil {
Fail(fmt.Sprintf("The following error occurred to marshal json: %v", err))
}

if _, err := file.Write(b); err != nil {
Fail(fmt.Sprintf("The following error occurred when writing to the file: %v", err))
}
}
30 changes: 30 additions & 0 deletions internal/utils/testutil/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2021 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package testutil

import (
"fmt"
"os"

"github.com/onsi/ginkgo"
)

func GinkgoCreateTmpFile(pattern string) string {
file, err := os.CreateTemp(os.TempDir(), pattern)
if err != nil {
ginkgo.Fail(fmt.Sprintf("The following error occurred when creating the file: %v", err))
}
return file.Name()
}

0 comments on commit 3cde903

Please sign in to comment.