Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <[email protected]>
  • Loading branch information
fjogeleit committed Sep 9, 2023
1 parent 776309b commit 53c7358
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
63 changes: 63 additions & 0 deletions pkg/api/basic_auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package api_test

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/kyverno/policy-reporter/pkg/api"
)

func Test_HTTPBasicSkipped(t *testing.T) {
handler := api.HTTPBasic(nil, api.HealthzHandler(func() bool { return true }))

req, err := http.NewRequest("GET", "/healthz", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusUnauthorized)
}
}

func Test_HTTPBasicUnauthorized(t *testing.T) {
handler := api.HTTPBasic(&api.BasicAuth{Username: "user", Password: "password"}, api.HealthzHandler(func() bool { return true }))

req, err := http.NewRequest("GET", "/healthz", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusUnauthorized {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusUnauthorized)
}

if rr.Header().Get("WWW-Authenticate") == "" {
t.Errorf("Missing Header: WWW-Authenticate")
}
}

func Test_HTTPBasicAuthorized(t *testing.T) {
handler := api.HTTPBasic(&api.BasicAuth{Username: "user", Password: "password"}, api.HealthzHandler(func() bool { return true }))

req, err := http.NewRequest("GET", "/healthz", nil)
if err != nil {
t.Fatal(err)
}

req.SetBasicAuth("user", "password")

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusUnauthorized)
}
}
22 changes: 21 additions & 1 deletion pkg/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ func Test_NewServer(t *testing.T) {

port := int(rnd * 10000)

server := api.NewServer(make([]target.Client, 0), port, logger, nil, func() bool { return true })
server := api.NewServer(
make([]target.Client, 0),
port,
logger,
nil,
func() bool { return true },
)

server.RegisterMetricsHandler()
server.RegisterV1Handler(nil)
Expand Down Expand Up @@ -64,3 +70,17 @@ func Test_NewServer(t *testing.T) {

<-serviceDone
}

func Test_SetupServerWithAuth(t *testing.T) {
server := api.NewServer(
make([]target.Client, 0),
8080,
logger,
&api.BasicAuth{Username: "user", Password: "password"},
func() bool { return true },
)

server.RegisterMetricsHandler()
server.RegisterV1Handler(nil)
server.RegisterProfilingHandler()
}

0 comments on commit 53c7358

Please sign in to comment.