Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions server/api/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ func (h *BotHandler) connect(c *Context, w http.ResponseWriter, r *http.Request)
} else if app.ShouldSendDailyDigestMessage(info, timezone, currentTime) {
sendRegularDigest(DigestSenderParams{isWeekly: false})
}

w.WriteHeader(http.StatusOK)
}

func (h *BotHandler) createDigestSender(c *Context, w http.ResponseWriter, userID string, userInfo *app.UserInfo) func(DigestSenderParams) {
Expand Down
52 changes: 52 additions & 0 deletions server/api_bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/mattermost/mattermost/server/public/model"
)
Expand Down Expand Up @@ -53,3 +54,54 @@ func TestTrialLicences(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
}

// TestBotConnect exercises the real GET /bot/connect endpoint end-to-end (real server, real
// UserInfoStore and PlaybookRunService — no mocks), confirming it returns 200 in the conditions
// it handles. The weekly/daily digest-timing decision itself is unit-tested in
// app/regular_digest_service_test.go; this only confirms the handler reaches a 200 either way.
func TestBotConnect(t *testing.T) {
e := Setup(t)
e.CreateBasic()

// e.RegularUser already owns BasicRun from e.CreateBasic() (via CreateBasicRun()), so
// there's something to digest here without any extra setup.
botUser, _, err := e.ServerAdminClient.GetUserByUsername(context.Background(), "playbooks", "")
require.NoError(t, err)

dmChannel, _, err := e.ServerClient.CreateDirectChannel(context.Background(), botUser.Id, e.RegularUser.Id)
require.NoError(t, err)

var digestPost *model.Post

t.Run("digest due for a user connecting for the first time", func(t *testing.T) {
resp, err := e.DoPluginAPIRequestWithHeaders(context.Background(), e.ServerClient, http.MethodGet, "/api/v0/bot/connect", "", nil)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)

// The bot posts the digest DM synchronously within connect() (Bot.DM ->
// pluginAPI.Post.CreatePost, no goroutine/queue), so it's already there by the
// time the HTTP response comes back — no need to poll or wait for it.
posts, _, err := e.ServerClient.GetPostsForChannel(context.Background(), dmChannel.Id, 0, 10, "", false, false)
require.NoError(t, err)
require.NotEmpty(t, posts.Order, "expected the bot to have DMed a digest")
digestPost = posts.Posts[posts.Order[0]]
assert.Equal(t, botUser.Id, digestPost.UserId)
assert.NotEmpty(t, digestPost.Message)
})

t.Run("no digest due immediately after the first connect", func(t *testing.T) {
// The first subtest's connect() call already persisted LastDailyTodoDMAt = now,
// so calling connect() again moments later deterministically finds both
// ShouldSendWeeklyDigestMessage (same ISO week) and ShouldSendDailyDigestMessage
// (well under an hour elapsed) false — no need for a second user or settings.
resp, err := e.DoPluginAPIRequestWithHeaders(context.Background(), e.ServerClient, http.MethodGet, "/api/v0/bot/connect", "", nil)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)

// GetPostsSince is exclusive (server queries UpdateAt > since), so digestPost
// itself won't reappear here.
sincePosts, _, err := e.ServerClient.GetPostsSince(context.Background(), dmChannel.Id, digestPost.CreateAt, false)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we're using a global variables here and the subtests are dependent, we should require.NotNil on digestPost.

Having the protection inside for first subtest does not protect the second one, and the second subtest will panic.

require.NoError(t, err)
assert.Empty(t, sincePosts.Order, "expected no digest on the second immediate connect")
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Comment thread
lieut-data marked this conversation as resolved.
})
}
Loading