func (h UpdatePreferencesHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, context stack.Context) { database := context.Get("database").(DatabaseInterface) connection := database.Connection() token := context.Get("token").(*jwt.Token) if _, ok := token.Claims["user_id"]; !ok { h.errorWriter.Write(w, webutil.MissingUserTokenError("Missing user_id from token claims.")) return } userID := token.Claims["user_id"].(string) builder := services.NewPreferencesBuilder() validator := valiant.NewValidator(req.Body) err := validator.Validate(&builder) if err != nil { h.errorWriter.Write(w, webutil.ValidationError([]string{err.Error()})) return } preferences, err := builder.ToPreferences() if err != nil { h.errorWriter.Write(w, webutil.ValidationError([]string{err.Error()})) return } transaction := connection.Transaction() transaction.Begin() err = h.preferences.Update(transaction, preferences, builder.GlobalUnsubscribe, userID) if err != nil { transaction.Rollback() switch err.(type) { case services.MissingKindOrClientError, services.CriticalKindError: h.errorWriter.Write(w, webutil.ValidationError([]string{err.Error()})) default: h.errorWriter.Write(w, err) } return } err = transaction.Commit() if err != nil { h.errorWriter.Write(w, models.NewTransactionCommitError(err.Error())) return } w.WriteHeader(http.StatusNoContent) }
func (h GetPreferencesHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, context stack.Context) { token := context.Get("token").(*jwt.Token) if _, ok := token.Claims["user_id"]; !ok { h.errorWriter.Write(w, webutil.MissingUserTokenError("Missing user_id from token claims.")) return } userID := token.Claims["user_id"].(string) parsed, err := h.preferences.Find(context.Get("database").(DatabaseInterface), userID) if err != nil { h.errorWriter.Write(w, err) return } writeJSON(w, http.StatusOK, parsed) }
handler.ServeHTTP(writer, request, context) Expect(errorWriter.WriteCall.Receives.Error).To(Equal(preferencesFinder.FindCall.Returns.Error)) }) }) Context("when the request does not container a user_id claim", func() { It("writes a MissingUserTokenError to the error writer", func() { tokenHeader := map[string]interface{}{ "alg": "FAST", } tokenClaims := map[string]interface{}{} request, err := http.NewRequest("GET", "/user_preferences", nil) if err != nil { panic(err) } token, err := jwt.Parse(helpers.BuildToken(tokenHeader, tokenClaims), func(token *jwt.Token) (interface{}, error) { return []byte(application.UAAPublicKey), nil }) context = stack.NewContext() context.Set("token", token) handler.ServeHTTP(writer, request, context) Expect(errorWriter.WriteCall.Receives.Error).To(BeAssignableToTypeOf(webutil.MissingUserTokenError(""))) }) }) })
It("returns a 422 when a template cannot be assigned", func() { writer.Write(recorder, services.TemplateAssignmentError("The template could not be assigned")) Expect(recorder.Code).To(Equal(422)) body := make(map[string]interface{}) err := json.Unmarshal(recorder.Body.Bytes(), &body) if err != nil { panic(err) } Expect(body["errors"]).To(ContainElement("The template could not be assigned")) }) It("returns a 422 when a user token was expected but is not present", func() { writer.Write(recorder, webutil.MissingUserTokenError("Missing user_id from token claims.")) Expect(recorder.Code).To(Equal(422)) body := make(map[string]interface{}) err := json.Unmarshal(recorder.Body.Bytes(), &body) if err != nil { panic(err) } Expect(body["errors"]).To(ContainElement("Missing user_id from token claims.")) }) It("panics for unknown errors", func() { Expect(func() { writer.Write(recorder, errors.New("BOOM!")) }).To(Panic())