Skip to content

Commit 88726cd

Browse files
author
cx-Margarita-LevitM
committed
Fix Unit tests
1 parent 07c8e90 commit 88726cd

2 files changed

Lines changed: 40 additions & 53 deletions

File tree

internal/commands/result_test.go

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ import (
2727
const fileName = "cx_result"
2828

2929
const (
30-
resultsCommand = "results"
31-
codeBashingCommand = "codebashing"
32-
vulnerabilityValue = "Reflected XSS All Clients"
33-
languageValue = "PHP"
34-
cweValue = "79"
35-
jsonValue = "json"
36-
tableValue = "table"
37-
listValue = "list"
38-
secretDetectionLine = "| Secret Detection 0 1 1 0 0 Completed |"
30+
resultsCommand = "results"
31+
codeBashingCommand = "codebashing"
32+
queryIDValue = "8481125285487743346"
33+
queryIDWrongValueValue = "11666704984804998184"
34+
jsonValue = "json"
35+
tableValue = "table"
36+
listValue = "list"
37+
secretDetectionLine = "| Secret Detection 0 1 1 0 0 Completed |"
3938
)
4039

4140
func flag(f string) string {
@@ -553,53 +552,37 @@ func TestRunGetResultsByScanIdWithEmptyOutputPath(t *testing.T) {
553552
_ = execCmdNotNilAssertion(t, "results", "show", "--scan-id", "MOCK", "--output-path", "")
554553
}
555554

556-
func TestRunGetCodeBashingWithoutLanguage(t *testing.T) {
555+
func TestRunGetCodeBashingWithEmptyQueryId(t *testing.T) {
557556
err := execCmdNotNilAssertion(
558557
t,
559558
resultsCommand,
560559
codeBashingCommand,
561-
flag(params.CweIDFlag),
562-
cweValue,
563-
flag(params.VulnerabilityTypeFlag),
564-
vulnerabilityValue)
565-
assert.Equal(t, err.Error(), "required flag(s) \"language\" not set", "Wrong expected error message")
560+
flag(params.QueryIDFlag),
561+
"")
562+
assert.Equal(t, err.Error(), "Cannot GET /lessons/mapping/", "Wrong expected error message")
566563
}
567564

568-
func TestRunGetCodeBashingWithoutVulnerabilityType(t *testing.T) {
569-
err := execCmdNotNilAssertion(
570-
t,
565+
func TestRunGetCodeBashingWithEmptyQueryIdThatDoesNotHaveAnLesson(t *testing.T) {
566+
cmd := createASTTestCommand()
567+
buffer, err := executeRedirectedOsStdoutTestCommand(cmd,
571568
resultsCommand,
572569
codeBashingCommand,
573-
flag(params.CweIDFlag),
574-
cweValue,
575-
flag(params.LanguageFlag),
576-
languageValue)
577-
assert.Equal(t, err.Error(), "required flag(s) \"vulnerability-type\" not set", "Wrong expected error message")
578-
}
570+
flag(params.QueryIDFlag),
571+
queryIDWrongValueValue)
579572

580-
func TestRunGetCodeBashingWithoutCweId(t *testing.T) {
581-
err := execCmdNotNilAssertion(
582-
t,
583-
resultsCommand,
584-
codeBashingCommand,
585-
flag(params.VulnerabilityTypeFlag),
586-
vulnerabilityValue,
587-
flag(params.LanguageFlag),
588-
languageValue)
589-
assert.Equal(t, err.Error(), "required flag(s) \"cwe-id\" not set", "Wrong expected error message")
573+
assert.NilError(t, err, "Command should not return an error")
574+
output := buffer.String()
575+
assert.Assert(t, strings.Contains(output, "/app/home"), "Expected response to contain /app/home path")
576+
assert.Assert(t, !strings.Contains(output, "Cannot GET /lessons/mapping/"), "Response should not contain error message")
590577
}
591578

592579
func TestRunGetCodeBashingWithFormatJson(t *testing.T) {
593580
execCmdNilAssertion(
594581
t,
595582
resultsCommand,
596583
codeBashingCommand,
597-
flag(params.VulnerabilityTypeFlag),
598-
vulnerabilityValue,
599-
flag(params.LanguageFlag),
600-
languageValue,
601-
flag(params.CweIDFlag),
602-
cweValue,
584+
flag(params.QueryIDFlag),
585+
queryIDValue,
603586
flag(params.FormatFlag),
604587
jsonValue)
605588
}
@@ -609,12 +592,8 @@ func TestRunGetCodeBashingWithFormatTable(t *testing.T) {
609592
t,
610593
resultsCommand,
611594
codeBashingCommand,
612-
flag(params.VulnerabilityTypeFlag),
613-
vulnerabilityValue,
614-
flag(params.LanguageFlag),
615-
languageValue,
616-
flag(params.CweIDFlag),
617-
cweValue,
595+
flag(params.QueryIDFlag),
596+
queryIDValue,
618597
flag(params.FormatFlag),
619598
tableValue)
620599
}
@@ -624,12 +603,8 @@ func TestRunGetCodeBashingWithFormatList(t *testing.T) {
624603
t,
625604
resultsCommand,
626605
codeBashingCommand,
627-
flag(params.VulnerabilityTypeFlag),
628-
vulnerabilityValue,
629-
flag(params.LanguageFlag),
630-
languageValue,
631-
flag(params.CweIDFlag),
632-
cweValue,
606+
flag(params.QueryIDFlag),
607+
queryIDValue,
633608
flag(params.FormatFlag),
634609
listValue)
635610
}

internal/wrappers/mock/codebashing-mock.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@ import (
66

77
type CodeBashingMockWrapper struct{}
88

9-
func (r CodeBashingMockWrapper) GetCodeBashingLinks(params map[string]string, codeBashingURL string) (*[]wrappers.CodeBashingCollection, *wrappers.WebError, error) {
9+
func (r CodeBashingMockWrapper) GetCodeBashingLinks(queryId string, codeBashingURL string) (*[]wrappers.CodeBashingCollection, *wrappers.WebError, error) {
10+
if queryId == "" {
11+
return nil, &wrappers.WebError{Message: "Cannot GET /lessons/mapping/"}, nil
12+
}
13+
14+
if queryId == "11666704984804998184" {
15+
collection := &wrappers.CodeBashingCollection{
16+
Path: "/app/home",
17+
}
18+
ret := []wrappers.CodeBashingCollection{*collection}
19+
return &ret, nil, nil
20+
}
21+
1022
collection := &wrappers.CodeBashingCollection{
1123
Path: "http://example.com/courses/php/lessons/dom_xss",
1224
}

0 commit comments

Comments
 (0)