mirror of
https://github.com/hanzoai/team.git
synced 2026-09-30 01:57:35 +00:00
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.
199 lines
5.4 KiB
Go
199 lines
5.4 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package commands
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/hanzoteam/server/server/public/model"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/hanzoteam/server/server/v8/cmd/mmctl/printer"
|
|
)
|
|
|
|
const (
|
|
fakeLicensePayload = "This is the license."
|
|
)
|
|
|
|
func (s *MmctlUnitTestSuite) TestRemoveLicenseCmd() {
|
|
s.Run("Remove license successfully", func() {
|
|
printer.Clean()
|
|
|
|
s.client.
|
|
EXPECT().
|
|
RemoveLicenseFile(context.TODO()).
|
|
Return(&model.Response{StatusCode: http.StatusBadRequest}, nil).
|
|
Times(1)
|
|
|
|
err := removeLicenseCmdF(s.client, &cobra.Command{}, []string{})
|
|
s.Require().Nil(err)
|
|
s.Require().Len(printer.GetLines(), 1)
|
|
s.Require().Len(printer.GetErrorLines(), 0)
|
|
s.Require().Equal(printer.GetLines()[0], "Removed license")
|
|
})
|
|
|
|
s.Run("Fail to remove license", func() {
|
|
printer.Clean()
|
|
mockErr := errors.New("mock error")
|
|
|
|
s.client.
|
|
EXPECT().
|
|
RemoveLicenseFile(context.TODO()).
|
|
Return(&model.Response{StatusCode: http.StatusBadRequest}, mockErr).
|
|
Times(1)
|
|
|
|
err := removeLicenseCmdF(s.client, &cobra.Command{}, []string{})
|
|
s.Require().NotNil(err)
|
|
s.Require().Len(printer.GetLines(), 0)
|
|
s.Require().Len(printer.GetErrorLines(), 0)
|
|
s.Require().Equal(err, mockErr)
|
|
})
|
|
}
|
|
|
|
func (s *MmctlUnitTestSuite) TestUploadLicenseCmdF() {
|
|
// create temporary file
|
|
tmpFile, err := os.CreateTemp(os.TempDir(), "testLicense-")
|
|
s.NoError(err)
|
|
|
|
text := []byte(fakeLicensePayload)
|
|
_, err = tmpFile.Write(text)
|
|
s.NoError(err)
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
mockLicenseFile := []byte(fakeLicensePayload)
|
|
|
|
s.Run("Upload license successfully", func() {
|
|
printer.Clean()
|
|
s.client.
|
|
EXPECT().
|
|
UploadLicenseFile(context.TODO(), mockLicenseFile).
|
|
Return(&model.Response{StatusCode: http.StatusOK}, nil).
|
|
Times(1)
|
|
|
|
err := uploadLicenseCmdF(s.client, &cobra.Command{}, []string{tmpFile.Name()})
|
|
s.Require().Nil(err)
|
|
})
|
|
|
|
s.Run("Fail to upload license if file not found", func() {
|
|
printer.Clean()
|
|
path := "/path/to/nonexistentfile"
|
|
errMsg := "open " + path + ": no such file or directory"
|
|
s.client.
|
|
EXPECT().
|
|
UploadLicenseFile(context.TODO(), mockLicenseFile).
|
|
Times(0)
|
|
|
|
err := uploadLicenseCmdF(s.client, &cobra.Command{}, []string{path})
|
|
s.Require().EqualError(err, errMsg)
|
|
})
|
|
|
|
s.Run("Fail to upload license if no path is given", func() {
|
|
printer.Clean()
|
|
err := uploadLicenseCmdF(s.client, &cobra.Command{}, []string{})
|
|
s.Require().EqualError(err, "enter one license file to upload")
|
|
})
|
|
}
|
|
|
|
func (s *MmctlUnitTestSuite) TestUploadLicenseStringCmdF() {
|
|
// create temporary file
|
|
licenseString := string(fakeLicensePayload)
|
|
|
|
mockLicenseFile := []byte(fakeLicensePayload)
|
|
|
|
s.Run("Upload license successfully", func() {
|
|
printer.Clean()
|
|
s.client.
|
|
EXPECT().
|
|
UploadLicenseFile(context.TODO(), mockLicenseFile).
|
|
Return(&model.Response{StatusCode: http.StatusOK}, nil).
|
|
Times(1)
|
|
|
|
err := uploadLicenseStringCmdF(s.client, &cobra.Command{}, []string{licenseString})
|
|
s.Require().Nil(err)
|
|
})
|
|
|
|
s.Run("Fail to upload license if no license string is given", func() {
|
|
printer.Clean()
|
|
err := uploadLicenseStringCmdF(s.client, &cobra.Command{}, []string{})
|
|
s.Require().EqualError(err, "enter one license file to upload")
|
|
})
|
|
}
|
|
|
|
func (s *MmctlUnitTestSuite) TestGetLicenseCmdF() {
|
|
s.Run("Get license successfully", func() {
|
|
printer.Clean()
|
|
|
|
mockLicense := map[string]string{
|
|
"IsLicensed": "true",
|
|
"Id": "test-license-id",
|
|
"Company": "Test Company",
|
|
"Name": "Test Contact",
|
|
"Email": "[email protected]",
|
|
"SkuShortName": "enterprise",
|
|
"Users": "100",
|
|
"IssuedAt": "1609459200000",
|
|
"StartsAt": "1609459200000",
|
|
"ExpiresAt": "1640995200000",
|
|
"IsTrial": "false",
|
|
}
|
|
|
|
s.client.
|
|
EXPECT().
|
|
GetOldClientLicense(context.TODO(), "").
|
|
Return(mockLicense, &model.Response{StatusCode: http.StatusOK}, nil).
|
|
Times(1)
|
|
|
|
err := getLicenseCmdF(s.client, &cobra.Command{}, []string{})
|
|
s.Require().Nil(err)
|
|
s.Require().Len(printer.GetLines(), 1)
|
|
s.Require().Len(printer.GetErrorLines(), 0)
|
|
|
|
// Verify human-readable dates are formatted correctly
|
|
output := printer.GetLines()[0].(map[string]string)
|
|
s.Equal("2021-01-01T00:00:00Z", output["IssuedAtReadable"])
|
|
s.Equal("2021-01-01T00:00:00Z", output["StartsAtReadable"])
|
|
s.Equal("2022-01-01T00:00:00Z", output["ExpiresAtReadable"])
|
|
s.Equal("Test Company", output["Company"])
|
|
s.Equal("enterprise", output["SkuShortName"])
|
|
})
|
|
|
|
s.Run("No license installed", func() {
|
|
printer.Clean()
|
|
|
|
mockLicense := map[string]string{
|
|
"IsLicensed": "false",
|
|
}
|
|
|
|
s.client.
|
|
EXPECT().
|
|
GetOldClientLicense(context.TODO(), "").
|
|
Return(mockLicense, &model.Response{StatusCode: http.StatusOK}, nil).
|
|
Times(1)
|
|
|
|
err := getLicenseCmdF(s.client, &cobra.Command{}, []string{})
|
|
s.Require().Nil(err)
|
|
s.Require().Len(printer.GetLines(), 1)
|
|
s.Require().Equal(printer.GetLines()[0], "No license installed")
|
|
})
|
|
|
|
s.Run("Fail to get license", func() {
|
|
printer.Clean()
|
|
mockErr := errors.New("mock error")
|
|
|
|
s.client.
|
|
EXPECT().
|
|
GetOldClientLicense(context.TODO(), "").
|
|
Return(nil, &model.Response{StatusCode: http.StatusInternalServerError}, mockErr).
|
|
Times(1)
|
|
|
|
err := getLicenseCmdF(s.client, &cobra.Command{}, []string{})
|
|
s.Require().NotNil(err)
|
|
s.Require().Equal(err, mockErr)
|
|
s.Require().Len(printer.GetLines(), 0)
|
|
})
|
|
}
|