From 08b7b1c4144f18a9dbf52bfe0eb9182f2b4eba4a Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 23 Oct 2017 02:39:51 -0700 Subject: Reduce cfg boilerplate (#7689) * reduce cfg boilerplate * fix compile error * user test fix * another test fix * simplify diagnostics / testing --- api4/user_test.go | 82 +++---------------------------------------------------- 1 file changed, 4 insertions(+), 78 deletions(-) (limited to 'api4/user_test.go') diff --git a/api4/user_test.go b/api4/user_test.go index 59057f347..664b7375a 100644 --- a/api4/user_test.go +++ b/api4/user_test.go @@ -63,12 +63,6 @@ func TestCreateUser(t *testing.T) { CheckErrorMessage(t, resp, "model.user.is_valid.email.app_error") CheckBadRequestStatus(t, resp) - openServer := *th.App.Config().TeamSettings.EnableOpenServer - canCreateAccount := th.App.Config().TeamSettings.EnableUserCreation - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = openServer }) - th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableUserCreation = canCreateAccount }) - }() th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = false }) th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableUserCreation = false }) @@ -185,15 +179,13 @@ func TestCreateUserWithHash(t *testing.T) { data := model.MapToJson(props) hash := utils.HashSha256(fmt.Sprintf("%v:%v", data, th.App.Config().EmailSettings.InviteSalt)) - canCreateAccount := th.App.Config().TeamSettings.EnableUserCreation - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableUserCreation = canCreateAccount }) - }() th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableUserCreation = false }) _, resp := Client.CreateUserWithHash(&user, hash, data) CheckNotImplementedStatus(t, resp) CheckErrorMessage(t, resp, "api.user.create_user.signup_email_disabled.app_error") + + th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableUserCreation = true }) }) t.Run("EnableOpenServerDisable", func(t *testing.T) { @@ -208,10 +200,6 @@ func TestCreateUserWithHash(t *testing.T) { data := model.MapToJson(props) hash := utils.HashSha256(fmt.Sprintf("%v:%v", data, th.App.Config().EmailSettings.InviteSalt)) - openServer := *th.App.Config().TeamSettings.EnableOpenServer - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = openServer }) - }() th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = false }) ruser, resp := Client.CreateUserWithHash(&user, hash, data) @@ -291,10 +279,6 @@ func TestCreateUserWithInviteId(t *testing.T) { t.Run("EnableUserCreationDisable", func(t *testing.T) { user := model.User{Email: GenerateTestEmail(), Nickname: "Corey Hulen", Password: "hello1", Username: GenerateTestUsername(), Roles: model.ROLE_SYSTEM_ADMIN.Id + " " + model.ROLE_SYSTEM_USER.Id} - canCreateAccount := th.App.Config().TeamSettings.EnableUserCreation - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableUserCreation = canCreateAccount }) - }() th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableUserCreation = false }) inviteId := th.BasicTeam.InviteId @@ -302,15 +286,13 @@ func TestCreateUserWithInviteId(t *testing.T) { _, resp := Client.CreateUserWithInviteId(&user, inviteId) CheckNotImplementedStatus(t, resp) CheckErrorMessage(t, resp, "api.user.create_user.signup_email_disabled.app_error") + + th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableUserCreation = true }) }) t.Run("EnableOpenServerDisable", func(t *testing.T) { user := model.User{Email: GenerateTestEmail(), Nickname: "Corey Hulen", Password: "hello1", Username: GenerateTestUsername(), Roles: model.ROLE_SYSTEM_ADMIN.Id + " " + model.ROLE_SYSTEM_USER.Id} - openServer := *th.App.Config().TeamSettings.EnableOpenServer - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = openServer }) - }() th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = false }) inviteId := th.BasicTeam.InviteId @@ -380,12 +362,6 @@ func TestGetUser(t *testing.T) { CheckNotFoundStatus(t, resp) // Check against privacy config settings - emailPrivacy := th.App.Config().PrivacySettings.ShowEmailAddress - namePrivacy := th.App.Config().PrivacySettings.ShowFullName - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowEmailAddress = emailPrivacy }) - th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = namePrivacy }) - }() th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowEmailAddress = false }) th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = false }) @@ -441,12 +417,6 @@ func TestGetUserByUsername(t *testing.T) { CheckNotFoundStatus(t, resp) // Check against privacy config settings - emailPrivacy := th.App.Config().PrivacySettings.ShowEmailAddress - namePrivacy := th.App.Config().PrivacySettings.ShowFullName - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowEmailAddress = emailPrivacy }) - th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = namePrivacy }) - }() th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowEmailAddress = false }) th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = false }) @@ -505,12 +475,6 @@ func TestGetUserByEmail(t *testing.T) { CheckNotFoundStatus(t, resp) // Check against privacy config settings - emailPrivacy := th.App.Config().PrivacySettings.ShowEmailAddress - namePrivacy := th.App.Config().PrivacySettings.ShowFullName - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowEmailAddress = emailPrivacy }) - th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = namePrivacy }) - }() th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowEmailAddress = false }) th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = false }) @@ -672,12 +636,6 @@ func TestSearchUsers(t *testing.T) { search.Term = th.BasicUser.Username - emailPrivacy := th.App.Config().PrivacySettings.ShowEmailAddress - namePrivacy := th.App.Config().PrivacySettings.ShowFullName - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowEmailAddress = emailPrivacy }) - th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = namePrivacy }) - }() th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowEmailAddress = false }) th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = false }) @@ -830,10 +788,6 @@ func TestAutocompleteUsers(t *testing.T) { CheckNoError(t, resp) // Check against privacy config settings - namePrivacy := th.App.Config().PrivacySettings.ShowFullName - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = namePrivacy }) - }() th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = false }) th.LoginBasic() @@ -1739,10 +1693,6 @@ func TestUpdateUserPassword(t *testing.T) { th.LoginBasic() // Test lockout - passwordAttempts := *th.App.Config().ServiceSettings.MaximumLoginAttempts - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.MaximumLoginAttempts = passwordAttempts }) - }() th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.MaximumLoginAttempts = 2 }) // Fail twice @@ -2181,10 +2131,6 @@ func TestSwitchAccount(t *testing.T) { defer th.TearDown() Client := th.Client - enableGitLab := th.App.Config().GitLabSettings.Enable - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.Enable = enableGitLab }) - }() th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.Enable = true }) Client.Logout() @@ -2275,10 +2221,6 @@ func TestCreateUserAccessToken(t *testing.T) { testDescription := "test token" - enableUserAccessTokens := *th.App.Config().ServiceSettings.EnableUserAccessTokens - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = enableUserAccessTokens }) - }() th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = true }) _, resp := Client.CreateUserAccessToken(th.BasicUser.Id, testDescription) @@ -2360,10 +2302,6 @@ func TestGetUserAccessToken(t *testing.T) { testDescription := "test token" - enableUserAccessTokens := *th.App.Config().ServiceSettings.EnableUserAccessTokens - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = enableUserAccessTokens }) - }() th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = true }) _, resp := Client.GetUserAccessToken("123") @@ -2431,10 +2369,6 @@ func TestRevokeUserAccessToken(t *testing.T) { testDescription := "test token" - enableUserAccessTokens := *th.App.Config().ServiceSettings.EnableUserAccessTokens - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = enableUserAccessTokens }) - }() th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = true }) th.App.UpdateUserRoles(th.BasicUser.Id, model.ROLE_SYSTEM_USER.Id+" "+model.ROLE_SYSTEM_USER_ACCESS_TOKEN.Id) @@ -2572,10 +2506,6 @@ func TestUserAccessTokenInactiveUser(t *testing.T) { testDescription := "test token" - enableUserAccessTokens := *th.App.Config().ServiceSettings.EnableUserAccessTokens - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = enableUserAccessTokens }) - }() th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = true }) th.App.UpdateUserRoles(th.BasicUser.Id, model.ROLE_SYSTEM_USER.Id+" "+model.ROLE_SYSTEM_USER_ACCESS_TOKEN.Id) @@ -2599,10 +2529,6 @@ func TestUserAccessTokenDisableConfig(t *testing.T) { testDescription := "test token" - enableUserAccessTokens := *th.App.Config().ServiceSettings.EnableUserAccessTokens - defer func() { - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = enableUserAccessTokens }) - }() th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = true }) th.App.UpdateUserRoles(th.BasicUser.Id, model.ROLE_SYSTEM_USER.Id+" "+model.ROLE_SYSTEM_USER_ACCESS_TOKEN.Id) -- cgit v1.2.3-1-g7c22