github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/auto_environment.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"math/rand"
     8  	"time"
     9  
    10  	"github.com/mattermost/mattermost-server/model"
    11  	"github.com/mattermost/mattermost-server/utils"
    12  )
    13  
    14  type TestEnvironment struct {
    15  	Teams        []*model.Team
    16  	Environments []TeamEnvironment
    17  }
    18  
    19  func CreateTestEnvironmentWithTeams(a *App, client *model.Client4, rangeTeams utils.Range, rangeChannels utils.Range, rangeUsers utils.Range, rangePosts utils.Range, fuzzy bool) (TestEnvironment, bool) {
    20  	rand.Seed(time.Now().UTC().UnixNano())
    21  
    22  	teamCreator := NewAutoTeamCreator(client)
    23  	teamCreator.Fuzzy = fuzzy
    24  	teams, err := teamCreator.CreateTestTeams(rangeTeams)
    25  	if !err {
    26  		return TestEnvironment{}, false
    27  	}
    28  
    29  	environment := TestEnvironment{teams, make([]TeamEnvironment, len(teams))}
    30  
    31  	for i, team := range teams {
    32  		userCreator := NewAutoUserCreator(a, client, team)
    33  		userCreator.Fuzzy = fuzzy
    34  		randomUser, err := userCreator.createRandomUser()
    35  		if !err {
    36  			return TestEnvironment{}, false
    37  		}
    38  		client.LoginById(randomUser.Id, USER_PASSWORD)
    39  		teamEnvironment, err := CreateTestEnvironmentInTeam(a, client, team, rangeChannels, rangeUsers, rangePosts, fuzzy)
    40  		if !err {
    41  			return TestEnvironment{}, false
    42  		}
    43  		environment.Environments[i] = teamEnvironment
    44  	}
    45  
    46  	return environment, true
    47  }
    48  
    49  func CreateTestEnvironmentInTeam(a *App, client *model.Client4, team *model.Team, rangeChannels utils.Range, rangeUsers utils.Range, rangePosts utils.Range, fuzzy bool) (TeamEnvironment, bool) {
    50  	rand.Seed(time.Now().UTC().UnixNano())
    51  
    52  	// We need to create at least one user
    53  	if rangeUsers.Begin <= 0 {
    54  		rangeUsers.Begin = 1
    55  	}
    56  
    57  	userCreator := NewAutoUserCreator(a, client, team)
    58  	userCreator.Fuzzy = fuzzy
    59  	users, err := userCreator.CreateTestUsers(rangeUsers)
    60  	if !err {
    61  		return TeamEnvironment{}, false
    62  	}
    63  	usernames := make([]string, len(users))
    64  	for i, user := range users {
    65  		usernames[i] = user.Username
    66  	}
    67  
    68  	channelCreator := NewAutoChannelCreator(client, team)
    69  	channelCreator.Fuzzy = fuzzy
    70  	channels, err := channelCreator.CreateTestChannels(rangeChannels)
    71  
    72  	// Have every user join every channel
    73  	for _, user := range users {
    74  		for _, channel := range channels {
    75  			client.LoginById(user.Id, USER_PASSWORD)
    76  			client.AddChannelMember(channel.Id, user.Id)
    77  		}
    78  	}
    79  
    80  	if !err {
    81  		return TeamEnvironment{}, false
    82  	}
    83  
    84  	numPosts := utils.RandIntFromRange(rangePosts)
    85  	numImages := utils.RandIntFromRange(rangePosts) / 4
    86  	for j := 0; j < numPosts; j++ {
    87  		user := users[utils.RandIntFromRange(utils.Range{Begin: 0, End: len(users) - 1})]
    88  		client.LoginById(user.Id, USER_PASSWORD)
    89  		for i, channel := range channels {
    90  			postCreator := NewAutoPostCreator(client, channel.Id)
    91  			postCreator.HasImage = i < numImages
    92  			postCreator.Users = usernames
    93  			postCreator.Fuzzy = fuzzy
    94  			postCreator.CreateRandomPost()
    95  		}
    96  	}
    97  
    98  	return TeamEnvironment{users, channels}, true
    99  }