github.com/mattermost/mattermost-server/v5@v5.39.3/services/users/helper_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package users 5 6 import ( 7 "bytes" 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "runtime" 12 "sync" 13 "testing" 14 15 "github.com/mattermost/mattermost-server/v5/app/request" 16 "github.com/mattermost/mattermost-server/v5/config" 17 "github.com/mattermost/mattermost-server/v5/model" 18 "github.com/mattermost/mattermost-server/v5/services/cache" 19 "github.com/mattermost/mattermost-server/v5/store" 20 ) 21 22 var initBasicOnce sync.Once 23 24 type TestHelper struct { 25 service *UserService 26 configStore *config.Store 27 dbStore store.Store 28 workspace string 29 30 Context *request.Context 31 BasicUser *model.User 32 BasicUser2 *model.User 33 34 SystemAdminUser *model.User 35 LogBuffer *bytes.Buffer 36 } 37 38 func Setup(tb testing.TB) *TestHelper { 39 if testing.Short() { 40 tb.SkipNow() 41 } 42 dbStore := mainHelper.GetStore() 43 dbStore.DropAllTables() 44 dbStore.MarkSystemRanUnitTests() 45 mainHelper.PreloadMigrations() 46 47 return setupTestHelper(dbStore, false, tb) 48 } 49 50 func setupTestHelper(s store.Store, includeCacheLayer bool, tb testing.TB) *TestHelper { 51 tempWorkspace, err := ioutil.TempDir("", "userservicetest") 52 if err != nil { 53 panic(err) 54 } 55 56 configStore := config.NewTestMemoryStore() 57 58 config := configStore.Get() 59 *config.PluginSettings.Directory = filepath.Join(tempWorkspace, "plugins") 60 *config.PluginSettings.ClientDirectory = filepath.Join(tempWorkspace, "webapp") 61 *config.PluginSettings.AutomaticPrepackagedPlugins = false 62 *config.LogSettings.EnableSentry = false // disable error reporting during tests 63 *config.AnnouncementSettings.AdminNoticesEnabled = false 64 *config.AnnouncementSettings.UserNoticesEnabled = false 65 *config.TeamSettings.MaxUsersPerTeam = 50 66 *config.RateLimitSettings.Enable = false 67 *config.TeamSettings.EnableOpenServer = true 68 // Disable strict password requirements for test 69 *config.PasswordSettings.MinimumLength = 5 70 *config.PasswordSettings.Lowercase = false 71 *config.PasswordSettings.Uppercase = false 72 *config.PasswordSettings.Symbol = false 73 *config.PasswordSettings.Number = false 74 configStore.Set(config) 75 76 buffer := &bytes.Buffer{} 77 provider := cache.NewProvider() 78 cache, err := provider.NewCache(&cache.CacheOptions{ 79 Size: model.SESSION_CACHE_SIZE, 80 Striped: true, 81 StripedBuckets: maxInt(runtime.NumCPU()-1, 1), 82 }) 83 if err != nil { 84 panic(err) 85 } 86 return &TestHelper{ 87 service: &UserService{ 88 store: s.User(), 89 sessionStore: s.Session(), 90 oAuthStore: s.OAuth(), 91 sessionCache: cache, 92 config: configStore.Get, 93 sessionPool: sync.Pool{ 94 New: func() interface{} { 95 return &model.Session{} 96 }, 97 }, 98 }, 99 Context: &request.Context{}, 100 configStore: configStore, 101 dbStore: s, 102 LogBuffer: buffer, 103 workspace: tempWorkspace, 104 } 105 } 106 107 func (th *TestHelper) InitBasic() *TestHelper { 108 // create users once and cache them because password hashing is slow 109 initBasicOnce.Do(func() { 110 th.SystemAdminUser = th.CreateUser() 111 th.SystemAdminUser, _ = th.service.GetUser(th.SystemAdminUser.Id) 112 113 th.BasicUser = th.CreateUser() 114 th.BasicUser, _ = th.service.GetUser(th.BasicUser.Id) 115 116 th.BasicUser2 = th.CreateUser() 117 th.BasicUser2, _ = th.service.GetUser(th.BasicUser2.Id) 118 }) 119 120 return th 121 } 122 123 func (th *TestHelper) CreateUser() *model.User { 124 return th.CreateUserOrGuest(false) 125 } 126 127 func (th *TestHelper) CreateGuest() *model.User { 128 return th.CreateUserOrGuest(true) 129 } 130 131 func (th *TestHelper) CreateUserOrGuest(guest bool) *model.User { 132 id := model.NewId() 133 134 user := &model.User{ 135 Email: "success+" + id + "@simulator.amazonses.com", 136 Username: "un_" + id, 137 Nickname: "nn_" + id, 138 Password: "Password1", 139 EmailVerified: true, 140 } 141 142 var err error 143 if guest { 144 if user, err = th.service.CreateUser(user, UserCreateOptions{Guest: true}); err != nil { 145 panic(err) 146 } 147 } else { 148 if user, err = th.service.CreateUser(user, UserCreateOptions{}); err != nil { 149 panic(err) 150 } 151 } 152 return user 153 } 154 155 func (th *TestHelper) TearDown() { 156 th.configStore.Close() 157 158 th.dbStore.Close() 159 160 if th.workspace != "" { 161 os.RemoveAll(th.workspace) 162 } 163 } 164 165 func (th *TestHelper) UpdateConfig(f func(*model.Config)) { 166 if th.configStore.IsReadOnly() { 167 return 168 } 169 old := th.configStore.Get() 170 updated := old.Clone() 171 f(updated) 172 if _, _, err := th.configStore.Set(updated); err != nil { 173 panic(err) 174 } 175 }