Skip to content

Commit

Permalink
Test project:info command
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Oct 12, 2024
1 parent fe736f0 commit 364339d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/mockapi/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func NewHandler(t *testing.T) *Handler {
})

h.Mux.Get("/projects/{id}", h.handleGetProject)
h.Mux.Patch("/projects/{id}", h.handlePatchProject)
h.Mux.Get("/projects/{id}/environments", h.handleListEnvironments)
h.Mux.Get("/projects/{project_id}/environments/{environment_id}/deployments/current", h.handleGetCurrentDeployment)
h.Mux.Get("/projects/{project_id}/user-access", h.handleProjectUserAccess)
Expand Down
21 changes: 21 additions & 0 deletions internal/mockapi/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"strings"
"time"

"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -36,6 +37,26 @@ func (h *Handler) handleGetProject(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNotFound)
}

func (h *Handler) handlePatchProject(w http.ResponseWriter, req *http.Request) {
h.store.Lock()
defer h.store.Unlock()
projectID := chi.URLParam(req, "id")
p, ok := h.store.projects[projectID]
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
patched := *p
err := json.NewDecoder(req.Body).Decode(&patched)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
patched.UpdatedAt = time.Now()
h.store.projects[projectID] = &patched
_ = json.NewEncoder(w).Encode(&patched)
}

func (h *Handler) handleListEnvironments(w http.ResponseWriter, req *http.Request) {
h.store.RLock()
defer h.store.RUnlock()
Expand Down
78 changes: 78 additions & 0 deletions tests/project_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package tests

import (
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/platformsh/cli/internal/mockapi"
)

func TestProjectInfo(t *testing.T) {
authServer := mockapi.NewAuthServer(t)
defer authServer.Close()

myUserID := "my-user-id"
vendor := "test-vendor"

apiHandler := mockapi.NewHandler(t)
apiHandler.SetMyUser(&mockapi.User{ID: myUserID})
apiServer := httptest.NewServer(apiHandler)
defer apiServer.Close()

apiHandler.SetOrgs([]*mockapi.Org{
makeOrg("org-id-1", "org-1", "Org 1", myUserID),
})
created, err := time.Parse(time.RFC3339, "2014-04-01T10:00:00+01:00")
require.NoError(t, err)
apiHandler.SetProjects([]*mockapi.Project{
{
ID: mockProjectID,
Title: "Project 1",
Region: "region-1",
Organization: "org-id-1",
Vendor: vendor,
Repository: mockapi.ProjectRepository{
URL: "[email protected]:mock-project.git",
},
DefaultBranch: "main",
CreatedAt: created,
UpdatedAt: created.Add(time.Second * 86400),
Links: mockapi.MakeHALLinks(
"self=/projects/"+url.PathEscape(mockProjectID),
"#edit=/projects/"+url.PathEscape(mockProjectID),
),
},
})

run := runnerWithAuth(t, apiServer.URL, authServer.URL)

assert.Equal(t, strings.TrimLeft(`
Property Value
id abcdefg123456
title Project 1
region region-1
organization org-id-1
vendor test-vendor
repository url: '[email protected]:mock-project.git'
default_branch main
created_at 2014-04-01T09:00:00+00:00
updated_at 2014-04-02T09:00:00+00:00
git [email protected]:mock-project.git
url `+apiServer.URL+`/projects/abcdefg123456
`, "\n"), run("pro:info", "-p", mockProjectID, "--format", "plain"))

assert.Equal(t, "2014-04-02\n", run("pro:info", "-p", mockProjectID, "updated_at", "--date-fmt", "Y-m-d"))

assert.Equal(t, "Project 1\n", run("pro:info", "-p", mockProjectID, "title"))

run("pro:info", "-v", "-p", mockProjectID, "title", "New Title")

// TODO --refresh should not be needed here
assert.Equal(t, "New Title\n", run("pro:info", "-p", mockProjectID, "title", "--refresh"))
}
1 change: 1 addition & 0 deletions tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,6 @@ func testEnv() []string {
EnvPrefix+"NO_INTERACTION=1",
EnvPrefix+"VERSION=1.0.0",
EnvPrefix+"HOME="+os.TempDir(),
"TZ=UTC",
)
}

0 comments on commit 364339d

Please sign in to comment.