Skip to content

Commit

Permalink
update console handler test with new url and fix handler init
Browse files Browse the repository at this point in the history
  • Loading branch information
sechmann committed Nov 6, 2023
1 parent bb87b37 commit 8f367c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 6 additions & 0 deletions pkg/hookd/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ func New(cfg Config) chi.Router {
} else {
r.Post("/provision", provisionHandler.ProvisionInternal)
r.Post("/apikey", provisionHandler.ApiKey)
}

if cfg.PSKValidator == nil {
log.Error("Refusing to set up internal console API endpoint without psk validator; try configuring --frontend-keys")
log.Error("Note: /internal/api/v1/console will be unavailable")
} else {
r.Route("/console", func(r chi.Router) {
r.Use(cfg.PSKValidator)
r.Get("/apikey/{team}", apiKeyHandler.GetTeamApiKey)
Expand Down
16 changes: 9 additions & 7 deletions pkg/hookd/api/v1/console/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/go-chi/chi/middleware"
"github.com/nais/deploy/pkg/grpc/dispatchserver"
"github.com/nais/deploy/pkg/hookd/api"
api_v1_dashboard "github.com/nais/deploy/pkg/hookd/api/v1/console"
"github.com/nais/deploy/pkg/hookd/api/v1/console"
"github.com/nais/deploy/pkg/hookd/database"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand All @@ -22,7 +22,7 @@ type request struct{}

type response struct {
StatusCode int
Body api_v1_dashboard.DeploymentsResponse
Body api_v1_console.DeploymentsResponse
}

type testCase struct {
Expand Down Expand Up @@ -66,8 +66,8 @@ var tests = []testCase{
},
Response: response{
StatusCode: 200,
Body: api_v1_dashboard.DeploymentsResponse{
Deployments: []api_v1_dashboard.FullDeployment{
Body: api_v1_console.DeploymentsResponse{
Deployments: []api_v1_console.FullDeployment{
{
Deployment: database.Deployment{
ID: "1",
Expand Down Expand Up @@ -150,7 +150,7 @@ var tests = []testCase{

func subTest(t *testing.T, test testCase) {
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/api/v1/dashboard/deployments", nil)
request := httptest.NewRequest("GET", "/internal/api/v1/console/deployments", nil)
request.Header.Set("content-type", "application/json")

apiKeyStore := &database.MockApiKeyStore{}
Expand All @@ -166,6 +166,7 @@ func subTest(t *testing.T, test testCase) {
DispatchServer: deployServer,
DeploymentStore: deployStore,
ValidatorMiddlewares: chi.Middlewares{middleware.WithValue("foo", nil)},
PSKValidator: middleware.WithValue("foo", nil),
MetricsPath: "/metrics",
})

Expand All @@ -175,14 +176,15 @@ func subTest(t *testing.T, test testCase) {
}

func testResponse(t *testing.T, recorder *httptest.ResponseRecorder, response response) {
decodedBody := api_v1_dashboard.DeploymentsResponse{}
t.Helper()
decodedBody := api_v1_console.DeploymentsResponse{}
_ = json.Unmarshal(recorder.Body.Bytes(), &decodedBody)
assert.Equal(t, response.StatusCode, recorder.Code)
assert.Equal(t, response.Body.Deployments, decodedBody.Deployments)
}

// Deployment server integration tests using mocks; see table tests definitions above.
func TestDashboardHandler_Deployments(t *testing.T) {
func TestConsoleHandler_Deployments(t *testing.T) {
for _, test := range tests {
t.Logf("Running test: %s", test.Name)
subTest(t, test)
Expand Down

0 comments on commit 8f367c1

Please sign in to comment.