Skip to content

Commit 4c93b3b

Browse files
authored
chore: safer SDK deprecation (#14425)
* Handle sdk deprecation better by maintaining cli functions
1 parent e68f18d commit 4c93b3b

28 files changed

+129
-137
lines changed

cli/clitest/golden.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func prepareTestData(t *testing.T) (*codersdk.Client, map[string]string) {
184184
IncludeProvisionerDaemon: true,
185185
})
186186
firstUser := coderdtest.CreateFirstUser(t, rootClient)
187-
secondUser, err := rootClient.CreateUser(ctx, codersdk.CreateUserRequest{
187+
secondUser, err := rootClient.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
188188
Email: "testuser2@coder.com",
189189
Username: "testuser2",
190190
Password: coderdtest.FirstUserParams.Password,

cli/schedule_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func setupTestSchedule(t *testing.T, sched *cron.Schedule) (ownerClient, memberC
3535

3636
ownerClient, db = coderdtest.NewWithDatabase(t, nil)
3737
owner := coderdtest.CreateFirstUser(t, ownerClient)
38-
memberClient, memberUser := coderdtest.CreateAnotherUserMutators(t, ownerClient, owner.OrganizationID, nil, func(r *codersdk.CreateUserRequest) {
38+
memberClient, memberUser := coderdtest.CreateAnotherUserMutators(t, ownerClient, owner.OrganizationID, nil, func(r *codersdk.CreateUserRequestWithOrgs) {
3939
r.Username = "testuser2" // ensure deterministic ordering
4040
})
4141
_ = dbfake.WorkspaceBuild(t, db, database.Workspace{

cli/server_createadminuser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (r *RootCmd) newCreateAdminUserCommand() *serpent.Command {
8383

8484
validateInputs := func(username, email, password string) error {
8585
// Use the validator tags so we match the API's validation.
86-
req := codersdk.CreateUserRequest{
86+
req := codersdk.CreateUserRequestWithOrgs{
8787
Username: "username",
8888
Name: "Admin User",
8989
Email: "email@coder.com",

cli/user_delete_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ func TestUserDelete(t *testing.T) {
2727
pw, err := cryptorand.String(16)
2828
require.NoError(t, err)
2929

30-
_, err = client.CreateUser(ctx, codersdk.CreateUserRequest{
30+
_, err = client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
3131
Email: "colin5@coder.com",
3232
Username: "coolin",
3333
Password: pw,
3434
UserLoginType: codersdk.LoginTypePassword,
3535
OrganizationIDs: []uuid.UUID{owner.OrganizationID},
36-
DisableLogin: false,
3736
})
3837
require.NoError(t, err)
3938

@@ -58,13 +57,12 @@ func TestUserDelete(t *testing.T) {
5857
pw, err := cryptorand.String(16)
5958
require.NoError(t, err)
6059

61-
user, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
60+
user, err := client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
6261
Email: "colin5@coder.com",
6362
Username: "coolin",
6463
Password: pw,
6564
UserLoginType: codersdk.LoginTypePassword,
6665
OrganizationIDs: []uuid.UUID{owner.OrganizationID},
67-
DisableLogin: false,
6866
})
6967
require.NoError(t, err)
7068

@@ -89,13 +87,12 @@ func TestUserDelete(t *testing.T) {
8987
pw, err := cryptorand.String(16)
9088
require.NoError(t, err)
9189

92-
user, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
90+
user, err := client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
9391
Email: "colin5@coder.com",
9492
Username: "coolin",
9593
Password: pw,
9694
UserLoginType: codersdk.LoginTypePassword,
9795
OrganizationIDs: []uuid.UUID{owner.OrganizationID},
98-
DisableLogin: false,
9996
})
10097
require.NoError(t, err)
10198

@@ -122,13 +119,12 @@ func TestUserDelete(t *testing.T) {
122119
// pw, err := cryptorand.String(16)
123120
// require.NoError(t, err)
124121

125-
// toDelete, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
122+
// toDelete, err := client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
126123
// Email: "colin5@coder.com",
127124
// Username: "coolin",
128125
// Password: pw,
129126
// UserLoginType: codersdk.LoginTypePassword,
130127
// OrganizationID: aUser.OrganizationID,
131-
// DisableLogin: false,
132128
// })
133129
// require.NoError(t, err)
134130

cli/usercreate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (r *RootCmd) userCreate() *serpent.Command {
9595
}
9696
}
9797

98-
_, err = client.CreateUser(inv.Context(), codersdk.CreateUserRequest{
98+
_, err = client.CreateUserWithOrgs(inv.Context(), codersdk.CreateUserRequestWithOrgs{
9999
Email: email,
100100
Username: username,
101101
Name: name,

coderd/apidoc/docs.go

Lines changed: 2 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 2 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/coderdtest/coderdtest.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ func CreateAnotherUser(t testing.TB, client *codersdk.Client, organizationID uui
649649
return createAnotherUserRetry(t, client, []uuid.UUID{organizationID}, 5, roles)
650650
}
651651

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

@@ -676,8 +676,8 @@ func AuthzUserSubject(user codersdk.User, orgID uuid.UUID) rbac.Subject {
676676
}
677677
}
678678

679-
func createAnotherUserRetry(t testing.TB, client *codersdk.Client, organizationIDs []uuid.UUID, retries int, roles []rbac.RoleIdentifier, mutators ...func(r *codersdk.CreateUserRequest)) (*codersdk.Client, codersdk.User) {
680-
req := codersdk.CreateUserRequest{
679+
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) {
680+
req := codersdk.CreateUserRequestWithOrgs{
681681
Email: namesgenerator.GetRandomName(10) + "@coder.com",
682682
Username: RandomUsername(t),
683683
Name: RandomName(t),
@@ -688,7 +688,7 @@ func createAnotherUserRetry(t testing.TB, client *codersdk.Client, organizationI
688688
m(&req)
689689
}
690690

691-
user, err := client.CreateUser(context.Background(), req)
691+
user, err := client.CreateUserWithOrgs(context.Background(), req)
692692
var apiError *codersdk.Error
693693
// If the user already exists by username or email conflict, try again up to "retries" times.
694694
if err != nil && retries >= 0 && xerrors.As(err, &apiError) {
@@ -700,7 +700,7 @@ func createAnotherUserRetry(t testing.TB, client *codersdk.Client, organizationI
700700
require.NoError(t, err)
701701

702702
var sessionToken string
703-
if req.DisableLogin || req.UserLoginType == codersdk.LoginTypeNone {
703+
if req.UserLoginType == codersdk.LoginTypeNone {
704704
// Cannot log in with a disabled login user. So make it an api key from
705705
// the client making this user.
706706
token, err := client.CreateToken(context.Background(), user.ID.String(), codersdk.CreateTokenRequest{

coderd/insights_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ func TestTemplateInsights_Golden(t *testing.T) {
523523

524524
// Prepare all test users.
525525
for _, user := range users {
526-
user.client, user.sdk = coderdtest.CreateAnotherUserMutators(t, client, firstUser.OrganizationID, nil, func(r *codersdk.CreateUserRequest) {
526+
user.client, user.sdk = coderdtest.CreateAnotherUserMutators(t, client, firstUser.OrganizationID, nil, func(r *codersdk.CreateUserRequestWithOrgs) {
527527
r.Username = user.name
528528
})
529529
user.client.SetLogger(logger.Named("user").With(slog.Field{Name: "name", Value: user.name}))

coderd/userauth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C
14371437

14381438
//nolint:gocritic
14391439
user, err = api.CreateUser(dbauthz.AsSystemRestricted(ctx), tx, CreateUserRequest{
1440-
CreateUserRequest: codersdk.CreateUserRequest{
1440+
CreateUserRequestWithOrgs: codersdk.CreateUserRequestWithOrgs{
14411441
Email: params.Email,
14421442
Username: params.Username,
14431443
OrganizationIDs: []uuid.UUID{defaultOrganization.ID},

coderd/userauth_test.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,28 +106,12 @@ func TestUserLogin(t *testing.T) {
106106
require.ErrorAs(t, err, &apiErr)
107107
require.Equal(t, http.StatusUnauthorized, apiErr.StatusCode())
108108
})
109-
// Password auth should fail if the user is made without password login.
110-
t.Run("DisableLoginDeprecatedField", func(t *testing.T) {
111-
t.Parallel()
112-
client := coderdtest.New(t, nil)
113-
user := coderdtest.CreateFirstUser(t, client)
114-
anotherClient, anotherUser := coderdtest.CreateAnotherUserMutators(t, client, user.OrganizationID, nil, func(r *codersdk.CreateUserRequest) {
115-
r.Password = ""
116-
r.DisableLogin = true
117-
})
118-
119-
_, err := anotherClient.LoginWithPassword(context.Background(), codersdk.LoginWithPasswordRequest{
120-
Email: anotherUser.Email,
121-
Password: "SomeSecurePassword!",
122-
})
123-
require.Error(t, err)
124-
})
125109

126110
t.Run("LoginTypeNone", func(t *testing.T) {
127111
t.Parallel()
128112
client := coderdtest.New(t, nil)
129113
user := coderdtest.CreateFirstUser(t, client)
130-
anotherClient, anotherUser := coderdtest.CreateAnotherUserMutators(t, client, user.OrganizationID, nil, func(r *codersdk.CreateUserRequest) {
114+
anotherClient, anotherUser := coderdtest.CreateAnotherUserMutators(t, client, user.OrganizationID, nil, func(r *codersdk.CreateUserRequestWithOrgs) {
131115
r.Password = ""
132116
r.UserLoginType = codersdk.LoginTypeNone
133117
})
@@ -1470,7 +1454,7 @@ func TestUserLogout(t *testing.T) {
14701454
//nolint:gosec
14711455
password = "SomeSecurePassword123!"
14721456
)
1473-
newUser, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
1457+
newUser, err := client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
14741458
Email: email,
14751459
Username: username,
14761460
Password: password,

coderd/users.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (api *API) postFirstUser(rw http.ResponseWriter, r *http.Request) {
187187

188188
//nolint:gocritic // needed to create first user
189189
user, err := api.CreateUser(dbauthz.AsSystemRestricted(ctx), api.Database, CreateUserRequest{
190-
CreateUserRequest: codersdk.CreateUserRequest{
190+
CreateUserRequestWithOrgs: codersdk.CreateUserRequestWithOrgs{
191191
Email: createUser.Email,
192192
Username: createUser.Username,
193193
Name: createUser.Name,
@@ -342,7 +342,7 @@ func (api *API) GetUsers(rw http.ResponseWriter, r *http.Request) ([]database.Us
342342
// @Accept json
343343
// @Produce json
344344
// @Tags Users
345-
// @Param request body codersdk.CreateUserRequest true "Create user request"
345+
// @Param request body codersdk.CreateUserRequestWithOrgs true "Create user request"
346346
// @Success 201 {object} codersdk.User
347347
// @Router /users [post]
348348
func (api *API) postUser(rw http.ResponseWriter, r *http.Request) {
@@ -356,15 +356,11 @@ func (api *API) postUser(rw http.ResponseWriter, r *http.Request) {
356356
})
357357
defer commitAudit()
358358

359-
var req codersdk.CreateUserRequest
359+
var req codersdk.CreateUserRequestWithOrgs
360360
if !httpapi.Read(ctx, rw, r, &req) {
361361
return
362362
}
363363

364-
if req.UserLoginType == "" && req.DisableLogin {
365-
// Handle the deprecated field
366-
req.UserLoginType = codersdk.LoginTypeNone
367-
}
368364
if req.UserLoginType == "" {
369365
// Default to password auth
370366
req.UserLoginType = codersdk.LoginTypePassword
@@ -484,8 +480,8 @@ func (api *API) postUser(rw http.ResponseWriter, r *http.Request) {
484480
}
485481

486482
user, err := api.CreateUser(ctx, api.Database, CreateUserRequest{
487-
CreateUserRequest: req,
488-
LoginType: loginType,
483+
CreateUserRequestWithOrgs: req,
484+
LoginType: loginType,
489485
})
490486
if dbauthz.IsNotAuthorizedError(err) {
491487
httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{
@@ -1283,7 +1279,7 @@ func (api *API) organizationByUserAndName(rw http.ResponseWriter, r *http.Reques
12831279
}
12841280

12851281
type CreateUserRequest struct {
1286-
codersdk.CreateUserRequest
1282+
codersdk.CreateUserRequestWithOrgs
12871283
LoginType database.LoginType
12881284
SkipNotifications bool
12891285
}

0 commit comments

Comments
 (0)