From 7ed712f938588491da35b1d04b7f44700975c70b Mon Sep 17 00:00:00 2001 From: Carlos Tadeu Panato Junior Date: Wed, 11 Jul 2018 12:58:16 +0200 Subject: Restore config after finisht the test case (#9085) --- api4/commands_test.go | 25 ++++++++++++++++++++ api4/image_test.go | 9 ++++++++ api4/oauth_test.go | 36 +++++++++++++++++++++++++++++ api4/post_test.go | 9 ++++++++ api4/team_test.go | 14 +++++++++-- api4/user_test.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++--- 6 files changed, 152 insertions(+), 5 deletions(-) (limited to 'api4') diff --git a/api4/commands_test.go b/api4/commands_test.go index cb960c608..66238df7d 100644 --- a/api4/commands_test.go +++ b/api4/commands_test.go @@ -163,6 +163,11 @@ func TestLoadTestHelpCommands(t *testing.T) { Client := th.Client channel := th.BasicChannel + enableTesting := th.App.Config().ServiceSettings.EnableTesting + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableTesting = enableTesting }) + }() + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableTesting = true }) rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test help")).(*model.CommandResponse) @@ -180,6 +185,11 @@ func TestLoadTestSetupCommands(t *testing.T) { Client := th.Client channel := th.BasicChannel + enableTesting := th.App.Config().ServiceSettings.EnableTesting + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableTesting = enableTesting }) + }() + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableTesting = true }) rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test setup fuzz 1 1 1")).(*model.CommandResponse) @@ -197,6 +207,11 @@ func TestLoadTestUsersCommands(t *testing.T) { Client := th.Client channel := th.BasicChannel + enableTesting := th.App.Config().ServiceSettings.EnableTesting + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableTesting = enableTesting }) + }() + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableTesting = true }) rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test users fuzz 1 2")).(*model.CommandResponse) @@ -214,6 +229,11 @@ func TestLoadTestChannelsCommands(t *testing.T) { Client := th.Client channel := th.BasicChannel + enableTesting := th.App.Config().ServiceSettings.EnableTesting + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableTesting = enableTesting }) + }() + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableTesting = true }) rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test channels fuzz 1 2")).(*model.CommandResponse) @@ -231,6 +251,11 @@ func TestLoadTestPostsCommands(t *testing.T) { Client := th.Client channel := th.BasicChannel + enableTesting := th.App.Config().ServiceSettings.EnableTesting + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableTesting = enableTesting }) + }() + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableTesting = true }) rs := Client.Must(Client.ExecuteCommand(channel.Id, "/test posts fuzz 2 3 2")).(*model.CommandResponse) diff --git a/api4/image_test.go b/api4/image_test.go index aa3619cdd..3b029ac72 100644 --- a/api4/image_test.go +++ b/api4/image_test.go @@ -28,6 +28,15 @@ func TestGetImage(t *testing.T) { require.NoError(t, err) r.Header.Set(model.HEADER_AUTH, th.Client.AuthType+" "+th.Client.AuthToken) + imageProxyType := th.App.Config().ServiceSettings.ImageProxyType + imageProxyOptions := th.App.Config().ServiceSettings.ImageProxyOptions + imageProxyURL := th.App.Config().ServiceSettings.ImageProxyURL + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.ImageProxyType = imageProxyType }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.ImageProxyOptions = imageProxyOptions }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.ImageProxyOptions = imageProxyURL }) + }() + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.ImageProxyType = nil }) diff --git a/api4/oauth_test.go b/api4/oauth_test.go index 6bbd99ccc..5415e485e 100644 --- a/api4/oauth_test.go +++ b/api4/oauth_test.go @@ -26,8 +26,10 @@ func TestCreateOAuthApp(t *testing.T) { AdminClient := th.SystemAdminClient defaultRolePermissions := th.SaveDefaultRolePermissions() + enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider defer func() { th.RestoreDefaultRolePermissions(defaultRolePermissions) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = enableOAuthServiceProvider }) }() // Grant permission to regular users. @@ -97,8 +99,10 @@ func TestUpdateOAuthApp(t *testing.T) { AdminClient := th.SystemAdminClient defaultRolePermissions := th.SaveDefaultRolePermissions() + enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider defer func() { th.RestoreDefaultRolePermissions(defaultRolePermissions) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = enableOAuthServiceProvider }) }() // Grant permission to regular users. @@ -212,8 +216,10 @@ func TestGetOAuthApps(t *testing.T) { AdminClient := th.SystemAdminClient defaultRolePermissions := th.SaveDefaultRolePermissions() + enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider defer func() { th.RestoreDefaultRolePermissions(defaultRolePermissions) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = enableOAuthServiceProvider }) }() // Grant permission to regular users. @@ -284,8 +290,10 @@ func TestGetOAuthApp(t *testing.T) { AdminClient := th.SystemAdminClient defaultRolePermissions := th.SaveDefaultRolePermissions() + enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider defer func() { th.RestoreDefaultRolePermissions(defaultRolePermissions) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = enableOAuthServiceProvider }) }() // Grant permission to regular users. @@ -358,8 +366,10 @@ func TestGetOAuthAppInfo(t *testing.T) { AdminClient := th.SystemAdminClient defaultRolePermissions := th.SaveDefaultRolePermissions() + enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider defer func() { th.RestoreDefaultRolePermissions(defaultRolePermissions) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = enableOAuthServiceProvider }) }() // Grant permission to regular users. @@ -432,8 +442,10 @@ func TestDeleteOAuthApp(t *testing.T) { AdminClient := th.SystemAdminClient defaultRolePermissions := th.SaveDefaultRolePermissions() + enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider defer func() { th.RestoreDefaultRolePermissions(defaultRolePermissions) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = enableOAuthServiceProvider }) }() // Grant permission to regular users. @@ -500,8 +512,10 @@ func TestRegenerateOAuthAppSecret(t *testing.T) { AdminClient := th.SystemAdminClient defaultRolePermissions := th.SaveDefaultRolePermissions() + enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider defer func() { th.RestoreDefaultRolePermissions(defaultRolePermissions) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = enableOAuthServiceProvider }) }() // Grant permission to regular users. @@ -631,6 +645,11 @@ func TestAuthorizeOAuthApp(t *testing.T) { Client := th.Client AdminClient := th.SystemAdminClient + enableOAuth := th.App.Config().ServiceSettings.EnableOAuthServiceProvider + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = enableOAuth }) + }() + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = true }) oapp := &model.OAuthApp{Name: GenerateTestAppName(), Homepage: "https://nowhere.com", Description: "test", CallbackUrls: []string{"https://nowhere.com"}} @@ -958,6 +977,23 @@ func TestOAuthComplete(t *testing.T) { Client := th.Client + gitLabSettingsEnable := th.App.Config().GitLabSettings.Enable + gitLabSettingsAuthEndpoint := th.App.Config().GitLabSettings.AuthEndpoint + gitLabSettingsId := th.App.Config().GitLabSettings.Id + gitLabSettingsSecret := th.App.Config().GitLabSettings.Secret + gitLabSettingsTokenEndpoint := th.App.Config().GitLabSettings.TokenEndpoint + gitLabSettingsUserApiEndpoint := th.App.Config().GitLabSettings.UserApiEndpoint + enableOAuthServiceProvider := th.App.Config().ServiceSettings.EnableOAuthServiceProvider + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.Enable = gitLabSettingsEnable }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.AuthEndpoint = gitLabSettingsAuthEndpoint }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.Id = gitLabSettingsId }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.Secret = gitLabSettingsSecret }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.TokenEndpoint = gitLabSettingsTokenEndpoint }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.GitLabSettings.UserApiEndpoint = gitLabSettingsUserApiEndpoint }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOAuthServiceProvider = enableOAuthServiceProvider }) + }() + r, err := HttpGet(Client.Url+"/login/gitlab/complete?code=123", Client.HttpClient, "", true) assert.NotNil(t, err) closeBody(r) diff --git a/api4/post_test.go b/api4/post_test.go index 63d71b5bd..720fdc410 100644 --- a/api4/post_test.go +++ b/api4/post_test.go @@ -170,6 +170,15 @@ func testCreatePostWithOutgoingHook( team := th.BasicTeam channel := th.BasicChannel + enableOutgoingWebhooks := th.App.Config().ServiceSettings.EnableOutgoingWebhooks + allowedUntrustedInternalConnections := th.App.Config().ServiceSettings.AllowedUntrustedInternalConnections + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOutgoingWebhooks = enableOutgoingWebhooks }) + th.App.UpdateConfig(func(cfg *model.Config) { + cfg.ServiceSettings.AllowedUntrustedInternalConnections = allowedUntrustedInternalConnections + }) + }() + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableOutgoingWebhooks = true }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost 127.0.0.1" diff --git a/api4/team_test.go b/api4/team_test.go index 307e91635..fc49b794f 100644 --- a/api4/team_test.go +++ b/api4/team_test.go @@ -540,6 +540,11 @@ func TestPermanentDeleteTeam(t *testing.T) { team := &model.Team{DisplayName: "DisplayName", Name: GenerateTestTeamName(), Email: th.GenerateTestEmail(), Type: model.TEAM_OPEN} team, _ = Client.CreateTeam(team) + enableAPITeamDeletion := *th.App.Config().ServiceSettings.EnableAPITeamDeletion + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableAPITeamDeletion = &enableAPITeamDeletion }) + }() + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableAPITeamDeletion = false }) // Does not error when deletion is disabled, just soft deletes @@ -1935,15 +1940,20 @@ func TestInviteUsersToTeam(t *testing.T) { utils.DeleteMailBox(user1) utils.DeleteMailBox(user2) - th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableEmailInvitations = false }) + enableEmailInvitations := *th.App.Config().ServiceSettings.EnableEmailInvitations + restrictCreationToDomains := th.App.Config().TeamSettings.RestrictCreationToDomains + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableEmailInvitations = &enableEmailInvitations }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.RestrictCreationToDomains = restrictCreationToDomains }) + }() + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableEmailInvitations = false }) _, resp := th.SystemAdminClient.InviteUsersToTeam(th.BasicTeam.Id, emailList) if resp.Error == nil { t.Fatal("Should be disabled") } th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableEmailInvitations = true }) - okMsg, resp := th.SystemAdminClient.InviteUsersToTeam(th.BasicTeam.Id, emailList) CheckNoError(t, resp) if !okMsg { diff --git a/api4/user_test.go b/api4/user_test.go index ad77c8c4c..4cbaf449a 100644 --- a/api4/user_test.go +++ b/api4/user_test.go @@ -22,6 +22,13 @@ func TestCreateUser(t *testing.T) { Client := th.Client AdminClient := th.SystemAdminClient + enableOpenServer := th.App.Config().TeamSettings.EnableOpenServer + enableUserCreation := th.App.Config().TeamSettings.EnableUserCreation + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableOpenServer = enableOpenServer }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableUserCreation = enableUserCreation }) + }() + user := model.User{Email: th.GenerateTestEmail(), Nickname: "Corey Hulen", Password: "hello1", Username: GenerateTestUsername(), Roles: model.SYSTEM_ADMIN_ROLE_ID + " " + model.SYSTEM_USER_ROLE_ID} ruser, resp := Client.CreateUser(&user) @@ -161,6 +168,12 @@ func TestCreateUserWithToken(t *testing.T) { }) t.Run("EnableUserCreationDisable", func(t *testing.T) { + + enableUserCreation := th.App.Config().TeamSettings.EnableUserCreation + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableUserCreation = enableUserCreation }) + }() + user := model.User{Email: th.GenerateTestEmail(), Nickname: "Corey Hulen", Password: "hello1", Username: GenerateTestUsername(), Roles: model.SYSTEM_ADMIN_ROLE_ID + " " + model.SYSTEM_USER_ROLE_ID} token := model.NewToken( @@ -176,7 +189,6 @@ func TestCreateUserWithToken(t *testing.T) { 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) { @@ -188,6 +200,11 @@ func TestCreateUserWithToken(t *testing.T) { ) <-th.App.Srv.Store.Token().Save(token) + enableOpenServer := th.App.Config().TeamSettings.EnableOpenServer + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableOpenServer = enableOpenServer }) + }() + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = false }) ruser, resp := Client.CreateUserWithToken(&user, token.Token) @@ -270,6 +287,11 @@ func TestCreateUserWithInviteId(t *testing.T) { t.Run("EnableUserCreationDisable", func(t *testing.T) { user := model.User{Email: th.GenerateTestEmail(), Nickname: "Corey Hulen", Password: "hello1", Username: GenerateTestUsername(), Roles: model.SYSTEM_ADMIN_ROLE_ID + " " + model.SYSTEM_USER_ROLE_ID} + enableUserCreation := th.App.Config().TeamSettings.EnableUserCreation + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableUserCreation = enableUserCreation }) + }() + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableUserCreation = false }) inviteId := th.BasicTeam.InviteId @@ -277,13 +299,16 @@ 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: th.GenerateTestEmail(), Nickname: "Corey Hulen", Password: "hello1", Username: GenerateTestUsername(), Roles: model.SYSTEM_ADMIN_ROLE_ID + " " + model.SYSTEM_USER_ROLE_ID} + enableOpenServer := th.App.Config().TeamSettings.EnableOpenServer + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.TeamSettings.EnableOpenServer = enableOpenServer }) + }() + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = false }) inviteId := th.BasicTeam.InviteId @@ -332,6 +357,13 @@ func TestGetUser(t *testing.T) { th.App.UpdateUser(user, false) + showEmailAddress := th.App.Config().PrivacySettings.ShowEmailAddress + showFullName := th.App.Config().PrivacySettings.ShowFullName + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowEmailAddress = showEmailAddress }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = showFullName }) + }() + ruser, resp := Client.GetUser(user.Id, "") CheckNoError(t, resp) CheckUserSanitization(t, ruser) @@ -393,6 +425,13 @@ func TestGetUserByUsername(t *testing.T) { user := th.BasicUser + showEmailAddress := th.App.Config().PrivacySettings.ShowEmailAddress + showFullName := th.App.Config().PrivacySettings.ShowFullName + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowEmailAddress = showEmailAddress }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = showFullName }) + }() + ruser, resp := Client.GetUserByUsername(user.Username, "") CheckNoError(t, resp) CheckUserSanitization(t, ruser) @@ -452,6 +491,13 @@ func TestGetUserByEmail(t *testing.T) { defer th.TearDown() Client := th.Client + showEmailAddress := th.App.Config().PrivacySettings.ShowEmailAddress + showFullName := th.App.Config().PrivacySettings.ShowFullName + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowEmailAddress = showEmailAddress }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = showFullName }) + }() + user := th.CreateUser() ruser, resp := Client.GetUserByEmail(user.Email, "") @@ -510,6 +556,13 @@ func TestSearchUsers(t *testing.T) { defer th.TearDown() Client := th.Client + showEmailAddress := th.App.Config().PrivacySettings.ShowEmailAddress + showFullName := th.App.Config().PrivacySettings.ShowFullName + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowEmailAddress = showEmailAddress }) + th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = showFullName }) + }() + search := &model.UserSearch{Term: th.BasicUser.Username} users, resp := Client.SearchUsers(search) @@ -696,6 +749,11 @@ func TestAutocompleteUsers(t *testing.T) { channelId := th.BasicChannel.Id username := th.BasicUser.Username + showFullName := th.App.Config().PrivacySettings.ShowFullName + defer func() { + th.App.UpdateConfig(func(cfg *model.Config) { cfg.PrivacySettings.ShowFullName = showFullName }) + }() + rusers, resp := Client.AutocompleteUsersInChannel(teamId, channelId, username, "") CheckNoError(t, resp) -- cgit v1.2.3-1-g7c22