-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #308 from rstudio/dotnomad/title-api
Add `PUT /api/deployment/title` endpoint to set title
- Loading branch information
Showing
6 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package api | ||
|
||
// Copyright (C) 2023 by Posit Software, PBC. | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/rstudio/connect-client/internal/logging" | ||
"github.com/rstudio/connect-client/internal/services/api/deployments" | ||
) | ||
|
||
type PutDeploymentTitleRequestBody struct { | ||
Title string `json:"title"` | ||
} | ||
|
||
func PutDeploymentTitleHandlerFunc(deploymentsService deployments.DeploymentsService, log logging.Logger) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
dec := json.NewDecoder(r.Body) | ||
dec.DisallowUnknownFields() | ||
var b PutDeploymentTitleRequestBody | ||
err := dec.Decode(&b) | ||
if err != nil { | ||
BadRequestJson(w, r, log, err) | ||
return | ||
} | ||
|
||
d := deploymentsService.SetDeploymentTitle(b.Title) | ||
|
||
w.Header().Set("content-type", "application/json") | ||
json.NewEncoder(w).Encode(d) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package api | ||
|
||
// Copyright (C) 2023 by Posit Software, PBC. | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/rstudio/connect-client/internal/logging" | ||
"github.com/rstudio/connect-client/internal/state" | ||
"github.com/rstudio/connect-client/internal/util/utiltest" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type PutDeploymentTitleHandlerFuncSuite struct { | ||
utiltest.Suite | ||
log logging.Logger | ||
} | ||
|
||
func TestPutDeploymentTitleHandlerFuncSuite(t *testing.T) { | ||
suite.Run(t, new(PutDeploymentTitleHandlerFuncSuite)) | ||
} | ||
|
||
func (s *PutDeploymentTitleHandlerFuncSuite) SetupSuite() { | ||
s.log = logging.New() | ||
} | ||
|
||
func (s *PutDeploymentTitleHandlerFuncSuite) TestPutDeploymentTitleHandlerFunc() { | ||
deploymentsService := new(MockDeploymentsService) | ||
h := PutDeploymentTitleHandlerFunc(deploymentsService, s.log) | ||
s.NotNil(h) | ||
} | ||
|
||
func (s *PutDeploymentTitleHandlerFuncSuite) TestPutDeploymentTitleHandler() { | ||
|
||
src := state.NewDeployment() | ||
deploymentsService := new(MockDeploymentsService) | ||
deploymentsService.On("SetDeploymentTitle", mock.Anything).Return(src) | ||
|
||
var b PutDeploymentTitleRequestBody = PutDeploymentTitleRequestBody{ | ||
Title: "new-title", | ||
} | ||
j, _ := json.Marshal(b) | ||
br := bytes.NewReader(j) | ||
req, err := http.NewRequest("PUT", "", br) | ||
s.NoError(err) | ||
|
||
h := PutDeploymentTitleHandlerFunc(deploymentsService, s.log) | ||
|
||
rec := httptest.NewRecorder() | ||
|
||
h(rec, req) | ||
|
||
s.Equal(http.StatusOK, rec.Result().StatusCode) | ||
s.Equal("application/json", rec.Header().Get("content-type")) | ||
|
||
res := &state.Deployment{} | ||
dec := json.NewDecoder(rec.Body) | ||
dec.DisallowUnknownFields() | ||
s.NoError(dec.Decode(res)) | ||
|
||
s.Equal(src, res) | ||
} | ||
|
||
func (s *PutDeploymentTitleHandlerFuncSuite) TestPutDeploymentTitleHandlerFuncBadRequestJson() { | ||
|
||
deploymentsService := new(MockDeploymentsService) | ||
|
||
br := bytes.NewReader([]byte{}) | ||
req, err := http.NewRequest("PUT", "", br) | ||
s.NoError(err) | ||
|
||
h := PutDeploymentTitleHandlerFunc(deploymentsService, s.log) | ||
|
||
rec := httptest.NewRecorder() | ||
|
||
h(rec, req) | ||
|
||
s.Equal(http.StatusBadRequest, rec.Result().StatusCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters