Skip to content

Commit

Permalink
Return a teamname of admin team on status response
Browse files Browse the repository at this point in the history
  • Loading branch information
J12934 committed Nov 28, 2024
1 parent 6f8f9f3 commit d1729da
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions balancer/routes/teamStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type TeamStatus struct {
Readiness bool `json:"readiness"`
}

type AdminTeamStatus struct {
Name string `json:"name"`
}

func handleTeamStatus(bundle *bundle.Bundle, scoringSerivce *scoring.ScoringService) http.Handler {
return http.HandlerFunc(
func(responseWriter http.ResponseWriter, req *http.Request) {
Expand All @@ -29,6 +33,20 @@ func handleTeamStatus(bundle *bundle.Bundle, scoringSerivce *scoring.ScoringServ
return
}

if team == "admin" {
responseBytes, err := json.Marshal(AdminTeamStatus{Name: "admin"})
if err != nil {
bundle.Log.Printf("Failed to marshal response: %s", err)
http.Error(responseWriter, "", http.StatusInternalServerError)
return
}

responseWriter.Header().Set("Content-Type", "application/json")
responseWriter.WriteHeader(http.StatusOK)
responseWriter.Write(responseBytes)
return
}

deployment, err := bundle.ClientSet.AppsV1().Deployments(bundle.RuntimeEnvironment.Namespace).Get(req.Context(), fmt.Sprintf("juiceshop-%s", team), metav1.GetOptions{})
if err != nil {
http.Error(responseWriter, "team not found", http.StatusNotFound)
Expand Down
15 changes: 15 additions & 0 deletions balancer/routes/teamStatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,19 @@ func TestTeamStatusHandler(t *testing.T) {
assert.Equal(t, http.StatusUnauthorized, rr.Code)
assert.Equal(t, "", strings.TrimSpace(rr.Body.String()))
})

t.Run("returns a a simplified response when the logged in team is the admin", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/balancer/api/teams/status", nil)
req.Header.Set("Cookie", fmt.Sprintf("team=%s", testutil.SignTestTeamname("admin")))
rr := httptest.NewRecorder()
server := http.NewServeMux()

bundle := testutil.NewTestBundle()
AddRoutes(server, bundle, nil)

server.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
assert.JSONEq(t, `{"name":"admin"}`, rr.Body.String())
})
}

0 comments on commit d1729da

Please sign in to comment.