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 all commits
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
11 changes: 6 additions & 5 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 Expand Up @@ -183,11 +184,11 @@ func prepareTestData(t *testing.T) (*codersdk.Client, map[string]string) {
IncludeProvisionerDaemon: true,
})
firstUser := coderdtest.CreateFirstUser(t, rootClient)
secondUser, err := rootClient.CreateUser(ctx, codersdk.CreateUserRequest{
Email: "testuser2@coder.com",
Username: "testuser2",
Password: coderdtest.FirstUserParams.Password,
OrganizationID: firstUser.OrganizationID,
secondUser, err := rootClient.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
Email: "testuser2@coder.com",
Username: "testuser2",
Password: coderdtest.FirstUserParams.Password,
OrganizationIDs: []uuid.UUID{firstUser.OrganizationID},
})
require.NoError(t, err)
version := coderdtest.CreateTemplateVersion(t, rootClient, firstUser.OrganizationID, nil)
Expand Down
2 changes: 1 addition & 1 deletion cli/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func setupTestSchedule(t *testing.T, sched *cron.Schedule) (ownerClient, memberC

ownerClient, db = coderdtest.NewWithDatabase(t, nil)
owner := coderdtest.CreateFirstUser(t, ownerClient)
memberClient, memberUser := coderdtest.CreateAnotherUserMutators(t, ownerClient, owner.OrganizationID, nil, func(r *codersdk.CreateUserRequest) {
memberClient, memberUser := coderdtest.CreateAnotherUserMutators(t, ownerClient, owner.OrganizationID, nil, func(r *codersdk.CreateUserRequestWithOrgs) {
r.Username = "testuser2" // ensure deterministic ordering
})
_ = dbfake.WorkspaceBuild(t, db, database.Workspace{
Expand Down
12 changes: 6 additions & 6 deletions cli/server_createadminuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ 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(),
req := codersdk.CreateUserRequestWithOrgs{
Username: "username",
Name: "Admin User",
Email: "email@coder.com",
Password: "ValidPa$$word123!",
OrganizationIDs: []uuid.UUID{uuid.New()},
}
if username != "" {
req.Username = username
Expand Down
43 changes: 20 additions & 23 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 @@ -26,13 +27,12 @@ func TestUserDelete(t *testing.T) {
pw, err := cryptorand.String(16)
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,
_, err = client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
Email: "colin5@coder.com",
Username: "coolin",
Password: pw,
UserLoginType: codersdk.LoginTypePassword,
OrganizationIDs: []uuid.UUID{owner.OrganizationID},
})
require.NoError(t, err)

Expand All @@ -57,13 +57,12 @@ func TestUserDelete(t *testing.T) {
pw, err := cryptorand.String(16)
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,
user, err := client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
Email: "colin5@coder.com",
Username: "coolin",
Password: pw,
UserLoginType: codersdk.LoginTypePassword,
OrganizationIDs: []uuid.UUID{owner.OrganizationID},
})
require.NoError(t, err)

Expand All @@ -88,13 +87,12 @@ func TestUserDelete(t *testing.T) {
pw, err := cryptorand.String(16)
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,
user, err := client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
Email: "colin5@coder.com",
Username: "coolin",
Password: pw,
UserLoginType: codersdk.LoginTypePassword,
OrganizationIDs: []uuid.UUID{owner.OrganizationID},
})
require.NoError(t, err)

Expand All @@ -121,13 +119,12 @@ func TestUserDelete(t *testing.T) {
// pw, err := cryptorand.String(16)
// require.NoError(t, err)

// toDelete, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
// toDelete, err := client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
// Email: "colin5@coder.com",
// Username: "coolin",
// Password: pw,
// UserLoginType: codersdk.LoginTypePassword,
// OrganizationID: aUser.OrganizationID,
// DisableLogin: false,
// })
// require.NoError(t, err)

Expand Down
15 changes: 8 additions & 7 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 @@ -94,13 +95,13 @@ 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,
_, err = client.CreateUserWithOrgs(inv.Context(), codersdk.CreateUserRequestWithOrgs{
Email: email,
Username: username,
Name: name,
Password: password,
OrganizationIDs: []uuid.UUID{organization.ID},
UserLoginType: userLoginType,
})
if err != nil {
return err
Expand Down
18 changes: 9 additions & 9 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 16 additions & 15 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,11 +646,11 @@ func CreateFirstUser(t testing.TB, client *codersdk.Client) codersdk.CreateFirst
// CreateAnotherUser creates and authenticates a new user.
// Roles can include org scoped roles with 'roleName:<organization_id>'
func CreateAnotherUser(t testing.TB, client *codersdk.Client, organizationID uuid.UUID, roles ...rbac.RoleIdentifier) (*codersdk.Client, codersdk.User) {
return createAnotherUserRetry(t, client, organizationID, 5, roles)
return createAnotherUserRetry(t, client, []uuid.UUID{organizationID}, 5, roles)
}

func CreateAnotherUserMutators(t testing.TB, client *codersdk.Client, organizationID uuid.UUID, roles []rbac.RoleIdentifier, mutators ...func(r *codersdk.CreateUserRequest)) (*codersdk.Client, codersdk.User) {
return createAnotherUserRetry(t, client, organizationID, 5, roles, mutators...)
func CreateAnotherUserMutators(t testing.TB, client *codersdk.Client, organizationID uuid.UUID, roles []rbac.RoleIdentifier, mutators ...func(r *codersdk.CreateUserRequestWithOrgs)) (*codersdk.Client, codersdk.User) {
return createAnotherUserRetry(t, client, []uuid.UUID{organizationID}, 5, roles, mutators...)
}

// AuthzUserSubject does not include the user's groups.
Expand All @@ -676,31 +676,31 @@ func AuthzUserSubject(user codersdk.User, orgID uuid.UUID) rbac.Subject {
}
}

func createAnotherUserRetry(t testing.TB, client *codersdk.Client, organizationID uuid.UUID, retries int, roles []rbac.RoleIdentifier, mutators ...func(r *codersdk.CreateUserRequest)) (*codersdk.Client, codersdk.User) {
req := codersdk.CreateUserRequest{
Email: namesgenerator.GetRandomName(10) + "@coder.com",
Username: RandomUsername(t),
Name: RandomName(t),
Password: "SomeSecurePassword!",
OrganizationID: organizationID,
func createAnotherUserRetry(t testing.TB, client *codersdk.Client, organizationIDs []uuid.UUID, retries int, roles []rbac.RoleIdentifier, mutators ...func(r *codersdk.CreateUserRequestWithOrgs)) (*codersdk.Client, codersdk.User) {
req := codersdk.CreateUserRequestWithOrgs{
Email: namesgenerator.GetRandomName(10) + "@coder.com",
Username: RandomUsername(t),
Name: RandomName(t),
Password: "SomeSecurePassword!",
OrganizationIDs: organizationIDs,
}
for _, m := range mutators {
m(&req)
}

user, err := client.CreateUser(context.Background(), req)
user, err := client.CreateUserWithOrgs(context.Background(), req)
var apiError *codersdk.Error
// If the user already exists by username or email conflict, try again up to "retries" times.
if err != nil && retries >= 0 && xerrors.As(err, &apiError) {
if apiError.StatusCode() == http.StatusConflict {
retries--
return createAnotherUserRetry(t, client, organizationID, retries, roles)
return createAnotherUserRetry(t, client, organizationIDs, retries, roles)
}
}
require.NoError(t, err)

var sessionToken string
if req.DisableLogin || req.UserLoginType == codersdk.LoginTypeNone {
if req.UserLoginType == codersdk.LoginTypeNone {
// Cannot log in with a disabled login user. So make it an api key from
// the client making this user.
token, err := client.CreateToken(context.Background(), user.ID.String(), codersdk.CreateTokenRequest{
Expand Down Expand Up @@ -763,8 +763,9 @@ func createAnotherUserRetry(t testing.TB, client *codersdk.Client, organizationI
require.NoError(t, err, "update site roles")

// isMember keeps track of which orgs the user was added to as a member
isMember := map[uuid.UUID]bool{
organizationID: true,
isMember := make(map[uuid.UUID]bool)
for _, orgID := range organizationIDs {
isMember[orgID] = true
}

// Update org roles
Expand Down
2 changes: 1 addition & 1 deletion coderd/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ func TestTemplateInsights_Golden(t *testing.T) {

// Prepare all test users.
for _, user := range users {
user.client, user.sdk = coderdtest.CreateAnotherUserMutators(t, client, firstUser.OrganizationID, nil, func(r *codersdk.CreateUserRequest) {
user.client, user.sdk = coderdtest.CreateAnotherUserMutators(t, client, firstUser.OrganizationID, nil, func(r *codersdk.CreateUserRequestWithOrgs) {
r.Username = user.name
})
user.client.SetLogger(logger.Named("user").With(slog.Field{Name: "name", Value: user.name}))
Expand Down
10 changes: 5 additions & 5 deletions coderd/userauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1436,11 +1436,11 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C
}

//nolint:gocritic
user, _, err = api.CreateUser(dbauthz.AsSystemRestricted(ctx), tx, CreateUserRequest{
CreateUserRequest: codersdk.CreateUserRequest{
Email: params.Email,
Username: params.Username,
OrganizationID: defaultOrganization.ID,
user, err = api.CreateUser(dbauthz.AsSystemRestricted(ctx), tx, CreateUserRequest{
CreateUserRequestWithOrgs: codersdk.CreateUserRequestWithOrgs{
Email: params.Email,
Username: params.Username,
OrganizationIDs: []uuid.UUID{defaultOrganization.ID},
},
LoginType: params.LoginType,
})
Expand Down
28 changes: 6 additions & 22 deletions coderd/userauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,12 @@ func TestUserLogin(t *testing.T) {
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusUnauthorized, apiErr.StatusCode())
})
// Password auth should fail if the user is made without password login.
t.Run("DisableLoginDeprecatedField", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
anotherClient, anotherUser := coderdtest.CreateAnotherUserMutators(t, client, user.OrganizationID, nil, func(r *codersdk.CreateUserRequest) {
r.Password = ""
r.DisableLogin = true
})

_, err := anotherClient.LoginWithPassword(context.Background(), codersdk.LoginWithPasswordRequest{
Email: anotherUser.Email,
Password: "SomeSecurePassword!",
})
require.Error(t, err)
})

t.Run("LoginTypeNone", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
anotherClient, anotherUser := coderdtest.CreateAnotherUserMutators(t, client, user.OrganizationID, nil, func(r *codersdk.CreateUserRequest) {
anotherClient, anotherUser := coderdtest.CreateAnotherUserMutators(t, client, user.OrganizationID, nil, func(r *codersdk.CreateUserRequestWithOrgs) {
r.Password = ""
r.UserLoginType = codersdk.LoginTypeNone
})
Expand Down Expand Up @@ -1470,11 +1454,11 @@ func TestUserLogout(t *testing.T) {
//nolint:gosec
password = "SomeSecurePassword123!"
)
newUser, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
Email: email,
Username: username,
Password: password,
OrganizationID: firstUser.OrganizationID,
newUser, err := client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
Email: email,
Username: username,
Password: password,
OrganizationIDs: []uuid.UUID{firstUser.OrganizationID},
})
require.NoError(t, err)

Expand Down
Loading
Loading