Skip to content

Commit

Permalink
user password (grafana-tools#219)
Browse files Browse the repository at this point in the history
* user password

* add password integration test

* password not return bug
  • Loading branch information
mouuii authored and wurbanski committed Sep 4, 2023
1 parent afbe2cc commit b21de6d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
21 changes: 20 additions & 1 deletion rest-admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *Client) DeleteUser(ctx context.Context, uid uint) (StatusMessage, error

// UpdateUserPermissions updates the permissions of a global user.
// Requires basic authentication and that the authenticated user is a Grafana Admin.
// Reflects PUT /api/admin/users/:userId/password API call.
// Reflects PUT /api/admin/users/:userId/permissions API call.
func (r *Client) UpdateUserPermissions(ctx context.Context, permissions UserPermissions, uid uint) (StatusMessage, error) {
var (
raw []byte
Expand Down Expand Up @@ -82,3 +82,22 @@ func (r *Client) SwitchUserContext(ctx context.Context, uid uint, oid uint) (Sta
}
return resp, nil
}

// UpdateUserPassword updates the password of a global user.
// Requires basic authentication and that the authenticated user is a Grafana Admin.
// Reflects PUT /api/admin/users/:userId/password API call.
func (r *Client) UpdateUserPassword(ctx context.Context, password UserPassword, uid uint) (StatusMessage, error) {
var (
raw []byte
reply StatusMessage
err error
)
if raw, err = json.Marshal(password); err != nil {
return StatusMessage{}, err
}
if raw, _, err = r.put(ctx, fmt.Sprintf("api/admin/users/%d/password", uid), nil, raw); err != nil {
return StatusMessage{}, err
}
err = json.Unmarshal(raw, &reply)
return reply, err
}
7 changes: 6 additions & 1 deletion rest-admin_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package sdk_test

import (
"context"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"

sdk "github.com/kubermatic/grafanasdk"
)

Expand Down Expand Up @@ -53,6 +54,10 @@ func TestAdminOperations(t *testing.T) {
if retrievedUser.IsGrafanaAdmin != true {
t.Fatal("user should be an admin but is not")
}
statusMessage, err := client.UpdateUserPassword(ctx, sdk.UserPassword{Password: "123456"}, uid)
if err != nil || *statusMessage.Message != "User password updated" {
t.Fatalf("failed to change the user's password")
}
//Delete user
msg, err := client.DeleteUser(ctx, retrievedUser.ID)
assert.Nil(t, err)
Expand Down
4 changes: 4 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ type PageUsers struct {
Page int `json:"page"`
PerPage int `json:"perPage"`
}

type UserPassword struct {
Password string `json:"password"`
}

0 comments on commit b21de6d

Please sign in to comment.