Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ignore:
- cmd
- cmd
- pkg/server/docs/docs.go
7 changes: 6 additions & 1 deletion pkg/server/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ func ScanFromAPI(url string, rqOptions model.Options, options model.Options, sid
escapedURL := cleanURL(url)
vLog.WithField("data1", sid).Debug(escapedURL)
vLog.WithField("data1", sid).Debug(newOptions)
_, _ = scan.Scan(url, newOptions, sid)
_, err := scan.Scan(url, newOptions, sid)
if err != nil {
vLog.WithField("data1", sid).Error("Scan failed: ", err)
return
}
vLog.WithField("data1", sid).Info("Scan completed successfully")
}

// GetScan is get scan information
Expand Down
73 changes: 73 additions & 0 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package server

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -77,3 +79,74 @@ func Test_healthHandler(t *testing.T) {
assert.Contains(t, rec.Body.String(), "ok")
}
}

func Test_postScanHandler(t *testing.T) {
e := echo.New()
rq := Req{
URL: "http://example.com",
Options: model.Options{
Method: "GET",
},
}
body, _ := json.Marshal(rq)
req := httptest.NewRequest(http.MethodPost, "/scan", bytes.NewReader(body))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

scans := []string{}
options := model.Options{
Scan: map[string]model.Scan{},
}

if assert.NoError(t, postScanHandler(c, &scans, options)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Contains(t, rec.Body.String(), "code")
assert.Contains(t, rec.Body.String(), "msg")
assert.Contains(t, rec.Body.String(), "data")
}
}

func Test_GetScan(t *testing.T) {
options := model.Options{
Scan: map[string]model.Scan{
"test-scan": {URL: "http://example.com", Results: []model.PoC{{Type: "finish"}}},
},
}
scan := GetScan("test-scan", options)
assert.Equal(t, "http://example.com", scan.URL)
assert.Equal(t, "finish", scan.Results[0].Type)
}

func Test_GetScans(t *testing.T) {
options := model.Options{
Scan: map[string]model.Scan{
"test-scan1": {URL: "http://example1.com"},
"test-scan2": {URL: "http://example2.com"},
},
}
scans := GetScans(options)
assert.Contains(t, scans, "test-scan1")
assert.Contains(t, scans, "test-scan2")
}

func Test_ScanFromAPI(t *testing.T) {
options := model.Options{
Debug: true,
Scan: map[string]model.Scan{},
}
rqOptions := model.Options{
Method: "GET",
}
sid := "test-scan-id"

t.Run("Successful Scan", func(t *testing.T) {
ScanFromAPI("http://example.com", rqOptions, options, sid)
// Add assertions to verify the scan was successful
})
Comment on lines +146 to +147
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Add assertions to verify the scan was successful. For example, check if the scan ID is added to the scans list or if the scan results are as expected.

// Add assertions to verify the scan was successful
// Example:
// assert.Contains(t, scans, sid)


t.Run("Scan with Error", func(t *testing.T) {
ScanFromAPI("http://invalid-url", rqOptions, options, sid)
// Add assertions to verify error handling
})
Comment on lines +151 to +152
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Add assertions to verify error handling. For example, check if an error is logged or if the scan ID is not added to the scans list.

// Add assertions to verify error handling
// Example:
// assert.NotContains(t, scans, sid)

}
Loading