Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dramatically reduce usage of mocked plugin tests #707

Merged
merged 25 commits into from
Jul 9, 2024
Merged

Conversation

lieut-data
Copy link
Member

@lieut-data lieut-data commented Jul 2, 2024

Summary

This is a suite of changes to dramatically reduce the use of mocked plugin tests in favour of the new reattached plugin testing methodology. This is mostly just unit tests, but a few changes were completed to the core product to simplify along the way.

This is a precursor to another round of simplification to help us converge on v10 deliverables.

Ticket Link

Fixes: https://mattermost.atlassian.net/browse/MM-55512

@@ -147,7 +147,7 @@ func (ah *ActivityHandler) handleAttachments(channelID, userID, text string, msg

// handle a message reference (reply)
if a.ContentType == "messageReference" {
parentID, newText = ah.handleMessageReference(a, msg.ChatID+msg.ChannelID, newText)
Copy link
Member Author

Choose a reason for hiding this comment

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

newText is never used, and thus never modified.

Comment on lines -34 to -40
func (p *Plugin) handlePromptForConnection(userID, channelID string) {
// For now, don't display the connect message

// message := "Some users in this conversation rely on Microsoft Teams to receive your messages, but your account isn't connected. "
// p.SendEphemeralConnectMessage(channelID, userID, message)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

As we're not showing the message at all, let's simplify this.

@@ -300,7 +300,7 @@ func (ah *ActivityHandler) handleCreatedActivity(msg *clientmodels.Message, subs

var chatShouldSync bool
var discardReason string
chatShouldSync, _, _, discardReason, err = ah.plugin.ChatShouldSync(channel)
Copy link
Member Author

Choose a reason for hiding this comment

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

We no longer need the containsRemoteUser return value anywhere.

Comment on lines +580 to +586
if !ah.plugin.GetSyncChats() {
return metrics.DiscardedReasonChatsDisabled
}
} else {
if !ah.plugin.GetSyncLinkedChannels() {
return metrics.DiscardedReasonLinkedChannelsDisabled
}
Copy link
Member Author

Choose a reason for hiding this comment

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

We now also discard if sync is enabled for chats or channels. Previously, this code would potentially try to still delete messages despite this functionality being disabled.

@@ -585,8 +595,10 @@ func (ah *ActivityHandler) handleDeletedActivity(activityIds clientmodels.Activi
return metrics.DiscardedReasonOther
}

ah.IgnorePluginHooksMap.Store(fmt.Sprintf("delete_post_%s", postInfo.MattermostID), true)
Copy link
Member Author

Choose a reason for hiding this comment

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

Switching to the new unit tests exposed a race condition that the plugin elsewhere solves using the IgnorePluginHooksMap -- the idea is that when we call DeletePost below, the server will reflect that event back to us as a message hook, and we'll try to delete the Teams post (which will in turn fail because it's already deleted). I'm uncertain still if this is the right long term solution, but adding it here to mimic what we already do for creating posts and reactions.

@@ -89,7 +94,7 @@ func (p *Plugin) MessageHasBeenPosted(_ *plugin.Context, post *model.Post) {
}

if isDirectOrGroupMessage {
chatShouldSync, containsRemoteUser, members, _, err := p.ChatShouldSync(channel)
Copy link
Member Author

Choose a reason for hiding this comment

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

The return value of containsRemoteUser is no longer needed.

@@ -102,7 +107,7 @@ func (p *Plugin) MessageHasBeenPosted(_ *plugin.Context, post *model.Post) {
dstUsers = append(dstUsers, m.UserId)
}

_, err = p.SendChat(post.UserId, dstUsers, post, containsRemoteUser)
Copy link
Member Author

Choose a reason for hiding this comment

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

The parameter containsRemoteUser is no longer used in p.SendChat.

@@ -234,7 +239,7 @@ func (p *Plugin) MessageHasBeenUpdated(c *plugin.Context, newPost, _ /*oldPost*/

var chatShouldSync bool
var members []*model.ChannelMember
chatShouldSync, _, members, _, err = p.ChatShouldSync(channel)
Copy link
Member Author

Choose a reason for hiding this comment

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

The return value of containsRemoteUser is no longer needed.

Comment on lines +174 to +176
func (p *Plugin) GetURL() string {
return getURL(p.API.GetConfig())
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Testing these without a mock proved tricky because of how the server intervenes and changes the SiteURL. So I opted to refactor and just test getURL and getRelativeURL directly from passed *model.Config parameters.

@@ -17,28 +17,28 @@ import (
//
// TODO: This method does too much, but it's reflective of the underlying complexity of the
// business logic. Thankfully, it's well tested!
func (p *Plugin) ChatShouldSync(channel *model.Channel) (bool, bool, []*model.ChannelMember, string, error) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The return value of containsRemoteUser is no longer needed.

@lieut-data lieut-data requested review from JulienTant and sbishel and removed request for JulienTant July 2, 2024 14:57
@lieut-data lieut-data marked this pull request as ready for review July 2, 2024 14:57
@lieut-data lieut-data requested a review from enahum July 8, 2024 17:00
@lieut-data
Copy link
Member Author

@enahum, I know this is outside your usual wheel house, but wondering if you can help give a second review?

Copy link
Contributor

@enahum enahum left a comment

Choose a reason for hiding this comment

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

Overall looks good to me

Comment on lines +142 to 149
// Simulate the post having originated from Mattermost. Later, we'll let the code
// do this itself once.
err = th.p.GetStore().LinkPosts(storemodels.PostInfo{
MattermostID: post.Id,
MSTeamsID: messageID,
MSTeamsChannel: chatOrChannelID,
MSTeamsLastUpdateAt: time.Now(),
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Just out of curiosity, what is the reason to have to force it here?

@lieut-data lieut-data merged commit c8403bd into main Jul 9, 2024
9 checks passed
@lieut-data lieut-data deleted the less-newTestPlugin branch July 9, 2024 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants