From 5bdb7bc3f03f86eedd166a2bfa418bf04eacfafa Mon Sep 17 00:00:00 2001 From: Wilfred Asomani Date: Wed, 9 Aug 2023 00:14:28 +0000 Subject: [PATCH] satellite/console: add update project http endpoint This change adds an endpoint update projects, to be used in place of the graphql alternative. Issue: https://github.com/storj/storj/issues/6134 Change-Id: I26c04f4400f71721cbddb7f64405e6c9b78edb4d --- .../console/consoleweb/consoleapi/projects.go | 40 +++++++++++++++++++ .../console/consoleweb/endpoints_test.go | 13 ++++++ satellite/console/consoleweb/server.go | 1 + 3 files changed, 54 insertions(+) diff --git a/satellite/console/consoleweb/consoleapi/projects.go b/satellite/console/consoleweb/consoleapi/projects.go index 08aa6678e..3bf42772d 100644 --- a/satellite/console/consoleweb/consoleapi/projects.go +++ b/satellite/console/consoleweb/consoleapi/projects.go @@ -165,6 +165,45 @@ func (p *Projects) GetPagedProjects(w http.ResponseWriter, r *http.Request) { } } +// UpdateProject handles updating projects. +func (p *Projects) UpdateProject(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + var err error + defer mon.Task()(&ctx)(&err) + + var ok bool + var idParam string + + if idParam, ok = mux.Vars(r)["id"]; !ok { + p.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("missing project id route param")) + return + } + + id, err := uuid.FromString(idParam) + if err != nil { + p.serveJSONError(ctx, w, http.StatusBadRequest, err) + return + } + + var payload console.UpsertProjectInfo + + err = json.NewDecoder(r.Body).Decode(&payload) + if err != nil { + p.serveJSONError(ctx, w, http.StatusBadRequest, err) + return + } + + _, err = p.service.UpdateProject(ctx, id, payload) + if err != nil { + if console.ErrUnauthorized.Has(err) { + p.serveJSONError(ctx, w, http.StatusUnauthorized, err) + return + } + + p.serveJSONError(ctx, w, http.StatusInternalServerError, err) + } +} + // GetMembersAndInvitations returns the project's members and invitees. func (p *Projects) GetMembersAndInvitations(w http.ResponseWriter, r *http.Request) { ctx := r.Context() @@ -457,6 +496,7 @@ func (p *Projects) RespondToInvitation(w http.ResponseWriter, r *http.Request) { id, err := uuid.FromString(idParam) if err != nil { p.serveJSONError(ctx, w, http.StatusBadRequest, err) + return } var payload struct { diff --git a/satellite/console/consoleweb/endpoints_test.go b/satellite/console/consoleweb/endpoints_test.go index eeb954098..7264c301d 100644 --- a/satellite/console/consoleweb/endpoints_test.go +++ b/satellite/console/consoleweb/endpoints_test.go @@ -787,6 +787,13 @@ func TestProjects(t *testing.T) { require.Contains(t, body, "error") // TODO: this should return a better error require.Equal(t, http.StatusInternalServerError, resp.StatusCode) + + resp, body = test.request(http.MethodPatch, fmt.Sprintf("/projects/%s", test.defaultProjectID()), + test.toJSON(map[string]interface{}{ + "name": "My Second Project with a long name", + })) + require.Contains(t, body, "error") + require.Equal(t, http.StatusInternalServerError, resp.StatusCode) } { // Post_ProjectRename @@ -806,6 +813,12 @@ func TestProjects(t *testing.T) { }`})) require.Contains(t, body, "updateProject") require.Equal(t, http.StatusOK, resp.StatusCode) + + resp, _ = test.request(http.MethodPatch, fmt.Sprintf("/projects/%s", test.defaultProjectID()), + test.toJSON(map[string]interface{}{ + "name": "new name", + })) + require.Equal(t, http.StatusOK, resp.StatusCode) } }) } diff --git a/satellite/console/consoleweb/server.go b/satellite/console/consoleweb/server.go index b49524e4e..0fd4b0ece 100644 --- a/satellite/console/consoleweb/server.go +++ b/satellite/console/consoleweb/server.go @@ -277,6 +277,7 @@ func NewServer(logger *zap.Logger, config Config, service *console.Service, oidc projectsRouter.Use(server.withAuth) projectsRouter.Handle("", http.HandlerFunc(projectsController.GetUserProjects)).Methods(http.MethodGet, http.MethodOptions) projectsRouter.Handle("/paged", http.HandlerFunc(projectsController.GetPagedProjects)).Methods(http.MethodGet, http.MethodOptions) + projectsRouter.Handle("/{id}", http.HandlerFunc(projectsController.UpdateProject)).Methods(http.MethodPatch, http.MethodOptions) projectsRouter.Handle("/{id}/salt", http.HandlerFunc(projectsController.GetSalt)).Methods(http.MethodGet, http.MethodOptions) projectsRouter.Handle("/{id}/members", http.HandlerFunc(projectsController.GetMembersAndInvitations)).Methods(http.MethodGet, http.MethodOptions) projectsRouter.Handle("/{id}/invite", http.HandlerFunc(projectsController.InviteUsers)).Methods(http.MethodPost, http.MethodOptions)