github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/auto_teams.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  	"github.com/mattermost/mattermost-server/model"
     8  	"github.com/mattermost/mattermost-server/utils"
     9  )
    10  
    11  type TeamEnvironment struct {
    12  	Users    []*model.User
    13  	Channels []*model.Channel
    14  }
    15  
    16  type AutoTeamCreator struct {
    17  	client        *model.Client4
    18  	Fuzzy         bool
    19  	NameLength    utils.Range
    20  	NameCharset   string
    21  	DomainLength  utils.Range
    22  	DomainCharset string
    23  	EmailLength   utils.Range
    24  	EmailCharset  string
    25  }
    26  
    27  func NewAutoTeamCreator(client *model.Client4) *AutoTeamCreator {
    28  	return &AutoTeamCreator{
    29  		client:        client,
    30  		Fuzzy:         false,
    31  		NameLength:    TEAM_NAME_LEN,
    32  		NameCharset:   utils.LOWERCASE,
    33  		DomainLength:  TEAM_DOMAIN_NAME_LEN,
    34  		DomainCharset: utils.LOWERCASE,
    35  		EmailLength:   TEAM_EMAIL_LEN,
    36  		EmailCharset:  utils.LOWERCASE,
    37  	}
    38  }
    39  
    40  func (cfg *AutoTeamCreator) createRandomTeam() (*model.Team, bool) {
    41  	var teamEmail string
    42  	var teamDisplayName string
    43  	var teamName string
    44  	if cfg.Fuzzy {
    45  		teamEmail = "success+" + model.NewId() + "simulator.amazonses.com"
    46  		teamDisplayName = utils.FuzzName()
    47  		teamName = utils.FuzzName()
    48  	} else {
    49  		teamEmail = "success+" + model.NewId() + "simulator.amazonses.com"
    50  		teamDisplayName = utils.RandomName(cfg.NameLength, cfg.NameCharset)
    51  		teamName = utils.RandomName(cfg.NameLength, cfg.NameCharset) + model.NewId()
    52  	}
    53  	team := &model.Team{
    54  		DisplayName: teamDisplayName,
    55  		Name:        teamName,
    56  		Email:       teamEmail,
    57  		Type:        model.TEAM_OPEN,
    58  	}
    59  
    60  	createdTeam, err := cfg.client.CreateTeam(team)
    61  	if err != nil {
    62  		return nil, false
    63  	}
    64  	return createdTeam, true
    65  }
    66  
    67  func (cfg *AutoTeamCreator) CreateTestTeams(num utils.Range) ([]*model.Team, bool) {
    68  	numTeams := utils.RandIntFromRange(num)
    69  	teams := make([]*model.Team, numTeams)
    70  
    71  	for i := 0; i < numTeams; i++ {
    72  		var err bool
    73  		teams[i], err = cfg.createRandomTeam()
    74  		if !err {
    75  			return teams, false
    76  		}
    77  	}
    78  
    79  	return teams, true
    80  }