fakeValidator = new(fakes.FakeValidator) fakeUserContextReader = new(fakes.FakeUserContextReader) a := make(chan bool, 1) tn := make(chan string, 1) ti := make(chan int, 1) ia := make(chan bool, 1) f := make(chan bool, 1) authenticated = a teamNameChan = tn teamIDChan = ti isAdminChan = ia foundChan = f simpleHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { a <- auth.IsAuthenticated(r) teamName, teamID, isAdmin, found := auth.GetTeam(r) f <- found tn <- teamName ti <- teamID ia <- isAdmin }) server = httptest.NewServer(auth.WrapHandler( simpleHandler, fakeValidator, fakeUserContextReader, )) client = &http.Client{ Transport: &http.Transport{}, }
func (s *Server) SetTeam(w http.ResponseWriter, r *http.Request) { hLog := s.logger.Session("create-team") _, _, isAdmin, found := auth.GetTeam(r) if !found { w.WriteHeader(http.StatusInternalServerError) return } if !isAdmin { w.WriteHeader(http.StatusForbidden) return } teamName := r.FormValue(":team_name") var team db.Team err := json.NewDecoder(r.Body).Decode(&team) if err != nil { hLog.Error("malformed-request", err) w.WriteHeader(http.StatusBadRequest) return } team.Name = teamName err = s.validate(team) if err != nil { hLog.Error("request-body-validation-error", err) w.WriteHeader(http.StatusBadRequest) return } savedTeam, found, err := s.db.GetTeamByName(team.Name) if err != nil { hLog.Error("failed-to-get-team", err) w.WriteHeader(http.StatusInternalServerError) return } if found { err = s.updateCredentials(team) if err != nil { hLog.Error("failed-to-update-team", err) w.WriteHeader(http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) } else { savedTeam, err = s.db.SaveTeam(team) if err != nil { hLog.Error("failed-to-save-team", err) w.WriteHeader(http.StatusInternalServerError) return } w.WriteHeader(http.StatusCreated) } json.NewEncoder(w).Encode(present.Team(savedTeam)) }