Skip to content

chore!: allow CreateUser to accept multiple organizations #14383

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

Merged
merged 10 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup tests
  • Loading branch information
Emyrk committed Aug 22, 2024
commit 0070ccabe7d70fa41533870a766986c4486a3fa9
1 change: 1 addition & 0 deletions cli/clitest/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/cli/config"
Expand Down
10 changes: 5 additions & 5 deletions cli/server_createadminuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ func (r *RootCmd) newCreateAdminUserCommand() *serpent.Command {
validateInputs := func(username, email, password string) error {
// Use the validator tags so we match the API's validation.
req := codersdk.CreateUserRequest{
Username: "username",
Name: "Admin User",
Email: "email@coder.com",
Password: "ValidPa$$word123!",
OrganizationID: uuid.New(),
Username: "username",
Name: "Admin User",
Email: "email@coder.com",
Password: "ValidPa$$word123!",
OrganizationIDs: []uuid.UUID{uuid.New()},
}
if username != "" {
req.Username = username
Expand Down
37 changes: 19 additions & 18 deletions cli/user_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/cli/clitest"
Expand All @@ -27,12 +28,12 @@ func TestUserDelete(t *testing.T) {
require.NoError(t, err)

_, err = client.CreateUser(ctx, codersdk.CreateUserRequest{
Email: "colin5@coder.com",
Username: "coolin",
Password: pw,
UserLoginType: codersdk.LoginTypePassword,
OrganizationID: owner.OrganizationID,
DisableLogin: false,
Email: "colin5@coder.com",
Username: "coolin",
Password: pw,
UserLoginType: codersdk.LoginTypePassword,
OrganizationIDs: []uuid.UUID{owner.OrganizationID},
DisableLogin: false,
})
require.NoError(t, err)

Expand All @@ -58,12 +59,12 @@ func TestUserDelete(t *testing.T) {
require.NoError(t, err)

user, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
Email: "colin5@coder.com",
Username: "coolin",
Password: pw,
UserLoginType: codersdk.LoginTypePassword,
OrganizationID: owner.OrganizationID,
DisableLogin: false,
Email: "colin5@coder.com",
Username: "coolin",
Password: pw,
UserLoginType: codersdk.LoginTypePassword,
OrganizationIDs: []uuid.UUID{owner.OrganizationID},
DisableLogin: false,
})
require.NoError(t, err)

Expand All @@ -89,12 +90,12 @@ func TestUserDelete(t *testing.T) {
require.NoError(t, err)

user, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
Email: "colin5@coder.com",
Username: "coolin",
Password: pw,
UserLoginType: codersdk.LoginTypePassword,
OrganizationID: owner.OrganizationID,
DisableLogin: false,
Email: "colin5@coder.com",
Username: "coolin",
Password: pw,
UserLoginType: codersdk.LoginTypePassword,
OrganizationIDs: []uuid.UUID{owner.OrganizationID},
DisableLogin: false,
})
require.NoError(t, err)

Expand Down
13 changes: 7 additions & 6 deletions cli/usercreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"golang.org/x/xerrors"

"github.com/coder/pretty"
Expand Down Expand Up @@ -95,12 +96,12 @@ func (r *RootCmd) userCreate() *serpent.Command {
}

_, err = client.CreateUser(inv.Context(), codersdk.CreateUserRequest{
Email: email,
Username: username,
Name: name,
Password: password,
OrganizationID: organization.ID,
UserLoginType: userLoginType,
Email: email,
Username: username,
Name: name,
Password: password,
OrganizationIDs: []uuid.UUID{organization.ID},
UserLoginType: userLoginType,
})
if err != nil {
return err
Expand Down
16 changes: 8 additions & 8 deletions coderd/workspaceapps/apptest/apptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,10 +1207,10 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
// since they have access to everything.
ownerClient = appDetails.SDKClient
user, err := ownerClient.CreateUser(ctx, codersdk.CreateUserRequest{
Email: "user@coder.com",
Username: "user",
Password: password,
OrganizationID: appDetails.FirstUser.OrganizationID,
Email: "user@coder.com",
Username: "user",
Password: password,
OrganizationIDs: []uuid.UUID{appDetails.FirstUser.OrganizationID},
})
require.NoError(t, err)

Expand Down Expand Up @@ -1259,10 +1259,10 @@ func Run(t *testing.T, appHostIsPrimary bool, factory DeploymentFactory) {
})
require.NoError(t, err)
userInOtherOrg, err := ownerClient.CreateUser(ctx, codersdk.CreateUserRequest{
Email: "no-template-access@coder.com",
Username: "no-template-access",
Password: password,
OrganizationID: otherOrg.ID,
Email: "no-template-access@coder.com",
Username: "no-template-access",
Password: password,
OrganizationIDs: []uuid.UUID{otherOrg.ID},
})
require.NoError(t, err)

Expand Down
8 changes: 4 additions & 4 deletions enterprise/coderd/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,10 @@ func TestGroup(t *testing.T) {

// cannot explicitly set a dormant user status so must create a new user
anotherUser, err := userAdminClient.CreateUser(ctx, codersdk.CreateUserRequest{
Email: "coder@coder.com",
Username: "coder",
Password: "SomeStrongPassword!",
OrganizationID: user.OrganizationID,
Email: "coder@coder.com",
Username: "coder",
Password: "SomeStrongPassword!",
OrganizationIDs: []uuid.UUID{user.OrganizationID},
})
require.NoError(t, err)

Expand Down
6 changes: 3 additions & 3 deletions enterprise/coderd/scim.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ func (api *API) scimPostUser(rw http.ResponseWriter, r *http.Request) {
//nolint:gocritic // needed for SCIM
dbUser, err = api.AGPL.CreateUser(dbauthz.AsSystemRestricted(ctx), api.Database, agpl.CreateUserRequest{
CreateUserRequest: codersdk.CreateUserRequest{
Username: sUser.UserName,
Email: email,
OrganizationID: defaultOrganization.ID,
Username: sUser.UserName,
Email: email,
OrganizationIDs: []uuid.UUID{defaultOrganization.ID},
},
LoginType: database.LoginTypeOIDC,
// Do not send notifications to user admins as SCIM endpoint might be called sequentially to all users.
Expand Down
26 changes: 11 additions & 15 deletions enterprise/coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

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

"github.com/coder/coder/v2/coderd/coderdtest"
Expand Down Expand Up @@ -510,10 +509,10 @@ func TestEnterprisePostUser(t *testing.T) {
})

_, err := notInOrg.CreateUser(ctx, codersdk.CreateUserRequest{
Email: "some@domain.com",
Username: "anotheruser",
Password: "SomeSecurePassword!",
OrganizationID: org.ID,
Email: "some@domain.com",
Username: "anotheruser",
Password: "SomeSecurePassword!",
OrganizationIDs: []uuid.UUID{org.ID},
})
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
Expand Down Expand Up @@ -544,10 +543,10 @@ func TestEnterprisePostUser(t *testing.T) {
org := coderdenttest.CreateOrganization(t, other, coderdenttest.CreateOrganizationOptions{})

_, err := notInOrg.CreateUser(ctx, codersdk.CreateUserRequest{
Email: "some@domain.com",
Username: "anotheruser",
Password: "SomeSecurePassword!",
OrganizationID: org.ID,
Email: "some@domain.com",
Username: "anotheruser",
Password: "SomeSecurePassword!",
OrganizationIDs: []uuid.UUID{org.ID},
})
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
Expand All @@ -559,7 +558,7 @@ func TestEnterprisePostUser(t *testing.T) {
dv := coderdtest.DeploymentValues(t)
dv.Experiments = []string{string(codersdk.ExperimentMultiOrganization)}

client, firstUser := coderdenttest.New(t, &coderdenttest.Options{
client, _ := coderdenttest.New(t, &coderdenttest.Options{
Options: &coderdtest.Options{
DeploymentValues: dv,
},
Expand All @@ -578,14 +577,11 @@ func TestEnterprisePostUser(t *testing.T) {

// nolint:gocritic // intentional using the owner.
// Manually making a user with the request instead of the coderdtest util
user, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
_, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
Email: "another@user.org",
Username: "someone-else",
Password: "SomeSecurePassword!",
})
require.NoError(t, err)

require.Len(t, user.OrganizationIDs, 1)
assert.Equal(t, firstUser.OrganizationID, user.OrganizationIDs[0])
require.ErrorContains(t, err, "No organization specified")
})
}
8 changes: 4 additions & 4 deletions scaletest/createworkspaces/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ func (r *Runner) Run(ctx context.Context, id string, logs io.Writer) error {
_, _ = fmt.Fprintln(logs, "Creating user:")

user, err = r.client.CreateUser(ctx, codersdk.CreateUserRequest{
OrganizationID: r.cfg.User.OrganizationID,
Username: r.cfg.User.Username,
Email: r.cfg.User.Email,
Password: password,
OrganizationIDs: []uuid.UUID{r.cfg.User.OrganizationID},
Username: r.cfg.User.Username,
Email: r.cfg.User.Email,
Password: password,
})
if err != nil {
return xerrors.Errorf("create user: %w", err)
Expand Down