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