- Notifications
You must be signed in to change notification settings - Fork927
chore: add organization member api + cli#13577
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -845,11 +845,24 @@ func New(options *Options) *API { | ||
}) | ||
r.Route("/{user}", func(r chi.Router) { | ||
r.Group(func(r chi.Router) { | ||
r.Use( | ||
// Adding a member requires "read" permission | ||
// on the site user. So limited to owners and user-admins. | ||
// TODO: Allow org-admins to add users via some new permission? Or give them | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. For a future PR: I think a new permission would be nice here, I don't think it's intuitive for developers to know that a read permission on a site user grants them access to add new org members. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. @f0ssel one option is just to not require the read permission to add a user, using the system context to fetch a user. I agree it is not intuitive at present. Since orgs are still a ways out, I'm not spending too much time on these details atm. | ||
// read on site users. | ||
httpmw.ExtractUserParam(options.Database), | ||
) | ||
r.Post("/", api.postOrganizationMember) | ||
}) | ||
r.Group(func(r chi.Router) { | ||
r.Use( | ||
httpmw.ExtractOrganizationMemberParam(options.Database), | ||
) | ||
r.Put("/roles", api.putMemberRoles) | ||
r.Post("/workspaces", api.postWorkspacesByOrganization) | ||
}) | ||
}) | ||
}) | ||
}) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -13,6 +13,65 @@ import ( | ||
"github.com/coder/coder/v2/testutil" | ||
) | ||
func TestAddMember(t *testing.T) { | ||
t.Parallel() | ||
t.Run("OK", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I think testing to error cases like missing user and org 404, etc for posterity could be good as well | ||
t.Parallel() | ||
owner := coderdtest.New(t, nil) | ||
first := coderdtest.CreateFirstUser(t, owner) | ||
ctx := testutil.Context(t, testutil.WaitMedium) | ||
org, err := owner.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{ | ||
Name: "other", | ||
DisplayName: "", | ||
Description: "", | ||
Icon: "", | ||
}) | ||
require.NoError(t, err) | ||
// Make a user not in the second organization | ||
_, user := coderdtest.CreateAnotherUser(t, owner, first.OrganizationID) | ||
members, err := owner.OrganizationMembers(ctx, org.ID) | ||
require.NoError(t, err) | ||
require.Len(t, members, 1) // Verify just the 1 member | ||
// Add user to org | ||
_, err = owner.PostOrganizationMember(ctx, org.ID, user.Username) | ||
require.NoError(t, err) | ||
members, err = owner.OrganizationMembers(ctx, org.ID) | ||
require.NoError(t, err) | ||
// Owner + new member | ||
require.Len(t, members, 2) | ||
require.ElementsMatch(t, | ||
[]uuid.UUID{first.UserID, user.ID}, | ||
db2sdk.List(members, onlyIDs)) | ||
}) | ||
t.Run("UserNotExists", func(t *testing.T) { | ||
t.Parallel() | ||
owner := coderdtest.New(t, nil) | ||
_ = coderdtest.CreateFirstUser(t, owner) | ||
ctx := testutil.Context(t, testutil.WaitMedium) | ||
org, err := owner.CreateOrganization(ctx, codersdk.CreateOrganizationRequest{ | ||
Name: "other", | ||
DisplayName: "", | ||
Description: "", | ||
Icon: "", | ||
}) | ||
require.NoError(t, err) | ||
// Add user to org | ||
_, err = owner.PostOrganizationMember(ctx, org.ID, uuid.NewString()) | ||
require.Error(t, err) | ||
var apiErr *codersdk.Error | ||
require.ErrorAs(t, err, &apiErr) | ||
require.Contains(t, apiErr.Message, "must be an existing") | ||
}) | ||
} | ||
func TestListMembers(t *testing.T) { | ||
t.Parallel() | ||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.