feat: allow passing password via flag in user password (#191)

This commit is contained in:
Daniel Moran 2021-07-13 14:37:23 -04:00 committed by GitHub
parent 9e24437403
commit 127f829226
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 7 deletions

View File

@ -138,11 +138,12 @@ func (c Client) Update(ctx context.Context, params *UpdateParmas) error {
}
type SetPasswordParams struct {
Id influxid.ID
Name string
Id influxid.ID
Name string
Password string
}
func (c Client) SetPassword(ctx context.Context, params *SetPasswordParams) error {
func (c Client) SetPassword(ctx context.Context, params *SetPasswordParams) (err error) {
if !params.Id.Valid() && params.Name == "" {
return ErrMustSpecifyUser
}
@ -160,9 +161,12 @@ func (c Client) SetPassword(ctx context.Context, params *SetPasswordParams) erro
id = (*users.Users)[0].GetId()
}
password, err := c.StdIO.GetPassword(fmt.Sprintf("Please type new password for %q", displayName))
if err != nil {
return err
password := params.Password
if password == "" {
password, err = c.StdIO.GetPassword(fmt.Sprintf("Please type new password for %q", displayName))
if err != nil {
return err
}
}
body := api.PasswordResetBody{Password: password}

View File

@ -11,7 +11,7 @@ import (
"github.com/influxdata/influx-cli/v2/api"
"github.com/influxdata/influx-cli/v2/clients"
"github.com/influxdata/influx-cli/v2/clients/user"
"github.com/influxdata/influx-cli/v2/config"
"github.com/influxdata/influx-cli/v2/config"
"github.com/influxdata/influx-cli/v2/internal/mock"
"github.com/influxdata/influx-cli/v2/internal/testutils"
"github.com/influxdata/influx-cli/v2/pkg/influxid"
@ -542,6 +542,24 @@ func TestClient_SetPassword(t *testing.T) {
})).Return(nil)
},
},
{
name: "with password via flag",
params: user.SetPasswordParams{
Id: id2,
Password: "mypassword",
},
noExpectAsk: true,
registerExpectations: func(t *testing.T, usersApi *mock.MockUsersApi) {
usersApi.EXPECT().PostUsersIDPassword(gomock.Any(), gomock.Eq(id2.String())).
Return(api.ApiPostUsersIDPasswordRequest{ApiService: usersApi}.UserID(id2.String()))
usersApi.EXPECT().PostUsersIDPasswordExecute(tmock.MatchedBy(func(in api.ApiPostUsersIDPasswordRequest) bool {
body := in.GetPasswordResetBody()
return assert.NotNil(t, body) &&
assert.Equal(t, id2.String(), in.GetUserID()) &&
assert.Equal(t, "mypassword", body.GetPassword())
})).Return(nil)
},
},
{
name: "user not found",
params: user.SetPasswordParams{

View File

@ -157,6 +157,11 @@ func newUserSetPasswordCmd() cli.Command {
Usage: "The user name",
Destination: &params.Name,
},
&cli.StringFlag{
Name: "password, p",
Usage: "Password to set on the user",
Destination: &params.Password,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true)),
Action: func(ctx *cli.Context) error {