* mmctl: add listing oauth apps

* Fix tests

* Update mmctl docs

* Fix tests

* Update server/cmd/mmctl/commands/oauth.go

Co-authored-by: Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com>

* Update server/cmd/mmctl/commands/oauth.go

Co-authored-by: Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com>

* Add paging

* Update docs

---------

Co-authored-by: Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com>
Этот коммит содержится в:
Joram Wilander
2023-12-05 12:25:35 -05:00
коммит произвёл GitHub
родитель df6c8c5f5e
Коммит e0a36cf60a
7 изменённых файлов: 333 добавлений и 0 удалений

83
server/cmd/mmctl/commands/oauth.go Обычный файл
Просмотреть файл

@@ -0,0 +1,83 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package commands
import (
"context"
"fmt"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/client"
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var OAuthCmd = &cobra.Command{
Use: "oauth",
Short: "Management of OAuth2 apps",
}
var ListOAuthAppsCmd = &cobra.Command{
Use: "list",
Short: "List OAuth2 apps",
Long: "list all OAuth2 apps",
Example: " oauth list",
RunE: withClient(listOAuthAppsCmdF),
Args: cobra.NoArgs,
}
func listOAuthAppsCmdF(c client.Client, command *cobra.Command, args []string) error {
page, err := command.Flags().GetInt("page")
if err != nil {
return err
}
perPage, err := command.Flags().GetInt("per-page")
if err != nil {
return err
}
apps, _, err := c.GetOAuthApps(context.Background(), page, perPage)
if err != nil {
return errors.Wrap(err, "Failed to fetch oauth2 apps")
}
userIds := make([]string, len(apps))
for i := range apps {
userIds[i] = apps[i].CreatorId
}
users, _, err := c.GetUsersByIds(context.Background(), userIds)
if err != nil {
return errors.Wrap(err, "Failed to fetch users for oauth2 apps")
}
usersByID := map[string]*model.User{}
for _, user := range users {
usersByID[user.Id] = user
}
for _, app := range apps {
ownerName := app.CreatorId
if owner, ok := usersByID[app.CreatorId]; ok {
ownerName = owner.Username
}
printer.PrintT(fmt.Sprintf("{{.Id}}: {{.Name}} (Created by %s)", ownerName), app)
}
return nil
}
func init() {
ListOAuthAppsCmd.Flags().Int("page", 0, "Page number to fetch for the list of OAuth2 apps")
ListOAuthAppsCmd.Flags().Int("per-page", 200, "Number of OAuth2 apps to be fetched")
OAuthCmd.AddCommand(
ListOAuthAppsCmd,
)
RootCmd.AddCommand(OAuthCmd)
}

138
server/cmd/mmctl/commands/oauth_test.go Обычный файл
Просмотреть файл

@@ -0,0 +1,138 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package commands
import (
"context"
"strconv"
"github.com/mattermost/mattermost/server/public/model"
"github.com/pkg/errors"
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
"github.com/spf13/cobra"
)
func (s *MmctlUnitTestSuite) TestListOAuthAppsCmd() {
oauthAppID := "oauthAppID"
oauthAppName := "oauthAppName"
userID := "userID"
cmd := &cobra.Command{}
cmd.Flags().Int("page", 0, "")
cmd.Flags().Int("per-page", 200, "")
s.Run("Listing oauth apps", func() {
printer.Clean()
mockOAuthApp := model.OAuthApp{
Id: oauthAppID,
Name: oauthAppName,
CreatorId: userID,
}
mockUser := model.User{Id: mockOAuthApp.CreatorId, Username: "mockuser"}
s.client.
EXPECT().
GetOAuthApps(context.Background(), 0, 200).
Return([]*model.OAuthApp{&mockOAuthApp}, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUsersByIds(context.Background(), []string{mockOAuthApp.CreatorId}).
Return([]*model.User{&mockUser}, &model.Response{}, nil).
Times(1)
err := listOAuthAppsCmdF(s.client, cmd, []string{})
s.Require().Nil(err)
s.Len(printer.GetLines(), 1)
s.Len(printer.GetErrorLines(), 0)
s.Require().Equal(&mockOAuthApp, printer.GetLines()[0])
})
s.Run("Listing oauth apps with paging", func() {
printer.Clean()
mockOAuthApp := model.OAuthApp{
Id: oauthAppID,
Name: oauthAppName,
CreatorId: userID,
}
mockUser := model.User{Id: mockOAuthApp.CreatorId, Username: "mockuser"}
pageCmd := &cobra.Command{}
pageCmd.Flags().Int("page", 0, "")
pageCmd.Flags().Int("per-page", 200, "")
page := 1
perPage := 2
_ = pageCmd.Flags().Set("page", strconv.Itoa(page))
_ = pageCmd.Flags().Set("per-page", strconv.Itoa(perPage))
s.client.
EXPECT().
GetOAuthApps(context.Background(), 1, 2).
Return([]*model.OAuthApp{&mockOAuthApp}, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUsersByIds(context.Background(), []string{mockOAuthApp.CreatorId}).
Return([]*model.User{&mockUser}, &model.Response{}, nil).
Times(1)
err := listOAuthAppsCmdF(s.client, pageCmd, []string{})
s.Require().Nil(err)
s.Len(printer.GetLines(), 1)
s.Len(printer.GetErrorLines(), 0)
s.Require().Equal(&mockOAuthApp, printer.GetLines()[0])
})
s.Run("Unable to list oauth apps", func() {
printer.Clean()
mockError := errors.New("mock error")
s.client.
EXPECT().
GetOAuthApps(context.Background(), 0, 200).
Return(nil, &model.Response{}, mockError).
Times(1)
err := listOAuthAppsCmdF(s.client, cmd, []string{})
s.Require().NotNil(err)
s.Len(printer.GetLines(), 0)
s.EqualError(err, "Failed to fetch oauth2 apps: mock error")
})
s.Run("Unable to get users for oauth apps", func() {
printer.Clean()
mockOAuthApp := model.OAuthApp{
Id: oauthAppID,
Name: oauthAppName,
CreatorId: userID,
}
mockError := errors.New("mock error")
s.client.
EXPECT().
GetOAuthApps(context.Background(), 0, 200).
Return([]*model.OAuthApp{&mockOAuthApp}, &model.Response{}, nil).
Times(1)
s.client.
EXPECT().
GetUsersByIds(context.Background(), []string{mockOAuthApp.CreatorId}).
Return(nil, &model.Response{}, mockError).
Times(1)
err := listOAuthAppsCmdF(s.client, cmd, []string{})
s.Require().NotNil(err)
s.Len(printer.GetLines(), 0)
s.EqualError(err, "Failed to fetch users for oauth2 apps: mock error")
})
}