Skip to content

Instantly share code, notes, and snippets.

@lieut-data
Created April 16, 2020 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lieut-data/a44ffd2c0bb7ec5a7b17c5a263f52906 to your computer and use it in GitHub Desktop.
Save lieut-data/a44ffd2c0bb7ec5a7b17c5a263f52906 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/pkg/errors"
)
func main() {
server := "https://community.mattermost.com"
authToken := os.Getenv("MM_AUTH_TOKEN")
teamName := "core"
for _, arg := range os.Args {
if arg == "--daily" {
server = "https://community-daily.mattermost.com"
}
if arg == "--release" {
server = "https://community-release.mattermost.com"
}
if arg == "--test2" {
server = "https://debug2.test.mattermost.cloud/"
teamName = "test2"
}
}
fmt.Printf("Testing against %s\n", server)
client := model.NewAPIv4Client(server)
client.AuthToken = authToken
client.AuthType = model.HEADER_BEARER
if testServer(client, teamName) {
fmt.Println("PASS")
} else {
fmt.Println("FAIL")
}
}
// testServer tried 10 times to reproduce MM-24165.
func testServer(client *model.Client4, teamName string) bool {
i := 1
for ; i < 10; i++ {
err := testAttempt(client, teamName)
if err != nil {
fmt.Printf("Failed after %d attempts: %s\n", i, err.Error())
return false
}
}
fmt.Printf("Passed after %d attempts\n", i)
return true
}
// testAttempt tries to reproduce MM-24165 by creating a random private channel and querying
// the channel members to determine if the creating user successfully joined the channel.
func testAttempt(client *model.Client4, teamName string) error {
me, response := client.GetMe("")
if response.Error != nil {
return errors.Wrap(response.Error, "failed to get me")
}
team, response := client.GetTeamByName(teamName, "")
if response.Error != nil {
return errors.Wrap(response.Error, "failed to get core team")
}
name := model.NewId()
channel, response := client.CreateChannel(&model.Channel{
TeamId: team.Id,
Name: name,
Type: model.CHANNEL_PRIVATE,
})
if response.Error != nil {
return errors.Wrap(response.Error, "failed to create channel")
}
channelMembers, response := client.GetChannelMembers(channel.Id, 0, 100, "")
if response.Error != nil {
return errors.Wrapf(response.Error, "failed to get channel members for channel %s", channel.Id)
}
if len(*channelMembers) != 1 {
return errors.Errorf("unexpected membership count: %d", len(*channelMembers))
}
if (*channelMembers)[0].UserId != me.Id {
return errors.Errorf("unexpected channel member: %s", (*channelMembers)[0].UserId)
}
_, response = client.DeleteChannel(channel.Id)
if response.Error != nil {
return errors.Wrap(response.Error, "failed to delete channel")
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment