Skip to content

Commit

Permalink
Test user:list command
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Oct 12, 2024
1 parent 276fb05 commit 72bd9b1
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 4 deletions.
1 change: 1 addition & 0 deletions internal/mockapi/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions internal/mockapi/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions internal/mockapi/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion internal/mockapi/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
80 changes: 80 additions & 0 deletions tests/user_list_test.go
Original file line number Diff line number Diff line change
@@ -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%"))
}

0 comments on commit 72bd9b1

Please sign in to comment.