github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/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/masterhung0112/hk_server/v5/app/request" 16 "github.com/masterhung0112/hk_server/v5/config" 17 "github.com/masterhung0112/hk_server/v5/model" 18 "github.com/masterhung0112/hk_server/v5/services/cache" 19 "github.com/masterhung0112/hk_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 78 provider := cache.NewProvider() 79 cache, err := provider.NewCache(&cache.CacheOptions{ 80 Size: model.SESSION_CACHE_SIZE, 81 Striped: true, 82 StripedBuckets: maxInt(runtime.NumCPU()-1, 1), 83 }) 84 if err != nil { 85 panic(err) 86 } 87 return &TestHelper{ 88 service: &UserService{ 89 store: s.User(), 90 sessionStore: s.Session(), 91 oAuthStore: s.OAuth(), 92 sessionCache: cache, 93 config: configStore.Get, 94 sessionPool: sync.Pool{ 95 New: func() interface{} { 96 return &model.Session{} 97 }, 98 }, 99 }, 100 Context: &request.Context{}, 101 configStore: configStore, 102 dbStore: s, 103 LogBuffer: buffer, 104 workspace: tempWorkspace, 105 } 106 } 107 108 func (th *TestHelper) InitBasic() *TestHelper { 109 // create users once and cache them because password hashing is slow 110 initBasicOnce.Do(func() { 111 th.SystemAdminUser = th.CreateUser() 112 th.SystemAdminUser, _ = th.service.GetUser(th.SystemAdminUser.Id) 113 114 th.BasicUser = th.CreateUser() 115 th.BasicUser, _ = th.service.GetUser(th.BasicUser.Id) 116 117 th.BasicUser2 = th.CreateUser() 118 th.BasicUser2, _ = th.service.GetUser(th.BasicUser2.Id) 119 }) 120 121 return th 122 } 123 124 func (th *TestHelper) CreateUser() *model.User { 125 return th.CreateUserOrGuest(false) 126 } 127 128 func (th *TestHelper) CreateGuest() *model.User { 129 return th.CreateUserOrGuest(true) 130 } 131 132 func (th *TestHelper) CreateUserOrGuest(guest bool) *model.User { 133 id := model.NewId() 134 135 user := &model.User{ 136 Email: "success+" + id + "@simulator.amazonses.com", 137 Username: "un_" + id, 138 Nickname: "nn_" + id, 139 Password: "Password1", 140 EmailVerified: true, 141 } 142 143 var err error 144 if guest { 145 if user, err = th.service.CreateUser(user, UserCreateOptions{Guest: true}); err != nil { 146 panic(err) 147 } 148 } else { 149 if user, err = th.service.CreateUser(user, UserCreateOptions{}); err != nil { 150 panic(err) 151 } 152 } 153 return user 154 } 155 156 func (th *TestHelper) TearDown() { 157 th.configStore.Close() 158 159 th.dbStore.Close() 160 161 if th.workspace != "" { 162 os.RemoveAll(th.workspace) 163 } 164 } 165 166 func (th *TestHelper) UpdateConfig(f func(*model.Config)) { 167 if th.configStore.IsReadOnly() { 168 return 169 } 170 old := th.configStore.Get() 171 updated := old.Clone() 172 f(updated) 173 if _, _, err := th.configStore.Set(updated); err != nil { 174 panic(err) 175 } 176 }