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

195 lines
4.8 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package commands
import (
"context"
"crypto/x509"
"encoding/json"
"net"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/mux"
"github.com/hanzoteam/server/server/public/model"
"github.com/stretchr/testify/require"
)
func TestCheckVersionMatch(t *testing.T) {
testCases := []struct {
Name string
Version string
ServerVersion string
Expected bool
ErrExpected bool
}{
{
Name: "Both versions are equal",
Version: "1.2.3",
ServerVersion: "1.2.3.dev.993e5a2cb546b0116ecaae1862b6a1c6.true",
Expected: true,
},
{
Name: "Only patch version is different",
Version: "1.2.3",
ServerVersion: "1.2.7.dev.993e5a2cb546b0116ecaae1862b6a1c6.false",
Expected: true,
},
{
Name: "Major version is greater",
Version: "1.2.3",
ServerVersion: "7.0.0.7.0.0.8215d92df0b8458789408eb07ccfdaae.false",
Expected: false,
},
{
Name: "Major version is less",
Version: "8.2.3",
ServerVersion: "7.0.0.7.0.0.8215d92df0b8458789408eb07ccfdaae.false",
Expected: false,
},
{
Name: "Minor version is greater",
Version: "1.2.3",
ServerVersion: "1.3.3.1.3.3.8215d92df0b8458789408eb07ccfdaae.true",
Expected: true,
},
{
Name: "Minor version is less",
Version: "1.2.3",
ServerVersion: "1.1.3.1.1.3.8215d92df0b8458789408eb07ccfdaae.false",
Expected: false,
},
{
Name: "Both versions are equal but one has v in front of it",
Version: "v1.2.3",
ServerVersion: "1.2.3.dev.8215d92df0b8458789408eb07ccfdaae.false",
Expected: true,
},
{
Name: "unspecified version",
Version: "",
ServerVersion: "1.2.3",
Expected: false,
ErrExpected: true,
},
{
Name: "bad version",
Version: "1.2.3",
ServerVersion: "1.2",
Expected: false,
ErrExpected: true,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
res, err := CheckVersionMatch(tc.Version, tc.ServerVersion)
require.True(t, (err != nil) == tc.ErrExpected)
require.Equal(t, tc.Expected, res)
})
}
}
func TestVerifyCertificates(t *testing.T) {
testCases := []struct {
Name string
Chains [][]*x509.Certificate
ExpectedError bool
}{
{
Name: "One chain with a root SHA1 cert",
Chains: [][]*x509.Certificate{
{
{SignatureAlgorithm: x509.SHA256WithRSA},
{SignatureAlgorithm: x509.SHA1WithRSA},
},
},
ExpectedError: false,
},
{
Name: "One chain with an intermediate SHA1 cert",
Chains: [][]*x509.Certificate{
{
{SignatureAlgorithm: x509.SHA256WithRSA},
{SignatureAlgorithm: x509.SHA1WithRSA},
{SignatureAlgorithm: x509.SHA1WithRSA},
},
},
ExpectedError: true,
},
{
Name: "One valid chain and other invalid",
Chains: [][]*x509.Certificate{
{
{SignatureAlgorithm: x509.SHA256WithRSA},
{SignatureAlgorithm: x509.SHA1WithRSA},
{SignatureAlgorithm: x509.SHA1WithRSA},
},
{
{SignatureAlgorithm: x509.SHA256WithRSA},
{SignatureAlgorithm: x509.SHA256WithRSA},
{SignatureAlgorithm: x509.SHA1WithRSA},
},
},
ExpectedError: false,
},
{
Name: "Two invalid chains",
Chains: [][]*x509.Certificate{
{
{SignatureAlgorithm: x509.SHA256WithRSA},
{SignatureAlgorithm: x509.SHA1WithRSA},
{SignatureAlgorithm: x509.SHA1WithRSA},
},
{
{SignatureAlgorithm: x509.SHA256WithRSA},
{SignatureAlgorithm: x509.DSAWithSHA1},
{SignatureAlgorithm: x509.DSAWithSHA1},
},
},
ExpectedError: true,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
err := VerifyCertificates([][]byte{}, tc.Chains)
if tc.ExpectedError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
func TestNewAPIv4Client(t *testing.T) {
t.Run("should take http proxy into account", func(t *testing.T) {
router := mux.NewRouter()
router.Handle("/v1/workspace/users/me", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user := &model.User{
Id: model.NewId(),
}
err := json.NewEncoder(w).Encode(user)
require.NoError(t, err)
w.WriteHeader(http.StatusOK)
}))
s := httptest.NewServer(router)
defer s.Close()
proxyAddr := s.Listener.Addr().String()
_, port, err := net.SplitHostPort(proxyAddr)
require.NoError(t, err)
// t.Setenv prevents t.Parallel — env var has no config equivalent
t.Setenv("HTTP_PROXY", proxyAddr)
client := NewAPIv4Client("http://somethingelse:"+port, false, false)
_, _, err = client.GetMe(context.Background(), "")
require.NoError(t, err)
})
}