Files
Jesse HallamandGitHub edf2075381 MM-68149: Upgrade to Go 1.26.2 (#36418)
* MM-68149: upgrade to Go 1.26.2

Update go directive in go.mod and .go-version.

* MM-68149: replace pointer helpers with Go 1.26 new()

Go 1.26 extends the built-in new() to accept an initial value expression,
making typed-pointer helpers like model.NewPointer(x), bToP(x), and boolPtr(x)
redundant. Replace every call site with new(x) and remove the now-unused
helper functions and their //go:fix inline directives.

* MM-68149: apply go fix for reflect API and format-string changes

- reflect.Ptr → reflect.Pointer (renamed in Go 1.18, deprecated alias removed in 1.26)
- reflect range-over-struct: for i := 0; i < t.NumField(); i++ → for field := range t.Fields()
  and the equivalent for Methods() and interface types
- Fix format-string concatenation and variadic-arg mismatches flagged by go vet

* MM-68149: update JPEG fixtures and test infrastructure for Go 1.26 encoder

Go 1.26 ships a new image/jpeg encoder that produces slightly different output.
Regenerate all JPEG fixture files and switch the comparison helpers from
byte-equality to pixel-level comparison with a small per-channel tolerance,
so minor encoder drift across patch versions is handled automatically.

Add -update-fixtures flag to make it easy to regenerate fixtures after future
major Go upgrades. Document the update procedure in tests/README.md.

* MM-68149: CI check that go fix ./... produces no changes

* Fix real bugs flagged by CodeRabbit review

- group.go: set newGroup.MemberCount not group.MemberCount (member count
  was populated on the wrong variable and lost before publish/return)
- file_test.go: guard compareImage(GetFilePreview) on the preview slice
  length, not the thumbnail slice length (copy-paste error)
- config_test.go: remove duplicate MinimumLength assignment

* fixup! Fix real bugs flagged by CodeRabbit review
2026-05-12 15:59:12 +00:00

441 lines
14 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package email
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/platform/shared/mail"
)
func TestCondenseSiteURL(t *testing.T) {
mainHelper.Parallel(t)
require.Equal(t, "", condenseSiteURL(""))
require.Equal(t, "mattermost.com", condenseSiteURL("mattermost.com"))
require.Equal(t, "mattermost.com", condenseSiteURL("mattermost.com/"))
require.Equal(t, "chat.mattermost.com", condenseSiteURL("chat.mattermost.com"))
require.Equal(t, "chat.mattermost.com", condenseSiteURL("chat.mattermost.com/"))
require.Equal(t, "mattermost.com/subpath", condenseSiteURL("mattermost.com/subpath"))
require.Equal(t, "mattermost.com/subpath", condenseSiteURL("mattermost.com/subpath/"))
require.Equal(t, "chat.mattermost.com/subpath", condenseSiteURL("chat.mattermost.com/subpath"))
require.Equal(t, "chat.mattermost.com/subpath", condenseSiteURL("chat.mattermost.com/subpath/"))
require.Equal(t, "mattermost.com:8080", condenseSiteURL("http://mattermost.com:8080"))
require.Equal(t, "mattermost.com:8080", condenseSiteURL("http://mattermost.com:8080/"))
require.Equal(t, "chat.mattermost.com:8080", condenseSiteURL("http://chat.mattermost.com:8080"))
require.Equal(t, "chat.mattermost.com:8080", condenseSiteURL("http://chat.mattermost.com:8080/"))
require.Equal(t, "mattermost.com:8080/subpath", condenseSiteURL("http://mattermost.com:8080/subpath"))
require.Equal(t, "mattermost.com:8080/subpath", condenseSiteURL("http://mattermost.com:8080/subpath/"))
require.Equal(t, "chat.mattermost.com:8080/subpath", condenseSiteURL("http://chat.mattermost.com:8080/subpath"))
require.Equal(t, "chat.mattermost.com:8080/subpath", condenseSiteURL("http://chat.mattermost.com:8080/subpath/"))
}
func TestGetLicenseSkuName(t *testing.T) {
tests := []struct {
name string
license *model.License
expectedSku string
expectedPrefixed string
}{
{
name: "nil license",
license: nil,
expectedSku: "Mattermost",
expectedPrefixed: "Mattermost",
},
{
name: "empty sku name",
license: &model.License{SkuName: ""},
expectedSku: "Mattermost",
expectedPrefixed: "Mattermost",
},
{
name: "Professional",
license: &model.License{SkuName: "Professional"},
expectedSku: "Professional",
expectedPrefixed: "Mattermost Professional",
},
{
name: "Enterprise",
license: &model.License{SkuName: "Enterprise"},
expectedSku: "Enterprise",
expectedPrefixed: "Mattermost Enterprise",
},
{
name: "Enterprise Advanced",
license: &model.License{SkuName: "Enterprise Advanced"},
expectedSku: "Enterprise Advanced",
expectedPrefixed: "Mattermost Enterprise Advanced",
},
{
name: "Entry",
license: &model.License{SkuName: "Entry"},
expectedSku: "Entry",
expectedPrefixed: "Mattermost Entry",
},
{
name: "Mattermost Entry (prefixed by license server)",
license: &model.License{SkuName: "Mattermost Entry"},
expectedSku: "Entry",
expectedPrefixed: "Mattermost Entry",
},
{
name: "E10",
license: &model.License{SkuName: "E10"},
expectedSku: "E10",
expectedPrefixed: "Mattermost E10",
},
{
name: "E20",
license: &model.License{SkuName: "E20"},
expectedSku: "E20",
expectedPrefixed: "Mattermost E20",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
es := &Service{
license: func() *model.License { return tt.license },
}
require.Equal(t, tt.expectedSku, es.getLicenseSkuName())
require.Equal(t, tt.expectedPrefixed, es.getPrefixedLicenseSkuName())
})
}
}
func TestSendInviteEmails(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic(t)
th.ConfigureInbucketMail(t)
emailTo := "[email protected]"
retrieveEmail := func(t *testing.T) mail.JSONMessageInbucket {
t.Helper()
var resultsMailbox mail.JSONMessageHeaderInbucket
err2 := mail.RetryInbucket(5, func() error {
var err error
resultsMailbox, err = mail.GetMailBox(emailTo)
return err
})
if err2 != nil {
t.Skipf("No email was received, maybe due load on the server: %v", err2)
}
require.Len(t, resultsMailbox, 1)
require.Contains(t, resultsMailbox[0].To[0], emailTo, "Wrong To: recipient")
resultsEmail, err := mail.GetMessageFromMailbox(emailTo, resultsMailbox[0].ID)
require.NoError(t, err, "Could not get message from mailbox")
return resultsEmail
}
verifyMailbox := func(t *testing.T) {
t.Helper()
email := retrieveEmail(t)
require.Contains(t, email.Body.HTML, "http://testserver", "Wrong received message %s", email.Body.Text)
require.Contains(t, email.Body.HTML, "test-user", "Wrong received message %s", email.Body.Text)
require.Contains(t, email.Body.Text, "http://testserver", "Wrong received message %s", email.Body.Text)
require.Contains(t, email.Body.Text, "test-user", "Wrong received message %s", email.Body.Text)
}
th.UpdateConfig(t, func(cfg *model.Config) {
*cfg.ServiceSettings.EnableEmailInvitations = true
*cfg.EmailSettings.SendEmailNotifications = false
})
t.Run("SendInviteEmails", func(t *testing.T) {
err := mail.DeleteMailBox(emailTo)
require.NoError(t, err, "Failed to delete mailbox")
err = th.service.SendInviteEmails(th.Context, th.BasicTeam, "test-user", th.BasicUser.Id, []string{emailTo}, "http://testserver", nil, false, false, false)
require.NoError(t, err)
verifyMailbox(t)
})
t.Run("SendInviteEmails can return error when SMTP connection fails", func(t *testing.T) {
originalPort := *th.service.config().EmailSettings.SMTPPort
originalTimeout := *th.service.config().EmailSettings.SMTPServerTimeout
th.UpdateConfig(t, func(cfg *model.Config) {
*cfg.EmailSettings.SMTPPort = "5432"
*cfg.EmailSettings.SMTPServerTimeout = 4
})
defer th.UpdateConfig(t, func(cfg *model.Config) {
*cfg.EmailSettings.SMTPPort = originalPort
*cfg.EmailSettings.SMTPServerTimeout = originalTimeout
})
err := th.service.SendInviteEmails(th.Context, th.BasicTeam, "test-user", th.BasicUser.Id, []string{emailTo}, "http://testserver", nil, true, false, false)
require.Error(t, err)
err = th.service.SendInviteEmails(th.Context, th.BasicTeam, "test-user", th.BasicUser.Id, []string{emailTo}, "http://testserver", nil, false, false, false)
require.NoError(t, err)
})
t.Run("SendGuestInviteEmails", func(t *testing.T) {
err := mail.DeleteMailBox(emailTo)
require.NoError(t, err, "Failed to delete mailbox")
err = th.service.SendGuestInviteEmails(
th.Context,
th.BasicTeam,
[]*model.Channel{th.BasicChannel},
"test-user",
th.BasicUser.Id,
nil,
[]string{emailTo},
"http://testserver",
"hello world",
false,
false,
false,
false,
)
require.NoError(t, err)
verifyMailbox(t)
})
t.Run("SendGuestInviteEmail can return error when SMTP connection fails", func(t *testing.T) {
originalTimeout := *th.service.config().EmailSettings.SMTPServerTimeout
originalPort := *th.service.config().EmailSettings.SMTPPort
th.UpdateConfig(t, func(cfg *model.Config) {
*cfg.EmailSettings.SMTPPort = "5432"
*cfg.EmailSettings.SMTPServerTimeout = 4
})
defer th.UpdateConfig(t, func(cfg *model.Config) {
*cfg.EmailSettings.SMTPPort = originalPort
*cfg.EmailSettings.SMTPServerTimeout = originalTimeout
})
err := th.service.SendGuestInviteEmails(
th.Context,
th.BasicTeam,
[]*model.Channel{th.BasicChannel},
"test-user",
th.BasicUser.Id,
nil,
[]string{emailTo},
"http://testserver",
"hello world",
false,
false,
false,
false,
)
require.NoError(t, err)
err = th.service.SendGuestInviteEmails(
th.Context,
th.BasicTeam,
[]*model.Channel{th.BasicChannel},
"test-user",
th.BasicUser.Id,
nil,
[]string{emailTo},
"http://testserver",
"hello world",
true,
false,
false,
false,
)
require.Error(t, err)
})
t.Run("SendGuestInviteEmails should sanitize HTML input", func(t *testing.T) {
err := mail.DeleteMailBox(emailTo)
require.NoError(t, err, "Failed to delete mailbox")
message := `<a href="http://testserver">sanitized message</a>`
err = th.service.SendGuestInviteEmails(
th.Context,
th.BasicTeam,
[]*model.Channel{th.BasicChannel},
"test-user",
th.BasicUser.Id,
nil,
[]string{emailTo},
"http://testserver",
message,
false,
false,
false,
false,
)
require.NoError(t, err)
email := retrieveEmail(t)
require.NotContains(t, email.Body.HTML, message)
require.Contains(t, email.Body.HTML, "sanitized message")
require.Contains(t, email.Body.Text, "sanitized message")
})
t.Run("SendInviteEmails should contain button URL with 'started by role' param for system user", func(t *testing.T) {
err := mail.DeleteMailBox(emailTo)
require.NoError(t, err, "Failed to delete mailbox")
err = th.service.SendInviteEmails(
th.Context,
th.BasicTeam,
"test-user",
th.BasicUser.Id,
[]string{emailTo},
"http://testserver",
nil,
false,
false,
false,
)
require.NoError(t, err)
email := retrieveEmail(t)
require.Contains(t, email.Body.HTML, "&amp;sbr=su")
})
t.Run("SendInviteEmails should contain button URL with 'started by role' param for system admin", func(t *testing.T) {
err := mail.DeleteMailBox(emailTo)
require.NoError(t, err, "Failed to delete mailbox")
err = th.service.SendInviteEmails(
th.Context,
th.BasicTeam,
"test-user",
th.BasicUser.Id,
[]string{emailTo},
"http://testserver",
nil,
false,
true,
false,
)
require.NoError(t, err)
email := retrieveEmail(t)
require.Contains(t, email.Body.HTML, "&amp;sbr=sa")
})
t.Run("SendInviteEmails should contain button URL with 'started by role' param for first system admin", func(t *testing.T) {
err := mail.DeleteMailBox(emailTo)
require.NoError(t, err, "Failed to delete mailbox")
err = th.service.SendInviteEmails(
th.Context,
th.BasicTeam,
"test-user",
th.BasicUser.Id,
[]string{emailTo},
"http://testserver",
nil,
false,
true,
true,
)
require.NoError(t, err)
email := retrieveEmail(t)
require.Contains(t, email.Body.HTML, "&amp;sbr=fa")
})
}
func TestSendCloudWelcomeEmail(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic(t)
th.ConfigureInbucketMail(t)
emailTo := "[email protected]"
t.Run("TestSendCloudWelcomeEmail", func(t *testing.T) {
verifyMailbox := func(t *testing.T) {
t.Helper()
var resultsMailbox mail.JSONMessageHeaderInbucket
err2 := mail.RetryInbucket(5, func() error {
var err error
resultsMailbox, err = mail.GetMailBox(emailTo)
return err
})
if err2 != nil {
t.Skipf("No email was received, maybe due load on the server: %v", err2)
}
require.Len(t, resultsMailbox, 1)
require.Contains(t, resultsMailbox[0].To[0], emailTo, "Wrong To: recipient")
resultsEmail, err := mail.GetMessageFromMailbox(emailTo, resultsMailbox[0].ID)
require.NoError(t, err, "Could not get message from mailbox")
require.Contains(t, resultsEmail.Subject, "Congratulations!", "Wrong subject message %s", resultsEmail.Subject)
require.Contains(t, resultsEmail.Body.Text, "Your workspace is ready to go!", "Wrong body %s", resultsEmail.Body.Text)
}
err := mail.DeleteMailBox(emailTo)
require.NoError(t, err, "Failed to delete mailbox")
err = th.service.SendCloudWelcomeEmail(emailTo, th.BasicUser.Locale, "inviteID", "SomeName", "example.com", "https://example.com")
require.NoError(t, err)
verifyMailbox(t)
})
}
func TestMailServiceConfig(t *testing.T) {
mainHelper.Parallel(t)
configuredReplyTo := "[email protected]"
customReplyTo := "[email protected]"
emailService := Service{
config: func() *model.Config {
return &model.Config{
ServiceSettings: model.ServiceSettings{
SiteURL: new(""),
},
EmailSettings: model.EmailSettings{
EnableSignUpWithEmail: new(bool),
EnableSignInWithEmail: new(bool),
EnableSignInWithUsername: new(bool),
SendEmailNotifications: new(bool),
UseChannelInEmailNotifications: new(bool),
RequireEmailVerification: new(bool),
FeedbackName: new(string),
FeedbackEmail: new(string),
ReplyToAddress: new(configuredReplyTo),
FeedbackOrganization: new(string),
EnableSMTPAuth: new(bool),
SMTPUsername: new(string),
SMTPPassword: new(string),
SMTPServer: new(string),
SMTPPort: new(string),
SMTPServerTimeout: new(int),
ConnectionSecurity: new(string),
SendPushNotifications: new(bool),
PushNotificationServer: new(string),
PushNotificationContents: new(string),
PushNotificationBuffer: new(int),
EnableEmailBatching: new(bool),
EmailBatchingBufferSize: new(int),
EmailBatchingInterval: new(int),
EnablePreviewModeBanner: new(bool),
SkipServerCertificateVerification: new(bool),
EmailNotificationContentsType: new(string),
LoginButtonColor: new(string),
LoginButtonBorderColor: new(string),
LoginButtonTextColor: new(string),
},
}
},
}
t.Run("use custom replyto instead of configured replyto", func(t *testing.T) {
mailConfig := emailService.mailServiceConfig(customReplyTo)
require.Equal(t, customReplyTo, mailConfig.ReplyToAddress)
})
t.Run("use configured replyto", func(t *testing.T) {
mailConfig := emailService.mailServiceConfig("")
require.Equal(t, configuredReplyTo, mailConfig.ReplyToAddress)
})
}