Skip to content

Commit

Permalink
publish API test
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarchetti committed Sep 14, 2023
1 parent 8479de1 commit 0cd6b68
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/services/api/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type PublishReponse struct {
LocalID state.LocalDeploymentID `json:"local_id"` // Unique ID of this publishing operation. Only valid for this run of the agent.
}

var publishFn = publish.PublishManifestFiles

func PostPublishHandlerFunc(publishArgs *cli_types.PublishArgs, lister accounts.AccountList, log logging.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
localID, err := state.NewLocalID()
Expand All @@ -34,7 +36,7 @@ func PostPublishHandlerFunc(publishArgs *cli_types.PublishArgs, lister accounts.

go func() {
log = log.WithArgs("local_id", localID)
err := publish.PublishManifestFiles(publishArgs, lister, log)
err := publishFn(publishArgs, lister, log)
if err != nil {
log.Error("Deployment failed", "error", err.Error())
}
Expand Down
57 changes: 57 additions & 0 deletions internal/services/api/publish_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package api

// Copyright (C) 2023 by Posit Software, PBC.

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/rstudio/connect-client/internal/accounts"
"github.com/rstudio/connect-client/internal/cli_types"
"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/suite"
)

type PublishHandlerFuncSuite struct {
utiltest.Suite
}

func TestPublishHandlerFuncSuite(t *testing.T) {
suite.Run(t, new(PublishHandlerFuncSuite))
}

func (s *PublishHandlerFuncSuite) TestPublishHandlerFunc() {
publishArgs := &cli_types.PublishArgs{
State: state.NewDeployment(),
}
oldID := publishArgs.State.LocalID
log := logging.New()

rec := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/api/publish", nil)
s.NoError(err)

publishFn = func(args *cli_types.PublishArgs, lister accounts.AccountList, log logging.Logger) error {
s.NotNil(args)
s.NotEqual(state.LocalDeploymentID(""), args.State.LocalID)
return nil
}
handler := PostPublishHandlerFunc(publishArgs, nil, log)
handler(rec, req)

s.Equal(http.StatusAccepted, rec.Result().StatusCode)
s.Equal("application/json", rec.Header().Get("content-type"))

res := &PublishReponse{}
dec := json.NewDecoder(rec.Body)
dec.DisallowUnknownFields()
s.NoError(dec.Decode(res))

s.NotEqual(state.LocalDeploymentID(""), publishArgs.State.LocalID)
s.NotEqual(oldID, publishArgs.State.LocalID)
s.Equal(publishArgs.State.LocalID, res.LocalID)
}

0 comments on commit 0cd6b68

Please sign in to comment.