-
Notifications
You must be signed in to change notification settings - Fork 6
fix(org:user): report missing organization permissions plainly #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vrobert78
wants to merge
2
commits into
main
Choose a base branch
from
fix/org-user-permission-message
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,170 @@ | ||
| package tests | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/upsun/cli/pkg/mockapi" | ||
| ) | ||
|
|
||
| // TestOrgUserNoPermission covers the organization user commands when the user | ||
| // has no permission on the organization itself, as happens when they are only | ||
| // an admin on one of its projects. The API then returns an organization without | ||
| // the "members" and "invitations" links, and the commands must say so plainly | ||
| // instead of failing on an API error or an unresolved HAL link. | ||
| func TestOrgUserNoPermission(t *testing.T) { | ||
| authServer := mockapi.NewAuthServer(t) | ||
| defer authServer.Close() | ||
|
|
||
| myUserID := "my-user-id" | ||
| orgID := "org-id-no-perms" | ||
| orgName := "no-perms-org" | ||
|
|
||
| apiHandler := mockapi.NewHandler(t) | ||
| apiHandler.SetMyUser(&mockapi.User{ID: myUserID}) | ||
|
|
||
| // An organization the user cannot administer: no "members" link, and no | ||
| // "invitations" link. Only the owner-less "self" link is present. | ||
| apiHandler.SetOrgs([]*mockapi.Org{makeOrg(orgID, orgName, "No Perms Org", "another-user-id", "flexible")}) | ||
|
|
||
| // The members endpoint is what the commands used to reach anyway, bypassing | ||
| // the links: it must not be requested at all. | ||
| membersRequested := false | ||
| apiHandler.Get("/organizations/"+orgID+"/members", func(w http.ResponseWriter, _ *http.Request) { | ||
| membersRequested = true | ||
| w.WriteHeader(http.StatusForbidden) | ||
| _, _ = w.Write([]byte(`{"status":403,"title":"Forbidden",` + | ||
| `"detail":"You do not have access to view this organization's members."}`)) | ||
| }) | ||
|
|
||
| apiServer := httptest.NewServer(apiHandler) | ||
| defer apiServer.Close() | ||
|
|
||
| f := newCommandFactory(t, apiServer.URL, authServer.URL) | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| args []string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "org:user:add", | ||
| args: []string{"org:user:add", "-o", orgName, "test@example.com"}, | ||
| expected: "You do not have permission to add users to the organization", | ||
| }, | ||
| { | ||
| name: "org:user:update", | ||
| args: []string{"org:user:update", "-o", orgName, "test@example.com", "--permission", "billing"}, | ||
| expected: "You do not have permission to update users in the organization", | ||
| }, | ||
| { | ||
| name: "org:user:delete", | ||
| args: []string{"org:user:delete", "-o", orgName, "test@example.com"}, | ||
| expected: "You do not have permission to delete users from the organization", | ||
| }, | ||
| { | ||
| // The read-only commands already behaved this way; keep them covered | ||
| // so the wording of the whole family stays consistent. | ||
| name: "org:user:list", | ||
| args: []string{"org:user:list", "-o", orgName}, | ||
| expected: "You do not have permission to view users in the organization", | ||
| }, | ||
| { | ||
| name: "org:user:get", | ||
| args: []string{"org:user:get", "-o", orgName, "test@example.com"}, | ||
| expected: "You do not have permission to view users in the organization", | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| stdout, stderr, err := f.RunCombinedOutput(c.args...) | ||
| require.Error(t, err, "the command must fail\nstdout: %s\nstderr: %s", stdout, stderr) | ||
| assert.Contains(t, stderr, c.expected) | ||
| assert.Contains(t, stderr, orgName) | ||
|
|
||
| // None of the raw failures should surface. | ||
| assert.NotContains(t, stderr, "Link not found") | ||
| assert.NotContains(t, stderr, "ApiResponseException") | ||
| assert.NotContains(t, stderr, "403 Forbidden") | ||
| assert.NotContains(t, stderr, "Permission denied. Check your permissions") | ||
| // Nor should the usage summary, which Symfony prints for exceptions. | ||
| assert.NotContains(t, stderr, "[-o|--org ORG]") | ||
| }) | ||
| } | ||
|
|
||
| // Interactively, with no email given, org:user:update used to pick the user | ||
| // from a members listing built from the organization URL, bypassing the links | ||
| // entirely — which produced a raw 403 on top of the permission error. | ||
| t.Run("org:user:update interactive", func(t *testing.T) { | ||
| stdout, stderr, err := f.RunInteractive("", "org:user:update", "-o", orgName) | ||
| require.Error(t, err, "stdout: %s\nstderr: %s", stdout, stderr) | ||
| assert.Contains(t, stderr, "You do not have permission to update users in the organization") | ||
| assert.NotContains(t, stderr, "403 Forbidden") | ||
| assert.NotContains(t, stderr, "ApiResponseException") | ||
| assert.NotContains(t, stderr, "Permission denied. Check your permissions") | ||
| }) | ||
|
|
||
| assert.False(t, membersRequested, "the members endpoint must not be requested without the link") | ||
| } | ||
|
|
||
| // TestOrgUserWithPermission is the counterpart: with the links present, the | ||
| // commands must get past the permission check and reach their normal behavior. | ||
| func TestOrgUserWithPermission(t *testing.T) { | ||
| authServer := mockapi.NewAuthServer(t) | ||
| defer authServer.Close() | ||
|
|
||
| myUserID := "my-user-id" | ||
| orgID := "org-id-with-perms" | ||
| orgName := "with-perms-org" | ||
|
|
||
| apiHandler := mockapi.NewHandler(t) | ||
| apiHandler.SetMyUser(&mockapi.User{ID: myUserID}) | ||
|
|
||
| org := makeOrg(orgID, orgName, "With Perms Org", myUserID, "flexible") | ||
| org.Links["members"] = mockapi.HALLink{HREF: "/organizations/" + url.PathEscape(orgID) + "/members"} | ||
| org.Links["invitations"] = mockapi.HALLink{HREF: "/organizations/" + url.PathEscape(orgID) + "/invitations"} | ||
| apiHandler.SetOrgs([]*mockapi.Org{org}) | ||
|
|
||
| // An empty members list: the commands get past the permission check and then | ||
| // report that the user was not found. | ||
| apiHandler.Get("/organizations/"+orgID+"/members", func(w http.ResponseWriter, _ *http.Request) { | ||
| _, _ = w.Write([]byte(`{"items":[],"_links":{"self":{"href":"/organizations/` + orgID + `/members"}}}`)) | ||
| }) | ||
|
|
||
| apiServer := httptest.NewServer(apiHandler) | ||
| defer apiServer.Close() | ||
|
|
||
| f := newCommandFactory(t, apiServer.URL, authServer.URL) | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| args []string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "org:user:update", | ||
| args: []string{"org:user:update", "-o", orgName, "test@example.com", "--permission", "billing"}, | ||
| expected: "was not found in the organization", | ||
| }, | ||
| { | ||
| name: "org:user:delete", | ||
| args: []string{"org:user:delete", "-o", orgName, "test@example.com"}, | ||
| expected: "User not found", | ||
| }, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| stdout, stderr, err := f.RunCombinedOutput(c.args...) | ||
| require.Error(t, err, "stdout: %s\nstderr: %s", stdout, stderr) | ||
| assert.Contains(t, stderr, c.expected) | ||
| assert.NotContains(t, stderr, "You do not have permission") | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.