github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/migrations/helper_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package migrations
     5  
     6  import (
     7  	"os"
     8  
     9  	"github.com/mattermost/mattermost-server/v5/app"
    10  	"github.com/mattermost/mattermost-server/v5/config"
    11  	"github.com/mattermost/mattermost-server/v5/model"
    12  	"github.com/mattermost/mattermost-server/v5/store/localcachelayer"
    13  	"github.com/mattermost/mattermost-server/v5/utils"
    14  )
    15  
    16  type TestHelper struct {
    17  	App          *app.App
    18  	Server       *app.Server
    19  	BasicTeam    *model.Team
    20  	BasicUser    *model.User
    21  	BasicUser2   *model.User
    22  	BasicChannel *model.Channel
    23  	BasicPost    *model.Post
    24  
    25  	SystemAdminUser *model.User
    26  
    27  	tempWorkspace string
    28  }
    29  
    30  func setupTestHelper(enterprise bool) *TestHelper {
    31  	store := mainHelper.GetStore()
    32  	store.DropAllTables()
    33  
    34  	memoryStore := config.NewTestMemoryStore()
    35  	var options []app.Option
    36  	options = append(options, app.ConfigStore(memoryStore))
    37  	options = append(options, app.StoreOverride(mainHelper.Store))
    38  
    39  	s, err := app.NewServer(options...)
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  	// Adds the cache layer to the test store
    44  	s.Store, err = localcachelayer.NewLocalCacheLayer(s.Store, s.Metrics, s.Cluster, s.CacheProvider)
    45  	if err != nil {
    46  		panic(err)
    47  	}
    48  
    49  	th := &TestHelper{
    50  		App:    app.New(app.ServerConnector(s)),
    51  		Server: s,
    52  	}
    53  
    54  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.MaxUsersPerTeam = 50 })
    55  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.RateLimitSettings.Enable = false })
    56  	prevListenAddress := *th.App.Config().ServiceSettings.ListenAddress
    57  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = ":0" })
    58  
    59  	serverErr := th.Server.Start()
    60  	if serverErr != nil {
    61  		panic(serverErr)
    62  	}
    63  
    64  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = prevListenAddress })
    65  
    66  	th.App.DoAppMigrations()
    67  
    68  	th.App.Srv().Store.MarkSystemRanUnitTests()
    69  
    70  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = true })
    71  
    72  	if enterprise {
    73  		th.App.Srv().SetLicense(model.NewTestLicense())
    74  	} else {
    75  		th.App.Srv().SetLicense(nil)
    76  	}
    77  
    78  	return th
    79  }
    80  
    81  func SetupEnterprise() *TestHelper {
    82  	return setupTestHelper(true)
    83  }
    84  
    85  func Setup() *TestHelper {
    86  	return setupTestHelper(false)
    87  }
    88  
    89  func (th *TestHelper) InitBasic() *TestHelper {
    90  	th.SystemAdminUser = th.CreateUser()
    91  	th.App.UpdateUserRoles(th.SystemAdminUser.Id, model.SYSTEM_USER_ROLE_ID+" "+model.SYSTEM_ADMIN_ROLE_ID, false)
    92  	th.SystemAdminUser, _ = th.App.GetUser(th.SystemAdminUser.Id)
    93  
    94  	th.BasicTeam = th.CreateTeam()
    95  	th.BasicUser = th.CreateUser()
    96  	th.LinkUserToTeam(th.BasicUser, th.BasicTeam)
    97  	th.BasicUser2 = th.CreateUser()
    98  	th.LinkUserToTeam(th.BasicUser2, th.BasicTeam)
    99  	th.BasicChannel = th.CreateChannel(th.BasicTeam)
   100  	th.BasicPost = th.CreatePost(th.BasicChannel)
   101  
   102  	return th
   103  }
   104  
   105  func (*TestHelper) MakeEmail() string {
   106  	return "success_" + model.NewId() + "@simulator.amazonses.com"
   107  }
   108  
   109  func (th *TestHelper) CreateTeam() *model.Team {
   110  	id := model.NewId()
   111  	team := &model.Team{
   112  		DisplayName: "dn_" + id,
   113  		Name:        "name" + id,
   114  		Email:       "success+" + id + "@simulator.amazonses.com",
   115  		Type:        model.TEAM_OPEN,
   116  	}
   117  
   118  	utils.DisableDebugLogForTest()
   119  	var err *model.AppError
   120  	if team, err = th.App.CreateTeam(team); err != nil {
   121  		panic(err)
   122  	}
   123  	utils.EnableDebugLogForTest()
   124  	return team
   125  }
   126  
   127  func (th *TestHelper) CreateUser() *model.User {
   128  	id := model.NewId()
   129  
   130  	user := &model.User{
   131  		Email:         "success+" + id + "@simulator.amazonses.com",
   132  		Username:      "un_" + id,
   133  		Nickname:      "nn_" + id,
   134  		Password:      "Password1",
   135  		EmailVerified: true,
   136  	}
   137  
   138  	utils.DisableDebugLogForTest()
   139  	var err *model.AppError
   140  	if user, err = th.App.CreateUser(user); err != nil {
   141  		panic(err)
   142  	}
   143  	utils.EnableDebugLogForTest()
   144  	return user
   145  }
   146  
   147  func (th *TestHelper) CreateChannel(team *model.Team) *model.Channel {
   148  	return th.createChannel(team, model.CHANNEL_OPEN)
   149  }
   150  
   151  func (th *TestHelper) createChannel(team *model.Team, channelType string) *model.Channel {
   152  	id := model.NewId()
   153  
   154  	channel := &model.Channel{
   155  		DisplayName: "dn_" + id,
   156  		Name:        "name_" + id,
   157  		Type:        channelType,
   158  		TeamId:      team.Id,
   159  		CreatorId:   th.BasicUser.Id,
   160  	}
   161  
   162  	utils.DisableDebugLogForTest()
   163  	var err *model.AppError
   164  	if channel, err = th.App.CreateChannel(channel, true); err != nil {
   165  		panic(err)
   166  	}
   167  	utils.EnableDebugLogForTest()
   168  	return channel
   169  }
   170  
   171  func (th *TestHelper) CreateDmChannel(user *model.User) *model.Channel {
   172  	utils.DisableDebugLogForTest()
   173  	var err *model.AppError
   174  	var channel *model.Channel
   175  	if channel, err = th.App.GetOrCreateDirectChannel(th.BasicUser.Id, user.Id); err != nil {
   176  		panic(err)
   177  	}
   178  	utils.EnableDebugLogForTest()
   179  	return channel
   180  }
   181  
   182  func (th *TestHelper) CreatePost(channel *model.Channel) *model.Post {
   183  	id := model.NewId()
   184  
   185  	post := &model.Post{
   186  		UserId:    th.BasicUser.Id,
   187  		ChannelId: channel.Id,
   188  		Message:   "message_" + id,
   189  		CreateAt:  model.GetMillis() - 10000,
   190  	}
   191  
   192  	utils.DisableDebugLogForTest()
   193  	var err *model.AppError
   194  	if post, err = th.App.CreatePost(post, channel, false, true); err != nil {
   195  		panic(err)
   196  	}
   197  	utils.EnableDebugLogForTest()
   198  	return post
   199  }
   200  
   201  func (th *TestHelper) LinkUserToTeam(user *model.User, team *model.Team) {
   202  	utils.DisableDebugLogForTest()
   203  
   204  	err := th.App.JoinUserToTeam(team, user, "")
   205  	if err != nil {
   206  		panic(err)
   207  	}
   208  
   209  	utils.EnableDebugLogForTest()
   210  }
   211  
   212  func (th *TestHelper) AddUserToChannel(user *model.User, channel *model.Channel) *model.ChannelMember {
   213  	utils.DisableDebugLogForTest()
   214  
   215  	member, err := th.App.AddUserToChannel(user, channel)
   216  	if err != nil {
   217  		panic(err)
   218  	}
   219  
   220  	utils.EnableDebugLogForTest()
   221  
   222  	return member
   223  }
   224  
   225  func (th *TestHelper) TearDown() {
   226  	// Clean all the caches
   227  	th.App.Srv().InvalidateAllCaches()
   228  	th.Server.Shutdown()
   229  	if th.tempWorkspace != "" {
   230  		os.RemoveAll(th.tempWorkspace)
   231  	}
   232  }
   233  
   234  func (*TestHelper) ResetRoleMigration() {
   235  	sqlStore := mainHelper.GetSQLStore()
   236  	if _, err := sqlStore.GetMaster().Exec("DELETE from Roles"); err != nil {
   237  		panic(err)
   238  	}
   239  
   240  	mainHelper.GetClusterInterface().SendClearRoleCacheMessage()
   241  
   242  	if _, err := sqlStore.GetMaster().Exec("DELETE from Systems where Name = :Name", map[string]interface{}{"Name": model.ADVANCED_PERMISSIONS_MIGRATION_KEY}); err != nil {
   243  		panic(err)
   244  	}
   245  }
   246  
   247  func (th *TestHelper) DeleteAllJobsByTypeAndMigrationKey(jobType string, migrationKey string) {
   248  	jobs, err := th.App.Srv().Store.Job().GetAllByType(model.JOB_TYPE_MIGRATIONS)
   249  	if err != nil {
   250  		panic(err)
   251  	}
   252  
   253  	for _, job := range jobs {
   254  		if key, ok := job.Data[JobDataKeyMigration]; ok && key == migrationKey {
   255  			if _, err = th.App.Srv().Store.Job().Delete(job.Id); err != nil {
   256  				panic(err)
   257  			}
   258  		}
   259  	}
   260  }