summaryrefslogtreecommitdiffstats
path: root/app/auto_teams.go
blob: 9dce5bd157ffa1de51fcbd0f71f3b5596b431b9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package app

import (
	"github.com/mattermost/mattermost-server/model"
	"github.com/mattermost/mattermost-server/utils"
)

type TeamEnvironment struct {
	Users    []*model.User
	Channels []*model.Channel
}

type AutoTeamCreator struct {
	client        *model.Client
	Fuzzy         bool
	NameLength    utils.Range
	NameCharset   string
	DomainLength  utils.Range
	DomainCharset string
	EmailLength   utils.Range
	EmailCharset  string
}

func NewAutoTeamCreator(client *model.Client) *AutoTeamCreator {
	return &AutoTeamCreator{
		client:        client,
		Fuzzy:         false,
		NameLength:    TEAM_NAME_LEN,
		NameCharset:   utils.LOWERCASE,
		DomainLength:  TEAM_DOMAIN_NAME_LEN,
		DomainCharset: utils.LOWERCASE,
		EmailLength:   TEAM_EMAIL_LEN,
		EmailCharset:  utils.LOWERCASE,
	}
}

func (cfg *AutoTeamCreator) createRandomTeam() (*model.Team, bool) {
	var teamEmail string
	var teamDisplayName string
	var teamName string
	if cfg.Fuzzy {
		teamEmail = "success+" + model.NewId() + "simulator.amazonses.com"
		teamDisplayName = utils.FuzzName()
		teamName = utils.FuzzName()
	} else {
		teamEmail = "success+" + model.NewId() + "simulator.amazonses.com"
		teamDisplayName = utils.RandomName(cfg.NameLength, cfg.NameCharset)
		teamName = utils.RandomName(cfg.NameLength, cfg.NameCharset) + model.NewId()
	}
	team := &model.Team{
		DisplayName: teamDisplayName,
		Name:        teamName,
		Email:       teamEmail,
		Type:        model.TEAM_OPEN,
	}

	result, err := cfg.client.CreateTeam(team)
	if err != nil {
		return nil, false
	}
	createdTeam := result.Data.(*model.Team)
	return createdTeam, true
}

func (cfg *AutoTeamCreator) CreateTestTeams(num utils.Range) ([]*model.Team, bool) {
	numTeams := utils.RandIntFromRange(num)
	teams := make([]*model.Team, numTeams)

	for i := 0; i < numTeams; i++ {
		var err bool
		teams[i], err = cfg.createRandomTeam()
		if !err {
			return teams, false
		}
	}

	return teams, true
}