Files
team/server/channels/api4/scheduled_post_test.go
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

156 lines
4.8 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package api4
import (
"context"
"testing"
"github.com/mattermost/mattermost/server/public/model"
"github.com/stretchr/testify/require"
)
func TestUpdateScheduledPost(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic(t)
th.App.Srv().SetLicense(model.NewTestLicenseSKU(model.LicenseShortSkuProfessional))
t.Run("should not allow updating a scheduled post not belonging to the user", func(t *testing.T) {
scheduledPost := &model.ScheduledPost{
Draft: model.Draft{
CreateAt: model.GetMillis(),
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "this is a scheduled post",
},
ScheduledAt: model.GetMillis() + 100000,
}
createdScheduledPost, _, err := th.Client.CreateScheduledPost(context.Background(), scheduledPost)
require.NoError(t, err)
require.NotNil(t, createdScheduledPost)
originalMessage := createdScheduledPost.Message
originalScheduledAt := createdScheduledPost.ScheduledAt
createdScheduledPost.ScheduledAt = model.GetMillis() + 9999999
createdScheduledPost.Message = "Updated Message!!!"
// Switch to BasicUser2
th.LoginBasic2(t)
_, resp, err := th.Client.UpdateScheduledPost(context.Background(), createdScheduledPost)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
// Switch back to original user and verify the post wasn't modified
th.LoginBasic(t)
fetchedPost, err := th.App.Srv().Store().ScheduledPost().Get(createdScheduledPost.Id)
require.NoError(t, err)
require.NotNil(t, fetchedPost)
require.Equal(t, originalMessage, fetchedPost.Message)
require.Equal(t, originalScheduledAt, fetchedPost.ScheduledAt)
})
}
func TestDeleteScheduledPost(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic(t)
th.App.Srv().SetLicense(model.NewTestLicenseSKU(model.LicenseShortSkuProfessional))
t.Run("should not allow deleting a scheduled post not belonging to the user", func(t *testing.T) {
scheduledPost := &model.ScheduledPost{
Draft: model.Draft{
CreateAt: model.GetMillis(),
UserId: th.BasicUser.Id,
ChannelId: th.BasicChannel.Id,
Message: "this is a scheduled post",
},
ScheduledAt: model.GetMillis() + 100000,
}
createdScheduledPost, _, err := th.Client.CreateScheduledPost(context.Background(), scheduledPost)
require.NoError(t, err)
require.NotNil(t, createdScheduledPost)
// Switch to BasicUser2
th.LoginBasic2(t)
_, resp, err := th.Client.DeleteScheduledPost(context.Background(), createdScheduledPost.Id)
require.Error(t, err)
CheckForbiddenStatus(t, resp)
// Switch back to original user and verify the post wasn't deleted
th.LoginBasic(t)
fetchedPost, err := th.App.Srv().Store().ScheduledPost().Get(createdScheduledPost.Id)
require.NoError(t, err)
require.NotNil(t, fetchedPost)
require.Equal(t, createdScheduledPost.Id, fetchedPost.Id)
require.Equal(t, createdScheduledPost.Message, fetchedPost.Message)
})
}
func TestCreateScheduledPost(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic(t)
th.App.Srv().SetLicense(model.NewTestLicenseSKU(model.LicenseShortSkuProfessional))
client := th.Client
t.Run("base case", func(t *testing.T) {
userId := model.NewId()
scheduledPost := &model.ScheduledPost{
Draft: model.Draft{
CreateAt: model.GetMillis(),
UserId: userId,
ChannelId: th.BasicChannel.Id,
Message: "this is a scheduled post",
},
ScheduledAt: model.GetMillis() + 100000, // 100 seconds in the future
}
createdScheduledPost, _, err := client.CreateScheduledPost(context.Background(), scheduledPost)
require.NoError(t, err)
require.NotNil(t, createdScheduledPost)
})
t.Run("should not allow created scheduled post in read-only channel", func(t *testing.T) {
channel := th.CreatePublicChannel(t)
th.AddUserToChannel(t, th.BasicUser, channel)
channelModerationPatches := []*model.ChannelModerationPatch{
{
Name: new(model.PermissionCreatePost.Id),
Roles: &model.ChannelModeratedRolesPatch{
Guests: new(true),
Members: new(false),
},
},
}
err := th.App.SetPhase2PermissionsMigrationStatus(true)
require.NoError(t, err)
_, appErr := th.App.PatchChannelModerationsForChannel(th.Context, channel, channelModerationPatches)
require.Nil(t, appErr)
scheduledPost := &model.ScheduledPost{
Draft: model.Draft{
CreateAt: model.GetMillis(),
UserId: th.BasicUser.Id,
ChannelId: channel.Id,
Message: "this is a scheduled post",
},
ScheduledAt: model.GetMillis() + 100000, // 100 seconds in the future
}
createdScheduledPost, _, httpErr := client.CreateScheduledPost(context.Background(), scheduledPost)
require.Error(t, httpErr)
require.Contains(t, httpErr.Error(), "You do not have the appropriate permissions.")
require.Nil(t, createdScheduledPost)
})
}