From 72bd9b196130069a08705fc3f9dbb94da44221d4 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Sat, 12 Oct 2024 20:31:12 +0100 Subject: [PATCH] Test user:list command --- internal/mockapi/api_server.go | 1 + internal/mockapi/model.go | 17 ++++++-- internal/mockapi/projects.go | 34 +++++++++++++++ internal/mockapi/users.go | 2 +- tests/user_list_test.go | 80 ++++++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 tests/user_list_test.go diff --git a/internal/mockapi/api_server.go b/internal/mockapi/api_server.go index 861df5d..6655e78 100644 --- a/internal/mockapi/api_server.go +++ b/internal/mockapi/api_server.go @@ -65,6 +65,7 @@ func NewHandler(t *testing.T) *Handler { h.Mux.Get("/projects/{id}", h.handleGetProject) 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) h.Mux.Get("/ref/projects", h.handleProjectRefs) h.Mux.Get("/regions", h.handleListRegions) diff --git a/internal/mockapi/model.go b/internal/mockapi/model.go index 8867c31..bff5eef 100644 --- a/internal/mockapi/model.go +++ b/internal/mockapi/model.go @@ -140,6 +140,15 @@ type OrgRef struct { Owner string `json:"owner_id"` } +type ProjectUserGrant struct { + ProjectID string `json:"project_id"` + OrganizationID string `json:"organization_id"` + UserID string `json:"user_id"` + Permissions []string `json:"permissions"` + GrantedAt time.Time `json:"granted_at"` + UpdatedAt time.Time `json:"updated_at"` +} + type UserGrant struct { ResourceID string `json:"resource_id"` ResourceType string `json:"resource_type"` @@ -151,9 +160,11 @@ type UserGrant struct { } type UserRef struct { - ID string `json:"id"` - Email string `json:"email"` - Username string `json:"username"` + ID string `json:"id"` + Email string `json:"email"` + Username string `json:"username"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` } type ProjectRef struct { diff --git a/internal/mockapi/projects.go b/internal/mockapi/projects.go index 0ebadf7..4b6321f 100644 --- a/internal/mockapi/projects.go +++ b/internal/mockapi/projects.go @@ -84,3 +84,37 @@ func (h *Handler) handleListRegions(w http.ResponseWriter, _ *http.Request) { Available: true, }}}) } + +func (h *Handler) handleProjectUserAccess(w http.ResponseWriter, req *http.Request) { + h.store.RLock() + defer h.store.RUnlock() + projectID := chi.URLParam(req, "project_id") + require.NoError(h.t, req.ParseForm()) + var ( + projectGrants = make([]*ProjectUserGrant, 0, len(h.store.userGrants)) + userIDs = make(uniqueMap) + orgIDs = make(uniqueMap) + ) + for _, g := range h.store.userGrants { + if g.ResourceType == "project" && g.ResourceID == projectID { + projectGrants = append(projectGrants, &ProjectUserGrant{ + ProjectID: g.ResourceID, + OrganizationID: g.OrganizationID, + UserID: g.UserID, + Permissions: g.Permissions, + GrantedAt: g.GrantedAt, + UpdatedAt: g.UpdatedAt, + }) + userIDs[g.UserID] = struct{}{} + orgIDs[g.OrganizationID] = struct{}{} + } + } + ret := struct { + Items []*ProjectUserGrant `json:"items"` + Links HalLinks `json:"_links"` + }{Items: projectGrants, Links: MakeHALLinks( + "ref:users:0=/ref/users?in="+strings.Join(userIDs.keys(), ","), + "ref:organizations:0=/ref/organizations?in="+strings.Join(orgIDs.keys(), ","), + )} + _ = json.NewEncoder(w).Encode(ret) +} diff --git a/internal/mockapi/users.go b/internal/mockapi/users.go index 6949a5b..2e60092 100644 --- a/internal/mockapi/users.go +++ b/internal/mockapi/users.go @@ -18,7 +18,7 @@ func (h *Handler) handleUserRefs(w http.ResponseWriter, req *http.Request) { ids := strings.Split(req.Form.Get("in"), ",") userRefs := make(map[string]UserRef, len(ids)) for _, id := range ids { - userRefs[id] = UserRef{ID: id, Email: id + "@example.com", Username: id} + userRefs[id] = UserRef{ID: id, Email: id + "@example.com", Username: id, FirstName: "User", LastName: id} } _ = json.NewEncoder(w).Encode(userRefs) } diff --git a/tests/user_list_test.go b/tests/user_list_test.go new file mode 100644 index 0000000..227aee5 --- /dev/null +++ b/tests/user_list_test.go @@ -0,0 +1,80 @@ +package tests + +import ( + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/platformsh/cli/internal/mockapi" +) + +func TestUserList(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), + }) + apiHandler.SetProjects([]*mockapi.Project{ + makeProject(mockProjectID, "org-id-1", vendor, "Project 1", "region-1"), + }) + apiHandler.SetUserGrants([]*mockapi.UserGrant{ + { + ResourceID: "org-id-1", + ResourceType: "organization", + OrganizationID: "org-id-1", + UserID: myUserID, + Permissions: []string{"admin"}, + }, + { + ResourceID: mockProjectID, + ResourceType: "project", + OrganizationID: "org-id-1", + UserID: myUserID, + Permissions: []string{"admin"}, + }, + { + ResourceID: mockProjectID, + ResourceType: "project", + OrganizationID: "org-id-1", + UserID: "user-id-2", + Permissions: []string{"viewer", "development:viewer"}, + }, + { + ResourceID: mockProjectID, + ResourceType: "project", + OrganizationID: "org-id-1", + UserID: "user-id-3", + Permissions: []string{"viewer", "production:viewer", "development:admin", "staging:contributor"}, + }, + }) + + run := runnerWithAuth(t, apiServer.URL, authServer.URL) + + assert.Equal(t, strings.TrimLeft(` ++------------------------+-----------------+--------------+------------+ +| Email address | Name | Project role | ID | ++------------------------+-----------------+--------------+------------+ +| my-user-id@example.com | User my-user-id | admin | my-user-id | +| user-id-2@example.com | User user-id-2 | viewer | user-id-2 | +| user-id-3@example.com | User user-id-3 | viewer | user-id-3 | ++------------------------+-----------------+--------------+------------+ +`, "\n"), run("users", "-p", mockProjectID)) + + assert.Equal(t, strings.TrimLeft(` +Email address Name Project role ID Permissions +my-user-id@example.com User my-user-id admin my-user-id admin +user-id-2@example.com User user-id-2 viewer user-id-2 viewer, development:viewer +user-id-3@example.com User user-id-3 viewer user-id-3 viewer, production:viewer, development:admin, staging:contributor +`, "\n"), run("users", "-p", mockProjectID, "--format", "plain", "--columns", "+perm%")) +}