-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Rob Best <[email protected]>
- Loading branch information
1 parent
26bd24f
commit d994025
Showing
9 changed files
with
397 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package dependencytrack | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestClientRequest(t *testing.T) { | ||
type testSchema struct { | ||
Name string | ||
Parameters []string | ||
} | ||
expectedReqBody := &testSchema{ | ||
Name: "foo", | ||
Parameters: []string{"one", "two", "three"}, | ||
} | ||
expectedRespBody := &testSchema{ | ||
Name: "bar", | ||
Parameters: []string{"apple", "orange", "banana"}, | ||
} | ||
|
||
client, mux, teardown := setup() | ||
defer teardown() | ||
|
||
client.opts.APIKey = "FAKEAPIKEY" | ||
|
||
mux.HandleFunc("/foobar", func(w http.ResponseWriter, r *http.Request) { | ||
if got := r.Header.Get("X-Api-Key"); !cmp.Equal(got, "FAKEAPIKEY") { | ||
t.Errorf("Got X-Api-Key header %v, wanted %v", got, "FAKEAPIKEY") | ||
} | ||
|
||
if got := r.Header.Get("Content-type"); !cmp.Equal(got, "application/json") { | ||
t.Errorf("Got Content-type header %v, wanted %v", got, "application/json") | ||
} | ||
|
||
got := &testSchema{} | ||
if err := json.NewDecoder(r.Body).Decode(got); err != nil { | ||
t.Errorf("Unexpected error decoding request body: %v", err) | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
if !cmp.Equal(got, expectedReqBody) { | ||
t.Errorf("Expected request body %v, got %v", expectedReqBody, got) | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(expectedRespBody) | ||
}) | ||
|
||
req, err := client.newRequest(http.MethodPost, "/foobar", expectedReqBody) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
data := &testSchema{} | ||
if err := client.do(req, data); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dependencytrack | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
) | ||
|
||
// setup sets up a test HTTP server and a client configured to talk to it | ||
func setup() (client *Client, mux *http.ServeMux, teardown func()) { | ||
mux = http.NewServeMux() | ||
|
||
server := httptest.NewServer(mux) | ||
|
||
client = New(WithAddress(server.URL)) | ||
|
||
return client, mux, server.Close | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package dependencytrack | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
// TestGetCurrentPortfolioMetrics tests getting current portfolio metrics | ||
func TestGetCurrentPortfolioMetrics(t *testing.T) { | ||
client, mux, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/api/v1/metrics/portfolio/current", func(w http.ResponseWriter, r *http.Request) { | ||
if got := r.Method; got != http.MethodGet { | ||
t.Errorf("Got request method %v, want %v", got, http.MethodGet) | ||
} | ||
fmt.Fprintf(w, | ||
` | ||
{ | ||
"inheritedRiskScore": 2500.42 | ||
} | ||
`, | ||
) | ||
}) | ||
|
||
got, err := client.GetCurrentPortfolioMetrics() | ||
if err != nil { | ||
t.Errorf("GetCurrentPortfolioMetrics returned error: %v", err) | ||
} | ||
|
||
want := &PortfolioMetrics{ | ||
InheritedRiskScore: 2500.42, | ||
} | ||
|
||
if !cmp.Equal(got, want) { | ||
t.Errorf("Got portfolio metrics %v, want %v", got, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package dependencytrack | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
// TestGetViolations tests listing policy violations | ||
func TestGetViolations(t *testing.T) { | ||
client, mux, teardown := setup() | ||
defer teardown() | ||
|
||
now := time.Now().Truncate(time.Second) | ||
|
||
mux.HandleFunc("/api/v1/violation", func(w http.ResponseWriter, r *http.Request) { | ||
if got := r.Method; got != http.MethodGet { | ||
t.Errorf("Got request method %v, want %v", got, http.MethodGet) | ||
} | ||
want := url.Values{} | ||
want.Set("suppressed", "true") | ||
if got := r.URL.Query(); !cmp.Equal(got, want) { | ||
t.Errorf("Got query parameters: %v, want %v", got, want) | ||
} | ||
fmt.Fprintf(w, | ||
` | ||
[ | ||
{ | ||
"analysis": { | ||
"analysisState": "APPROVED", | ||
"isSuppressed": true | ||
}, | ||
"policyCondition": { | ||
"policy": { | ||
"violationState": "WARN" | ||
} | ||
}, | ||
"project": { | ||
"name": "foo", | ||
"version": "bar", | ||
"active": true, | ||
"lastBomImport": %d, | ||
"metrics": { | ||
"critical": 0, | ||
"high": 1, | ||
"low": 2, | ||
"medium": 3, | ||
"unassigned": 4, | ||
"inheritedRiskScore": 1240 | ||
}, | ||
"uuid": "fd1b10b9-678d-4af9-ad8e-877d1f357b03" | ||
}, | ||
"type": "SECURITY" | ||
}, | ||
{ | ||
"policyCondition": { | ||
"policy": { | ||
"violationState": "WARN" | ||
} | ||
}, | ||
"project": { | ||
"name": "bar", | ||
"version": "foo", | ||
"active": false, | ||
"metrics": { | ||
"critical": 50, | ||
"high": 25, | ||
"low": 12, | ||
"medium": 6, | ||
"unassigned": 3, | ||
"inheritedRiskScore": 2560.26 | ||
}, | ||
"uuid": "9b9a702a-a8b4-49fb-bb99-c05c1a8c8d49" | ||
}, | ||
"type": "LICENSE" | ||
} | ||
] | ||
`, | ||
now.Unix(), | ||
) | ||
}) | ||
|
||
got, err := client.GetViolations(true) | ||
if err != nil { | ||
t.Errorf("GetViolations returned error: %v", err) | ||
} | ||
|
||
want := []*PolicyViolation{ | ||
{ | ||
Analysis: &ViolationAnalysis{ | ||
AnalysisState: "APPROVED", | ||
IsSuppressed: true, | ||
}, | ||
PolicyCondition: PolicyCondition{ | ||
Policy: Policy{ | ||
ViolationState: "WARN", | ||
}, | ||
}, | ||
Project: Project{ | ||
Name: "foo", | ||
Version: "bar", | ||
Active: true, | ||
LastBomImport: Time{now}, | ||
Metrics: ProjectMetrics{ | ||
Critical: 0, | ||
High: 1, | ||
Low: 2, | ||
Medium: 3, | ||
Unassigned: 4, | ||
InheritedRiskScore: 1240, | ||
}, | ||
UUID: "fd1b10b9-678d-4af9-ad8e-877d1f357b03", | ||
}, | ||
Type: "SECURITY", | ||
}, | ||
{ | ||
PolicyCondition: PolicyCondition{ | ||
Policy: Policy{ | ||
ViolationState: "WARN", | ||
}, | ||
}, | ||
Project: Project{ | ||
Name: "bar", | ||
Version: "foo", | ||
Active: false, | ||
LastBomImport: Time{}, | ||
Metrics: ProjectMetrics{ | ||
Critical: 50, | ||
High: 25, | ||
Low: 12, | ||
Medium: 6, | ||
Unassigned: 3, | ||
InheritedRiskScore: 2560.26, | ||
}, | ||
UUID: "9b9a702a-a8b4-49fb-bb99-c05c1a8c8d49", | ||
}, | ||
Type: "LICENSE", | ||
}, | ||
} | ||
|
||
if !cmp.Equal(got, want) { | ||
t.Errorf("Got violations %v, want %v", got, want) | ||
} | ||
} |
Oops, something went wrong.