Files
zeekay 8feb24ea1f One name for this fork, and it is ours
The server module named github.com/hanzoai/team/server/v8 — a path that
resolves to the retired workspace's repository, not this one — while
server/public still published github.com/mattermost/mattermost/server/public.
Both are github.com/hanzoteam/server/... now, which is where this code
actually lives.

That second half is what made the binary unbootable. Upstream's public path
stayed claimed by upstream, mattermost-plugin-ai imported it, and the build
linked public/model twice; public/plugin's gob.Register then ran twice under
one name and panicked at init, before main. Reverting the rename traded the
brand for a working binary. Rebranding the plugin alongside it needs no such
trade: the agents fork is github.com/hanzoteam/agents/v2 against our public
module, so no module named mattermost/mattermost is in the graph at all.

Measured: `go list -deps ./cmd/mattermost` names exactly one public/model, and
it is ours.
2026-08-28 23:35:36 -07:00

142 lines
5.1 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package commands
import (
"fmt"
"github.com/hanzoteam/server/server/public/model"
"github.com/spf13/cobra"
"github.com/hanzoteam/server/server/v8/cmd/mmctl/client"
"github.com/hanzoteam/server/server/v8/cmd/mmctl/printer"
)
func (s *MmctlE2ETestSuite) TestTokenGenerateForUserCmd() {
s.SetupTestHelper().InitBasic(s.T())
tokenDescription := model.NewRandomString(10)
previousVal := s.th.App.Config().ServiceSettings.EnableUserAccessTokens
s.th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = true })
defer s.th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = *previousVal })
s.RunForSystemAdminAndLocal("Generate token for user", func(c client.Client) {
printer.Clean()
user, appErr := s.th.App.CreateUser(s.th.Context, &model.User{Email: s.th.GenerateTestEmail(), Username: model.NewUsername(), Password: model.NewId()})
s.Require().Nil(appErr)
err := generateTokenForAUserCmdF(c, &cobra.Command{}, []string{user.Email, tokenDescription})
s.Require().Nil(err)
s.Require().Len(printer.GetErrorLines(), 0)
userTokens, appErr := s.th.App.GetUserAccessTokensForUser(user.Id, 0, 1)
s.Require().Nil(appErr)
s.Require().Equal(1, len(userTokens))
userToken, appErr := s.th.App.GetUserAccessToken(userTokens[0].Id, false)
s.Require().Nil(appErr)
expectedUserToken := printer.GetLines()[0].(*model.UserAccessToken)
s.Require().Equal(expectedUserToken, userToken)
})
s.RunForSystemAdminAndLocal("Generate token for nonexistent user", func(c client.Client) {
printer.Clean()
nonExistentUserEmail := s.th.GenerateTestEmail()
err := generateTokenForAUserCmdF(c, &cobra.Command{}, []string{nonExistentUserEmail, tokenDescription})
s.Require().NotNil(err)
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 0)
s.Require().Equal(
fmt.Sprintf(`could not retrieve user information of %q`, nonExistentUserEmail),
err.Error())
})
s.Run("Generate token without permission", func() {
printer.Clean()
user, appErr := s.th.App.CreateUser(s.th.Context, &model.User{Email: s.th.GenerateTestEmail(), Username: model.NewUsername(), Password: model.NewId()})
s.Require().Nil(appErr)
err := generateTokenForAUserCmdF(s.th.Client, &cobra.Command{}, []string{user.Email, tokenDescription})
s.Require().NotNil(err)
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 0)
s.Require().ErrorContains(
err,
fmt.Sprintf(`could not create token for %q: You do not have the appropriate permissions.`, user.Email),
)
userTokens, appErr := s.th.App.GetUserAccessTokensForUser(user.Id, 0, 1)
s.Require().Nil(appErr)
s.Require().Equal(0, len(userTokens))
})
}
func (s *MmctlE2ETestSuite) TestRevokeTokenForAUserCmdF() {
s.SetupTestHelper().InitBasic(s.T())
previousVal := s.th.App.Config().ServiceSettings.EnableUserAccessTokens
s.th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = true })
defer s.th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = *previousVal })
s.RunForSystemAdminAndLocal("Revoke user access tokens", func(c client.Client) {
printer.Clean()
user, appErr := s.th.App.CreateUser(s.th.Context, &model.User{Email: s.th.GenerateTestEmail(), Username: model.NewUsername(), Password: model.NewId()})
s.Require().Nil(appErr)
token, appErr := s.th.App.CreateUserAccessToken(s.th.Context, &model.UserAccessToken{
UserId: user.Id,
Description: "test token",
})
s.Require().Nil(appErr)
err := revokeTokenForAUserCmdF(c, &cobra.Command{}, []string{token.Id})
s.Require().NoError(err)
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 0)
_, appErr = s.th.App.GetUserAccessToken(token.Id, false)
s.Require().Error(appErr)
s.Require().Contains(appErr.Error(), "not found")
})
s.RunForSystemAdminAndLocal("Fail to revoke non-existent token", func(c client.Client) {
printer.Clean()
err := revokeTokenForAUserCmdF(c, &cobra.Command{}, []string{"non-existent-token-id"})
s.Require().Error(err)
s.Require().Contains(err.Error(), "could not revoke token")
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 0)
})
s.Run("Revoke token without permission", func() {
printer.Clean()
user, appErr := s.th.App.CreateUser(s.th.Context, &model.User{Email: s.th.GenerateTestEmail(), Username: model.NewUsername(), Password: model.NewId()})
s.Require().Nil(appErr)
token, appErr := s.th.App.CreateUserAccessToken(s.th.Context, &model.UserAccessToken{
UserId: user.Id,
Description: "test token",
})
s.Require().Nil(appErr)
err := revokeTokenForAUserCmdF(s.th.Client, &cobra.Command{}, []string{token.Id})
s.Require().Error(err)
s.Require().Len(printer.GetLines(), 0)
s.Require().Len(printer.GetErrorLines(), 0)
s.Require().ErrorContains(
err,
fmt.Sprintf(`could not revoke token %q: You do not have the appropriate permissions.`, token.Id),
)
})
}